Close response body when download is complete
Response.body().source() requires an explicit close() call.
This commit is contained in:
@@ -187,6 +187,7 @@ public class DownloadClient {
|
|||||||
public void onResponse(Response response) {
|
public void onResponse(Response response) {
|
||||||
Log.d(TAG, "Downloading");
|
Log.d(TAG, "Downloading");
|
||||||
|
|
||||||
|
final ResponseBody body = response.body();
|
||||||
final boolean resume = response.code() == 206;
|
final boolean resume = response.code() == 206;
|
||||||
if (resume) {
|
if (resume) {
|
||||||
mResumeOffset = destination.length();
|
mResumeOffset = destination.length();
|
||||||
@@ -194,6 +195,11 @@ public class DownloadClient {
|
|||||||
} else if (!isSuccessful(response.code())) {
|
} else if (!isSuccessful(response.code())) {
|
||||||
Log.e(TAG, "The server replied with code " + response.code());
|
Log.e(TAG, "The server replied with code " + response.code());
|
||||||
callback.onFailure(mCancelled);
|
callback.onFailure(mCancelled);
|
||||||
|
try {
|
||||||
|
body.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "Could not close reponse body", e);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,12 +207,18 @@ public class DownloadClient {
|
|||||||
new Headers(response.headers()));
|
new Headers(response.headers()));
|
||||||
try (BufferedSink sink = Okio.buffer(resume ?
|
try (BufferedSink sink = Okio.buffer(resume ?
|
||||||
Okio.appendingSink(destination) : Okio.sink(destination))) {
|
Okio.appendingSink(destination) : Okio.sink(destination))) {
|
||||||
sink.writeAll(response.body().source());
|
sink.writeAll(body.source());
|
||||||
Log.d(TAG, "Download complete");
|
Log.d(TAG, "Download complete");
|
||||||
sink.flush();
|
sink.flush();
|
||||||
callback.onSuccess(null);
|
callback.onSuccess(null);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
onFailure(request, e);
|
onFailure(request, e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
body.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e(TAG, "Could not close reponse body", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user