From 14cbeb5f97feae0927cc32c61619d0b264ab9937 Mon Sep 17 00:00:00 2001 From: Jens Schulze Date: Thu, 10 Oct 2024 10:58:19 +0200 Subject: [PATCH] optimize okhttp4 client to not hold local references to response body --- .../com/commercetools/http/okhttp4/CtOkHttp4Client.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/commercetools/commercetools-okhttp-client4/src/main/java/com/commercetools/http/okhttp4/CtOkHttp4Client.java b/commercetools/commercetools-okhttp-client4/src/main/java/com/commercetools/http/okhttp4/CtOkHttp4Client.java index 746f2450245..a79889b200f 100644 --- a/commercetools/commercetools-okhttp-client4/src/main/java/com/commercetools/http/okhttp4/CtOkHttp4Client.java +++ b/commercetools/commercetools-okhttp-client4/src/main/java/com/commercetools/http/okhttp4/CtOkHttp4Client.java @@ -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") @@ -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(); } }