Skip to content

Commit 629da4c

Browse files
committed
下载状态使用常量表达
1 parent 45fdf9f commit 629da4c

1 file changed

Lines changed: 68 additions & 41 deletions

File tree

‎okhttps/src/main/java/com/ejlchina/okhttps/Download.java‎

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public class Download {
1818

1919
private OnCallback<File> onSuccess;
2020
private OnCallback<Failure> onFailure;
21+
private OnCallback<Failure> onComplete;
2122
private long doneBytes;
2223
private int buffSize = 0;
2324
private long seekBytes = 0;
2425
private boolean appended;
25-
private volatile int status;
26+
private volatile Status status;
2627
private final Object lock = new Object();
2728

2829
protected boolean nextOnIO = false;
@@ -102,7 +103,19 @@ public Download setOnFailure(OnCallback<Failure> onFailure) {
102103
nextOnIO = false;
103104
return this;
104105
}
105-
106+
107+
/**
108+
* 设置下载结束回调
109+
* @param onFailure 失败回调函数
110+
* @return Download
111+
*/
112+
public Download setOnComplete(OnCallback<Failure> onFailure) {
113+
this.onFailure = onFailure;
114+
fOnIO = nextOnIO;
115+
nextOnIO = false;
116+
return this;
117+
}
118+
106119
/**
107120
* 开始下载
108121
* @return 下载控制器
@@ -112,7 +125,7 @@ public Ctrl start() {
112125
buffSize = Process.DEFAULT_STEP_BYTES;
113126
}
114127
RandomAccessFile raFile = randomAccessFile();
115-
status = Ctrl.STATUS__DOWNLOADING;
128+
status = Status.DOWNLOADING;
116129
taskExecutor.execute(() -> doDownload(raFile), true);
117130
return ctrl;
118131
}
@@ -125,41 +138,55 @@ public Ctrl getCtrl() {
125138
return ctrl;
126139
}
127140

128-
public class Ctrl {
129-
141+
/**
142+
* 下载状态
143+
*/
144+
public enum Status {
145+
130146
/**
131147
* 已取消
132148
*/
133-
public static final int STATUS__CANCELED = -1;
134-
149+
CANCELED(-1),
150+
135151
/**
136152
* 下载中
137153
*/
138-
public static final int STATUS__DOWNLOADING = 1;
139-
154+
DOWNLOADING(1),
155+
140156
/**
141157
* 已暂停
142158
*/
143-
public static final int STATUS__PAUSED = 2;
144-
159+
PAUSED(2),
160+
145161
/**
146-
* 已完成
162+
* 成功下载完成
147163
*/
148-
public static final int STATUS__DONE = 3;
149-
164+
DONE(3),
165+
150166
/**
151-
* 错误
167+
* 发送错误
152168
*/
153-
public static final int STATUS__ERROR = 4;
154-
169+
ERROR(4);
170+
171+
private final int value;
172+
173+
Status(int value) {
174+
this.value = value;
175+
}
176+
177+
public int value() {
178+
return value;
179+
}
180+
181+
}
182+
183+
public class Ctrl {
184+
155185
/**
156-
* @see #STATUS__CANCELED
157-
* @see #STATUS__DOWNLOADING
158-
* @see #STATUS__PAUSED
159-
* @see #STATUS__DONE
186+
* @see Status
160187
* @return 下载状态
161188
*/
162-
public int status() {
189+
public Status status() {
163190
return status;
164191
}
165192

@@ -168,8 +195,8 @@ public int status() {
168195
*/
169196
public void pause() {
170197
synchronized (lock) {
171-
if (status == STATUS__DOWNLOADING) {
172-
status = STATUS__PAUSED;
198+
if (status == Status.DOWNLOADING) {
199+
status = Status.PAUSED;
173200
}
174201
}
175202
}
@@ -179,8 +206,8 @@ public void pause() {
179206
*/
180207
public void resume() {
181208
synchronized (lock) {
182-
if (status == STATUS__PAUSED) {
183-
status = STATUS__DOWNLOADING;
209+
if (status == Status.PAUSED) {
210+
status = Status.DOWNLOADING;
184211
}
185212
}
186213
}
@@ -190,8 +217,8 @@ public void resume() {
190217
*/
191218
public void cancel() {
192219
synchronized (lock) {
193-
if (status == STATUS__PAUSED || status == STATUS__DOWNLOADING) {
194-
status = STATUS__CANCELED;
220+
if (status == Status.PAUSED || status == Status.DOWNLOADING) {
221+
status = Status.CANCELED;
195222
}
196223
}
197224
}
@@ -233,7 +260,7 @@ private RandomAccessFile randomAccessFile() {
233260
try {
234261
return new RandomAccessFile(file, "rw");
235262
} catch (FileNotFoundException e) {
236-
status = Ctrl.STATUS__ERROR;
263+
status = Status.ERROR;
237264
closeQuietly(input);
238265
throw new HttpException("无法获取文件[" + file.getAbsolutePath() + "]的输入流", e);
239266
}
@@ -245,48 +272,48 @@ private void doDownload(RandomAccessFile raFile) {
245272
// 使支持并行下载到同一个文件
246273
raFile.seek(seekBytes);
247274
}
248-
while (status != Ctrl.STATUS__CANCELED && status != Ctrl.STATUS__DONE) {
249-
if (status == Ctrl.STATUS__DOWNLOADING) {
275+
while (status != Status.CANCELED && status != Status.DONE) {
276+
if (status == Status.DOWNLOADING) {
250277
byte[] buff = new byte[buffSize];
251278
int len;
252279
while ((len = input.read(buff)) != -1) {
253280
raFile.write(buff, 0, len);
254281
doneBytes += len;
255-
if (status == Ctrl.STATUS__CANCELED
256-
|| status == Ctrl.STATUS__PAUSED) {
282+
if (status == Status.CANCELED
283+
|| status == Status.PAUSED) {
257284
break;
258285
}
259286
}
260287
if (len == -1) {
261288
synchronized (lock) {
262-
if (status != Ctrl.STATUS__CANCELED) {
263-
status = Ctrl.STATUS__DONE;
289+
if (status != Status.CANCELED) {
290+
status = Status.DONE;
264291
}
265292
}
266293
}
267294
}
268295
}
269296
} catch (IOException e) {
270297
synchronized (lock) {
271-
if (status != Ctrl.STATUS__CANCELED) {
272-
status = Ctrl.STATUS__ERROR;
298+
if (status != Status.CANCELED) {
299+
status = Status.ERROR;
273300
}
274301
}
275-
if (status == Ctrl.STATUS__ERROR) {
302+
if (status == Status.ERROR) {
276303
if (onFailure != null) {
277304
taskExecutor.execute(() -> onFailure.on(new Failure(e)), fOnIO);
278305
} else {
279-
throw new HttpException("流传输失败", e);
306+
throw new HttpException("Download failed: ", e);
280307
}
281308
}
282309
} finally {
283310
closeQuietly(raFile);
284311
closeQuietly(input);
285-
if (status == Ctrl.STATUS__CANCELED && !file.delete()) {
312+
if (status == Status.CANCELED && !file.delete()) {
286313
Platform.logError("can not delete canceled file: " + file);
287314
}
288315
}
289-
if (status == Ctrl.STATUS__DONE && onSuccess != null) {
316+
if (status == Status.DONE && onSuccess != null) {
290317
taskExecutor.execute(() -> onSuccess.on(file), sOnIO);
291318
}
292319
}

0 commit comments

Comments
 (0)