Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
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 spring-projectsgh-29775
  • Loading branch information
bclozel committed Sep 11, 2023
1 parent 6de0be1 commit f83c609
Showing 1 changed file with 4 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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()));
}

/**
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit f83c609

Please sign in to comment.