Skip to content

Commit f83c609

Browse files
committedSep 11, 2023
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
1 parent 6de0be1 commit f83c609

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed
 

‎spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.web.util;
1818

1919
import java.io.BufferedReader;
20-
import java.io.ByteArrayOutputStream;
2120
import java.io.IOException;
2221
import java.io.InputStreamReader;
2322
import java.net.URLEncoder;
@@ -36,6 +35,7 @@
3635
import org.springframework.http.HttpMethod;
3736
import org.springframework.http.MediaType;
3837
import org.springframework.lang.Nullable;
38+
import org.springframework.util.FastByteArrayOutputStream;
3939

4040
/**
4141
* {@link jakarta.servlet.http.HttpServletRequest} wrapper that caches all content read from
@@ -56,9 +56,7 @@
5656
*/
5757
public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
5858

59-
private static final int DEFAULT_BUFFER_SIZE = 1024;
60-
61-
private final ByteArrayOutputStream cachedContent;
59+
private final FastByteArrayOutputStream cachedContent = new FastByteArrayOutputStream();
6260

6361
@Nullable
6462
private final Integer contentCacheLimit;
@@ -76,9 +74,6 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
7674
*/
7775
public ContentCachingRequestWrapper(HttpServletRequest request) {
7876
super(request);
79-
int contentLength = request.getContentLength();
80-
this.cachedContent = new ByteArrayOutputStream(contentLength >= 0 ?
81-
contentLength : DEFAULT_BUFFER_SIZE);
8277
this.contentCacheLimit = null;
8378
}
8479

@@ -91,9 +86,6 @@ public ContentCachingRequestWrapper(HttpServletRequest request) {
9186
*/
9287
public ContentCachingRequestWrapper(HttpServletRequest request, int contentCacheLimit) {
9388
super(request);
94-
int contentLength = request.getContentLength();
95-
int initialBufferSize = contentLength >= 0 ? contentLength : DEFAULT_BUFFER_SIZE;
96-
this.cachedContent = new ByteArrayOutputStream(Math.min(initialBufferSize, contentCacheLimit));
9789
this.contentCacheLimit = contentCacheLimit;
9890
}
9991

@@ -213,7 +205,7 @@ public byte[] getContentAsByteArray() {
213205
* @see #getContentAsByteArray()
214206
*/
215207
public String getContentAsString() {
216-
return this.cachedContent.toString(Charset.forName(getCharacterEncoding()));
208+
return new String(this.cachedContent.toByteArray(), Charset.forName(getCharacterEncoding()));
217209
}
218210

219211
/**
@@ -262,7 +254,7 @@ public int read(byte[] b) throws IOException {
262254
return count;
263255
}
264256

265-
private void writeToCache(final byte[] b, final int off, int count) {
257+
private void writeToCache(final byte[] b, final int off, int count) throws IOException{
266258
if (!this.overflow && count > 0) {
267259
if (contentCacheLimit != null &&
268260
count + cachedContent.size() > contentCacheLimit) {

0 commit comments

Comments
 (0)
Please sign in to comment.