Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce toString(Charset) in FastByteArrayOutputStream #31737

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.ArrayDeque;
import java.util.Deque;
Expand Down Expand Up @@ -162,9 +163,26 @@ public void close() {
*/
@Override
public String toString() {
return new String(toByteArrayUnsafe());
return toString(Charset.defaultCharset());
}

/**
* Converts the buffer's contents into a string by decoding the bytes using
* the specified {@link java.nio.charset.Charset charset}.
*
* @param charset the {@linkplain java.nio.charset.Charset charset}
* to be used to decode the {@code bytes}
* @return a String decoded from the buffer's contents
*/
public String toString(Charset charset) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference, please keep in mind that we do not indent Javadoc tag content and that we need to add a @since tag for new elements in the public API.

However, I'll address those issues when merging, so there's no need to update this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, references to instance fields must be prefixed with this..

See Spring Framework's Code Style documentation for details.

if (size() == 0) {
return "";
}
if (buffers.size() == 1) {
return new String(buffers.getFirst(), 0, index, charset);
}
return new String(toByteArrayUnsafe(), charset);
}

// Custom methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ void resize() throws Exception {
assertThat(this.os.size()).isEqualTo(sizeBefore);
}

@Test
void stringConversion() throws Exception {
this.os.write(this.helloBytes);
assertThat(this.os.toString()).isEqualTo("Hello World");
assertThat(this.os.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");

FastByteArrayOutputStream empty = new FastByteArrayOutputStream();
assertThat(empty.toString()).isEqualTo("");
assertThat(empty.toString(StandardCharsets.US_ASCII)).isEqualTo("");

FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(5);
// Add bytes in multiple writes to ensure we get more than one buffer internally
outputStream.write(this.helloBytes, 0, 5);
outputStream.write(this.helloBytes, 5, 6);
assertThat(outputStream.toString(StandardCharsets.UTF_8)).isEqualTo("Hello World");
assertThat(outputStream.toString()).isEqualTo("Hello World");
}

@Test
void autoGrow() throws IOException {
this.os.resize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public byte[] getContentAsByteArray() {
* @see #getContentAsByteArray()
*/
public String getContentAsString() {
return new String(this.cachedContent.toByteArrayUnsafe(), Charset.forName(getCharacterEncoding()));
return this.cachedContent.toString(Charset.forName(getCharacterEncoding()));
}

/**
Expand Down