org.sapia.ubik.util
Class ByteVector

java.lang.Object
  extended by org.sapia.ubik.util.ByteVector

public class ByteVector
extends java.lang.Object

This class can conveniently be used as a replacement for the JDK's ByteArrayInputStream and ByteArrayOutputStream classes.

This class implements a vector of byte arrays (it encapsulates an array of byte arrays). An instance of this class will "grow" (according to a predefined increment) as new bytes are added to it (and if its current size does not allow for these new bytes to fit in).

In addition, each byte array that is internally kept (and stores the actual bytes written to this instance) is created with a fixed capacity (which can be specified when creating an instance of this class).

An instance of this class internally keeps track of its current position in the vector. That position is incremented according to the reads and writes that are performed. Thus, existing bytes are not overwritten and are not read twice.

An instance of this class can be reused without destroying the internal byte arrays that have been created. Similarly, reading bytes from the instance does not require copying the whole bytes that the instance stores to an intermediary array that is returned to the caller - instead, this instance directly supports reads (this is a workaround to the JDK's ByteArrayOutputStream class and its toByteArray() method.

WARNING: THIS CLASS IS NOT THREAD-SAFE.

Author:
Yanick Duchesne
See Also:
ByteVectorInputStream,
Copyright:
Copyright © 2002-2005 Sapia Open Source Software. All Rights Reserved.
License:
Read the license.txt file of the jar or visit the license page at the Sapia OSS web site

Nested Class Summary
protected static class ByteVector.ByteArray
           
 
Field Summary
protected  int _arrayCount
           
protected  int _arrayPos
           
protected  ByteVector.ByteArray[] _arrays
           
protected  int _capacity
           
protected  int _increment
           
protected  int _markOffset
           
protected  int _markPos
           
static int DEFAULT_ARRAY_CAPACITY
           
static int DEFAULT_INCREMENT
           
 
Constructor Summary
ByteVector()
          Creates an instance of this class with the default capacity and increment.
ByteVector(int capacity, int increment)
          Creates an instance of this class with the given capacity and increment.
 
Method Summary
 void clear(boolean freeMemory)
          Clears the data that this instance holds, making it suitable for reuse.
 boolean hasRemaining()
           
 int length()
          Returns the number of bytes that this instance holds.
 void mark(int mark)
          Internally sets the given position as a mark.
 int position()
           
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 int read(java.nio.ByteBuffer buf)
           
 void read(java.io.OutputStream out)
          Reads this instance's bytes and transfers them to the given stream.
 int remaining()
          Returns the number of remaining bytes in this instance.
 void reset()
          Resets the internal position to the specified mark.
 long skip(long skip)
          Skips the number of bytes within this instance.
 byte[] toByteArray()
          Returns the bytes that this instance holds (starting from this instance's current position).
 void write(byte[] b)
          Writes the given bytes to this instance.
 void write(byte[] b, int off, int len)
          Writes the given bytes to this instance.
 void write(java.nio.ByteBuffer buf)
          Writes the bytes contained in the given buffer to this instance.
 void write(int b)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ARRAY_CAPACITY

public static final int DEFAULT_ARRAY_CAPACITY
See Also:
Constant Field Values

DEFAULT_INCREMENT

public static final int DEFAULT_INCREMENT
See Also:
Constant Field Values

_arrayPos

protected int _arrayPos

_markPos

protected int _markPos

_markOffset

protected int _markOffset

_arrayCount

protected int _arrayCount

_capacity

protected int _capacity

_increment

protected int _increment

_arrays

protected ByteVector.ByteArray[] _arrays
Constructor Detail

ByteVector

public ByteVector()
Creates an instance of this class with the default capacity and increment.


ByteVector

public ByteVector(int capacity,
                  int increment)
Creates an instance of this class with the given capacity and increment. The capacity corresponds to the size of the internal bytes arrays (that are created to store data). The increment corresponds to the amount of internal byte arrays that will be created when this instance will grow.

Parameters:
capacity - some capacity.
increment - some increment.
Method Detail

mark

public void mark(int mark)
Internally sets the given position as a mark. The internal position counter will internally be set to that mark upon the reset method being called.

Parameters:
mark - some mark.
See Also:
reset()

reset

public void reset()
Resets the internal position to the specified mark. The position will be set to 0 if no mark has been specified.

See Also:
mark(int)

clear

public void clear(boolean freeMemory)
Clears the data that this instance holds, making it suitable for reuse.

Parameters:
freeMemory - if true, the internal byte arrays that have been created will be dereferenced (otherwise, they will be reused).

length

public int length()
Returns the number of bytes that this instance holds.


position

public int position()
Returns:
the current internal position of this instance, in terms of the bytes it holds.

remaining

public int remaining()
Returns the number of remaining bytes in this instance.

Returns:
lenth() - position()

hasRemaining

public boolean hasRemaining()
Returns:
true if this instance's current internal position is less than the number of bytes it holds.

toByteArray

public byte[] toByteArray()
Returns the bytes that this instance holds (starting from this instance's current position).


read

public int read()
Returns:
the next byte that this instance holds, or -1 if this instance holds no more bytes.

read

public int read(byte[] b)
Parameters:
b - a byte array that will be filled with the bytes that were read.
Returns:
the number of bytes that were read.

read

public int read(byte[] b,
                int off,
                int len)
Parameters:
b - a byte array that will be filled with the bytes that were read.
Returns:
the number of bytes that were read.

read

public int read(java.nio.ByteBuffer buf)
Parameters:
a - ByteBuffer.
Returns:
the number of bytes that were read.

read

public void read(java.io.OutputStream out)
          throws java.io.IOException
Reads this instance's bytes and transfers them to the given stream.

Parameters:
out - an OutputStream.
Throws:
java.io.IOException

write

public void write(byte[] b)
Writes the given bytes to this instance.

Parameters:
b - the array of bytes to write.

write

public void write(int b)

write

public void write(byte[] b,
                  int off,
                  int len)
Writes the given bytes to this instance.

Parameters:
b - the array of bytes to write.
off - the offset from which to start taking the bytes in the given array.
len - the number of bytes to read, starting from the given offset.

write

public void write(java.nio.ByteBuffer buf)
Writes the bytes contained in the given buffer to this instance.

Parameters:
buf - a ByteBuffer

skip

public long skip(long skip)
Skips the number of bytes within this instance.

Returns:
the number of bytes that were actually skipped.


Copyright © 2010 Sapia OSS. All Rights Reserved.