Skip to content

Commit 7cdacf3

Browse files
kilinksbrannen
authored andcommitted
Introduce toString(Charset) in FastByteArrayOutputStream
This commit introduces a toString() overload in FastByteArrayOutputStream that accepts a Charset in order to mirror the method that was introduced in ByteArrayOutputStream in JDK 10, including a special case for when a single buffer is in use internally to avoid the need to resize. This commit also updates getContentAsString() in ContentCachingRequestWrapper to use this new toString(Charset) method. Closes spring-projectsgh-31737
1 parent 0bec812 commit 7cdacf3

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
22+
import java.nio.charset.Charset;
2223
import java.security.MessageDigest;
2324
import java.util.ArrayDeque;
2425
import java.util.Deque;
@@ -162,9 +163,26 @@ public void close() {
162163
*/
163164
@Override
164165
public String toString() {
165-
return new String(toByteArrayUnsafe());
166+
return toString(Charset.defaultCharset());
166167
}
167168

169+
/**
170+
* Converts the buffer's contents into a string by decoding the bytes using
171+
* the specified {@link java.nio.charset.Charset charset}.
172+
*
173+
* @param charset the {@linkplain java.nio.charset.Charset charset}
174+
* to be used to decode the {@code bytes}
175+
* @return a String decoded from the buffer's contents
176+
*/
177+
public String toString(Charset charset) {
178+
if (size() == 0) {
179+
return "";
180+
}
181+
if (buffers.size() == 1) {
182+
return new String(buffers.getFirst(), 0, index, charset);
183+
}
184+
return new String(toByteArrayUnsafe(), charset);
185+
}
168186

169187
// Custom methods
170188

spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ void resize() throws Exception {
5757
assertThat(this.os.size()).isEqualTo(sizeBefore);
5858
}
5959

60+
@Test
61+
void stringConversion() throws Exception {
62+
this.os.write(this.helloBytes);
63+
assertThat(this.os.toString()).isEqualTo("Hello World");
64+
assertThat(this.os.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
65+
66+
FastByteArrayOutputStream empty = new FastByteArrayOutputStream();
67+
assertThat(empty.toString()).isEqualTo("");
68+
assertThat(empty.toString(StandardCharsets.US_ASCII)).isEqualTo("");
69+
70+
FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(5);
71+
// Add bytes in multiple writes to ensure we get more than one buffer internally
72+
outputStream.write(this.helloBytes, 0, 5);
73+
outputStream.write(this.helloBytes, 5, 6);
74+
assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
75+
assertThat(outputStream.toString()).isEqualTo("Hello World");
76+
}
77+
6078
@Test
6179
void autoGrow() throws IOException {
6280
this.os.resize(1);

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

+1-1
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.toByteArrayUnsafe(), Charset.forName(getCharacterEncoding()));
208+
return this.cachedContent.toString(Charset.forName(getCharacterEncoding()));
209209
}
210210

211211
/**

0 commit comments

Comments
 (0)