mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix: close stalled download streams on cancel
This commit is contained in:
1 parent
4babf24433
commit
b2185be039
1 file changed
+21
-1
@@ -35,6 +35,7 @@ public final class Downloader {
|
||||
public static final class Cancellation implements java.util.function.BooleanSupplier {
|
||||
private final AtomicBoolean cancelled = new AtomicBoolean();
|
||||
private final AtomicReference<HttpURLConnection> connection = new AtomicReference<>();
|
||||
private final AtomicReference<InputStream> input = new AtomicReference<>();
|
||||
private final java.util.function.BooleanSupplier external;
|
||||
|
||||
public Cancellation() { this(null); }
|
||||
@@ -44,6 +45,8 @@ public final class Downloader {
|
||||
cancelled.set(true);
|
||||
HttpURLConnection active = connection.get();
|
||||
if (active != null) disconnectAsync(active);
|
||||
InputStream activeInput = input.get();
|
||||
if (activeInput != null) closeAsync(activeInput);
|
||||
}
|
||||
|
||||
private static void disconnectAsync(HttpURLConnection active) {
|
||||
@@ -52,6 +55,12 @@ public final class Downloader {
|
||||
});
|
||||
}
|
||||
|
||||
private static void closeAsync(InputStream active) {
|
||||
CANCEL_EXECUTOR.execute(() -> {
|
||||
try { active.close(); } catch (IOException ignored) { }
|
||||
});
|
||||
}
|
||||
|
||||
@Override public boolean getAsBoolean() {
|
||||
return cancelled.get() || (external != null && external.getAsBoolean());
|
||||
}
|
||||
@@ -62,6 +71,13 @@ public final class Downloader {
|
||||
}
|
||||
|
||||
private void detach(HttpURLConnection active) { connection.compareAndSet(active, null); }
|
||||
|
||||
private void attach(InputStream active) {
|
||||
input.set(active);
|
||||
if (getAsBoolean()) closeAsync(active);
|
||||
}
|
||||
|
||||
private void detach(InputStream active) { input.compareAndSet(active, null); }
|
||||
}
|
||||
|
||||
public static String downloadString(String url) {
|
||||
@@ -278,7 +294,9 @@ public final class Downloader {
|
||||
identity.etag = strongETag(etag) ? etag : null;
|
||||
saveIdentity(destination, identity, expectedSha256);
|
||||
long written = have;
|
||||
try (InputStream in = new BufferedInputStream(connection.getInputStream(), 1 << 16);
|
||||
InputStream input = connection.getInputStream();
|
||||
if (cancelled instanceof Cancellation) ((Cancellation) cancelled).attach(input);
|
||||
try (InputStream in = new BufferedInputStream(input, 1 << 16);
|
||||
OutputStream out = new FileOutputStream(destination, appending)) {
|
||||
byte[] buffer = new byte[1 << 16];
|
||||
long lastReport = 0;
|
||||
@@ -291,6 +309,8 @@ public final class Downloader {
|
||||
progress.accept(total > 0 ? written / (float) total : -1f);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (cancelled instanceof Cancellation) ((Cancellation) cancelled).detach(input);
|
||||
}
|
||||
if (isCancelled(cancelled)) return Outcome.CANCELLED;
|
||||
if (total > 0 && written < total) return Outcome.RETRY;
|
||||
|
||||
Reference in new issue
Block a user