public class FastByteArrayOutputStream extends MeasurableOutputStream implements RepositionableStream
ByteArrayOutputStream
is nice, but to get its content you
must generate each time a new object. This doesn't happen here.
This class will automatically enlarge the backing array, doubling its
size whenever new space is needed. The reset()
method will
mark the content as empty, but will not decrease the capacity: use
trim()
for that purpose.
Modifier and Type | Field and Description |
---|---|
byte[] |
array
The array backing the output stream.
|
static int |
DEFAULT_INITIAL_CAPACITY
The array backing the output stream.
|
int |
length
The number of valid bytes in
array . |
Constructor and Description |
---|
FastByteArrayOutputStream()
Creates a new array output stream with an initial capacity of
DEFAULT_INITIAL_CAPACITY bytes. |
FastByteArrayOutputStream(byte[] a)
Creates a new array output stream wrapping a given byte array.
|
FastByteArrayOutputStream(int initialCapacity)
Creates a new array output stream with a given initial capacity.
|
Modifier and Type | Method and Description |
---|---|
long |
length()
Returns the overall length of this stream (optional operation).
|
long |
position()
Returns the current stream position.
|
void |
position(long newPosition)
Sets the current stream position.
|
void |
reset()
Marks this array output stream as empty.
|
void |
trim()
Ensures that the length of the backing array is equal to
length . |
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
close, flush, write
public static final int DEFAULT_INITIAL_CAPACITY
public byte[] array
public int length
array
.public FastByteArrayOutputStream()
DEFAULT_INITIAL_CAPACITY
bytes.public FastByteArrayOutputStream(int initialCapacity)
initialCapacity
- the initial length of the backing array.public FastByteArrayOutputStream(byte[] a)
a
- the byte array to wrap.public void reset()
public void trim()
length
.public void write(int b)
write
in class OutputStream
public void write(byte[] b, int off, int len) throws IOException
write
in class OutputStream
IOException
public void position(long newPosition)
RepositionableStream
position
in interface RepositionableStream
newPosition
- the new stream position.public long position()
RepositionableStream
position
in interface MeasurableStream
position
in interface RepositionableStream
public long length() throws IOException
MeasurableStream
length
in interface MeasurableStream
IOException