Skip to content

Commit 4e0fa59

Browse files
committed
优化下载工具
1 parent f66fad8 commit 4e0fa59

File tree

2 files changed

+11
-34
lines changed

2 files changed

+11
-34
lines changed

base_iotutils/src/main/java/com/face_chtj/base_iotutils/DownloadUtils.java

+10-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.face_chtj.base_iotutils;
22

3-
import com.face_chtj.base_iotutils.KLog;
43
import com.face_chtj.base_iotutils.entity.FileCacheData;
54
import com.face_chtj.base_iotutils.entity.DownloadStatus;
65
import com.face_chtj.base_iotutils.callback.IDownloadCallback;
@@ -43,8 +42,7 @@ public class DownloadUtils {
4342
private OkHttpClient client;
4443
private int MAX_BUFF_SIZE = 2048;
4544
private Call call;
46-
//当前正在里的任务
47-
private static Map<String, Integer> currentTaskList = new HashMap<>();
45+
private Map<String, Integer> currentTaskList = new HashMap<>();
4846
private List<FileCacheData> fileCacheDataList = new ArrayList<>();
4947

5048
/**
@@ -95,7 +93,6 @@ private void initData(int maxBuffSize) {
9593
*/
9694
private Call newCall(FileCacheData fileCacheData) {
9795
long fileLength = new File(fileCacheData.getFilePath()).length();
98-
KLog.d("newCall: fileLength=" + fileLength);
9996
Request request = new Request.Builder()
10097
.url(fileCacheData.getUrl())
10198
.tag(fileCacheData.getRequestTag())
@@ -144,7 +141,6 @@ public void addStartTask(final FileCacheData fileCacheData, final IDownloadCallb
144141
call.enqueue(new Callback() {
145142
@Override
146143
public void onFailure(Call call, IOException e) {
147-
KLog.d("onFailure:>=" + e.getMessage());
148144
downloadCallBack.error(e);
149145
}
150146

@@ -167,8 +163,6 @@ public void pause() {
167163

168164
/**
169165
* 按tag暂停任务
170-
*
171-
* @param requestTag
172166
*/
173167
public void pause(String requestTag) {
174168
if (currentTaskList != null && currentTaskList.size() > 0 && currentTaskList.containsKey(requestTag)) {
@@ -179,10 +173,6 @@ public void pause(String requestTag) {
179173

180174
/**
181175
* 将文件写入到本地
182-
*
183-
* @param response
184-
* @param fileCacheData
185-
* @param downloadCallBack
186176
*/
187177
private void save(Response response, FileCacheData fileCacheData, IDownloadCallback downloadCallBack) {
188178
ResponseBody body = response.body();
@@ -195,8 +185,6 @@ private void save(Response response, FileCacheData fileCacheData, IDownloadCallb
195185
long currentFileLenght = randomAccessFile.length();
196186
long bodyContentLength = body.contentLength();
197187
fileCacheData.setTotal(bodyContentLength + currentFileLenght);
198-
KLog.d("save: currentFileLength=" + currentFileLenght + ",bodyContentLength=" + bodyContentLength + ",totalLength=" + fileCacheData.getTotal());
199-
//body.contentLength()存放了这次下载的文件的总长度 current得到之前下载过的文件长度
200188
if (currentFileLenght >= fileCacheData.getTotal()) {
201189
downloadCallBack.downloadProgress(fileCacheData, 100);
202190
currentTaskList.put(fileCacheData.getRequestTag(), DownloadStatus.STATUS_COMPLETE);
@@ -233,7 +221,7 @@ private void save(Response response, FileCacheData fileCacheData, IDownloadCallb
233221
fileCacheDataList.add(fileCacheData);
234222
//删除当前的这个执行任务
235223
currentTaskList.remove(fileCacheData.getRequestTag());
236-
if (currentTaskList.size() <= 0) {
224+
if (currentTaskList.size() == 0) {
237225
//将完成的所有任务回调回去
238226
downloadCallBack.allDownloadComplete(fileCacheDataList);
239227
//回调之后进行清除操作
@@ -242,25 +230,18 @@ private void save(Response response, FileCacheData fileCacheData, IDownloadCallb
242230
} catch (Throwable e) {
243231
downloadCallBack.error(e);
244232
} finally {
245-
if (bis != null) {
246-
try {
247-
bis.close();
248-
} catch (Throwable e) {
249-
KLog.e("errMeg:" + e.getMessage());
250-
}
233+
try {
234+
bis.close();
235+
} catch (Throwable e) {
251236
}
252-
if (in != null) {
253-
try {
254-
in.close();
255-
} catch (Throwable e) {
256-
KLog.e("errMeg:" + e.getMessage());
257-
}
237+
try {
238+
in.close();
239+
} catch (Throwable e) {
258240
}
259241
if (randomAccessFile != null) {
260242
try {
261243
randomAccessFile.close();
262244
} catch (Throwable e) {
263-
KLog.e("errMeg:" + e.getMessage());
264245
}
265246
}
266247
}
@@ -276,11 +257,7 @@ public void cancel() {
276257
if (call != null) {
277258
call.cancel();
278259
}
279-
if (currentTaskList != null) {
280-
currentTaskList.clear();
281-
}
282-
if (fileCacheDataList != null) {
283-
currentTaskList.clear();
284-
}
260+
currentTaskList.clear();
261+
currentTaskList.clear();
285262
}
286263
}

config.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ext {
22
//选择对应签名 "cainiao" "rockchip" "imx8" "freescale"
3-
BUILD_SIGN="freescale"
3+
BUILD_SIGN="rockchip"
44
BUILD_SIGN_PWD="android"
55
BUILD_SIGN_PATH="../other/facefuture.jks"
66
//android开发版本配置

0 commit comments

Comments
 (0)