Commit 6a6efc75 by pye52

优化安装指令的业务逻辑

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