au.com.zip.cs
Class ByteBuffer

java.lang.Object
  extended by au.com.zip.cs.ByteBuffer
All Implemented Interfaces:
Blob

public class ByteBuffer
extends Object
implements Blob

A self managed byte[] buffer after the fashion of StringBuffer, plus a few utility functions for byte arrays.

Author:
Cameron Simpson <cs@zip.com.au> 13aug2001

Field Summary
 byte[] buf
           
 int off
           
 int used
           
 
Constructor Summary
ByteBuffer()
          Constructs an empty ByteBuffer.
ByteBuffer(byte[] data)
          Constructs a ByteBuffer initially containing the bytes in the array data
ByteBuffer(byte[] data, int doff, int dsize, int palloc, int ialloc)
          Constructs a ByteBuffer with initial content takens from the portion of data denoted by the offset doff and size dsize, with initial internal allocation of palloc bytes and incremental reallocation size of ialloc bytes.
 
Method Summary
static ByteBuffer allocate()
          Allocate a buffer from the pool.
 void append(byte b)
          Append a single byte to the buffer.
 void append(byte[] data)
          Append a byte[] to the buffer.
 void append(byte[] data, int aoff, int asize)
          Append an arbitrary chunk of a byte[] to the buffer.
static void copyDown(byte[] dest, byte[] src, int soff, int ssize)
          Copy an arbitrary chunk of src to the start of the array dest.
static void deallocate(ByteBuffer buf)
          Return a buffer to the pool.
 void delete(int doff, int dsize)
          Delete the specified data from the buffer.
 void empty()
          Discard the data in the buffer.
 InputStream getBinaryStream()
           
 byte[] getBytes(long pos, int len)
           
 void insert(byte[] data, int srcoff, int isize, int dstoff)
          Insert new data into the buffer.
 long length()
           
static void move(byte[] buf, int off1, int off2, int size)
          Move an chunk of buf from one location to another.
 void needsize(int nsize)
          Ensure the buffer is at least as big as nsize.
 long position(Blob pattern, long start)
           
 long position(byte[] pattern, long start)
           
 OutputStream setBinaryStream(long pos)
           
 int setBytes(long pos, byte[] bytes)
           
 int setBytes(long pos, byte[] bytes, int offset, int len)
           
 byte[] toArray()
          Return an array of bytes holding the current content.
 void truncate(long len)
           
 void write(OutputStream s)
          Write the contents of the buffer to the specified OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buf

public byte[] buf

used

public int used

off

public int off
Constructor Detail

ByteBuffer

public ByteBuffer()
Constructs an empty ByteBuffer.


ByteBuffer

public ByteBuffer(byte[] data)
Constructs a ByteBuffer initially containing the bytes in the array data

Parameters:
data - the initial content for the buffer

ByteBuffer

public ByteBuffer(byte[] data,
                  int doff,
                  int dsize,
                  int palloc,
                  int ialloc)
Constructs a ByteBuffer with initial content takens from the portion of data denoted by the offset doff and size dsize, with initial internal allocation of palloc bytes and incremental reallocation size of ialloc bytes.

For no initial data pass null for data and zero for the offset and size.

For the default allocation settings pass zero for palloc and ialloc.

Parameters:
data - the array holding the initial content for the buffer
doff - the start of the initial data
dsize - the size of the initial data
palloc - the initial internal buffer allocation
ialloc - the incremental internal buffer reallocation
Method Detail

allocate

public static final ByteBuffer allocate()
Allocate a buffer from the pool.


deallocate

public static final void deallocate(ByteBuffer buf)
Return a buffer to the pool.

Parameters:
buf - The ByteBuffer to release.

empty

public void empty()
Discard the data in the buffer.


toArray

public final byte[] toArray()
Return an array of bytes holding the current content.


needsize

public final void needsize(int nsize)
Ensure the buffer is at least as big as nsize.

Parameters:
nsize - the requisite new size

copyDown

public static final void copyDown(byte[] dest,
                                  byte[] src,
                                  int soff,
                                  int ssize)
Copy an arbitrary chunk of src to the start of the array dest.

Parameters:
dest - the target array
src - the source array
soff - the offset into the source array
ssize - the number of bytes to copy

move

public static final void move(byte[] buf,
                              int off1,
                              int off2,
                              int size)
Move an chunk of buf from one location to another. The old and new regions may overlap.

Parameters:
buf - the buffer to manipulate
off1 - the current start of the data
off2 - the new start of the data
size - the amount of data to move

insert

public final void insert(byte[] data,
                         int srcoff,
                         int isize,
                         int dstoff)
Insert new data into the buffer.

Parameters:
data - a byte[] holding the new data to insert
srcoff - the start of the new data in data
isize - the amount of data to insert
dstoff - the location in the buffer at which to insert the new data

delete

public final void delete(int doff,
                         int dsize)
Delete the specified data from the buffer.

Parameters:
doff - the start of the data to delete
dsize - the amount of data to delete

append

public final void append(byte[] data,
                         int aoff,
                         int asize)
Append an arbitrary chunk of a byte[] to the buffer.

Parameters:
data - the new data to append to the buffer
aoff - the start of the new data
asize - the size of the new data

append

public final void append(byte[] data)
Append a byte[] to the buffer.

Parameters:
data - the new data to append

append

public final void append(byte b)
Append a single byte to the buffer.

Parameters:
b - the new byte to append

write

public final void write(OutputStream s)
                 throws IOException
Write the contents of the buffer to the specified OutputStream.

Parameters:
s - the stream to which to write the buffer
Throws:
IOException

getBinaryStream

public final InputStream getBinaryStream()
Specified by:
getBinaryStream in interface Blob

getBytes

public final byte[] getBytes(long pos,
                             int len)
Specified by:
getBytes in interface Blob

length

public final long length()
Specified by:
length in interface Blob

position

public final long position(Blob pattern,
                           long start)
                    throws SQLException
Specified by:
position in interface Blob
Throws:
SQLException

position

public final long position(byte[] pattern,
                           long start)
                    throws SQLException
Specified by:
position in interface Blob
Throws:
SQLException

setBinaryStream

public final OutputStream setBinaryStream(long pos)
                                   throws SQLException
Specified by:
setBinaryStream in interface Blob
Throws:
SQLException

setBytes

public final int setBytes(long pos,
                          byte[] bytes)
                   throws SQLException
Specified by:
setBytes in interface Blob
Throws:
SQLException

setBytes

public final int setBytes(long pos,
                          byte[] bytes,
                          int offset,
                          int len)
                   throws SQLException
Specified by:
setBytes in interface Blob
Throws:
SQLException

truncate

public final void truncate(long len)
Specified by:
truncate in interface Blob