public final class ByteBuffer extends Object implements Comparable<ByteBuffer>
Modifier and Type | Field and Description |
---|---|
static int |
CHECKSUM_MODE |
static int |
MIN_CAPACITY |
Constructor and Description |
---|
ByteBuffer()
Constructs a new ByteBuffer instance with an initial storage capacity.
|
ByteBuffer(byte[] buffer)
Constructs a ByteBuffer object using the provided byte array.
|
ByteBuffer(byte[] buffer,
int length)
Constructs a new ByteBuffer with a specified initial data buffer and length.
|
ByteBuffer(ByteBuffer byteBuffer)
Constructs a new ByteBuffer object by copying the content of the provided ByteBuffer.
|
ByteBuffer(ByteBuffer byteBuffer,
int length)
Constructs a given ByteBuffer into a new ByteBuffer with a specified length.
|
ByteBuffer(ByteBuffer buffer,
int offset,
int length)
Constructs a new ByteBuffer by copying a portion of an existing buffer.
|
ByteBuffer(int capacity)
Constructs a ByteBuffer with the specified capacity.
|
ByteBuffer(String str)
Constructs a ByteBuffer object with the given string.
|
ByteBuffer(String str,
Charset charset)
Constructs a ByteBuffer object by converting the specified string into bytes using
the provided character set.
|
Modifier and Type | Method and Description |
---|---|
ByteBuffer |
append(byte b)
Appends a single byte to the ByteBuffer.
|
ByteBuffer |
append(byte[] buffer)
Appends the specified byte array to the buffer.
|
ByteBuffer |
append(byte[] buffer,
int length)
Appends the specified portion of the byte array to the buffer.
|
ByteBuffer |
append(byte[] buffer,
int offset,
int length)
Appends a portion of the given byte array to the current storage buffer.
|
ByteBuffer |
append(ByteBuffer byteBuffer)
Appends the contents of the provided ByteBuffer to the current buffer.
|
ByteBuffer |
append(char c)
Appends the specified character to this buffer.
|
ByteBuffer |
append(long value)
Appends the string representation of a specified long value to the current ByteBuffer.
|
ByteBuffer |
append(String str)
Appends the content of the provided string to the ByteBuffer using ISO-8859-1 encoding.
|
ByteBuffer |
append(String str,
Charset charset)
Appends the specified string to the buffer using the given charset for encoding.
|
ByteBuffer |
append(ValuePtr value)
Appends the given value to the buffer.
|
ByteBuffer |
appendCheckSum(int checkSum)
Appends a checksum value to the current buffer by converting it into three bytes.
|
int |
capacity()
Returns the maximum capacity of the underlying storage.
|
ByteBuffer |
clear()
Clears the current buffer by resetting its size to zero.
|
int |
compareTo(ByteBuffer o) |
ByteBuffer |
copy()
Creates a new ByteBuffer that is a copy of the current ByteBuffer.
|
boolean |
equals(Object obj) |
ByteBuffer |
erase(int length)
Erases a portion of the buffer by specifying the length to erase.
|
ByteBuffer |
erase(int length,
int startPsn)
Erases a portion of the ByteBuffer starting from the specified position
and of the specified length.
|
int |
findIndex(int startPsn,
byte[] subBuffer)
Searches for the first occurrence of the specified sub-buffer starting from the given position.
|
byte[] |
get()
Retrieves the stored byte array.
|
int |
getCheckSum()
Computes the checksum of the elements stored in the 'storage' array.
|
byte[] |
getCopy()
Creates and returns a copy of the underlying storage array up to the current size.
|
ByteBuffer |
incLength(int size)
Increases the length of the current buffer by the specified size.
|
ByteBuffer |
insert(byte[] bytes,
int pos)
Inserts the specified byte array into a buffer at the given position.
|
ByteBuffer |
insert(byte[] buffer,
int length,
int startPsn)
Inserts a specified range of bytes from the provided buffer into a ByteBuffer starting at the given position.
|
ByteBuffer |
insert(byte[] buffer,
int offset,
int length,
int startPsn)
Inserts a portion of the provided byte array into the internal storage at the specified position.
|
ByteBuffer |
insert(ByteBuffer buffer,
int startPsn)
Inserts content from the given ByteBuffer into a specific position.
|
ByteBuffer |
insert(byte b,
int pos)
Inserts a byte at the specified position in the buffer.
|
ByteBuffer |
insert(byte b,
int pos,
int times)
Inserts the specified byte into the buffer multiple times at the given position.
|
ByteBuffer |
insert(char c,
int pos)
Inserts the given character at the specified position in the ByteBuffer.
|
ByteBuffer |
insert(long l,
int pos)
Inserts the string representation of the specified long value at the given position.
|
ByteBuffer |
insert(String s,
int pos)
Inserts the specified string into a byte buffer at the given position.
|
ByteBuffer |
insert(String s,
int pos,
Charset charset)
Inserts the byte representation of the provided string at the specified position
in the byte buffer using the given character set for encoding.
|
int |
length()
Returns the current size.
|
ByteBuffer |
overwrite(byte[] buffer,
int length)
Overwrites a portion of the buffer with the given byte array starting at a default offset of 0.
|
ByteBuffer |
overwrite(ByteBuffer byteBuffer)
Overwrites the current buffer with the contents of the provided ByteBuffer.
|
ByteBuffer |
overwrite(int startPsn,
byte[] buffer,
int length)
Overwrites the content at the specified position in a ByteBuffer with the provided byte array.
|
ByteBuffer |
overwrite(int startPsn,
byte[] buffer,
int length,
int offset)
Overwrites a portion of the buffer starting at the specified position with the
provided byte array.
|
ByteBuffer |
reserve(int expectedCapacity)
Reserves the specified capacity in the storage.
|
ByteBuffer |
reserveSpace(int space)
Reserves additional space in the ByteBuffer.
|
ByteBuffer |
resize(int newSize)
Resizes the ByteBuffer to the specified new size.
|
ByteBuffer |
reverse()
Reverses the order of the elements in the buffer within the specified
range, starting from the beginning to the current size.
|
ByteBuffer |
reverse(int startIdx)
Reverses the elements in the specified range within this byte buffer.
|
ByteBuffer |
reverse(int startIdx,
int endIdx)
Reverses the bytes in the specified range of the storage array.
|
int |
size()
Returns the current size.
|
ByteBuffer |
subBuffer(int offset)
Creates a new sub-buffer starting at the specified offset and extending to the end of the current buffer.
|
ByteBuffer |
subBuffer(int offset,
int length)
Creates a new ByteBuffer that is a subsection of the original buffer.
|
String |
toString() |
ByteBuffer |
write(OutputStream outputStream)
Writes the contents of the internal storage buffer to the specified OutputStream.
|
public static final int MIN_CAPACITY
public static final int CHECKSUM_MODE
public ByteBuffer()
public ByteBuffer(int capacity)
capacity
- the initial capacity of the byte buffer. Must be non-negative.IllegalArgumentException
- if the specified capacity is less than zero.public ByteBuffer(byte[] buffer)
buffer
- the byte array to initialize the ByteBuffer.public ByteBuffer(byte[] buffer, int length)
buffer
- The byte array containing the initial data to populate the ByteBuffer.length
- The number of bytes from the buffer to copy into the ByteBuffer.
Must be less than or equal to the size of the provided buffer.public ByteBuffer(String str, Charset charset)
str
- the input string to be converted into bytescharset
- the character set to be used for encoding the string to bytespublic ByteBuffer(String str)
str
- the input string to be converted into a ByteBufferpublic ByteBuffer(ByteBuffer byteBuffer)
byteBuffer
- the ByteBuffer instance to copy frompublic ByteBuffer(ByteBuffer byteBuffer, int length)
byteBuffer
- the original ByteBuffer to wraplength
- the length of the new ByteBufferpublic ByteBuffer(ByteBuffer buffer, int offset, int length)
buffer
- The source ByteBuffer from which data is copied. Must not be null.offset
- The start position in the source buffer to begin copying. Must be non-negative.length
- The number of bytes to copy from the source buffer. Must be non-negative, and
the sum of offset and length must not exceed the source buffer's length.NullPointerException
- If the provided buffer is null.IllegalArgumentException
- If the offset is negative, the length is negative,
or offset plus length exceeds the source buffer's length.public ByteBuffer copy()
public ByteBuffer subBuffer(int offset)
offset
- the starting position of the sub-buffer within the current buffer.public ByteBuffer subBuffer(int offset, int length)
offset
- the starting position within the original buffer where the sub buffer beginslength
- the number of elements to include in the sub bufferpublic ByteBuffer overwrite(ByteBuffer byteBuffer)
byteBuffer
- the ByteBuffer containing the data to overwrite withpublic ByteBuffer overwrite(byte[] buffer, int length)
buffer
- the byte array containing the data to overwritelength
- the number of bytes to write from the provided byte arraypublic ByteBuffer overwrite(int startPsn, byte[] buffer, int length)
startPsn
- the starting position in the ByteBuffer where the overwriting beginsbuffer
- the byte array containing the data to be writtenlength
- the number of bytes to write from the provided byte arraypublic ByteBuffer overwrite(int startPsn, byte[] buffer, int length, int offset)
startPsn
- the start position in the buffer where data will be writtenbuffer
- the byte array containing data to writelength
- the length of the data in the byte array to be writtenoffset
- the starting offset in the buffer from where data will be writtenIllegalArgumentException
- if startPsn is negative, startPsn is greater than or equal to the current size,
length is negative, or offset is out of the valid rangeNullPointerException
- if the buffer is nullpublic ByteBuffer append(ByteBuffer byteBuffer)
byteBuffer
- the ByteBuffer whose data will be appendedpublic ByteBuffer append(char c)
c
- the character to be appended, represented as a char.public ByteBuffer append(byte b)
b
- the byte to be appended to the ByteBufferpublic ByteBuffer append(byte[] buffer)
buffer
- the byte array to appendpublic ByteBuffer append(String str)
str
- the string whose content is to be appendedpublic ByteBuffer append(String str, Charset charset)
str
- the string to append to the buffercharset
- the charset to be used for encoding the stringpublic ByteBuffer append(ValuePtr value)
NullPointerException
or IllegalArgumentException
will be thrown.value
- the ValuePtr
object containing the data to append.
Must not be null and must be set.ByteBuffer
after appending the value.NullPointerException
- if the value is null.IllegalArgumentException
- if the value is not set.public ByteBuffer append(byte[] buffer, int length)
buffer
- the byte array containing the data to be appendedlength
- the number of bytes to append from the beginning of the arraypublic ByteBuffer append(byte[] buffer, int offset, int length)
buffer
- the byte array containing the data to appendoffset
- the starting position in the byte array to begin appending fromlength
- the number of bytes to append from the byte arraypublic ByteBuffer append(long value)
value
- the long value to be appendedpublic ByteBuffer appendCheckSum(int checkSum)
checkSum
- the checksum value to append, represented as an integerpublic ByteBuffer insert(ByteBuffer buffer, int startPsn)
buffer
- the ByteBuffer containing the data to be inserted; must not be nullstartPsn
- the starting position where the content is to be insertedNullPointerException
- if the provided buffer is nullpublic ByteBuffer insert(char c, int pos)
c
- the character to be inserted, provided as a charpos
- the position at which the character should be insertedpublic ByteBuffer insert(byte b, int pos)
b
- the byte to insertpos
- the position at which the byte should be insertedpublic ByteBuffer insert(byte b, int pos, int times)
b
- the byte value to insertpos
- the position in the buffer where the byte is to be insertedtimes
- the number of times the byte should be insertedpublic ByteBuffer insert(long l, int pos)
l
- the long value to be converted to a string and insertedpos
- the position at which the string representation of the long value is to be insertedpublic ByteBuffer insert(String s, int pos, Charset charset)
s
- the string to insertpos
- the position in the byte buffer where the string's bytes will be insertedcharset
- the charset used to encode the string into bytespublic ByteBuffer insert(String s, int pos)
s
- the string to be inserted, which will be converted to bytes using ISO_8859_1 encodingpos
- the position at which the string's bytes should be inserted in the byte bufferpublic ByteBuffer insert(byte[] bytes, int pos)
bytes
- the byte array to be insertedpos
- the position at which the byte array should be insertedpublic ByteBuffer insert(byte[] buffer, int length, int startPsn)
buffer
- the byte array containing the data to be insertedlength
- the number of bytes to insert from the bufferstartPsn
- the starting position in the ByteBuffer where the insertion will occurpublic ByteBuffer insert(byte[] buffer, int offset, int length, int startPsn)
buffer
- the byte array containing the data to be insertedoffset
- the starting position in the byte array from which data should be readlength
- the number of bytes to be inserted from the byte arraystartPsn
- the position in the internal storage where the data should be insertedpublic int capacity()
public ByteBuffer clear()
public ByteBuffer erase(int length)
length
- the number of bytes to erase from the bufferpublic ByteBuffer erase(int length, int startPsn)
length
- the number of bytes to be erased from the bufferstartPsn
- the starting position in the buffer from where the erasing beginspublic byte[] get()
public byte[] getCopy()
public int getCheckSum()
public ByteBuffer reserveSpace(int space)
space
- the amount of additional space to reserve in the ByteBufferpublic ByteBuffer reserve(int expectedCapacity)
expectedCapacity
- the desired capacity to be reserved in the storagepublic int size()
public int length()
public ByteBuffer resize(int newSize)
newSize
- the new size to which the ByteBuffer should be resizedpublic ByteBuffer incLength(int size)
size
- the amount by which to increase the length must not result in a negative total lengthIllegalArgumentException
- if the resulting length is negativepublic ByteBuffer reverse()
public ByteBuffer reverse(int startIdx)
startIdx
- the starting index of the range to reverse, inclusive.public ByteBuffer reverse(int startIdx, int endIdx)
startIdx
- the starting index of the range to reverse (inclusive)endIdx
- the ending index of the range to reverse (inclusive)public int findIndex(int startPsn, byte[] subBuffer)
startPsn
- the starting position in the buffer from which the search begins. Must be non-negative.subBuffer
- the byte array to search for within the buffer. Must not be null or empty.IllegalArgumentException
- if the starting position is negative, the sub-buffer is empty,
or its length exceeds the size of the buffer.NullPointerException
- if the sub-buffer is null.public ByteBuffer write(OutputStream outputStream) throws IOException
outputStream
- the OutputStream to which the data will be writtenIOException
- if an I/O error occurs during writingpublic int compareTo(ByteBuffer o)
compareTo
in interface Comparable<ByteBuffer>
Copyright © 2005–2025 Onix Solutions. All rights reserved.