< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  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()} and
  35  * {@code toString()}.
  36  * <p>
  37  * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
  38  * this class can be called after the stream has been closed without
  39  * generating an {@code IOException}.
  40  *
  41  * @author  Arthur van Hoff
  42  * @since   1.0
  43  */
  44 
  45 public class ByteArrayOutputStream extends OutputStream {
  46 
  47     /**


 206      * Converts the buffer's contents into a string decoding bytes using the
 207      * platform's default character set. The length of the new {@code String}
 208      * is a function of the character set, and hence may not be equal to the
 209      * size of the buffer.
 210      *
 211      * <p> This method always replaces malformed-input and unmappable-character
 212      * sequences with the default replacement string for the platform's
 213      * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
 214      * class should be used when more control over the decoding process is
 215      * required.
 216      *
 217      * @return String decoded from the buffer's contents.
 218      * @since  1.1
 219      */
 220     public synchronized String toString() {
 221         return new String(buf, 0, count);
 222     }
 223 
 224     /**
 225      * Converts the buffer's contents into a string by decoding the bytes using
 226      * the named {@link java.nio.charset.Charset charset}. The length of the new
 227      * {@code String} is a function of the charset, and hence may not be equal
 228      * to the length of the byte array.

















 229      *
 230      * <p> This method always replaces malformed-input and unmappable-character
 231      * sequences with this charset's default replacement string. The {@link
 232      * java.nio.charset.CharsetDecoder} class should be used when more control
 233      * over the decoding process is required.
 234      *
 235      * @param      charsetName  the name of a supported
 236      *             {@link java.nio.charset.Charset charset}
 237      * @return     String decoded from the buffer's contents.
 238      * @exception  UnsupportedEncodingException
 239      *             If the named charset is not supported
 240      * @since      1.1
 241      */
 242     public synchronized String toString(String charsetName)
 243         throws UnsupportedEncodingException
 244     {
 245         return new String(buf, 0, count, charsetName);
 246     }
 247 
 248     /**




















 249      * Creates a newly allocated string. Its size is the current size of
 250      * the output stream and the valid contents of the buffer have been
 251      * copied into it. Each character <i>c</i> in the resulting string is
 252      * constructed from the corresponding element <i>b</i> in the byte
 253      * array such that:
 254      * <blockquote><pre>{@code
 255      *     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
 256      * }</pre></blockquote>
 257      *
 258      * @deprecated This method does not properly convert bytes into characters.
 259      * As of JDK&nbsp;1.1, the preferred way to do this is via the
 260      * {@code toString(String enc)} method, which takes an encoding-name
 261      * argument, or the {@code toString()} method, which uses the
 262      * platform's default character encoding.

 263      *
 264      * @param      hibyte    the high byte of each resulting Unicode character.
 265      * @return     the current contents of the output stream, as a string.
 266      * @see        java.io.ByteArrayOutputStream#size()
 267      * @see        java.io.ByteArrayOutputStream#toString(String)
 268      * @see        java.io.ByteArrayOutputStream#toString()
 269      */
 270     @Deprecated
 271     public synchronized String toString(int hibyte) {
 272         return new String(buf, hibyte, 0, count);
 273     }
 274 
 275     /**
 276      * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
 277      * this class can be called after the stream has been closed without
 278      * generating an {@code IOException}.
 279      */
 280     public void close() throws IOException {
 281     }
 282 
   1 /*
   2  * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  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.nio.charset.Charset;
  29 import java.util.Arrays;
  30 
  31 /**
  32  * This class implements an output stream in which the data is
  33  * written into a byte array. The buffer automatically grows as data
  34  * is written to it.
  35  * The data can be retrieved using {@code toByteArray()} and
  36  * {@code toString()}.
  37  * <p>
  38  * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
  39  * this class can be called after the stream has been closed without
  40  * generating an {@code IOException}.
  41  *
  42  * @author  Arthur van Hoff
  43  * @since   1.0
  44  */
  45 
  46 public class ByteArrayOutputStream extends OutputStream {
  47 
  48     /**


 207      * Converts the buffer's contents into a string decoding bytes using the
 208      * platform's default character set. The length of the new {@code String}
 209      * is a function of the character set, and hence may not be equal to the
 210      * size of the buffer.
 211      *
 212      * <p> This method always replaces malformed-input and unmappable-character
 213      * sequences with the default replacement string for the platform's
 214      * default character set. The {@linkplain java.nio.charset.CharsetDecoder}
 215      * class should be used when more control over the decoding process is
 216      * required.
 217      *
 218      * @return String decoded from the buffer's contents.
 219      * @since  1.1
 220      */
 221     public synchronized String toString() {
 222         return new String(buf, 0, count);
 223     }
 224 
 225     /**
 226      * Converts the buffer's contents into a string by decoding the bytes using
 227      * the named {@link java.nio.charset.Charset charset}.
 228      *
 229      * <p> This method is equivalent to {@code #toString(charset)} that takes a
 230      * {@link java.nio.charset.Charset charset}.
 231      *
 232      * <p> An invocation of this method of the form
 233      *
 234      * <pre> {@code
 235      *      ByteArrayOutputStream b = ...
 236      *      b.toString("UTF-8")
 237      *      }
 238      * </pre>
 239      *
 240      * behaves in exactly the same way as the expression
 241      *
 242      * <pre> {@code
 243      *      ByteArrayOutputStream b = ...
 244      *      b.toString(StandardCharsets.UTF_8)
 245      *      }
 246      * </pre>
 247      *




 248      *
 249      * @param      charsetName  the name of a supported
 250      *             {@link java.nio.charset.Charset charset}
 251      * @return     String decoded from the buffer's contents.
 252      * @exception  UnsupportedEncodingException
 253      *             If the named charset is not supported
 254      * @since      1.1
 255      */
 256     public synchronized String toString(String charsetName)
 257         throws UnsupportedEncodingException
 258     {
 259         return new String(buf, 0, count, charsetName);
 260     }
 261 
 262     /**
 263      * Converts the buffer's contents into a string by decoding the bytes using
 264      * the specified {@link java.nio.charset.Charset charset}. The length of the new
 265      * {@code String} is a function of the charset, and hence may not be equal
 266      * to the length of the byte array.
 267      *
 268      * <p> This method always replaces malformed-input and unmappable-character
 269      * sequences with the charset's default replacement string. The {@link
 270      * java.nio.charset.CharsetDecoder} class should be used when more control
 271      * over the decoding process is required.
 272      *
 273      * @param      charset  the {@linkplain java.nio.charset.Charset charset}
 274      *             to be used to decode the {@code bytes}
 275      * @return     String decoded from the buffer's contents.
 276      * @since      10
 277      */
 278     public synchronized String toString(Charset charset) {
 279         return new String(buf, 0, count, charset);
 280     }
 281 
 282     /**
 283      * Creates a newly allocated string. Its size is the current size of
 284      * the output stream and the valid contents of the buffer have been
 285      * copied into it. Each character <i>c</i> in the resulting string is
 286      * constructed from the corresponding element <i>b</i> in the byte
 287      * array such that:
 288      * <blockquote><pre>{@code
 289      *     c == (char)(((hibyte & 0xff) << 8) | (b & 0xff))
 290      * }</pre></blockquote>
 291      *
 292      * @deprecated This method does not properly convert bytes into characters.
 293      * As of JDK&nbsp;1.1, the preferred way to do this is via the
 294      * {@link #toString(String charsetName)} or {@link #toString(Charset charset)}
 295      * method, which takes an encoding-name or charset argument,
 296      * or the {@code toString()} method, which uses the platform's default
 297      * character encoding.
 298      *
 299      * @param      hibyte    the high byte of each resulting Unicode character.
 300      * @return     the current contents of the output stream, as a string.
 301      * @see        java.io.ByteArrayOutputStream#size()
 302      * @see        java.io.ByteArrayOutputStream#toString(String)
 303      * @see        java.io.ByteArrayOutputStream#toString()
 304      */
 305     @Deprecated
 306     public synchronized String toString(int hibyte) {
 307         return new String(buf, hibyte, 0, count);
 308     }
 309 
 310     /**
 311      * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
 312      * this class can be called after the stream has been closed without
 313      * generating an {@code IOException}.
 314      */
 315     public void close() throws IOException {
 316     }
 317 
< prev index next >