< prev index next >

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

Print this page




  32 /**
  33  * This class implements an output stream in which the data is
  34  * written into a byte array. The buffer automatically grows as data
  35  * is written to it.
  36  * The data can be retrieved using {@code toByteArray()} and
  37  * {@code toString()}.
  38  * <p>
  39  * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
  40  * this class can be called after the stream has been closed without
  41  * generating an {@code IOException}.
  42  *
  43  * @author  Arthur van Hoff
  44  * @since   1.0
  45  */
  46 
  47 public class ByteArrayOutputStream extends OutputStream {
  48 
  49     /**
  50      * The buffer where data is stored.
  51      */
  52     protected byte buf[];
  53 
  54     /**
  55      * The number of valid bytes in the buffer.
  56      */
  57     protected int count;
  58 
  59     /**
  60      * Creates a new {@code ByteArrayOutputStream}. The buffer capacity is
  61      * initially 32 bytes, though its size increases if necessary.
  62      */
  63     public ByteArrayOutputStream() {
  64         this(32);
  65     }
  66 
  67     /**
  68      * Creates a new {@code ByteArrayOutputStream}, with a buffer capacity of
  69      * the specified size, in bytes.
  70      *
  71      * @param  size   the initial size.
  72      * @throws IllegalArgumentException if size is negative.


 134      * @param   b   the byte to be written.
 135      */
 136     public synchronized void write(int b) {
 137         ensureCapacity(count + 1);
 138         buf[count] = (byte) b;
 139         count += 1;
 140     }
 141 
 142     /**
 143      * Writes {@code len} bytes from the specified byte array
 144      * starting at offset {@code off} to this {@code ByteArrayOutputStream}.
 145      *
 146      * @param   b     the data.
 147      * @param   off   the start offset in the data.
 148      * @param   len   the number of bytes to write.
 149      * @throws  NullPointerException if {@code b} is {@code null}.
 150      * @throws  IndexOutOfBoundsException if {@code off} is negative,
 151      * {@code len} is negative, or {@code len} is greater than
 152      * {@code b.length - off}
 153      */
 154     public synchronized void write(byte b[], int off, int len) {
 155         Objects.checkFromIndexSize(off, len, b.length);
 156         ensureCapacity(count + len);
 157         System.arraycopy(b, off, buf, count, len);
 158         count += len;
 159     }
 160 
 161     /**
 162      * Writes the complete contents of the specified byte array
 163      * to this {@code ByteArrayOutputStream}.
 164      *
 165      * @apiNote
 166      * This method is equivalent to {@link #write(byte[],int,int)
 167      * write(b, 0, b.length)}.
 168      *
 169      * @param   b     the data.
 170      * @throws  NullPointerException if {@code b} is {@code null}.
 171      * @since   11
 172      */
 173     public void writeBytes(byte b[]) {
 174         write(b, 0, b.length);
 175     }
 176 
 177     /**
 178      * Writes the complete contents of this {@code ByteArrayOutputStream} to
 179      * the specified output stream argument, as if by calling the output
 180      * stream's write method using {@code out.write(buf, 0, count)}.
 181      *
 182      * @param   out   the output stream to which to write the data.
 183      * @throws  NullPointerException if {@code out} is {@code null}.
 184      * @throws  IOException if an I/O error occurs.
 185      */
 186     public synchronized void writeTo(OutputStream out) throws IOException {
 187         out.write(buf, 0, count);
 188     }
 189 
 190     /**
 191      * Resets the {@code count} field of this {@code ByteArrayOutputStream}
 192      * to zero, so that all currently accumulated output in the
 193      * output stream is discarded. The output stream can be used again,




  32 /**
  33  * This class implements an output stream in which the data is
  34  * written into a byte array. The buffer automatically grows as data
  35  * is written to it.
  36  * The data can be retrieved using {@code toByteArray()} and
  37  * {@code toString()}.
  38  * <p>
  39  * Closing a {@code ByteArrayOutputStream} has no effect. The methods in
  40  * this class can be called after the stream has been closed without
  41  * generating an {@code IOException}.
  42  *
  43  * @author  Arthur van Hoff
  44  * @since   1.0
  45  */
  46 
  47 public class ByteArrayOutputStream extends OutputStream {
  48 
  49     /**
  50      * The buffer where data is stored.
  51      */
  52     protected byte[] buf;
  53 
  54     /**
  55      * The number of valid bytes in the buffer.
  56      */
  57     protected int count;
  58 
  59     /**
  60      * Creates a new {@code ByteArrayOutputStream}. The buffer capacity is
  61      * initially 32 bytes, though its size increases if necessary.
  62      */
  63     public ByteArrayOutputStream() {
  64         this(32);
  65     }
  66 
  67     /**
  68      * Creates a new {@code ByteArrayOutputStream}, with a buffer capacity of
  69      * the specified size, in bytes.
  70      *
  71      * @param  size   the initial size.
  72      * @throws IllegalArgumentException if size is negative.


 134      * @param   b   the byte to be written.
 135      */
 136     public synchronized void write(int b) {
 137         ensureCapacity(count + 1);
 138         buf[count] = (byte) b;
 139         count += 1;
 140     }
 141 
 142     /**
 143      * Writes {@code len} bytes from the specified byte array
 144      * starting at offset {@code off} to this {@code ByteArrayOutputStream}.
 145      *
 146      * @param   b     the data.
 147      * @param   off   the start offset in the data.
 148      * @param   len   the number of bytes to write.
 149      * @throws  NullPointerException if {@code b} is {@code null}.
 150      * @throws  IndexOutOfBoundsException if {@code off} is negative,
 151      * {@code len} is negative, or {@code len} is greater than
 152      * {@code b.length - off}
 153      */
 154     public synchronized void write(byte[] b, int off, int len) {
 155         Objects.checkFromIndexSize(off, len, b.length);
 156         ensureCapacity(count + len);
 157         System.arraycopy(b, off, buf, count, len);
 158         count += len;
 159     }
 160 
 161     /**
 162      * Writes the complete contents of the specified byte array
 163      * to this {@code ByteArrayOutputStream}.
 164      *
 165      * @apiNote
 166      * This method is equivalent to {@link #write(byte[],int,int)
 167      * write(b, 0, b.length)}.
 168      *
 169      * @param   b     the data.
 170      * @throws  NullPointerException if {@code b} is {@code null}.
 171      * @since   11
 172      */
 173     public void writeBytes(byte[] b) {
 174         write(b, 0, b.length);
 175     }
 176 
 177     /**
 178      * Writes the complete contents of this {@code ByteArrayOutputStream} to
 179      * the specified output stream argument, as if by calling the output
 180      * stream's write method using {@code out.write(buf, 0, count)}.
 181      *
 182      * @param   out   the output stream to which to write the data.
 183      * @throws  NullPointerException if {@code out} is {@code null}.
 184      * @throws  IOException if an I/O error occurs.
 185      */
 186     public synchronized void writeTo(OutputStream out) throws IOException {
 187         out.write(buf, 0, count);
 188     }
 189 
 190     /**
 191      * Resets the {@code count} field of this {@code ByteArrayOutputStream}
 192      * to zero, so that all currently accumulated output in the
 193      * output stream is discarded. The output stream can be used again,


< prev index next >