public class ByteArrayOutputStream extends OutputStream
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, the
byte array can be requested from the class.ByteArrayInputStream
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buffer
The byte array containing the bytes written.
|
protected int |
count
The number of bytes written.
|
Constructor and Description |
---|
ByteArrayOutputStream()
Constructs a new ByteArrayOutputStream with a default size of 32 bytes.
|
ByteArrayOutputStream(int size)
Constructs a new
ByteArrayOutputStream with a default size of size bytes. |
Modifier and Type | Method and Description |
---|---|
ByteArrayRange |
closeAndGetByteArray()
Closes the stream and returns its contents as a ByteArrayRange.
|
protected void |
doClose()
Closes this stream.
|
ByteArrayRange |
getByteArray()
Returns the contents of this ByteArrayOutputStream as a byte array.
|
int |
getLength()
Returns the total number of bytes written to this stream so far.
|
void |
reset()
Resets this stream to the beginning of the underlying byte array.
|
byte[] |
toByteArray()
Returns the contents of this ByteArrayOutputStream as a byte array.
|
void |
write(byte[] buffer,
int offset,
int length)
Writes
count bytes from the byte array buffer starting at offset index to this stream. |
void |
write(int oneByte)
Writes the specified byte
oneByte to the OutputStream. |
close, flush, getClosedListener, setClosedListener, write, write, writeLatin1EncodedString, writeUtf8EncodedString
protected byte[] buffer
protected int count
public ByteArrayOutputStream()
public ByteArrayOutputStream(int size)
ByteArrayOutputStream
with a default size of size
bytes. If more than size
bytes are written to this instance, the underlying byte array will expand.size
- initial size for the underlying byte array, must be non-negativeprotected void doClose()
doClose
in class OutputStream
public ByteArrayRange closeAndGetByteArray()
public void reset()
public int getLength()
public ByteArrayRange getByteArray()
public byte[] toByteArray()
public void write(byte[] buffer, int offset, int length)
count
bytes from the byte array buffer
starting at offset index
to this stream.write
in class OutputStream
buffer
- the buffer to be written.offset
- the initial position in buffer
to retrieve bytes.length
- the number of bytes of buffer
to write.java.lang.NullPointerException
- if buffer
is null
.java.lang.IndexOutOfBoundsException
- if offset < 0
or len < 0
, or if offset + len
is greater
than the length of buffer
.public void write(int oneByte)
oneByte
to the OutputStream. Only the low order byte of oneByte
is
written.write
in class OutputStream
oneByte
- the byte to be written.