|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.sapia.ubik.util.ByteVector
public class ByteVector
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.
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 |
|---|
public static final int DEFAULT_ARRAY_CAPACITY
public static final int DEFAULT_INCREMENT
protected int _arrayPos
protected int _markPos
protected int _markOffset
protected int _arrayCount
protected int _capacity
protected int _increment
protected ByteVector.ByteArray[] _arrays
| Constructor Detail |
|---|
public ByteVector()
public ByteVector(int capacity,
int increment)
capacity - some capacity.increment - some increment.| Method Detail |
|---|
public void mark(int mark)
reset method
being called.
mark - some mark.reset()public void reset()
mark(int)public void clear(boolean freeMemory)
freeMemory - if true, the internal byte arrays that have
been created will be dereferenced (otherwise, they will be reused).public int length()
public int position()
public int remaining()
lenth() - position()public boolean hasRemaining()
true if this instance's current internal position is
less than the number of bytes it holds.public byte[] toByteArray()
public int read()
-1
if this instance holds no more bytes.public int read(byte[] b)
b - a byte array that will be filled with the bytes that were read.
public int read(byte[] b,
int off,
int len)
b - a byte array that will be filled with the bytes that were read.
public int read(java.nio.ByteBuffer buf)
a - ByteBuffer.
public void read(java.io.OutputStream out)
throws java.io.IOException
out - an OutputStream.
java.io.IOExceptionpublic void write(byte[] b)
b - the array of bytes to write.public void write(int b)
public void write(byte[] b,
int off,
int len)
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.public void write(java.nio.ByteBuffer buf)
buf - a ByteBufferpublic long skip(long skip)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||