Class FastByteArrayOutputStream
java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
VASSAL.tools.io.FastByteArrayOutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
A
ByteArrayOutputStream
which does not retain its byte buffer,
and can produce an InputStream
sharing the same byte buffer.
The advantage of this is that the byte buffer can be shared between the
output and input, and provides better performance when the stream
will be used only a single time and the buffer is full when
toByteArray()
is called.- Since:
- 3.1.0
- Author:
- Joel Uckelman
-
Field Summary
Fields inherited from class java.io.ByteArrayOutputStream
buf, count
-
Constructor Summary
ConstructorDescriptionCreates a new byte array output stream.FastByteArrayOutputStream
(int size) Creates a new byte array output stream, with the specified buffer capacity, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
Creates a newly allocated byte array.Creates anInputStream
which reads the bytes stored by thisFastByteArrayOutputStream
.Methods inherited from class java.io.ByteArrayOutputStream
close, reset, size, toString, toString, toString, toString, write, write, writeBytes, writeTo
Methods inherited from class java.io.OutputStream
flush, nullOutputStream, write
-
Constructor Details
-
FastByteArrayOutputStream
public FastByteArrayOutputStream()Creates a new byte array output stream. The buffer capacity is initially 32 bytes. -
FastByteArrayOutputStream
public FastByteArrayOutputStream(int size) Creates a new byte array output stream, with the specified buffer capacity, in bytes.- Parameters:
size
- the initial size
-
-
Method Details
-
toByteArray
public byte[] toByteArray()Creates a newly allocated byte array. Its size is the current size of this output stream. If the buffer is full, then the array returned is the buffer itself, not a copy, and a new empty buffer is created. Otherwise, a copy of valid portion of the buffer is returned.- Overrides:
toByteArray
in classByteArrayOutputStream
- Returns:
- the current contents of this output stream as a byte array
-
toInputStream
Creates anInputStream
which reads the bytes stored by thisFastByteArrayOutputStream
.- Returns:
- the
InputStream
for this buffer
-