17
17
package org .springframework .web .util ;
18
18
19
19
import java .io .BufferedReader ;
20
- import java .io .ByteArrayOutputStream ;
21
20
import java .io .IOException ;
22
21
import java .io .InputStreamReader ;
23
22
import java .net .URLEncoder ;
36
35
import org .springframework .http .HttpMethod ;
37
36
import org .springframework .http .MediaType ;
38
37
import org .springframework .lang .Nullable ;
38
+ import org .springframework .util .FastByteArrayOutputStream ;
39
39
40
40
/**
41
41
* {@link jakarta.servlet.http.HttpServletRequest} wrapper that caches all content read from
56
56
*/
57
57
public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
58
58
59
- private static final int DEFAULT_BUFFER_SIZE = 1024 ;
60
-
61
- private final ByteArrayOutputStream cachedContent ;
59
+ private final FastByteArrayOutputStream cachedContent = new FastByteArrayOutputStream ();
62
60
63
61
@ Nullable
64
62
private final Integer contentCacheLimit ;
@@ -76,9 +74,6 @@ public class ContentCachingRequestWrapper extends HttpServletRequestWrapper {
76
74
*/
77
75
public ContentCachingRequestWrapper (HttpServletRequest request ) {
78
76
super (request );
79
- int contentLength = request .getContentLength ();
80
- this .cachedContent = new ByteArrayOutputStream (contentLength >= 0 ?
81
- contentLength : DEFAULT_BUFFER_SIZE );
82
77
this .contentCacheLimit = null ;
83
78
}
84
79
@@ -91,9 +86,6 @@ public ContentCachingRequestWrapper(HttpServletRequest request) {
91
86
*/
92
87
public ContentCachingRequestWrapper (HttpServletRequest request , int contentCacheLimit ) {
93
88
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 ));
97
89
this .contentCacheLimit = contentCacheLimit ;
98
90
}
99
91
@@ -213,7 +205,7 @@ public byte[] getContentAsByteArray() {
213
205
* @see #getContentAsByteArray()
214
206
*/
215
207
public String getContentAsString () {
216
- return this .cachedContent .toString ( Charset .forName (getCharacterEncoding ()));
208
+ return new String ( this .cachedContent .toByteArray (), Charset .forName (getCharacterEncoding ()));
217
209
}
218
210
219
211
/**
@@ -262,7 +254,7 @@ public int read(byte[] b) throws IOException {
262
254
return count ;
263
255
}
264
256
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 {
266
258
if (!this .overflow && count > 0 ) {
267
259
if (contentCacheLimit != null &&
268
260
count + cachedContent .size () > contentCacheLimit ) {
0 commit comments