This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new cb857a86a4 Replace the unused buffer with a zero length, static instance. cb857a86a4 is described below commit cb857a86a44a073857647ad2aa6836cbd711b898 Author: Mark Thomas <ma...@apache.org> AuthorDate: Thu Jan 9 15:19:00 2025 +0000 Replace the unused buffer with a zero length, static instance. --- java/org/apache/catalina/connector/InputBuffer.java | 19 ++++++++++++++----- webapps/docs/changelog.xml | 5 +++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/java/org/apache/catalina/connector/InputBuffer.java b/java/org/apache/catalina/connector/InputBuffer.java index 45d64bc8e5..e7fe09cb1e 100644 --- a/java/org/apache/catalina/connector/InputBuffer.java +++ b/java/org/apache/catalina/connector/InputBuffer.java @@ -56,6 +56,8 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A private static final Log log = LogFactory.getLog(InputBuffer.class); + private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0); + public static final int DEFAULT_BUFFER_SIZE = 8 * 1024; // The buffer can be used for byte[] and char[] reading @@ -72,8 +74,11 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A // ----------------------------------------------------- Instance Variables - /** - * The byte buffer. + /* + * The byte buffer. Data is always injected into this class by calling {@link #setByteBuffer(ByteBuffer)} rather + * than copying data into any existing buffer. It is initialised to an empty buffer as there are code paths that + * access the buffer when it is expected to be empty and an empty buffer gives cleaner code than lots of null + * checks. */ private ByteBuffer bb; @@ -147,8 +152,8 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A */ public InputBuffer(int size, org.apache.coyote.Request coyoteRequest) { this.size = size; - bb = ByteBuffer.allocate(size); - clear(bb); + // Will be replaced when there is data to read so initialise to empty buffer. + bb = EMPTY_BUFFER; cb = CharBuffer.allocate(size); clear(cb); readLimit = size; @@ -175,7 +180,11 @@ public class InputBuffer extends Reader implements ByteChunk.ByteInputChannel, A } readLimit = size; markPos = -1; - clear(bb); + /* + * This buffer will have been be replaced if there was data to read so re-initialise to an empty buffer to clear + * any reference to an injected buffer. + */ + bb = EMPTY_BUFFER; closed = false; if (conv != null) { diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 3589714fea..ab16f2677b 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -182,6 +182,11 @@ Refactor the <code>SavedRequestInputFilter</code> so the buffered data is used directly rather than copied. (markt) </scode> + <scode> + Replace the unused buffer in + <code>org.apache.catalina.connector.InputBuffer</code> with a static, + zero length buffer. (markt) + </scode> </changelog> </subsection> <subsection name="Jasper"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org