public class SeekableByteArrayOutputStream
extends java.io.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 and Description |
---|---|
private byte[] |
buffer |
private boolean |
closed |
private int |
incrementStep |
private int |
offset |
private int |
size |
Constructor and 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 and 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(java.io.OutputStream out)
Writes the bytes in the internal byte array to the argument output stream.
|
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 java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
private void ensureSpace(int numBytes) throws java.io.IOException
java.io.IOException
public int getPosition()
getSize()
.public int getSize()
private void increaseBuffer(int newLength)
public void seek(int newOffset) throws java.io.IOException
newOffset
- new offset into the file, must be >= 0 and <= getSize()
java.io.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 java.io.IOException
int
to the internal array.write
in class java.io.OutputStream
b
- int variable that stores the byte value to be writtenjava.io.IOException
public void write(byte[] data) throws java.io.IOException
write(data, 0, data.length);
.write
in class java.io.OutputStream
data
- array to be copied to this streamjava.io.IOException
public void write(byte[] src, int srcOffset, int num) throws java.io.IOException
write
in class java.io.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 copiedjava.io.IOException
public void writeTo(java.io.OutputStream out) throws java.io.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 copiedjava.io.IOException
- if out has a problem writing the bytes