Commit 6a6efc75 by pye52

优化安装指令的业务逻辑

parent 1bc68326
......@@ -16,9 +16,7 @@ import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.OkHttpClient;
import okhttp3.Request;
......@@ -80,8 +78,8 @@ public class UpdateCommandWorker extends CommandWorker {
.sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), new TrustAllCerts())
.build();
InputStream is = null;
FileOutputStream fos = null;
BufferedSource source = null;
BufferedSink sink = null;
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
......@@ -90,14 +88,11 @@ public class UpdateCommandWorker extends CommandWorker {
return failed("更新包异常");
}
is = body.byteStream();
fos = new FileOutputStream(updateApk);
long total = body.contentLength();
float totalBytesRead = 0f;
BufferedSource source = body.source();
BufferedSink sink = Okio.buffer(Okio.sink(updateApk));
source = body.source();
sink = Okio.buffer(Okio.sink(updateApk));
Buffer sinkBuffer = sink.getBuffer();
for (long bytesRead; (bytesRead = source.read(sinkBuffer, BUFFER_SIZE)) != -1; ) {
......@@ -107,23 +102,22 @@ public class UpdateCommandWorker extends CommandWorker {
inProgress("下载进度: " + progress + "%");
}
sink.flush();
sink.close();
source.close();
body.close();
response.close();
} catch (IOException e) {
updateApk.deleteOnExit();
FileUtils.delete(updateApk);
LogUtils.w(TAG, "更新包下载失败: " + e.getMessage(), e);
return failed("更新包下载失败");
} finally {
if (is != null) {
if (sink != null) {
try {
is.close();
sink.close();
} catch (IOException ignored) {
}
}
if (fos != null) {
if (source != null) {
try {
fos.close();
source.close();
} catch (IOException ignored) {
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment