From f83c6094362b7e16fe08a4307cbcb0015e203d23 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 11 Sep 2023 10:58:01 +0200 Subject: [PATCH] Polish This commit replaces the initial allocation size for the content caching buffer by a `FastByteArrayOutputStream`. With this variant, allocations are cheap and we don't need to apply heuristics anymore to guess the best initial buffer size. See gh-29775 --- .../web/util/ContentCachingRequestWrapper.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java index e63f7fd8664e..a84da9e80e7f 100644 --- a/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java +++ b/spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java @@ -17,7 +17,6 @@ package org.springframework.web.util; import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.URLEncoder; @@ -36,6 +35,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.lang.Nullable; +import org.springframework.util.FastByteArrayOutputStream; /** * {@link jakarta.servlet.http.HttpServletRequest} wrapper that caches all content read from @@ -56,9 +56,7 @@ */ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper { - private static final int DEFAULT_BUFFER_SIZE = 1024; - - private final ByteArrayOutputStream cachedContent; + private final FastByteArrayOutputStream cachedContent = new FastByteArrayOutputStream(); @Nullable private final Integer contentCacheLimit; @@ -76,9 +74,6 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper { */ public ContentCachingRequestWrapper(HttpServletRequest request) { super(request); - int contentLength = request.getContentLength(); - this.cachedContent = new ByteArrayOutputStream(contentLength >= 0 ? - contentLength : DEFAULT_BUFFER_SIZE); this.contentCacheLimit = null; } @@ -91,9 +86,6 @@ public ContentCachingRequestWrapper(HttpServletRequest request) { */ public ContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit) { super(request); - int contentLength = request.getContentLength(); - int initialBufferSize = contentLength >= 0 ? contentLength : DEFAULT_BUFFER_SIZE; - this.cachedContent = new ByteArrayOutputStream(Math.min(initialBufferSize, contentCacheLimit)); this.contentCacheLimit = contentCacheLimit; } @@ -213,7 +205,7 @@ public byte[] getContentAsByteArray() { * @see #getContentAsByteArray() */ public String getContentAsString() { - return this.cachedContent.toString(Charset.forName(getCharacterEncoding())); + return new String(this.cachedContent.toByteArray(), Charset.forName(getCharacterEncoding())); } /** @@ -262,7 +254,7 @@ public int read(byte[] b) throws IOException { return count; } - private void writeToCache(final byte[] b, final int off, int count) { + private void writeToCache(final byte[] b, final int off, int count) throws IOException{ if (!this.overflow && count > 0) { if (contentCacheLimit != null && count + cachedContent.size() > contentCacheLimit) {