Skip to content

Commit e452c2e

Browse files
kilinksnicoll
authored andcommitted
Avoid byte array copy in getContentAsString
The getContentAsString method was originally added in d9b8826 to avoid the extra copying inherent to calling ByteArrayOutputStream.toByteArray; however, in f83c609 the class was updated to instead use FastByteArrayOutputStream, and in the process the extra copy was brought back when getContentAsString was changed to call toByteArray. Switch to calling toByteArrayUnsafe, a method provided by FastByteArrayOutputStream, which avoids the extra copy; since we immediately pass the byte array to the String constructor, and it isn't accessed anywhere else, the usage is safe. See gh-31731
1 parent 6ea9fdb commit e452c2e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public byte[] getContentAsByteArray() {
205205
* @see #getContentAsByteArray()
206206
*/
207207
public String getContentAsString() {
208-
return new String(this.cachedContent.toByteArray(), Charset.forName(getCharacterEncoding()));
208+
return new String(this.cachedContent.toByteArrayUnsafe(), Charset.forName(getCharacterEncoding()));
209209
}
210210

211211
/**

0 commit comments

Comments
 (0)