+ 修复多线程下载后不关闭流的问题
This commit is contained in:
parent
b93cb3ff69
commit
d791e0d89e
@ -1,22 +0,0 @@
|
||||
package com.ilummc.eagletdl;
|
||||
|
||||
/**
|
||||
* Test class
|
||||
*/
|
||||
public class Eaglet {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//new EagletTask().url("http://sgp-ping.vultr.com/vultr.com.100MB.bin")
|
||||
new EagletTask().url("https://gitee.com/bkm016/TabooLibCloud/raw/master/TabooMenu/TabooMenu.jar")
|
||||
.file("F:\\test.dl")
|
||||
.setThreads(1)
|
||||
.readTimeout(1000)
|
||||
.connectionTimeout(1000)
|
||||
.maxRetry(30)
|
||||
.setOnConnected(event -> System.out.println(event.getContentLength()))
|
||||
.setOnProgress(event -> System.out.println(event.getSpeedFormatted() + " " + event.getPercentageFormatted()))
|
||||
.setOnComplete(event -> System.out.println("Complete"))
|
||||
.start();
|
||||
}
|
||||
|
||||
}
|
@ -111,7 +111,7 @@ public class EagletTask {
|
||||
long progress = download.getCurrentProgress();
|
||||
// fire a new progress event
|
||||
if (onProgress != null) {
|
||||
onProgress.handle(new ProgressEvent(progress - last, this, ((double) progress) / Math.max((double) contentLength, 0D)));
|
||||
onProgress.handle(new ProgressEvent(progress - last < 0 ? 0 : progress - last, this, ((double) progress) / Math.max((double) contentLength, 0D)));
|
||||
}
|
||||
last = progress;
|
||||
// check complete
|
||||
|
@ -35,8 +35,7 @@ class SingleThreadDownload implements Runnable {
|
||||
public void run() {
|
||||
byte[] buf = new byte[1024];
|
||||
int len = 0;
|
||||
try (BufferedInputStream stream = new BufferedInputStream(connection.getInputStream());
|
||||
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(target))) {
|
||||
try (BufferedInputStream stream = new BufferedInputStream(connection.getInputStream()); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(target))) {
|
||||
while ((len = stream.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0, len);
|
||||
currentProgress += len;
|
||||
|
@ -80,7 +80,10 @@ class SplitDownload implements Runnable {
|
||||
}
|
||||
complete = true;
|
||||
}
|
||||
} else throw new DoNotSupportMultipleThreadException();
|
||||
file.close();
|
||||
} else {
|
||||
throw new DoNotSupportMultipleThreadException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
task.onError.handle(new ErrorEvent(e, task));
|
||||
retry++;
|
||||
|
Loading…
Reference in New Issue
Block a user