src/share/classes/java/io/ByteArrayOutputStream.java

Print this page
rev 10048 : 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
Reviewed-by:


  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.util.Arrays;
  29 
  30 /**
  31  * This class implements an output stream in which the data is
  32  * written into a byte array. The buffer automatically grows as data
  33  * is written to it.
  34  * The data can be retrieved using <code>toByteArray()</code> and
  35  * <code>toString()</code>.
  36  * <p>
  37  * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  38  * this class can be called after the stream has been closed without
  39  * generating an <tt>IOException</tt>.
  40  *
  41  * @author  Arthur van Hoff
  42  * @since   JDK1.0
  43  */
  44 
  45 public class ByteArrayOutputStream extends OutputStream {
  46 
  47     /**
  48      * The buffer where data is stored.
  49      */
  50     protected byte buf[];
  51 
  52     /**
  53      * The number of valid bytes in the buffer.
  54      */
  55     protected int count;
  56 
  57     /**
  58      * Creates a new byte array output stream. The buffer capacity is
  59      * initially 32 bytes, though its size increases if necessary.
  60      */
  61     public ByteArrayOutputStream() {
  62         this(32);


 185      *          of valid bytes in this output stream.
 186      * @see     java.io.ByteArrayOutputStream#count
 187      */
 188     public synchronized int size() {
 189         return count;
 190     }
 191 
 192     /**
 193      * Converts the buffer's contents into a string decoding bytes using the
 194      * platform's default character set. The length of the new <tt>String</tt>
 195      * is a function of the character set, and hence may not be equal to the
 196      * size of the buffer.
 197      *
 198      * <p> This method always replaces malformed-input and unmappable-character
 199      * sequences with the default replacement string for the platform's
 200      * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
 201      * class should be used when more control over the decoding process is
 202      * required.
 203      *
 204      * @return String decoded from the buffer's contents.
 205      * @since  JDK1.1
 206      */
 207     public synchronized String toString() {
 208         return new String(buf, 0, count);
 209     }
 210 
 211     /**
 212      * Converts the buffer's contents into a string by decoding the bytes using
 213      * the named {@link java.nio.charset.Charset charset}. The length of the new
 214      * <tt>String</tt> is a function of the charset, and hence may not be equal
 215      * to the length of the byte array.
 216      *
 217      * <p> This method always replaces malformed-input and unmappable-character
 218      * sequences with this charset's default replacement string. The {@link
 219      * java.nio.charset.CharsetDecoder} class should be used when more control
 220      * over the decoding process is required.
 221      *
 222      * @param      charsetName  the name of a supported
 223      *             {@link java.nio.charset.Charset charset}
 224      * @return     String decoded from the buffer's contents.
 225      * @exception  UnsupportedEncodingException
 226      *             If the named charset is not supported
 227      * @since      JDK1.1
 228      */
 229     public synchronized String toString(String charsetName)
 230         throws UnsupportedEncodingException
 231     {
 232         return new String(buf, 0, count, charsetName);
 233     }
 234 
 235     /**
 236      * Creates a newly allocated string. Its size is the current size of
 237      * the output stream and the valid contents of the buffer have been
 238      * copied into it. Each character <i>c</i> in the resulting string is
 239      * constructed from the corresponding element <i>b</i> in the byte
 240      * array such that:
 241      * <blockquote><pre>
 242      *     c == (char)(((hibyte &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
 243      * </pre></blockquote>
 244      *
 245      * @deprecated This method does not properly convert bytes into characters.
 246      * As of JDK&nbsp;1.1, the preferred way to do this is via the
 247      * <code>toString(String enc)</code> method, which takes an encoding-name




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.util.Arrays;
  29 
  30 /**
  31  * This class implements an output stream in which the data is
  32  * written into a byte array. The buffer automatically grows as data
  33  * is written to it.
  34  * The data can be retrieved using <code>toByteArray()</code> and
  35  * <code>toString()</code>.
  36  * <p>
  37  * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in
  38  * this class can be called after the stream has been closed without
  39  * generating an <tt>IOException</tt>.
  40  *
  41  * @author  Arthur van Hoff
  42  * @since   1.0
  43  */
  44 
  45 public class ByteArrayOutputStream extends OutputStream {
  46 
  47     /**
  48      * The buffer where data is stored.
  49      */
  50     protected byte buf[];
  51 
  52     /**
  53      * The number of valid bytes in the buffer.
  54      */
  55     protected int count;
  56 
  57     /**
  58      * Creates a new byte array output stream. The buffer capacity is
  59      * initially 32 bytes, though its size increases if necessary.
  60      */
  61     public ByteArrayOutputStream() {
  62         this(32);


 185      *          of valid bytes in this output stream.
 186      * @see     java.io.ByteArrayOutputStream#count
 187      */
 188     public synchronized int size() {
 189         return count;
 190     }
 191 
 192     /**
 193      * Converts the buffer's contents into a string decoding bytes using the
 194      * platform's default character set. The length of the new <tt>String</tt>
 195      * is a function of the character set, and hence may not be equal to the
 196      * size of the buffer.
 197      *
 198      * <p> This method always replaces malformed-input and unmappable-character
 199      * sequences with the default replacement string for the platform's
 200      * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
 201      * class should be used when more control over the decoding process is
 202      * required.
 203      *
 204      * @return String decoded from the buffer's contents.
 205      * @since  1.1
 206      */
 207     public synchronized String toString() {
 208         return new String(buf, 0, count);
 209     }
 210 
 211     /**
 212      * Converts the buffer's contents into a string by decoding the bytes using
 213      * the named {@link java.nio.charset.Charset charset}. The length of the new
 214      * <tt>String</tt> is a function of the charset, and hence may not be equal
 215      * to the length of the byte array.
 216      *
 217      * <p> This method always replaces malformed-input and unmappable-character
 218      * sequences with this charset's default replacement string. The {@link
 219      * java.nio.charset.CharsetDecoder} class should be used when more control
 220      * over the decoding process is required.
 221      *
 222      * @param      charsetName  the name of a supported
 223      *             {@link java.nio.charset.Charset charset}
 224      * @return     String decoded from the buffer's contents.
 225      * @exception  UnsupportedEncodingException
 226      *             If the named charset is not supported
 227      * @since      1.1
 228      */
 229     public synchronized String toString(String charsetName)
 230         throws UnsupportedEncodingException
 231     {
 232         return new String(buf, 0, count, charsetName);
 233     }
 234 
 235     /**
 236      * Creates a newly allocated string. Its size is the current size of
 237      * the output stream and the valid contents of the buffer have been
 238      * copied into it. Each character <i>c</i> in the resulting string is
 239      * constructed from the corresponding element <i>b</i> in the byte
 240      * array such that:
 241      * <blockquote><pre>
 242      *     c == (char)(((hibyte &amp; 0xff) &lt;&lt; 8) | (b &amp; 0xff))
 243      * </pre></blockquote>
 244      *
 245      * @deprecated This method does not properly convert bytes into characters.
 246      * As of JDK&nbsp;1.1, the preferred way to do this is via the
 247      * <code>toString(String enc)</code> method, which takes an encoding-name