Skip to content

Commit

Permalink
optimize okhttp4 client to not hold local references to response body
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude committed Oct 10, 2024
1 parent c12b266 commit 14cbeb5
Showing 1 changed file with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,15 @@ public okhttp3.Response intercept(Chain chain) throws IOException {
return unzip(response);
}

private okhttp3.Response unzip(final okhttp3.Response response) throws IOException {
private okhttp3.Response unzip(final okhttp3.Response response) {
if (!"gzip".equalsIgnoreCase(response.header("Content-Encoding"))) {
return response;
}

okhttp3.ResponseBody responseBody = response.body();
if (responseBody == null) {
if (response.body() == null) {
return response;
}

GzipSource gzipSource = new GzipSource(responseBody.source());
okhttp3.Headers strippedHeaders = response.headers()
.newBuilder()
.removeAll("Content-Encoding")
Expand All @@ -220,7 +218,8 @@ private okhttp3.Response unzip(final okhttp3.Response response) throws IOExcepti
String contentType = response.header("Content-Type");
return response.newBuilder()
.headers(strippedHeaders)
.body(okhttp3.ResponseBody.create(Okio.buffer(gzipSource), okhttp3.MediaType.get(contentType), -1L))
.body(okhttp3.ResponseBody.create(Okio.buffer(new GzipSource(response.body().source())),
okhttp3.MediaType.get(contentType), -1L))
.build();
}
}
Expand Down

0 comments on commit 14cbeb5

Please sign in to comment.