Closeable
, Flushable
, AutoCloseable
public class SeekableByteArrayOutputStream extends OutputStream
OutputStream
that writes data to an internal
byte array, resizing it when necessary.
Similar to ByteArrayOutputStream
, but also enables seeking and truncating.Modifier and Type | Field | Description |
---|---|---|
private byte[] |
buffer |
|
private boolean |
closed |
|
private int |
incrementStep |
|
private int |
offset |
|
private int |
size |
Constructor | Description |
---|---|
SeekableByteArrayOutputStream() |
Creates a new object of this class, setting initial capacity and increment size
to default values.
|
SeekableByteArrayOutputStream(int initialCapacity) |
Creates a new object of this class, setting initial capacity to the argument
value.
|
SeekableByteArrayOutputStream(int initialCapacity,
int increment) |
Creates a new object of this class, setting initial capacity and increment
to the argument values.
|
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Closes this output stream.
|
private void |
ensureSpace(int numBytes) |
|
int |
getPosition() |
Returns the current offset in the output stream.
|
int |
getSize() |
Returns the current size of the output stream.
|
private void |
increaseBuffer(int newLength) |
|
void |
seek(int newOffset) |
Sets the current position in the output stream to the argument.
|
byte[] |
toByteArray() |
Allocates a new
byte[] object, copies getSize() bytes
from the internal byte array to that new array and returns the array. |
void |
truncate() |
Removes all bytes after the current position.
|
void |
write(byte[] data) |
Write the complete argument array to this stream.
|
void |
write(byte[] src,
int srcOffset,
int num) |
Write some bytes from the argument array to this stream.
|
void |
write(int b) |
Writes the least significant eight bits of the argument
int to the internal array. |
void |
writeTo(OutputStream out) |
Writes the bytes in the internal byte array to the argument output stream.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
flush
private byte[] buffer
private boolean closed
private int incrementStep
private int offset
private int size
public SeekableByteArrayOutputStream()
public SeekableByteArrayOutputStream(int initialCapacity)
initialCapacity
- the number of bytes that are allocated in this constructor (0 or larger)public SeekableByteArrayOutputStream(int initialCapacity, int increment)
initialCapacity
- the number of bytes that are allocated in this constructor (0 or larger)increment
- the number of bytes by which the internal byte array is increased if it is full (1 or larger)public void close() throws IOException
close
in interface AutoCloseable
close
in interface Closeable
close
in class OutputStream
IOException
private void ensureSpace(int numBytes) throws IOException
IOException
public int getPosition()
getSize()
.public int getSize()
private void increaseBuffer(int newLength)
public void seek(int newOffset) throws IOException
newOffset
- new offset into the file, must be >= 0 and <= getSize()
IOException
- if the argument is invalidpublic byte[] toByteArray()
byte[]
object, copies getSize()
bytes
from the internal byte array to that new array and returns the array.public void truncate()
getSize()
is equal to getPosition()
.public void write(int b) throws IOException
int
to the internal array.write
in class OutputStream
b
- int variable that stores the byte value to be writtenIOException
public void write(byte[] data) throws IOException
write(data, 0, data.length);
.write
in class OutputStream
data
- array to be copied to this streamIOException
public void write(byte[] src, int srcOffset, int num) throws IOException
write
in class OutputStream
src
- the array from which data is copiedsrcOffset
- int index into that array pointing to the first byte to be copiednum
- number of bytes to be copiedIOException
public void writeTo(OutputStream out) throws IOException
byte[] copy = toByteArray(); out.write(copy, 0, copy.length);However, you with this method you save the allocation of an additional byte array and the copying to that new array.
out
- the output stream to which this stream's content is copiedIOException
- if out has a problem writing the bytes