You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ck...@apache.org on 2020/08/14 10:32:01 UTC
[httpcomponents-core] branch 5.1.x updated: HTTPCORE-646:
ChunkedInputStream avoids creating unnecessary buffers
This is an automated email from the ASF dual-hosted git repository.
ckozak pushed a commit to branch 5.1.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/5.1.x by this push:
new 89e0dfe HTTPCORE-646: ChunkedInputStream avoids creating unnecessary buffers
89e0dfe is described below
commit 89e0dfeb2a0f4efefc104d59463eb7438b66ca01
Author: Carter Kozak <ck...@ckozak.net>
AuthorDate: Thu Aug 13 17:34:46 2020 -0400
HTTPCORE-646: ChunkedInputStream avoids creating unnecessary buffers
---
.../org/apache/hc/core5/http/impl/io/ChunkedInputStream.java | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java
index 6effb56..f939d42 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedInputStream.java
@@ -64,6 +64,7 @@ public class ChunkedInputStream extends InputStream {
}
private static final int BUFFER_SIZE = 2048;
+ private static final Header[] EMPTY_FOOTERS = new Header[0];
/** The session input buffer */
private final SessionInputBuffer buffer;
@@ -85,7 +86,7 @@ public class ChunkedInputStream extends InputStream {
/** True if this stream is closed */
private boolean closed = false;
- private Header[] footers = new Header[] {};
+ private Header[] footers = EMPTY_FOOTERS;
/**
* Default constructor.
@@ -306,6 +307,13 @@ public class ChunkedInputStream extends InputStream {
if (!closed) {
try {
if (!eof && state != State.CHUNK_INVALID) {
+ // Optimistically check if the content has been fully read
+ // when there's no data remaining in the current chunk.
+ // This is common when self-terminating content (e.g. JSON)
+ // is parsed from response streams.
+ if (chunkSize == pos && chunkSize > 0 && read() == -1) {
+ return;
+ }
// read and discard the remainder of the message
final byte[] buff = new byte[BUFFER_SIZE];
while (read(buff) >= 0) {
@@ -319,7 +327,7 @@ public class ChunkedInputStream extends InputStream {
}
public Header[] getFooters() {
- return this.footers.clone();
+ return footers.length > 0 ? footers.clone() : EMPTY_FOOTERS;
}
}