From 9213fc69996218a9659d65313a27617859fef30f Mon Sep 17 00:00:00 2001 From: len Date: Tue, 19 Apr 2016 14:08:35 +0200 Subject: [PATCH] Always close response body --- .../kanade/tachiyomi/data/cache/ChapterCache.kt | 2 +- .../tachiyomi/data/network/NetworkHelper.kt | 16 ++++++++-------- .../java/eu/kanade/tachiyomi/util/DiskUtils.java | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt index 1ff58e6f30..8225472bd8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt @@ -202,9 +202,9 @@ class ChapterCache(private val context: Context) { diskCache.flush() editor.commit() } catch (e: Exception) { - response.body().close() throw IOException("Unable to save image") } finally { + response.body().close() editor?.abortUnlessCommitted() } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt index baeb104643..e65d8ee23e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/network/NetworkHelper.kt @@ -44,21 +44,21 @@ class NetworkHelper(context: Context) { @JvmOverloads fun request(request: Request, forceCache: Boolean = false): Observable { - var response: Response? = null return Observable.fromCallable { val c = if (forceCache) forceCacheClient else client - c.newCall(request).execute().apply { response = this } - }.doOnUnsubscribe { response?.body()?.close() } + c.newCall(request).execute().apply { body().close() } + } } @JvmOverloads fun requestBody(request: Request, forceCache: Boolean = false): Observable { - return request(request, forceCache) - .map { it.body().string() } + return Observable.fromCallable { + val c = if (forceCache) forceCacheClient else client + c.newCall(request).execute().body().string() + } } fun requestBodyProgress(request: Request, listener: ProgressListener): Observable { - var response: Response? = null return Observable.fromCallable { val progressClient = client.newBuilder() .cache(null) @@ -70,8 +70,8 @@ class NetworkHelper(context: Context) { } .build() - progressClient.newCall(request).execute().apply { response = this } - }.doOnUnsubscribe { response?.body()?.close() }.retry(1) + progressClient.newCall(request).execute() + } } val cookies: CookieStore 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 ea2d83ddd6..c529b8eaaf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtils.java +++ b/app/src/main/java/eu/kanade/tachiyomi/util/DiskUtils.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import okhttp3.internal.Util; import okio.BufferedSink; import okio.BufferedSource; import okio.Okio; @@ -59,9 +60,8 @@ public final class DiskUtils { writeFile.delete(); throw new IOException("Unable to save image"); } finally { - if (bufferedSink != null) { - bufferedSink.close(); - } + Util.closeQuietly(bufferedSink); + Util.closeQuietly(bufferedSource); } return writeFile;