From d23205da0204e39ab427b5e779b1042a2e4899b0 Mon Sep 17 00:00:00 2001 From: Patrick Strawderman Date: Thu, 30 Nov 2023 15:39:48 -0800 Subject: [PATCH] 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. --- .../springframework/web/util/ContentCachingRequestWrapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a84da9e80e7f..a54475afeae7 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 @@ -205,7 +205,7 @@ public byte[] getContentAsByteArray() { * @see #getContentAsByteArray() */ public String getContentAsString() { - return new String(this.cachedContent.toByteArray(), Charset.forName(getCharacterEncoding())); + return new String(this.cachedContent.toByteArrayUnsafe(), Charset.forName(getCharacterEncoding())); } /**