diff options
| author | Joe Wang <joehw@openjdk.org> | 2017-12-12 11:10:12 -0800 |
|---|---|---|
| committer | Joe Wang <joehw@openjdk.org> | 2017-12-12 11:10:12 -0800 |
| commit | 4f080a83af63fe275a273e369657bc1de42e74f9 (patch) | |
| tree | 90cbc6c6c66d58d88df05b9264370a513d82c9db /src/java.base/share/classes/java/io/ByteArrayOutputStream.java | |
| parent | f065141ddc23ab4cf8436af719042d0faf52b04b (diff) | |
| download | openjdk-4f080a83af63fe275a273e369657bc1de42e74f9.tar.gz openjdk-4f080a83af63fe275a273e369657bc1de42e74f9.zip | |
8183743: Umbrella: add overloads that take a Charset parameter
Reviewed-by: alanb, rriggs
Diffstat (limited to 'src/java.base/share/classes/java/io/ByteArrayOutputStream.java')
| -rw-r--r-- | src/java.base/share/classes/java/io/ByteArrayOutputStream.java | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/src/java.base/share/classes/java/io/ByteArrayOutputStream.java b/src/java.base/share/classes/java/io/ByteArrayOutputStream.java index bd867c08d76..7448eac2560 100644 --- a/src/java.base/share/classes/java/io/ByteArrayOutputStream.java +++ b/src/java.base/share/classes/java/io/ByteArrayOutputStream.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,6 +25,7 @@ package java.io; +import java.nio.charset.Charset; import java.util.Arrays; /** @@ -223,14 +224,27 @@ public class ByteArrayOutputStream extends OutputStream { /** * Converts the buffer's contents into a string by decoding the bytes using - * the named {@link java.nio.charset.Charset charset}. The length of the new - * {@code String} is a function of the charset, and hence may not be equal - * to the length of the byte array. + * the named {@link java.nio.charset.Charset charset}. + * + * <p> This method is equivalent to {@code #toString(charset)} that takes a + * {@link java.nio.charset.Charset charset}. + * + * <p> An invocation of this method of the form + * + * <pre> {@code + * ByteArrayOutputStream b = ... + * b.toString("UTF-8") + * } + * </pre> + * + * behaves in exactly the same way as the expression + * + * <pre> {@code + * ByteArrayOutputStream b = ... + * b.toString(StandardCharsets.UTF_8) + * } + * </pre> * - * <p> This method always replaces malformed-input and unmappable-character - * sequences with this charset's default replacement string. The {@link - * java.nio.charset.CharsetDecoder} class should be used when more control - * over the decoding process is required. * * @param charsetName the name of a supported * {@link java.nio.charset.Charset charset} @@ -246,6 +260,26 @@ public class ByteArrayOutputStream extends OutputStream { } /** + * Converts the buffer's contents into a string by decoding the bytes using + * the specified {@link java.nio.charset.Charset charset}. The length of the new + * {@code String} is a function of the charset, and hence may not be equal + * to the length of the byte array. + * + * <p> This method always replaces malformed-input and unmappable-character + * sequences with the charset's default replacement string. The {@link + * java.nio.charset.CharsetDecoder} class should be used when more control + * over the decoding process is required. + * + * @param charset the {@linkplain java.nio.charset.Charset charset} + * to be used to decode the {@code bytes} + * @return String decoded from the buffer's contents. + * @since 10 + */ + public synchronized String toString(Charset charset) { + return new String(buf, 0, count, charset); + } + + /** * Creates a newly allocated string. Its size is the current size of * the output stream and the valid contents of the buffer have been * copied into it. Each character <i>c</i> in the resulting string is @@ -257,9 +291,10 @@ public class ByteArrayOutputStream extends OutputStream { * * @deprecated This method does not properly convert bytes into characters. * As of JDK 1.1, the preferred way to do this is via the - * {@code toString(String enc)} method, which takes an encoding-name - * argument, or the {@code toString()} method, which uses the - * platform's default character encoding. + * {@link #toString(String charsetName)} or {@link #toString(Charset charset)} + * method, which takes an encoding-name or charset argument, + * or the {@code toString()} method, which uses the platform's default + * character encoding. * * @param hibyte the high byte of each resulting Unicode character. * @return the current contents of the output stream, as a string. |
