--- old/src/java.base/share/classes/java/io/ByteArrayOutputStream.java 2017-12-11 11:59:41.529891980 -0800 +++ new/src/java.base/share/classes/java/io/ByteArrayOutputStream.java 2017-12-11 11:59:41.225876756 -0800 @@ -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 @@ /** * 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}. + * + *

This method is equivalent to {@code #toString(charset)} that takes a + * {@link java.nio.charset.Charset charset}. + * + *

An invocation of this method of the form + * + *

 {@code
+     *      ByteArrayOutputStream b = ...
+     *      b.toString("UTF-8")
+     *      }
+     * 
+ * + * behaves in exactly the same way as the expression + * + *
 {@code
+     *      ByteArrayOutputStream b = ...
+     *      b.toString(StandardCharsets.UTF_8)
+     *      }
+     * 
* - *

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 @@ } /** + * 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. + * + *

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 c in the resulting string is @@ -257,9 +291,10 @@ * * @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.