+ 修复多线程下载后不关闭流的问题

This commit is contained in:
坏黑 2018-10-24 22:51:27 +08:00
parent b93cb3ff69
commit d791e0d89e
4 changed files with 6 additions and 26 deletions

View File

@ -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();
}
}

View File

@ -111,7 +111,7 @@ public class EagletTask {
long progress = download.getCurrentProgress(); long progress = download.getCurrentProgress();
// fire a new progress event // fire a new progress event
if (onProgress != null) { 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; last = progress;
// check complete // check complete

View File

@ -35,8 +35,7 @@ class SingleThreadDownload implements Runnable {
public void run() { public void run() {
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len = 0; int len = 0;
try (BufferedInputStream stream = new BufferedInputStream(connection.getInputStream()); try (BufferedInputStream stream = new BufferedInputStream(connection.getInputStream()); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(target))) {
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(target))) {
while ((len = stream.read(buf)) > 0) { while ((len = stream.read(buf)) > 0) {
outputStream.write(buf, 0, len); outputStream.write(buf, 0, len);
currentProgress += len; currentProgress += len;

View File

@ -80,7 +80,10 @@ class SplitDownload implements Runnable {
} }
complete = true; complete = true;
} }
} else throw new DoNotSupportMultipleThreadException(); file.close();
} else {
throw new DoNotSupportMultipleThreadException();
}
} catch (Exception e) { } catch (Exception e) {
task.onError.handle(new ErrorEvent(e, task)); task.onError.handle(new ErrorEvent(e, task));
retry++; retry++;