net.sourceforge.jiu.util

Class 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.
Author:
Marco Schmidt
Since:
0.10.0

Field Summary

private byte[]
buffer
private boolean
closed
private int
incrementStep
private int
offset
private int
size

Constructor Summary

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.

Method Summary

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.

Field Details

buffer

private byte[] buffer

closed

private boolean closed

incrementStep

private int incrementStep

offset

private int offset

size

private int size

Constructor Details

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.
Parameters:
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.
Parameters:
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)

Method Details

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:
current position in the output stream, 0-based

getSize

public int getSize()
Returns the current size of the output stream.
Returns:
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.

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.
Returns:
a copy of the byte[] data stored internally

truncate

public void truncate()

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);.
Parameters:
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.
Parameters:
src - the array from which data is copied
srcOffset - int index into that array pointing to the first byte to be copied
num - 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.
Parameters:
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.
Parameters:
out - the output stream to which this stream's content is copied