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)
Writes
len bytes from the specified byte array
starting at offset off to this output stream. |
void |
write(int b)
Writes the specified byte to this output stream.
|
close, flush, writepublic 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)
java.io.OutputStreamwrite is that one byte is written
to the output stream. The byte to be written is the eight
low-order bits of the argument b. The 24
high-order bits of b are ignored.
Subclasses of OutputStream must provide an
implementation for this method.
write in class OutputStreamb - the byte.public void write(byte[] b,
int off,
int len)
throws IOException
java.io.OutputStreamlen bytes from the specified byte array
starting at offset off to this output stream.
The general contract for write(b, off, len) is that
some of the bytes in the array b are written to the
output stream in order; element b[off] is the first
byte written and b[off+len-1] is the last byte written
by this operation.
The write method of OutputStream calls
the write method of one argument on each of the bytes to be
written out. Subclasses are encouraged to override this method and
provide a more efficient implementation.
If b is null, a
NullPointerException is thrown.
If off is negative, or len is negative, or
off+len is greater than the length of the array
b, then an IndexOutOfBoundsException is thrown.
write in class OutputStreamb - the data.off - the start offset in the data.len - the number of bytes to write.IOException - if an I/O error occurs. In particular,
an IOException is thrown if the output
stream is closed.public void position(long newPosition)
RepositionableStreamposition in interface RepositionableStreamnewPosition - the new stream position.public long position()
RepositionableStreamposition in interface MeasurableStreamposition in interface RepositionableStreampublic long length()
throws IOException
MeasurableStreamlength in interface MeasurableStreamIOException