< prev index next >
src/java.base/share/classes/java/io/ByteArrayOutputStream.java
Print this page
@@ -1,7 +1,7 @@
/*
- * 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -23,10 +23,11 @@
* questions.
*/
package java.io;
+import java.nio.charset.Charset;
import java.util.Arrays;
/**
* This class implements an output stream in which the data is
* written into a byte array. The buffer automatically grows as data
@@ -221,18 +222,31 @@
return new String(buf, 0, count);
}
/**
* 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}
* @return String decoded from the buffer's contents.
* @exception UnsupportedEncodingException
@@ -244,10 +258,30 @@
{
return new String(buf, 0, count, charsetName);
}
/**
+ * 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
* constructed from the corresponding element <i>b</i> in the byte
* array such that:
@@ -255,13 +289,14 @@
* c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
* }</pre></blockquote>
*
* @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.
* @see java.io.ByteArrayOutputStream#size()
* @see java.io.ByteArrayOutputStream#toString(String)
< prev index next >