Proper closing of okhttp streams

captchafix2
Floens 11 years ago
parent 08f94ed17f
commit 76e14bd5d1
  1. 2
      Clover/app/src/main/java/org/floens/chan/ui/view/ThumbnailImageView.java
  2. 33
      Clover/app/src/main/java/org/floens/chan/utils/FileCache.java

@ -312,7 +312,7 @@ public class ThumbnailImageView extends LoadView implements View.OnClickListener
} }
public void onNotFoundError() { public void onNotFoundError() {
Toast.makeText(getContext(), R.string.image_not_found, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.image_not_found, Toast.LENGTH_SHORT).show();
callback.setProgress(false); callback.setProgress(false);
} }

@ -2,6 +2,7 @@ package org.floens.chan.utils;
import android.util.Log; import android.util.Log;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request; import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response; import com.squareup.okhttp.Response;
@ -145,6 +146,8 @@ public class FileCache {
private Closeable downloadInput; private Closeable downloadInput;
private Closeable downloadOutput; private Closeable downloadOutput;
private Call call;
private ResponseBody body;
public FileCacheDownloader(FileCache fileCache, String url, File output, DownloadedCallback callback) { public FileCacheDownloader(FileCache fileCache, String url, File output, DownloadedCallback callback) {
this.fileCache = fileCache; this.fileCache = fileCache;
@ -158,9 +161,14 @@ public class FileCache {
execute(); execute();
} catch (InterruptedIOException | InterruptedException e) { } catch (InterruptedIOException | InterruptedException e) {
cancelDueToCancellation(e); cancelDueToCancellation(e);
return;
} catch (Exception e) { } catch (Exception e) {
cancelDueToException(e); cancelDueToException(e);
return;
} }
finish();
success();
} }
private void cancelDueToException(Exception e) { private void cancelDueToException(Exception e) {
@ -170,7 +178,7 @@ public class FileCache {
Log.w(TAG, "IOException downloading file", e); Log.w(TAG, "IOException downloading file", e);
purgeOutput(); purgeOutput();
closeStreams(); finish();
post(new Runnable() { post(new Runnable() {
@Override @Override
@ -188,7 +196,7 @@ public class FileCache {
Log.w(TAG, "Cancel due to http error, code: " + code); Log.w(TAG, "Cancel due to http error, code: " + code);
purgeOutput(); purgeOutput();
closeStreams(); finish();
post(new Runnable() { post(new Runnable() {
@Override @Override
@ -206,14 +214,22 @@ public class FileCache {
Log.d(TAG, "Cancel due to cancellation"); Log.d(TAG, "Cancel due to cancellation");
purgeOutput(); purgeOutput();
closeStreams(); finish();
// No callback // No callback
} }
private void closeStreams() { private void finish() {
Util.closeQuietly(downloadInput); Util.closeQuietly(downloadInput);
Util.closeQuietly(downloadOutput); Util.closeQuietly(downloadOutput);
if (call != null) {
call.cancel();
}
if (body != null) {
Util.closeQuietly(body);
}
} }
private void purgeOutput() { private void purgeOutput() {
@ -260,13 +276,14 @@ public class FileCache {
private void execute() throws Exception { private void execute() throws Exception {
Request request = new Request.Builder().url(url).build(); Request request = new Request.Builder().url(url).build();
Response response = fileCache.httpClient.newCall(request).execute(); call = fileCache.httpClient.newCall(request);
Response response = call.execute();
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
cancelDueToHttpError(response.code()); cancelDueToHttpError(response.code());
return; return;
} }
ResponseBody body = response.body(); body = response.body();
long contentLength = body.contentLength(); long contentLength = body.contentLength();
BufferedSource source = body.source(); BufferedSource source = body.source();
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(output)); OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(output));
@ -289,10 +306,6 @@ public class FileCache {
if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException(); if (Thread.currentThread().isInterrupted()) throw new InterruptedIOException();
} }
closeStreams();
success();
} }
} }
} }

Loading…
Cancel
Save