org.blinkenbyte.io
Class ByteArrayBuffer

java.lang.Object
  extended by java.io.OutputStream
      extended by org.blinkenbyte.io.ByteArrayBuffer
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class ByteArrayBuffer
extends java.io.OutputStream

This class represents an output stream that uses two-dimensional byte arrays for storage. This allows it to allocate storage space incrementally without the usual create/copy cycle usually needed to expand an array. It also supports creating an InputStream that can read directly from the byte arrays.


Nested Class Summary
static class ByteArrayBuffer.ByteArrayBufferInputStream
          Class that can extends InputStream and can read directly from the internal buffers of the host ByteArrayBuffer.
 
Field Summary
protected  int bytesUsed
          The number of bytes currently used.
protected  int MAIN_BUFFER_SIZE
          The size of the main buffer.
protected  byte[][] mainBuffer
          The main buffer.
protected  int mainBuffersAllocated
          The number of buffers allocated in the main buffer.
protected  int SUB_BUFFER_LOG
          The base 2 logarithm of the size of the sub-buffers.
protected  int SUB_BUFFER_MASK
          The bitmask used to break an index into a main index and a sub index.
protected  int SUB_BUFFER_SIZE
          The size of the sub-buffers.
protected  int writePtr
          The current location of the write pointer.
 
Constructor Summary
ByteArrayBuffer()
          Constructs a new ByteArrayBuffer using default settings (64KB sub-buffers)
ByteArrayBuffer(int mainBufferSize, int subBufferLog)
          Constructs a new ByteArrayBuffer with the specified dimensions.
 
Method Summary
 void clean()
          Nulls all pointers to aid with garbage collection.
 void close()
          Inherited from OutputStream, but does nothing.
 void fillZeroes()
          Fills all allocated buffers with 0's.
 void flush()
          Inherited from OutputStream; does nothing.
 ByteArrayBuffer.ByteArrayBufferInputStream getInputStream()
          Returns a ByteArrayBufferInputStream that can be used to read directly from the internal buffers of this object.
 ByteArrayBuffer.ByteArrayBufferInputStream getInputStream(boolean _sharedPointer)
          Returns a ByteArrayBufferInputStream that can be used to read directly from the internal buffers of this object.
 int getWritePtr()
          Gets the current value of the write pointer
 int length()
          Returns the current number of bytes used in the buffer.
 int seek(int newWritePtr)
          Changes the write pointer.
 void setLength(int newLength)
          Adjusts the number of bytes used in the buffer.
 void write(byte[] b)
          Writes the byte array b to the buffers.
 void write(byte[] b, int offset, int len)
          Writes len bytes from a byte array starting at b[offset].
 void write(int b)
          Writes a single byte to the buffers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SUB_BUFFER_LOG

protected int SUB_BUFFER_LOG
The base 2 logarithm of the size of the sub-buffers.


SUB_BUFFER_SIZE

protected int SUB_BUFFER_SIZE
The size of the sub-buffers.


SUB_BUFFER_MASK

protected int SUB_BUFFER_MASK
The bitmask used to break an index into a main index and a sub index.


MAIN_BUFFER_SIZE

protected int MAIN_BUFFER_SIZE
The size of the main buffer.


mainBuffer

protected byte[][] mainBuffer
The main buffer.


bytesUsed

protected int bytesUsed
The number of bytes currently used.


writePtr

protected int writePtr
The current location of the write pointer.


mainBuffersAllocated

protected int mainBuffersAllocated
The number of buffers allocated in the main buffer.

Constructor Detail

ByteArrayBuffer

public ByteArrayBuffer()
Constructs a new ByteArrayBuffer using default settings (64KB sub-buffers)


ByteArrayBuffer

public ByteArrayBuffer(int mainBufferSize,
                       int subBufferLog)
Constructs a new ByteArrayBuffer with the specified dimensions.

Parameters:
mainBufferSize - The size of the main buffer. The main buffer simply holds sub-buffers, and will automatically grow as needed.
subBufferLog - The base 2 logarithm of the size of the sub-buffer. Sub-buffers are allocated at their full size as needed.
Method Detail

close

public void close()
Inherited from OutputStream, but does nothing.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream

clean

public void clean()
Nulls all pointers to aid with garbage collection.


flush

public void flush()
Inherited from OutputStream; does nothing.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream

write

public void write(byte[] b)
Writes the byte array b to the buffers.

Overrides:
write in class java.io.OutputStream

write

public void write(byte[] b,
                  int offset,
                  int len)
Writes len bytes from a byte array starting at b[offset].

Overrides:
write in class java.io.OutputStream

write

public void write(int b)
Writes a single byte to the buffers.

Specified by:
write in class java.io.OutputStream

length

public int length()
Returns the current number of bytes used in the buffer.


setLength

public void setLength(int newLength)
Adjusts the number of bytes used in the buffer. If the length is increasing, all new bytes are initialized to 0.


seek

public int seek(int newWritePtr)
Changes the write pointer.


getWritePtr

public int getWritePtr()
Gets the current value of the write pointer


fillZeroes

public void fillZeroes()
Fills all allocated buffers with 0's.


getInputStream

public ByteArrayBuffer.ByteArrayBufferInputStream getInputStream()
Returns a ByteArrayBufferInputStream that can be used to read directly from the internal buffers of this object. The returned stream will have its own read pointer independent of the write pointer.


getInputStream

public ByteArrayBuffer.ByteArrayBufferInputStream getInputStream(boolean _sharedPointer)
Returns a ByteArrayBufferInputStream that can be used to read directly from the internal buffers of this object. The returned stream can specify an independent or shared read pointer. If it is shared, reads affect the write pointer and writes affect the read pointer.

Parameters:
_sharedPointer - Specifies whether the read pointer should be shared with the write pointer.