[FIX] Downloader bug fixes

This commit is contained in:
Gravita 2021-06-18 10:07:00 +07:00
parent d01c27177c
commit 661fb94594
4 changed files with 16 additions and 4 deletions

View file

@ -43,6 +43,7 @@ public AsyncDownloader() {
}
public void downloadFile(URL url, Path target, long size) throws IOException {
if (isClosed) throw new IOException("Download interrupted");
URLConnection connection = url.openConnection();
if (isCertificatePinning) {
HttpsURLConnection connection1 = (HttpsURLConnection) connection;

View file

@ -1,6 +1,7 @@
package pro.gravit.utils;
import pro.gravit.launcher.AsyncDownloader;
import pro.gravit.utils.helper.LogHelper;
import java.nio.file.Path;
import java.util.List;
@ -37,6 +38,7 @@ public boolean isCanceled() {
public static Downloader downloadList(List<AsyncDownloader.SizedFile> files, String baseURL, Path targetDir, DownloadCallback callback, ExecutorService executor, int threads) throws Exception {
final boolean closeExecutor;
LogHelper.info("Download with legacy mode");
if (executor == null) {
executor = Executors.newWorkStealingPool(4);
closeExecutor = true;

View file

@ -2,6 +2,8 @@
import pro.gravit.launcher.AsyncDownloader;
import pro.gravit.launcher.LauncherInject;
import pro.gravit.utils.helper.IOHelper;
import pro.gravit.utils.helper.LogHelper;
import java.net.URI;
import java.net.URISyntaxException;
@ -10,6 +12,7 @@
import java.net.http.HttpResponse;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -60,6 +63,7 @@ public CompletableFuture<Void> getFuture() {
public static Downloader downloadList(List<AsyncDownloader.SizedFile> files, String baseURL, Path targetDir, DownloadCallback callback, ExecutorService executor, int threads) throws Exception {
boolean closeExecutor = false;
LogHelper.info("Download with Java 11+ HttpClient");
if (executor == null) {
executor = Executors.newWorkStealingPool(Math.min(3, threads));
closeExecutor = true;
@ -118,8 +122,12 @@ public CompletableFuture<Void> downloadFiles(List<AsyncDownloader.SizedFile> fil
}
try {
DownloadTask task = sendAsync(file, baseUri, targetDir, callback);
task.completableFuture.thenAccept(consumerObject.next);
task.completableFuture.thenAccept(consumerObject.next).exceptionally(ec -> {
future.completeExceptionally(ec);
return null;
});
} catch (Exception exception) {
LogHelper.error(exception);
future.completeExceptionally(exception);
}
};
@ -149,6 +157,7 @@ public void cancel() {
}
protected DownloadTask sendAsync(AsyncDownloader.SizedFile file, URI baseUri, Path targetDir, DownloadCallback callback) throws Exception {
IOHelper.createParentDirs(targetDir.resolve(file.filePath));
ProgressTrackingBodyHandler<Path> bodyHandler = makeBodyHandler(targetDir.resolve(file.filePath), callback);
CompletableFuture<HttpResponse<Path>> future = client.sendAsync(makeHttpRequest(baseUri, file.urlPath), bodyHandler);
var ref = new Object() {
@ -177,7 +186,7 @@ protected HttpRequest makeHttpRequest(URI baseUri, String filePath) throws URISy
}
protected ProgressTrackingBodyHandler<Path> makeBodyHandler(Path file, DownloadCallback callback) {
return new ProgressTrackingBodyHandler<>(HttpResponse.BodyHandlers.ofFile(file), callback);
return new ProgressTrackingBodyHandler<>(HttpResponse.BodyHandlers.ofFile(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE), callback);
}
public static class ProgressTrackingBodyHandler<T> implements HttpResponse.BodyHandler<T> {
@ -223,12 +232,12 @@ public void onSubscribe(Flow.Subscription subscription) {
@Override
public void onNext(List<ByteBuffer> byteBuffers) {
delegate.onNext(byteBuffers);
long diff = 0;
for (ByteBuffer buffer : byteBuffers) {
diff += buffer.remaining();
}
if (callback != null) callback.apply(diff);
delegate.onNext(byteBuffers);
}
@Override

@ -1 +1 @@
Subproject commit 8b616fff0946652ebba6d6dd432b73b8075421a5
Subproject commit 4678535d0bc74016d43c23017c5530a81c62e56d