1
1
package com .face_chtj .base_iotutils ;
2
2
3
- import com .face_chtj .base_iotutils .KLog ;
4
3
import com .face_chtj .base_iotutils .entity .FileCacheData ;
5
4
import com .face_chtj .base_iotutils .entity .DownloadStatus ;
6
5
import com .face_chtj .base_iotutils .callback .IDownloadCallback ;
@@ -43,8 +42,7 @@ public class DownloadUtils {
43
42
private OkHttpClient client ;
44
43
private int MAX_BUFF_SIZE = 2048 ;
45
44
private Call call ;
46
- //当前正在里的任务
47
- private static Map <String , Integer > currentTaskList = new HashMap <>();
45
+ private Map <String , Integer > currentTaskList = new HashMap <>();
48
46
private List <FileCacheData > fileCacheDataList = new ArrayList <>();
49
47
50
48
/**
@@ -95,7 +93,6 @@ private void initData(int maxBuffSize) {
95
93
*/
96
94
private Call newCall (FileCacheData fileCacheData ) {
97
95
long fileLength = new File (fileCacheData .getFilePath ()).length ();
98
- KLog .d ("newCall: fileLength=" + fileLength );
99
96
Request request = new Request .Builder ()
100
97
.url (fileCacheData .getUrl ())
101
98
.tag (fileCacheData .getRequestTag ())
@@ -144,7 +141,6 @@ public void addStartTask(final FileCacheData fileCacheData, final IDownloadCallb
144
141
call .enqueue (new Callback () {
145
142
@ Override
146
143
public void onFailure (Call call , IOException e ) {
147
- KLog .d ("onFailure:>=" + e .getMessage ());
148
144
downloadCallBack .error (e );
149
145
}
150
146
@@ -167,8 +163,6 @@ public void pause() {
167
163
168
164
/**
169
165
* 按tag暂停任务
170
- *
171
- * @param requestTag
172
166
*/
173
167
public void pause (String requestTag ) {
174
168
if (currentTaskList != null && currentTaskList .size () > 0 && currentTaskList .containsKey (requestTag )) {
@@ -179,10 +173,6 @@ public void pause(String requestTag) {
179
173
180
174
/**
181
175
* 将文件写入到本地
182
- *
183
- * @param response
184
- * @param fileCacheData
185
- * @param downloadCallBack
186
176
*/
187
177
private void save (Response response , FileCacheData fileCacheData , IDownloadCallback downloadCallBack ) {
188
178
ResponseBody body = response .body ();
@@ -195,8 +185,6 @@ private void save(Response response, FileCacheData fileCacheData, IDownloadCallb
195
185
long currentFileLenght = randomAccessFile .length ();
196
186
long bodyContentLength = body .contentLength ();
197
187
fileCacheData .setTotal (bodyContentLength + currentFileLenght );
198
- KLog .d ("save: currentFileLength=" + currentFileLenght + ",bodyContentLength=" + bodyContentLength + ",totalLength=" + fileCacheData .getTotal ());
199
- //body.contentLength()存放了这次下载的文件的总长度 current得到之前下载过的文件长度
200
188
if (currentFileLenght >= fileCacheData .getTotal ()) {
201
189
downloadCallBack .downloadProgress (fileCacheData , 100 );
202
190
currentTaskList .put (fileCacheData .getRequestTag (), DownloadStatus .STATUS_COMPLETE );
@@ -233,7 +221,7 @@ private void save(Response response, FileCacheData fileCacheData, IDownloadCallb
233
221
fileCacheDataList .add (fileCacheData );
234
222
//删除当前的这个执行任务
235
223
currentTaskList .remove (fileCacheData .getRequestTag ());
236
- if (currentTaskList .size () < = 0 ) {
224
+ if (currentTaskList .size () = = 0 ) {
237
225
//将完成的所有任务回调回去
238
226
downloadCallBack .allDownloadComplete (fileCacheDataList );
239
227
//回调之后进行清除操作
@@ -242,25 +230,18 @@ private void save(Response response, FileCacheData fileCacheData, IDownloadCallb
242
230
} catch (Throwable e ) {
243
231
downloadCallBack .error (e );
244
232
} 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 ) {
251
236
}
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 ) {
258
240
}
259
241
if (randomAccessFile != null ) {
260
242
try {
261
243
randomAccessFile .close ();
262
244
} catch (Throwable e ) {
263
- KLog .e ("errMeg:" + e .getMessage ());
264
245
}
265
246
}
266
247
}
@@ -276,11 +257,7 @@ public void cancel() {
276
257
if (call != null ) {
277
258
call .cancel ();
278
259
}
279
- if (currentTaskList != null ) {
280
- currentTaskList .clear ();
281
- }
282
- if (fileCacheDataList != null ) {
283
- currentTaskList .clear ();
284
- }
260
+ currentTaskList .clear ();
261
+ currentTaskList .clear ();
285
262
}
286
263
}
0 commit comments