diff --git a/LauncherCore/src/main/java/pro/gravit/launcher/AsyncDownloader.java b/LauncherCore/src/main/java/pro/gravit/launcher/AsyncDownloader.java index b8d7e89d..6b02b960 100644 --- a/LauncherCore/src/main/java/pro/gravit/launcher/AsyncDownloader.java +++ b/LauncherCore/src/main/java/pro/gravit/launcher/AsyncDownloader.java @@ -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; diff --git a/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java b/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java index 0208a169..5bfa4e13 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java @@ -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 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; diff --git a/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java b/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java index 7c9a47d1..5778ae74 100644 --- a/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java +++ b/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java @@ -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 getFuture() { public static Downloader downloadList(List 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 downloadFiles(List 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 bodyHandler = makeBodyHandler(targetDir.resolve(file.filePath), callback); CompletableFuture> 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 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 implements HttpResponse.BodyHandler { @@ -223,12 +232,12 @@ public void onSubscribe(Flow.Subscription subscription) { @Override public void onNext(List byteBuffers) { - delegate.onNext(byteBuffers); long diff = 0; for (ByteBuffer buffer : byteBuffers) { diff += buffer.remaining(); } if (callback != null) callback.apply(diff); + delegate.onNext(byteBuffers); } @Override diff --git a/modules b/modules index 8b616fff..4678535d 160000 --- a/modules +++ b/modules @@ -1 +1 @@ -Subproject commit 8b616fff0946652ebba6d6dd432b73b8075421a5 +Subproject commit 4678535d0bc74016d43c23017c5530a81c62e56d