From 19bf47b6d2e1c542889a298c08b7131e700ad2d5 Mon Sep 17 00:00:00 2001 From: len Date: Tue, 19 Apr 2016 21:04:28 +0200 Subject: [PATCH] Release resources before trying to delete an incomplete file #264 #211 --- .../eu/kanade/tachiyomi/data/download/DownloadManager.kt | 6 +++++- app/src/main/java/eu/kanade/tachiyomi/util/DiskUtils.java | 7 ++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt index cbb234db2..f78738a3a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.kt @@ -233,7 +233,11 @@ class DownloadManager(private val context: Context, private val sourceManager: S page.status = Page.DOWNLOAD_IMAGE return source.getImageProgressResponse(page) .flatMap({ resp -> - DiskUtils.saveBufferedSourceToDirectory(resp.body().source(), directory, filename) + try { + DiskUtils.saveBufferedSourceToDirectory(resp.body().source(), directory, filename) + } finally { + resp.body().close() + } Observable.just(page) }).retry(2) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtils.java b/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtils.java index c529b8eaa..3f7f3a804 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtils.java +++ b/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtils.java @@ -56,12 +56,12 @@ public final class DiskUtils { try { bufferedSink = Okio.buffer(Okio.sink(writeFile)); bufferedSink.writeAll(bufferedSource); + Util.closeQuietly(bufferedSink); } catch (Exception e) { + Util.closeQuietly(bufferedSink); + //noinspection ResultOfMethodCallIgnored writeFile.delete(); throw new IOException("Unable to save image"); - } finally { - Util.closeQuietly(bufferedSink); - Util.closeQuietly(bufferedSource); } return writeFile; @@ -74,6 +74,7 @@ public final class DiskUtils { } } + //noinspection ResultOfMethodCallIgnored inputFile.delete(); }