Repository navigation
Conversation
Decode the body straight from the ByteArrayOutputStream buffer instead of copying it with toByteArray() first, and keep it as a String in HttpResponse: getBody() no longer copies it, appendBody() no longer copies it into a StringBuilder, and toString() copies it once instead of twice. For an ASCII body of N bytes, the peak goes from about Cf + 2N to Cf + N (Cf = N with an uncompressed Content-Length). Add a test of the body read path against a local server (Content-Length, chunked, charset from Content-Type, gzip, empty body, error body). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ead-the-response-body
NassimBtk
deleted the
feature/issue-60-reduce-the-memory-used-to-read-the-response-body
branch
October 8, 2026 13:28
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #60
What changed
HttpClientdecodes the body straight from theByteArrayOutputStreambuffer withbodyBytes.toString(charset.name()). Before, it copied the buffer withtoByteArray()and then decoded the copy.toString(String)exists since Java 1.1 and goes through the same decoder, with the same replacement of malformed input. The charset name comes fromCharset.forName(), so it always resolves to the same charset.HttpResponsekeeps the body as a String instead of aStringBuilder:getBody()returns it without a copy;appendBody()keeps its behavior: it concatenates, and appends"null"fornullasStringBuilder.append()did. Each call after the first copies the body; the Javadoc says so, and the client calls it once per response;toString()(header and body) usesconcat(), which copies the body once instead of twice.Memory
For an ASCII body of N bytes on JDK 9+, the peak goes from about Cf + 2N to Cf + N, where Cf is the buffer capacity: N with an uncompressed
Content-Length, N to 2N when chunked or compressed. In every case one N-byte copy disappears. The benchmark is in #60.The allocation per call is halved (for example 238.7 → 95.5 MiB). With a UTF-16 body, the client now needs no more heap than
Files.readStringon the same content.Tests
HttpClientTestreads bodies from a localcom.sun.net.httpserverserver: with aContent-Length, chunked, charset fromContent-Type(ISO-8859-1), gzip, empty, and an error status (the body comes from the error stream, andtoString()is checked).HttpResponseTestalso checksappendBody(null), appending"", and thatgetBody()returns the String without copying it.mvn verifywith JDK 17 (as in CI): 10 tests pass. The Javadoc error aboutsun.net.www.protocol.httpin the build log is not new:mainshows it too. The integration tests (httpbin: gzip, deflate, UTF-8,downloadTo, status codes) run in the CI workflow.🤖 Generated with Claude Code