public class FastByteArrayOutputStream
extends java.io.OutputStream
ByteArrayOutputStream
. Note that
this variant does not extend ByteArrayOutputStream
, unlike
its sibling ResizableByteArrayOutputStream
.
Unlike ByteArrayOutputStream
, this implementation is backed
by a LinkedList
of byte[]
instead of 1 constantly
resizing byte[]
. It does not copy buffers when it gets expanded.
The initial buffer is only created when the stream is first written.
There is also no copying of the internal buffer if its contents is extracted
with the writeTo(OutputStream)
method.
resize(int)
,
ResizableByteArrayOutputStream
Constructor and Description |
---|
FastByteArrayOutputStream()
Create a new
FastByteArrayOutputStream
with the default initial capacity of 256 bytes. |
FastByteArrayOutputStream(int initialBlockSize)
Create a new
FastByteArrayOutputStream
with the specified initial capacity. |
Modifier and Type | Method and Description |
---|---|
void |
close() |
java.io.InputStream |
getInputStream()
Get an
InputStream to retrieve the data in this OutputStream. |
void |
reset()
Reset the contents of this
FastByteArrayOutputStream . |
void |
resize(int targetCapacity)
Resize the internal buffer size to a specified capacity.
|
int |
size()
Return the number of bytes stored in this
FastByteArrayOutputStream . |
byte[] |
toByteArray()
Creates a newly allocated byte array.
|
byte[] |
toByteArrayUnsafe()
Convert the stream's data to a byte array and return the byte array.
|
java.lang.String |
toString()
Convert the buffer's contents into a string decoding bytes using the
platform's default character set.
|
void |
write(byte[] data,
int offset,
int length) |
void |
write(int datum) |
void |
writeTo(java.io.OutputStream out)
Write the buffers content to the given OutputStream.
|
public FastByteArrayOutputStream()
FastByteArrayOutputStream
with the default initial capacity of 256 bytes.public FastByteArrayOutputStream(int initialBlockSize)
FastByteArrayOutputStream
with the specified initial capacity.initialBlockSize
- the initial buffer size in bytespublic void write(int datum) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] data, int offset, int length) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
public java.lang.String toString()
This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The CharsetDecoder class should be used when more control over the decoding process is required.
toString
in class java.lang.Object
public int size()
FastByteArrayOutputStream
.public byte[] toByteArrayUnsafe()
Also replaces the internal structures with the byte array to conserve memory: if the byte array is being made anyways, mind as well as use it. This approach also means that if this method is called twice without any writes in between, the second call is a no-op.
This method is "unsafe" as it returns the internal buffer. Callers should not modify the returned buffer.
size()
,
toByteArray()
public byte[] toByteArray()
Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
size()
,
toByteArrayUnsafe()
public void reset()
FastByteArrayOutputStream
.
All currently accumulated output in the output stream is discarded. The output stream can be used again.
public java.io.InputStream getInputStream()
InputStream
to retrieve the data in this OutputStream.
Note that if any methods are called on the OutputStream
(including, but not limited to, any of the write methods, reset()
,
toByteArray()
, and toByteArrayUnsafe()
) then the
InputStream
's behavior is undefined.
InputStream
of the contents of this OutputStreampublic void writeTo(java.io.OutputStream out) throws java.io.IOException
out
- the OutputStream to write tojava.io.IOException
public void resize(int targetCapacity)
targetCapacity
- the desired size of the bufferjava.lang.IllegalArgumentException
- if the given capacity is smaller than
the actual size of the content stored in the buffer alreadysize()