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() diskCache.flush()
editor.commit() editor.commit()
} catch (e: Exception) { } catch (e: Exception) {
response.body().close()
throw IOException("Unable to save image") throw IOException("Unable to save image")
} finally { } finally {
response.body().close()
editor?.abortUnlessCommitted() editor?.abortUnlessCommitted()
} }
} }

View file

@ -44,21 +44,21 @@ class NetworkHelper(context: Context) {
@JvmOverloads @JvmOverloads
fun request(request: Request, forceCache: Boolean = false): Observable<Response> { fun request(request: Request, forceCache: Boolean = false): Observable<Response> {
var response: Response? = null
return Observable.fromCallable { return Observable.fromCallable {
val c = if (forceCache) forceCacheClient else client val c = if (forceCache) forceCacheClient else client
c.newCall(request).execute().apply { response = this } c.newCall(request).execute().apply { body().close() }
}.doOnUnsubscribe { response?.body()?.close() } }
} }
@JvmOverloads @JvmOverloads
fun requestBody(request: Request, forceCache: Boolean = false): Observable<String> { fun requestBody(request: Request, forceCache: Boolean = false): Observable<String> {
return request(request, forceCache) return Observable.fromCallable {
.map { it.body().string() } val c = if (forceCache) forceCacheClient else client
c.newCall(request).execute().body().string()
}
} }
fun requestBodyProgress(request: Request, listener: ProgressListener): Observable<Response> { fun requestBodyProgress(request: Request, listener: ProgressListener): Observable<Response> {
var response: Response? = null
return Observable.fromCallable { return Observable.fromCallable {
val progressClient = client.newBuilder() val progressClient = client.newBuilder()
.cache(null) .cache(null)
@ -70,8 +70,8 @@ class NetworkHelper(context: Context) {
} }
.build() .build()
progressClient.newCall(request).execute().apply { response = this } progressClient.newCall(request).execute()
}.doOnUnsubscribe { response?.body()?.close() }.retry(1) }
} }
val cookies: CookieStore val cookies: CookieStore

View file

@ -5,6 +5,7 @@ import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import okhttp3.internal.Util;
import okio.BufferedSink; import okio.BufferedSink;
import okio.BufferedSource; import okio.BufferedSource;
import okio.Okio; import okio.Okio;
@ -59,9 +60,8 @@ public final class DiskUtils {
writeFile.delete(); writeFile.delete();
throw new IOException("Unable to save image"); throw new IOException("Unable to save image");
} finally { } finally {
if (bufferedSink != null) { Util.closeQuietly(bufferedSink);
bufferedSink.close(); Util.closeQuietly(bufferedSource);
}
} }
return writeFile; return writeFile;