Repository navigation
Read a declared-length entity into one array in EntityUtils - #1538
Conversation
When Content-Length is known and within maxResultLength, toByteArray and toString read the content straight into a byte array of that size instead of copying it through a 4 KB buffer, a ByteArrayBuffer and a final copy. Content of unknown length, or longer than declared, takes the old path.
|
@shinseongsu I never cared about This class exists for some extremely special cases such as content asserts in integration tests. Master is enough. We are not going to release off 5.4.x anymore unless there is some super severe security issue. |
|
Thanks for the review and approval! 🙏 Fully agreed on streaming For context, I started by trying out the examples on the Getting Would you be open to a small docs PR that adds a streaming example |
@shinseongsu I am going to remove old "Quick Start" pages as soon as HttpClient 5.7 has been released. They are just terrible. I do see a few instances of Please do feel free to contribute a streaming example. It will be very much appreciated. |
I was profiling the classic client with async-profiler while comparing it with a few other Java HTTP clients, and
EntityUtils.toByteArraystood out on large responses. For a 1 MB body it allocates about 2 MB per call (theByteArrayBuffersized to Content-Length, then thetoByteArray()copy) and reads the socket in 4 KB chunks, so roughly 256read()calls per MB.toStringadds an 8 KBInputStreamReaderbuffer, achar[1024]and aCharArrayBufferon top of that.With this change, when Content-Length is known and within
maxResultLength, the content is read straight into abyte[contentLength]and thenread()is called once more to confirm the end of the stream.toStringdecodes that array withnew String(bytes, charset). If the stream turns out to be longer than declared, the bytes already read are handed to the old loop, so the result is the same as before. Unknown length takes the old path unchanged.Numbers below are the classic client (httpclient5 master) against a Go HTTP/1.1 server on loopback, JDK 21, 60 s per run after a warm-up, two rounds in alternating order. Only the httpcore5 jar differs: 5.5 vs 5.5 with this change (
EntityUtils.javain 5.5 is the same as master). Request counts match the server's own counter in every run.toByteArraytoByteArraytoStringtoStringAt 1 KB the load generator on the same machine is the limit, so the difference there is only allocation. Code that reads
getContent()directly isn't affected; this helpsEntityUtilsandBasicHttpClientResponseHandlerusers.round 2 and CPU flame graphs (1 MB, before / after)
Side effects I checked:
EntityUtilson 300k random inputs (declared length shorter, exact, longer, zero or unknown; differentmaxResultLengthvalues and charsets; random read sizes; streams that throw at the end). Result, exception andclose()are identical except for a stream that is longer than its declared length and also throws at the end, whichContentLengthInputStreamcan't produce.toByteArrayandtoString: stale pooled connection, connection reset on a reused and on a new connection (GET and POST), body shorter than Content-Length, 503 and 429 withRetry-After, response timeout, connect timeout, connection refused, and 32 requests on the default pool. Same result and the same number of attempts on the server before and after. A truncated body still fails withConnectionClosedException: Premature end of Content-Length delimited message body.mvn -pl httpcore5 verifypasses (2031 tests), and httpclient5 master passes against the patched core (2892 tests).Is master enough, or would you like this on 5.4.x too?