public class ByteBuffer extends Object
| Constructor and Description |
|---|
ByteBuffer()
Constructs a new instance of
ByteBuffer with an
initial default capacity of 1024 bytes (DEFAULT_BUFFER_CAPACITY) |
ByteBuffer(byte[] bytes)
Constructs a new instance of
ByteBuffer with a capacity
only large enough to hold they byte array. |
ByteBuffer(byte[] bytes,
int capacity)
Constructs a new instance of
ByteBuffer with the specified
capacity. |
ByteBuffer(byte[] bytes,
int offset,
int length)
Constructs a new instance of
ByteBuffer with the specified
capacity. |
ByteBuffer(byte[] bytes,
int offset,
int length,
int capacity)
Constructs a new instance of
ByteBuffer with the specified
capacity. |
ByteBuffer(int capacity)
Constructs a new instance of
ByteBuffer with the specified
capacity. |
ByteBuffer(String string0)
Constructs a new instance of
ByteBuffer. |
ByteBuffer(String string0,
int capacity)
Constructs a new instance of
ByteBuffer with the specified
capacity. |
| Modifier and Type | Method and Description |
|---|---|
void |
add(byte b)
Adds one byte to the buffer and throws an exception if the buffer is
full.
|
void |
add(byte[] bytes)
Adds a byte array to this buffer.
|
void |
add(byte[] bytes,
int offset,
int length)
Adds a byte array to this buffer starting from the offset up to the
length requested.
|
int |
capacity()
Gets the total buffer allocated capacity, not the remaining capacity.
|
protected static void |
checkOffsetLength(int bytesLength,
int offset,
int length)
Helper method for validating if an offset and length are valid for a given
byte array.
|
void |
clear()
Clears the buffer and resets it.
|
ByteBuffer |
copy()
Most efficient copy of this
ByteBuffer. |
ByteBuffer |
copy(int offset,
int length)
Most efficient copy of this
ByteBuffer. |
ByteBuffer |
copy(int offset,
int length,
int capacity)
Most efficient copy of this
ByteBuffer. |
void |
delete(int count)
Deletes the first N bytes of the buffer.
|
boolean |
endsWith(byte[] prefix)
Tests if the buffer ends with the bytes array prefix.
|
boolean |
equals(byte[] bytes)
Tests if the current ByteBuffer and the byte array have the same
sequence of bytes.
|
boolean |
equals(Object obj)
Tests if the current ByteBuffer and the parameter are both ByteBuffers
and whether it contains the same sequence of bytes.
|
int |
free()
Gets the number of free bytes this buffer has remaining for writing.
|
byte |
get(int index)
Gets the byte at the given index relative to the beginning the circular
buffer.
|
int |
hashCode()
Gets a hash code for this ByteBuffer object based on actual sequence
of bytes stored in this buffer.
|
int |
indexOf(byte[] bytes)
Returns the index within this buffer of the first occurrence of the
specified bytes.
|
int |
indexOf(byte[] bytes,
int offset)
Returns the index within this buffer of the first occurrence of the
specified bytes after the offset.
|
boolean |
isEmpty()
Tests if the buffer is empty.
|
boolean |
isFree(int count)
Tests if the buffer has enough free space to store N bytes.
|
boolean |
isFull()
Tests if the buffer is full.
|
int |
occurrences(byte b)
Counts the number of occurrences of the byte in this
ByteBuffer. |
int |
occurrences(byte[] bytes)
Counts the number of occurrences of the byte array in this
ByteBuffer. |
byte |
remove()
Removes the first (oldest/head) byte in the buffer.
|
byte[] |
remove(int count)
Removes a byte array of the first N bytes in the buffer.
|
int |
size()
Gets the current buffer size.
|
boolean |
startsWith(byte[] prefix)
Tests if the buffer starts with the byte array prefix.
|
byte[] |
toArray()
Gets a copy of the current buffer as byte array.
|
byte[] |
toArray(int offset,
int length)
Gets a copy of the current buffer as byte array, but only copies data
starting from an offset and length.
|
void |
toArray(int offset,
int length,
byte[] targetBuffer,
int targetOffset)
Will copy data from this ByteBuffer's buffer into the targetBuffer.
|
byte[] |
toArray(int offset,
int length,
int capacity)
Gets a copy of the current buffer as byte array, but the new byte[]
has the specified capacity.
|
String |
toHexString()
Return a hexidecimal String representation of the current buffer with each byte
displayed in a 2 character hexidecimal format.
|
String |
toHexString(int offset,
int length)
Return a hexidecimal String representation of the current buffer with each byte
displayed in a 2 character hexidecimal format.
|
String |
toString()
Returns a string representation of the current buffer.
|
public ByteBuffer()
ByteBuffer with an
initial default capacity of 1024 bytes (DEFAULT_BUFFER_CAPACITY)public ByteBuffer(int capacity)
throws IllegalArgumentException
ByteBuffer with the specified
capacity.capacity - The buffer capacity. Must be >= 1.IllegalArgumentExceptionpublic ByteBuffer(byte[] bytes)
ByteBuffer with a capacity
only large enough to hold they byte array. Buffer is initialized with
the byte[] bytes starting from offset 0 and adding the entire byte aray.bytes - The byte array to initialize buffer withpublic ByteBuffer(byte[] bytes,
int capacity)
throws IllegalArgumentException
ByteBuffer with the specified
capacity. Buffer is initialized with the byte[] bytes starting from
offset 0 and adding the entire byte aray. Capacity must be >= the length
of the byte array.bytes - The byte array to initialize buffer withcapacity - The buffer capacity. Must be >= 1.IllegalArgumentException - Thrown if the capacity is too smallpublic ByteBuffer(byte[] bytes,
int offset,
int length)
throws IllegalArgumentException
ByteBuffer with the specified
capacity. Buffer is initialized with the byte[] bytes starting from
offset and added up to length bytes.bytes - The byte array to initialize buffer withoffset - The offset in the byte array to start fromlength - The length starting from offset within the byte arrayIllegalArgumentException - Thrown if offset or length is negative,
capacity is too small, or if the offset+length would cause a read
past the length of the byte array.public ByteBuffer(byte[] bytes,
int offset,
int length,
int capacity)
throws IllegalArgumentException
ByteBuffer with the specified
capacity. Buffer is initialized with the byte[] bytes starting from
offset and added up to length bytes. Capacity
must be large enough to store the length.bytes - The byte array to initialize buffer withoffset - The offset in the byte array to start fromlength - The length starting from offset within the byte arraycapacity - The buffer capacity. Must be >= 1.IllegalArgumentException - Thrown if offset or length is negative,
capacity is too small, or if the offset+length would cause a read
past the length of the byte array.public ByteBuffer(String string0) throws IllegalArgumentException
ByteBuffer.
Buffer is initialized with the String converted to bytes using the
ISO-8859-1 character set.string0 - The string to initialize our buffer withIllegalArgumentException - If capacity is too small to store
the bytes from the Stringpublic ByteBuffer(String string0, int capacity) throws IllegalArgumentException
ByteBuffer with the specified
capacity. Buffer is initialized with the String converted to bytes using the
ISO-8859-1 character set. Capacity must be large enough to store the
bytes obtained after conversion. If only 8-bit data is stored in the String,
the capacity required would be the length() of the String.string0 - The string to initialize our buffer withcapacity - The capacity of the buffer. Must be >= byte length of stringIllegalArgumentException - If capacity is too small to store
the bytes from the Stringpublic int size()
public int capacity()
free()public int free()
public boolean isFree(int count)
public boolean isEmpty()
public boolean isFull()
public void clear()
public void add(byte b)
throws BufferIsFullException
b - Byte to add to the buffer.BufferIsFullException - If the buffer is full and the byte cannot
be stored.public void add(byte[] bytes)
throws BufferSizeException
bytes - A byte array to add to this buffer. Its size must less than or equal to this buffer's free spaceBufferSizeException - If this buffer's free space is not large enough to store add the byte arrayprotected static void checkOffsetLength(int bytesLength,
int offset,
int length)
throws IllegalArgumentException
bytesLength - The length of the byte array to validate againstoffset - The offset within the byte arraylength - The length to read starting from the offsetIllegalArgumentException - If any of the above conditions
are violated.public void add(byte[] bytes,
int offset,
int length)
throws IllegalArgumentException,
BufferSizeException
bytes - A byte array to add to this buffer.offset - The offset within the byte array to begin to addlength - The length starting from offset to begin to addBufferSizeException - If this buffer's free space is not large enough to store add the byte arrayIllegalArgumentExceptionpublic void delete(int count)
throws BufferSizeException
BufferSizeExceptionpublic byte remove()
throws BufferIsEmptyException
BufferIsEmptyException - If the buffer is empty and a byte cannot be
removed.public byte[] remove(int count)
throws BufferSizeException
BufferSizeException
will be thrown.count - The number of bytes to remove (a value between 1 and the buffer capacity).BufferSizeException - If the buffer is not large enough to fufill the request
(if N is > size)public int occurrences(byte b)
ByteBuffer.
This method will not overlap any bytes during its search. For example,
if you're search for bytes of "AA" in a buffer containing "AAA", this
method will only return a value of 1.b - The byte to search forpublic int occurrences(byte[] bytes)
ByteBuffer. This method will not overlap any bytes during its
search. For example, if you're search for bytes of "AA" in a buffer containing "AAA", this
method will only return a value of 1.bytes - The byte[] to search forpublic ByteBuffer copy()
ByteBuffer. The internal buffer
is copied to the new ByteBuffer using either 1 or 2 calls to System.arraycopy().public ByteBuffer copy(int offset, int length)
ByteBuffer. The internal buffer
is copied to the new ByteBuffer using either 1 or 2 calls to System.arraycopy().offset - The offset in the buffer to start fromlength - The length from the offsetpublic ByteBuffer copy(int offset, int length, int capacity)
ByteBuffer. The internal buffer
is copied to the new ByteBuffer using either 1 or 2 calls to System.arraycopy().offset - The offset in the buffer to start fromlength - The length from the offsetcapacity - The capacity of the new ByteBuffer. Must be >= this ByteBuffer's size()public byte get(int index)
throws IllegalArgumentException,
BufferSizeException
index - The index of the byte relative to the beginning the buffer
(a value between 0 and the the current size).BufferSizeException - If the index is >= size()IllegalArgumentExceptionpublic byte[] toArray()
public byte[] toArray(int offset,
int length)
offset - The offset to start fromlength - The length from the offsetIllegalArgumentException - If capacity isn't large enough enough
to hold the new byte[]public byte[] toArray(int offset,
int length,
int capacity)
offset - The offset to start fromlength - The length from the offsetcapacity - The size of the new byte[]. Must be >= this buffer's size()IllegalArgumentException - If capacity isn't large enough enough
to hold the new byte[]public void toArray(int offset,
int length,
byte[] targetBuffer,
int targetOffset)
offset - The offset within the ByteBuffer to start copy fromlength - The length from the offset to copytargetBuffer - The target byte array we'll copy data into. Must already
be allocated with enough capacity.targetOffset - The offset within the target byte array to start fromIllegalArgumentException - If the offset and length are invalid
for this ByteBuffer, if the targetOffset and targetLength are invalid
for the targetBuffer, or if if the targetBuffer's capacity is not
large enough to hold the copied data.public boolean startsWith(byte[] prefix)
public boolean endsWith(byte[] prefix)
public int indexOf(byte[] bytes)
bytes - The byte array to search forpublic int indexOf(byte[] bytes,
int offset)
bytes - The byte array to search foroffset - The offset within the buffer to search frompublic int hashCode()
public boolean equals(Object obj)
public boolean equals(byte[] bytes)
public String toString()
public String toHexString()
public String toHexString(int offset, int length)
offset - length - Copyright © 2012-2015 Cloudhopper by Twitter. All Rights Reserved.