JIU 0.14.3

net.sourceforge.jiu.util
Class SeekableByteArrayOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by net.sourceforge.jiu.util.SeekableByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable

public class SeekableByteArrayOutputStream
extends OutputStream

An extension of OutputStream that writes data to an internal byte array, resizing it when necessary. Similar to ByteArrayOutputStream, but also enables seeking and truncating.

Since:
0.10.0
Author:
Marco Schmidt

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.
 int getPosition()
          Returns the current offset in the output stream.
 int getSize()
          Returns the current size of the output stream.
 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.
 
Methods inherited from class java.io.OutputStream
flush
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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 Detail

close

public void close()
           throws IOException
Closes this output stream. After a call to this method, all write attempts will result in an exception.

Specified by:
close in interface Closeable
Overrides:
close in class OutputStream
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().

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)

seek

public void seek(int newOffset)
          throws IOException
Sets the current position in the output stream to the argument.

Parameters:
newOffset - new offset into the file, must be >= 0 and <= getSize()
Throws:
IOException - if the argument is invalid

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()
Removes all bytes after the current position. After a call to this method, getSize() is equal to getPosition().


write

public void write(int b)
           throws IOException
Writes the least significant eight bits of the argument int to the internal array.

Specified by:
write in class OutputStream
Parameters:
b - int variable that stores the byte value to be written
Throws:
IOException

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);.

Overrides:
write in class OutputStream
Parameters:
data - array to be copied to this stream
Throws:
IOException

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.

Overrides:
write in class OutputStream
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
Throws:
IOException

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
Throws:
IOException - if out has a problem writing the bytes

JIU 0.14.3

Copyright © 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 Marco Schmidt