Always close response body

This commit is contained in:
len 2016-04-19 14:08:35 +02:00
parent 447dfd1e3c
commit 9213fc6999
3 changed files with 12 additions and 12 deletions

View File

@ -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()
}
}

View File

@ -44,21 +44,21 @@ class NetworkHelper(context: Context) {
@JvmOverloads
fun request(request: Request, forceCache: Boolean = false): Observable<Response> {
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<String> {
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<Response> {
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

View File

@ -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;