|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.ibm.wbi.util.ByteBuffer
This class is something like java.lang.StringBuffer, except
that it provides a buffer of bytes instead of char
s. The provided append methods automatically allocate
storage (when the internal capacity is exceeded) before adding data.
It's useful for building up arbitrary length byte arrays.
| Field Summary | |
static int |
DEFAULT_SIZE
The default initial capacity. |
| Constructor Summary | |
ByteBuffer()
Constructs a ByteBuffer that is initially empty, with a default capacity of 512 bytes. |
|
ByteBuffer(byte[] data)
Constructs a ByteBuffer containing the given bytes, with a capacity a bit bigger than the array. |
|
ByteBuffer(byte[] data,
int offset,
int length)
Constructs a ByteBuffer containing the given bytes, with a capacity a bit bigger than the array. |
|
ByteBuffer(int capacity)
Constructs a ByteBuffer that is initially empty, with the given capacity. |
|
ByteBuffer(java.lang.String string)
Constructs a ByteBuffer from the passed String. |
|
ByteBuffer(java.lang.StringBuffer stringBuffer)
Constructs a ByteBuffer from the passed StringBuffer. |
|
ByteBuffer(java.lang.StringBuffer stringBuffer,
java.lang.String encoding)
Constructs a ByteBuffer from the passed StringBuffer. |
|
ByteBuffer(java.lang.String string,
java.lang.String encoding)
Constructs a ByteBuffer from the passed String. |
|
| Method Summary | |
void |
append(byte val)
Appends the given byte argument to this ByteBuffer. |
void |
append(byte[] bytes)
Appends the given byte array argument to this string buffer. |
void |
append(byte[] bytes,
int offset,
int length)
Appends the given byte subarray of the bytes array argument
to this buffer. |
void |
append(long val)
Appends the given long argument to this ByteBuffer. |
byte |
byteAt(int index)
Returns the byte at a specific index in this ByteBuffer. |
int |
capacity()
Returns the current capacity of the ByteBuffer. |
void |
ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least equal to the specified minimum. |
byte[] |
getByteArrayRef()
Return a reference to the byte array inside the ByteBuffer. |
byte[] |
getBytes()
Return a byte array of the current buffer size containing the bytes. |
void |
getBytes(int srcBegin,
int srcEnd,
byte[] dst,
int dstBegin)
Bytes are copied from this ByteBuffer into the destination byte array dst. |
void |
insert(int offset,
byte val)
Inserts the given byte argument into this ByteBuffer. |
void |
insert(int offset,
byte[] bytes)
Inserts the given byte array argument into this ByteBuffer. |
int |
length()
Returns the length (byte count) of this ByteBuffer. |
long |
longAt(int index)
Returns the long at a specific index in this ByteBuffer. |
void |
purge()
The current contents are released and an empty ByteBuffer with a default capacity of 256 characters is created. |
void |
purge(int capacity)
The current contents are released and an empty ByteBuffer with the specified capacity is created. |
void |
setByteAt(int index,
byte val)
The byte at the specified index of this ByteBuffer is set to val
. |
void |
setLength(int newLength)
Sets the length of this ByteBuffer. |
int |
size()
Returns the length (byte count) of this ByteBuffer. |
void |
write(java.io.OutputStream stream)
Writes the bytes contained in the ByteBuffer to an OutputStream. |
void |
write(java.io.OutputStream stream,
int offset,
int length)
Writes some of the bytes contained in the ByteBuffer to an OutputStream. |
| 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_SIZE
| Constructor Detail |
public ByteBuffer()
public ByteBuffer(int capacity)
capacity - The initial capacity.public ByteBuffer(byte[] data)
data - The initial contents of the ByteBuffer.
public ByteBuffer(byte[] data,
int offset,
int length)
data - The initial contents of the ByteBuffer.offset - The starting offset in data of the bytes
to be placed in the buffer.length - The number of bytes, starting at offset,
of the bytes to be placed in the buffer.public ByteBuffer(java.lang.StringBuffer stringBuffer)
stringBuffer - A reference to a StringBuffer object.
This object will be used to fill the newly
created ByteBuffer object.
public ByteBuffer(java.lang.StringBuffer stringBuffer,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException
stringBuffer - A reference to a StringBuffer object.
This object will be used to fill the newly
created ByteBuffer object.encoding - A character encoding string to be used when
converting char's within the
StringBuffer to bytes.java.io.UnsupportedEncodingException - The provided
encoding is invalid.public ByteBuffer(java.lang.String string)
string - A reference to a String object. This object
will be used to fill the newly created ByteBuffer
object.
public ByteBuffer(java.lang.String string,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException
string - A reference to a String object.
This object will be used to fill the newly
created ByteBuffer object.encoding - A character encoding string to be used when
converting char's within the
StringBuffer to bytes.java.io.UnsupportedEncodingException - The provided
encoding is invalid.| Method Detail |
public void purge()
purge in interface ByteStorepublic void purge(int capacity)
purge in interface ByteStorecapacity - The initial capacity.public int length()
length in interface ByteStorepublic int size()
size in interface ByteStorepublic int capacity()
capacity in interface ByteStorepublic void ensureCapacity(int minimumCapacity)
minimumCapacity argument.2.minimumCapacity argument is nonpositive, this
method takes no action and simply returns.ensureCapacity in interface ByteStoreminimumCapacity - The minimum desired capacity.
public void setLength(int newLength)
throws java.lang.StringIndexOutOfBoundsException
newLength
argument is less than the current length of the ByteBuffer, the
ByteBuffer is truncated to contain exactly the number of characters
given by the newLength argument.
If the newLength argument is greater than or equal to the
current length, sufficient null bytes will be appended to the byte
buffer so that length becomes equal to the newLength
argument.
The newLength argument must be greater than or equal
to 0.
setLength in interface ByteStorenewLength - The new length of the buffer.java.lang.StringIndexOutOfBoundsException - The newLength
argument is invalid
public byte byteAt(int index)
throws java.lang.ArrayIndexOutOfBoundsException
The first byte of the ByteBuffer is considered to be at index
0, the next at index 1, and so on, for
array indexing.
The index argument must be greater than or equal to
0, and less than the length of this ByteBuffer.
byteAt in interface ByteStoreindex - The index of the desired byte.java.lang.ArrayIndexOutOfBoundsException - The index is invalid.
public long longAt(int index)
throws java.lang.ArrayIndexOutOfBoundsException
The first byte of the ByteBuffer is considered to be at index
0, the next at index 1, and so on, for
array indexing.
The index argument must be greater than or equal to
0, and less than the length of this ByteBuffer.
The long must be in the ByteBuffer in the format used by
our append(long val) method.
index - The index of the first byte of the desired long.java.lang.ArrayIndexOutOfBoundsException - The index is invalid.public byte[] getBytes()
getBytes in interface ByteStorepublic byte[] getByteArrayRef()
getByteArrayRef in interface ByteStore
public void getBytes(int srcBegin,
int srcEnd,
byte[] dst,
int dstBegin)
throws java.lang.ArrayIndexOutOfBoundsException
dst. The first byte to be copied is at index
srcBegin; the last byte to be copied is at index
srcEnd-1. The total number of bytes to be copied is
srcEnd-srcBegin. The bytes are copied into the subarray of
dst starting at index dstBegin and ending at
index:
dstbegin + (srcEnd-srcBegin) - 1
getBytes in interface ByteStoresrcBegin - Start copying at this offset in the ByteBuffer.srcEnd - Stop copying at this offset in the ByteBuffer.dst - The array to copy the data into.dstBegin - Offset into dst.java.lang.ArrayIndexOutOfBoundsException - There is an invalid index
into the buffer.
public void setByteAt(int index,
byte val)
throws java.lang.ArrayIndexOutOfBoundsException
val
.setByteAt in interface ByteStoreindex - The index of the byte to overwrite.val - The new byte value.java.lang.ArrayIndexOutOfBoundsException - The index is invalid.public void append(byte val)
The argument is appended to the contents of this ByteBuffer.
The length of this ByteBuffer increases by 1.
append in interface ByteStoreval - A byte value.public void append(long val)
The argument is appended to the contents of this ByteBuffer.
The length of this ByteBuffer increases by 8.
This long can be retrieved with the longAt() method.
val - A long value.public void append(byte[] bytes)
The bytes of the array argument are appended, in order, to the contents of this ByteBuffer. The length of this ByteBuffer increases by the length of the argument.
append in interface ByteStorebytes - The bytes to be appended.
public void append(byte[] bytes,
int offset,
int length)
bytes array argument
to this buffer.
Bytes of the byte array bytes, starting at index
offset, are appended, in order, to the contents of this
byte buffer. The length of this ByteBuffer increases by the value of
length.
append in interface ByteStorebytes - The bytes to be appended.offset - The index of the first byte to append.length - The number of bytes to append.
public void insert(int offset,
byte[] bytes)
throws java.lang.ArrayIndexOutOfBoundsException
The bytes of the array argument are inserted into the
contents of this ByteBuffer at the position indicated by
offset. The length of this ByteBuffer increases by
the length of the argument.
insert in interface ByteStoreoffset - The offset.bytes - A byte array.java.lang.ArrayIndexOutOfBoundsException - There is an invalid index
into the buffer.
public void insert(int offset,
byte val)
The second argument is inserted into the contents of this byte
buffer at the position indicated by offset. The length
of this ByteBuffer increases by one.
The offset argument must be greater than or equal to
0, and less than or equal to the length of this
ByteBuffer.
insert in interface ByteStoreoffset - The offset.val - The byte value.
public void write(java.io.OutputStream stream)
throws java.io.IOException,
java.lang.ArrayIndexOutOfBoundsException
stream - OutputStream to which bytes should be written.java.io.IOException - The write() failed.java.lang.ArrayIndexOutOfBoundsException - The index is invalid.
public void write(java.io.OutputStream stream,
int offset,
int length)
throws java.io.IOException,
java.lang.ArrayIndexOutOfBoundsException
offset and length will be written before
return.stream - OutputStream to which bytes should be written.offset - Offset of the first byte from the buffer to be written.length - Number of bytes from the buffer to be written.java.io.IOException - The write() failed.java.lang.ArrayIndexOutOfBoundsException - The index is invalid.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||