net.sourceforge.jiu.util
Class SeekableByteArrayOutputStream
OutputStream
net.sourceforge.jiu.util.SeekableByteArrayOutputStream
public class SeekableByteArrayOutputStream
extends OutputStream
An extension of java.io.OutputStream
that writes data to an internal
byte array, resizing it when necessary.
Similar to java.io.ByteArrayOutputStream
, but also enables seeking and truncating.
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.
|
buffer
private byte[] buffer
closed
private boolean closed
incrementStep
private int incrementStep
SeekableByteArrayOutputStream
public SeekableByteArrayOutputStream()
Creates a new object of this class, setting initial capacity and increment size
to default values.
SeekableByteArrayOutputStream
public SeekableByteArrayOutputStream(int initialCapacity)
Creates a new object of this class, setting initial capacity to the argument
value.
The increment size is set to the initial capacity as well if that value is larger
than 0.
Otherwise it is set to a default value.
initialCapacity
- the number of bytes that are allocated in this constructor (0 or larger)
SeekableByteArrayOutputStream
public SeekableByteArrayOutputStream(int initialCapacity,
int increment)
Creates a new object of this class, setting initial capacity and increment
to the argument values.
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)
close
public void close()
throws IOException
Closes this output stream.
After a call to this method, all write attempts will result in an exception.
ensureSpace
private void ensureSpace(int numBytes)
throws IOException
getPosition
public int getPosition()
Returns the current offset in the output stream.
Larger than or equal to 0 and smaller than or equal to
getSize()
.
- current position in the output stream, 0-based
getSize
public int getSize()
Returns the current size of the output stream.
- size of the output stream in bytes (0 or larger)
increaseBuffer
private void increaseBuffer(int newLength)
seek
public void seek(int newOffset)
throws IOException
Sets the current position in the output stream to the argument.
newOffset
- new offset into the file, must be >= 0 and <= getSize()
toByteArray
public byte[] toByteArray()
Allocates a new
byte[]
object, copies
getSize()
bytes
from the internal byte array to that new array and returns the array.
- a copy of the byte[] data stored internally
truncate
public void truncate()
Removes all bytes after the current position.
After a call to this method,
getSize()
is equal to
getPosition()
.
write
public void write(byte[] data)
throws IOException
Write the complete argument array to this stream.
Copies the data to the internal byte array.
Simply calls write(data, 0, data.length);
.
data
- array to be copied to this stream
write
public void write(byte[] src,
int srcOffset,
int num)
throws IOException
Write some bytes from the argument array to this stream.
Copies num bytes starting at src[srcOffset] to this stream.
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 copied
write
public void write(int b)
throws IOException
Writes the least significant eight bits of the argument int
to the internal array.
b
- int variable that stores the byte value to be written
writeTo
public void writeTo(OutputStream out)
throws IOException
Writes the bytes in the internal byte array to the argument output stream.
A call to this method has the same effect as
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 copied