A specialized OutputStream for class for writing content to an (internal) byte array. As bytes are written to this stream, the byte array may be expanded to hold more bytes. When the writing is considered to be finished, a copy of the byte array can be requested from the class. @see ByteArr
| 28 | * @see ByteArrayInputStream |
| 29 | */ |
| 30 | public class ByteArrayOutputStream extends OutputStream { |
| 31 | /** |
| 32 | * The byte array containing the bytes written. |
| 33 | */ |
| 34 | protected byte[] buf; |
| 35 | |
| 36 | /** |
| 37 | * The number of bytes written. |
| 38 | */ |
| 39 | protected int count; |
| 40 | |
| 41 | /** |
| 42 | * Constructs a new ByteArrayOutputStream with a default size of 32 bytes. |
| 43 | * If more than 32 bytes are written to this instance, the underlying byte |
| 44 | * array will expand. |
| 45 | */ |
| 46 | public ByteArrayOutputStream() { |
| 47 | super(); |
| 48 | buf = new byte[32]; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Constructs a new {@code ByteArrayOutputStream} with a default size of |
| 53 | * {@code size} bytes. If more than {@code size} bytes are written to this |
| 54 | * instance, the underlying byte array will expand. |
| 55 | * |
| 56 | * @param size |
| 57 | * initial size for the underlying byte array, must be |
| 58 | * non-negative. |
| 59 | * @throws IllegalArgumentException |
| 60 | * if {@code size} < 0. |
| 61 | */ |
| 62 | public ByteArrayOutputStream(int size) { |
| 63 | super(); |
| 64 | if (size >= 0) { |
| 65 | buf = new byte[size]; |
| 66 | } else { |
| 67 | throw new IllegalArgumentException(Messages.getString("luni.A8")); //$NON-NLS-1$ |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Closes this stream. This releases system resources used for this stream. |
| 73 | * |
| 74 | * @throws IOException |
| 75 | * if an error occurs while attempting to close this stream. |
| 76 | */ |
| 77 | @Override |
| 78 | public void close() throws IOException { |
| 79 | /** |
| 80 | * Although the spec claims "A closed stream cannot perform output |
| 81 | * operations and cannot be reopened.", this implementation must do |
| 82 | * nothing. |
| 83 | */ |
| 84 | super.close(); |
| 85 | } |
| 86 | |
| 87 | private void expand(int i) { |
nothing calls this directly
no outgoing calls
no test coverage detected