-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathDownload.java
More file actions
368 lines (323 loc) · 9.21 KB
/
Download.java
File metadata and controls
368 lines (323 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package cn.zhxu.okhttps;
import java.io.*;
import java.util.function.Consumer;
/**
* 文件下载
* @author Troy.Zhou
*/
public class Download {
private final File file;
private final InputStream input;
private final TaskExecutor taskExecutor;
private final Ctrl ctrl;
private Consumer<File> onSuccess;
private Consumer<Failure> onFailure;
private Consumer<Status> onComplete;
private long doneBytes;
private int buffSize = 0;
private long seekBytes = 0;
private boolean appended;
private volatile Status status;
private final Object lock = new Object();
protected boolean nextOnIO = false;
private boolean sOnIO;
private boolean fOnIO;
private boolean cOnIO;
public Download(File file, InputStream input, TaskExecutor taskExecutor, long skipBytes) {
this.file = file;
this.input = input;
this.taskExecutor = taskExecutor;
this.seekBytes = skipBytes;
this.ctrl = new Ctrl();
}
/**
* 设置缓冲区大小,默认 2K(2048)
* @param buffSize 缓冲区大小(单位:字节)
* @return Download
*/
public Download setBuffSize(int buffSize) {
if (buffSize > 0) {
this.buffSize = buffSize;
}
return this;
}
/**
* 设置文件追加模式
* 用预断点续传和分块下载
* @return Download
*/
public Download setAppended() {
this.appended = true;
return this;
}
/**
* 设置文件指针,从文件的 seekBytes 位置追加内容
* 只有配合 setAppended() 方法一起才会有作用
* @param seekBytes 跨越的字节数
* @return Download
*/
public Download setFilePointer(long seekBytes) {
this.seekBytes = seekBytes;
return this;
}
/**
* 在IO线程执行
* @return Download
*/
public Download nextOnIO() {
nextOnIO = true;
return this;
}
/**
* 设置下载成功回调
* @param onSuccess 成功回调函数
* @return Download
*/
public Download setOnSuccess(Consumer<File> onSuccess) {
this.onSuccess = onSuccess;
sOnIO = nextOnIO;
nextOnIO = false;
return this;
}
/**
* 设置下载失败回调(取消不执行)
* @param onFailure 失败回调函数
* @return Download
*/
public Download setOnFailure(Consumer<Failure> onFailure) {
this.onFailure = onFailure;
fOnIO = nextOnIO;
nextOnIO = false;
return this;
}
/**
* @since 3.2.0
* 设置下载结束回调(成功、失败、取消都执行)
* @param onComplete 结束回调函数
* @return Download
*/
public Download setOnComplete(Consumer<Status> onComplete) {
this.onComplete = onComplete;
cOnIO = nextOnIO;
nextOnIO = false;
return this;
}
/**
* 开始下载
* @return 下载控制器
*/
public Ctrl start() {
if (buffSize == 0) {
buffSize = Process.DEFAULT_STEP_BYTES;
}
RandomAccessFile raFile = randomAccessFile();
if (raFile != null) {
status = Status.DOWNLOADING;
taskExecutor.execute(() -> doDownload(raFile), true);
}
return ctrl;
}
/**
* 获取下载控制器
* @return Ctrl
*/
public Ctrl getCtrl() {
return ctrl;
}
/**
* 下载状态
* @since v3.2.0
*/
public enum Status {
/**
* 已取消
*/
CANCELED(-1),
/**
* 下载中
*/
DOWNLOADING(1),
/**
* 已暂停
*/
PAUSED(2),
/**
* 成功下载完成
*/
DONE(3),
/**
* 发送错误
*/
ERROR(4);
private final int value;
Status(int value) {
this.value = value;
}
public int value() {
return value;
}
}
/**
* 下载控制器
*/
public class Ctrl {
/**
* @return 下载状态(v3.2.0 之前方法 返回整型常量,v3.2.0 开始修改为枚举)
*/
public Status status() {
return status;
}
/**
* 暂停下载任务(只有处于下载中状态才能暂停成功)
* @return 是否暂停成功
*/
public boolean pause() {
synchronized (lock) {
if (status == Status.DOWNLOADING) {
status = Status.PAUSED;
return true;
}
}
return false;
}
/**
* 继续下载任务(只有处于暂停状态才能恢复成功)
* @return 是否恢复成功
*/
public boolean resume() {
synchronized (lock) {
if (status == Status.PAUSED) {
status = Status.DOWNLOADING;
return true;
}
}
return false;
}
/**
* 取消下载任务(只有处于暂停或下载中状态才能取消成功)
* @return 是否取消成功
*/
public boolean cancel() {
synchronized (lock) {
if (status == Status.PAUSED || status == Status.DOWNLOADING) {
status = Status.CANCELED;
return true;
}
}
return false;
}
}
public class Failure {
private final IOException exception;
Failure(IOException exception) {
this.exception = exception;
}
/**
* @return 下载文件
*/
public File getFile() {
return file;
}
/**
* @return 本次已下载字节数
*/
public long getDoneBytes() {
return doneBytes;
}
/**
* @return 异常信息
*/
public IOException getException() {
return exception;
}
}
private RandomAccessFile randomAccessFile() {
try {
return new RandomAccessFile(file, "rw");
} catch (FileNotFoundException e) {
status = Status.ERROR;
closeQuietly(input);
fireOnComplete();
fireOnFailure(e);
}
return null;
}
private void doDownload(RandomAccessFile raFile) {
try {
if (appended && seekBytes > 0) {
// 使支持并行下载到同一个文件
raFile.seek(seekBytes);
}
while (status != Status.CANCELED && status != Status.DONE) {
if (status == Status.DOWNLOADING) {
byte[] buff = new byte[buffSize];
int len;
while ((len = input.read(buff)) != -1) {
raFile.write(buff, 0, len);
doneBytes += len;
if (status == Status.CANCELED
|| status == Status.PAUSED) {
break;
}
}
if (len == -1) {
synchronized (lock) {
if (status != Status.CANCELED) {
status = Status.DONE;
}
}
}
}
}
} catch (IOException e) {
synchronized (lock) {
if (status != Status.CANCELED) {
status = Status.ERROR;
}
}
if (status == Status.ERROR) {
fireOnFailure(e);
}
} finally {
closeQuietly(raFile);
closeQuietly(input);
if (status == Status.CANCELED && !file.delete()) {
Platform.logError("can not delete canceled file: " + file);
}
fireOnComplete();
}
if (status == Status.DONE) {
fireOnSuccess();
}
}
private void fireOnComplete() {
Consumer<Status> onComplete = this.onComplete;
if (onComplete != null) {
taskExecutor.execute(() -> onComplete.accept(status), cOnIO);
}
}
private void fireOnSuccess() {
Consumer<File> onSuccess = this.onSuccess;
if (onSuccess != null) {
taskExecutor.execute(() -> onSuccess.accept(file), sOnIO);
}
}
private void fireOnFailure(IOException e) {
Consumer<Failure> onFailure = this.onFailure;
if (onFailure != null) {
taskExecutor.execute(() -> onFailure.accept(new Failure(e)), fOnIO);
} else {
throw new OkHttpsException("Download failed: ", e);
}
}
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
}