public class UnsafeByteArrayOutputStream extends OutputStream
OutputStream
. Data is written in the given byte[] buffer, until its
length is insufficient. Than the buffer size is doubled and the data is
written.
This class is Unsafe as it is using a buffer which potentially can be changed
from the outside. Moreover, when toByteArray()
is called, the buffer
itself is returned, and not a copy.Constructor and Description |
---|
UnsafeByteArrayOutputStream()
Constructs a new output stream, with a default allocated buffer which can
later be obtained via
toByteArray() . |
UnsafeByteArrayOutputStream(byte[] buffer)
Constructs a new output stream, with a given buffer.
|
UnsafeByteArrayOutputStream(byte[] buffer,
int startPos)
Constructs a new output stream, with a given buffer.
|
Modifier and Type | Method and Description |
---|---|
int |
getStartPos()
Returns the start position data was written to.
|
int |
length()
Returns the number of relevant bytes.
|
void |
reInit(byte[] buffer)
For reuse-ability, this stream object can be re-initialized with another
given buffer, using 0 as default starting position.
|
void |
reInit(byte[] buffer,
int startPos)
For reuse-ability, this stream object can be re-initialized with another
given buffer and starting position.
|
byte[] |
toByteArray()
Returns the byte array saved within the buffer AS IS.
|
void |
write(byte[] b,
int off,
int len)
writes a given byte[], with offset and length to the buffer.
|
void |
write(int value)
writes a given byte(at the form of an int) to the buffer.
|
close, flush, write
public UnsafeByteArrayOutputStream()
toByteArray()
.public UnsafeByteArrayOutputStream(byte[] buffer)
buffer
- some space to which writing will be madepublic UnsafeByteArrayOutputStream(byte[] buffer, int startPos)
buffer
- some space to which writing will be made.startPos
- an index (inclusive) from white data will be written.public void reInit(byte[] buffer, int startPos)
buffer
- some space to which writing will be made.startPos
- an index (inclusive) from white data will be written.public void reInit(byte[] buffer)
buffer
- some space to which writing will be made.public void write(int value) throws IOException
write
in class OutputStream
value
- byte value to be writtenIOException
public void write(byte[] b, int off, int len) throws IOException
write
in class OutputStream
b
- byte buffer, containing the source data to be writtenoff
- index from which data from the buffer b should be writtenlen
- number of bytes that should be writtenIOException
public byte[] toByteArray()
public int length()
public int getStartPos()
reInit(byte[], int)
or
UnsafeByteArrayOutputStream(byte[], int)
and passed a start
position which is not 0.