< prev index next >

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

Print this page




  28 import java.nio.charset.Charset;
  29 import java.util.Arrays;
  30 import java.util.Objects;
  31 
  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.

  73      */
  74     public ByteArrayOutputStream(int size) {
  75         if (size < 0) {
  76             throw new IllegalArgumentException("Negative initial size: "
  77                                                + size);



  78         }
  79         buf = new byte[size];
  80     }
  81 
  82     /**
  83      * Increases the capacity if necessary to ensure that it can hold
  84      * at least the number of elements specified by the minimum
  85      * capacity argument.
  86      *
  87      * @param  minCapacity the desired minimum capacity
  88      * @throws OutOfMemoryError if {@code minCapacity < 0}.  This is
  89      * interpreted as a request for the unsatisfiably large capacity
  90      * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
  91      */
  92     private void ensureCapacity(int minCapacity) {
  93         // overflow-conscious code
  94         if (minCapacity - buf.length > 0)
  95             grow(minCapacity);
  96     }
  97 
  98     /**
  99      * The maximum size of array to allocate.
 100      * Some VMs reserve some header words in an array.
 101      * Attempts to allocate larger arrays may result in
 102      * OutOfMemoryError: Requested array size exceeds VM limit
 103      */
 104     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 105 
 106     /**
 107      * Increases the capacity to ensure that it can hold at least the
 108      * number of elements specified by the minimum capacity argument.
 109      *
 110      * @param minCapacity the desired minimum capacity
 111      */
 112     private void grow(int minCapacity) {




 113         // overflow-conscious code
 114         int oldCapacity = buf.length;
 115         int newCapacity = oldCapacity << 1;
 116         if (newCapacity - minCapacity < 0)
 117             newCapacity = minCapacity;
 118         if (newCapacity - MAX_ARRAY_SIZE > 0)
 119             newCapacity = hugeCapacity(minCapacity);


 120         buf = Arrays.copyOf(buf, newCapacity);
 121     }
 122 
 123     private static int hugeCapacity(int minCapacity) {
 124         if (minCapacity < 0) // overflow
 125             throw new OutOfMemoryError();
 126         return (minCapacity > MAX_ARRAY_SIZE) ?
 127             Integer.MAX_VALUE :
 128             MAX_ARRAY_SIZE;
 129     }
 130 
 131     /**
 132      * Writes the specified byte to this {@code ByteArrayOutputStream}.
 133      *
 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,




  28 import java.nio.charset.Charset;
  29 import java.util.Arrays;
  30 import java.util.Objects;
  31 
  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      * The maximum size of array to allocate.
  50      * Some VMs reserve some header words in an array.
  51      * Attempts to allocate larger arrays may result in
  52      * OutOfMemoryError: Requested array size exceeds VM limit
  53      */
  54     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  55 
  56     /**
  57      * The buffer where data is stored.
  58      */
  59     protected byte buf[];
  60 
  61     /**
  62      * The number of valid bytes in the buffer.
  63      */
  64     protected int count;
  65 
  66     /**
  67      * Creates a new {@code ByteArrayOutputStream}. The buffer capacity is
  68      * initially 32 bytes, though its size increases if necessary.
  69      */
  70     public ByteArrayOutputStream() {
  71         this(32);
  72     }
  73 
  74     /**
  75      * Creates a new {@code ByteArrayOutputStream}, with a buffer capacity of
  76      * the specified size, in bytes.
  77      *
  78      * @implNote
  79      * The maximum size permitted by this implementation is
  80      * {@code Integer.MAX_VALUE - 8}.
  81      *
  82      * @param  size   the initial size.
  83      * @throws IllegalArgumentException if size is negative or exceeds the
  84      * maximum permitted value.
  85      */
  86     public ByteArrayOutputStream(int size) {
  87         if (size < 0) {
  88             throw new IllegalArgumentException("Negative initial size: "
  89                                                + size);
  90         } else if (size > MAX_ARRAY_SIZE) {
  91             throw new IllegalArgumentException
  92                 ("Initial size " + size + " exceeds limit: " + MAX_ARRAY_SIZE);
  93         }
  94         buf = new byte[size];
  95     }
  96 
  97     /**
  98      * Increases the capacity if necessary to ensure that it can hold
  99      * at least the number of elements specified by the minimum
 100      * capacity argument.
 101      *
 102      * @param  minCapacity the desired minimum capacity
 103      * @throws OutOfMemoryError if {@code minCapacity < 0}.  This is
 104      * interpreted as a request for the unsatisfiably large capacity
 105      * {@code (long) Integer.MAX_VALUE + (minCapacity - Integer.MAX_VALUE)}.
 106      */
 107     private void ensureCapacity(int minCapacity) {
 108         // overflow-conscious code
 109         if (minCapacity - buf.length > 0)
 110             grow(minCapacity);
 111     }
 112 
 113     /**








 114      * Increases the capacity to ensure that it can hold at least the
 115      * number of elements specified by the minimum capacity argument.
 116      *
 117      * @param minCapacity the desired minimum capacity
 118      */
 119     private void grow(int minCapacity) {
 120         if (minCapacity < 0) // overflow
 121             throw new OutOfMemoryError("Memory capacity overflow: "
 122                 + minCapacity);
 123 
 124         // overflow-conscious code
 125         int oldCapacity = buf.length;
 126         int newCapacity = oldCapacity << 1;
 127         if (newCapacity - minCapacity < 0)
 128             newCapacity = minCapacity;
 129         if (newCapacity - MAX_ARRAY_SIZE > 0)
 130             throw new OutOfMemoryError
 131                 ("Required capacity " + newCapacity
 132                 + " exceeds limit: " + MAX_ARRAY_SIZE);
 133         buf = Arrays.copyOf(buf, newCapacity);
 134     }
 135 








 136     /**
 137      * Writes the specified byte to this {@code ByteArrayOutputStream}.
 138      *
 139      * @param   b   the byte to be written.
 140      */
 141     public synchronized void write(int b) {
 142         ensureCapacity(count + 1);
 143         buf[count] = (byte) b;
 144         count += 1;
 145     }
 146 
 147     /**
 148      * Writes {@code len} bytes from the specified byte array
 149      * starting at offset {@code off} to this {@code ByteArrayOutputStream}.
 150      *
 151      * @param   b     the data.
 152      * @param   off   the start offset in the data.
 153      * @param   len   the number of bytes to write.
 154      * @throws  NullPointerException if {@code b} is {@code null}.
 155      * @throws  IndexOutOfBoundsException if {@code off} is negative,


< prev index next >