Package biz.onixs.util
Class ByteBuffer
- java.lang.Object
-
- biz.onixs.util.ByteBuffer
-
- All Implemented Interfaces:
Comparable<ByteBuffer>
public final class ByteBuffer extends Object implements Comparable<ByteBuffer>
The ByteBuffer class provides a flexible container for handling and manipulating byte arrays. It includes methods for creating, copying, appending, overwriting, and inserting byte data, along with utility methods for encoding and decoding strings to and from byte arrays. This class is used to dynamically manage byte sequences with methods to expand or manipulate its capacity as needed.
-
-
Field Summary
Fields Modifier and Type Field Description static intCHECKSUM_MODEstatic intMIN_CAPACITY
-
Constructor Summary
Constructors Constructor 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(int capacity)Constructs a ByteBuffer with the specified capacity.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(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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteBufferappend(byte b)Appends a single byte to the ByteBuffer.ByteBufferappend(byte[] buffer)Appends the specified byte array to the buffer.ByteBufferappend(byte[] buffer, int length)Appends the specified portion of the byte array to the buffer.ByteBufferappend(byte[] buffer, int offset, int length)Appends a portion of the given byte array to the current storage buffer.ByteBufferappend(char c)Appends the specified character to this buffer.ByteBufferappend(long value)Appends the string representation of a specified long value to the current ByteBuffer.ByteBufferappend(ByteBuffer byteBuffer)Appends the contents of the provided ByteBuffer to the current buffer.ByteBufferappend(ValuePtr value)Appends the given value to the buffer.ByteBufferappend(String str)Appends the content of the provided string to the ByteBuffer using ISO-8859-1 encoding.ByteBufferappend(String str, Charset charset)Appends the specified string to the buffer using the given charset for encoding.ByteBufferappendBase64(byte[] buffer)Appends Base64 representation of all bytes from the provided byte array.ByteBufferappendBase64(byte[] buffer, int offset, int length)Appends Base64 representation of a byte array range.ByteBufferappendCheckSum(int checkSum)Appends a checksum value to the current buffer by converting it into three bytes.intcapacity()Returns the maximum capacity of the underlying storage.ByteBufferclear()Clears the current buffer by resetting its size to zero.intcompareTo(ByteBuffer o)ByteBuffercopy()Creates a new ByteBuffer that is a copy of the current ByteBuffer.booleanequals(Object obj)ByteBuffererase(int length)Erases a portion of the buffer by specifying the length to erase.ByteBuffererase(int length, int startPsn)Erases a portion of the ByteBuffer starting from the specified position and of the specified length.intfindIndex(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.intgetCheckSum()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.inthashCode()ByteBufferincLength(int size)Increases the length of the current buffer by the specified size.ByteBufferinsert(byte[] bytes, int pos)Inserts the specified byte array into a buffer at the given position.ByteBufferinsert(byte[] buffer, int length, int startPsn)Inserts a specified range of bytes from the provided buffer into a ByteBuffer starting at the given position.ByteBufferinsert(byte[] buffer, int offset, int length, int startPsn)Inserts a portion of the provided byte array into the internal storage at the specified position.ByteBufferinsert(byte b, int pos)Inserts a byte at the specified position in the buffer.ByteBufferinsert(byte b, int pos, int times)Inserts the specified byte into the buffer multiple times at the given position.ByteBufferinsert(char c, int pos)Inserts the given character at the specified position in the ByteBuffer.ByteBufferinsert(long l, int pos)Inserts the string representation of the specified long value at the given position.ByteBufferinsert(ByteBuffer buffer, int startPsn)Inserts content from the given ByteBuffer into a specific position.ByteBufferinsert(String s, int pos)Inserts the specified string into a byte buffer at the given position.ByteBufferinsert(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.intlength()Returns the current size.ByteBufferoverwrite(byte[] buffer, int length)Overwrites a portion of the buffer with the given byte array starting at a default offset of 0.ByteBufferoverwrite(int startPsn, byte[] buffer, int length)Overwrites the content at the specified position in a ByteBuffer with the provided byte array.ByteBufferoverwrite(int startPsn, byte[] buffer, int length, int offset)Overwrites a portion of the buffer starting at the specified position with the provided byte array.ByteBufferoverwrite(ByteBuffer byteBuffer)Overwrites the current buffer with the contents of the provided ByteBuffer.ByteBufferreserve(int expectedCapacity)Reserves the specified capacity in the storage.ByteBufferreserveSpace(int space)Reserves additional space in the ByteBuffer.ByteBufferresize(int newSize)Resizes the ByteBuffer to the specified new size.ByteBufferreverse()Reverses the order of the elements in the buffer within the specified range, starting from the beginning to the current size.ByteBufferreverse(int startIdx)Reverses the elements in the specified range within this byte buffer.ByteBufferreverse(int startIdx, int endIdx)Reverses the bytes in the specified range of the storage array.intsize()Returns the current size.ByteBuffersubBuffer(int offset)Creates a new sub-buffer starting at the specified offset and extending to the end of the current buffer.ByteBuffersubBuffer(int offset, int length)Creates a new ByteBuffer that is a subsection of the original buffer.StringtoString()ByteBufferwrite(OutputStream outputStream)Writes the contents of the internal storage buffer to the specified OutputStream.
-
-
-
Field Detail
-
MIN_CAPACITY
public static final int MIN_CAPACITY
- See Also:
- Constant Field Values
-
CHECKSUM_MODE
public static final int CHECKSUM_MODE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ByteBuffer
public ByteBuffer()
Constructs a new ByteBuffer instance with an initial storage capacity.
-
ByteBuffer
public ByteBuffer(int capacity)
Constructs a ByteBuffer with the specified capacity.- Parameters:
capacity- the initial capacity of the byte buffer. Must be non-negative.- Throws:
IllegalArgumentException- if the specified capacity is less than zero.
-
ByteBuffer
public ByteBuffer(byte[] buffer)
Constructs a ByteBuffer object using the provided byte array.- Parameters:
buffer- the byte array to initialize the ByteBuffer.
-
ByteBuffer
public ByteBuffer(byte[] buffer, int length)Constructs a new ByteBuffer with a specified initial data buffer and length.- Parameters:
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.- Throws:
NullPointerException- ifbufferis nullIllegalArgumentException- iflengthis negative, exceedsbuffer.length, or is too large to fit internal capacity limits
-
ByteBuffer
public ByteBuffer(String str, Charset charset)
Constructs a ByteBuffer object by converting the specified string into bytes using the provided character set.- Parameters:
str- the input string to be converted into bytescharset- the character set to be used for encoding the string to bytes
-
ByteBuffer
public ByteBuffer(String str)
Constructs a ByteBuffer object with the given string. The string is encoded using ISO-8859-1 character set.- Parameters:
str- the input string to be converted into a ByteBuffer
-
ByteBuffer
public ByteBuffer(ByteBuffer byteBuffer)
Constructs a new ByteBuffer object by copying the content of the provided ByteBuffer.- Parameters:
byteBuffer- the ByteBuffer instance to copy from
-
ByteBuffer
public ByteBuffer(ByteBuffer byteBuffer, int length)
Constructs a given ByteBuffer into a new ByteBuffer with a specified length.- Parameters:
byteBuffer- the original ByteBuffer to wraplength- the length of the new ByteBuffer
-
ByteBuffer
public ByteBuffer(ByteBuffer buffer, int offset, int length)
Constructs a new ByteBuffer by copying a portion of an existing buffer.- Parameters:
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.- Throws:
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.
-
-
Method Detail
-
copy
public ByteBuffer copy()
Creates a new ByteBuffer that is a copy of the current ByteBuffer.- Returns:
- a new instance of ByteBuffer with the same data as the original.
-
subBuffer
public ByteBuffer subBuffer(int offset)
Creates a new sub-buffer starting at the specified offset and extending to the end of the current buffer.- Parameters:
offset- the starting position of the sub-buffer within the current buffer.- Returns:
- a new ByteBuffer object representing the sub-buffer starting at the given offset.
-
subBuffer
public ByteBuffer subBuffer(int offset, int length)
Creates a new ByteBuffer that is a subsection of the original buffer.- Parameters:
offset- the starting position within the original buffer where the sub buffer beginslength- the number of elements to include in the sub buffer- Returns:
- a new ByteBuffer object representing the specified range of the original buffer
-
overwrite
public ByteBuffer overwrite(ByteBuffer byteBuffer)
Overwrites the current buffer with the contents of the provided ByteBuffer.- Parameters:
byteBuffer- the ByteBuffer containing the data to overwrite with- Returns:
- the updated ByteBuffer after the overwrite operation
- Throws:
NullPointerException- ifbyteBufferis null
-
overwrite
public ByteBuffer overwrite(byte[] buffer, int length)
Overwrites a portion of the buffer with the given byte array starting at a default offset of 0.- Parameters:
buffer- the byte array containing the data to overwritelength- the number of bytes to write from the provided byte array- Returns:
- a ByteBuffer object after the overwrite operation
-
overwrite
public ByteBuffer overwrite(int startPsn, byte[] buffer, int length)
Overwrites the content at the specified position in a ByteBuffer with the provided byte array.- Parameters:
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 array- Returns:
- a ByteBuffer containing the updated data
-
overwrite
public 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. The operation adjusts the buffer size if needed.- Parameters:
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 written- Returns:
- the updated buffer after the overwrite operation
- Throws:
IllegalArgumentException- if startPsn is negative, startPsn is greater than or equal to the current size, length is negative or greater thanbuffer.length, offset is outside[0, length], or the resulting size overflows int rangeNullPointerException- if the buffer is null
-
append
public ByteBuffer append(ByteBuffer byteBuffer)
Appends the contents of the provided ByteBuffer to the current buffer.- Parameters:
byteBuffer- the ByteBuffer whose data will be appended- Returns:
- the updated ByteBuffer after appending the data
-
append
public ByteBuffer append(char c)
Appends the specified character to this buffer.- Parameters:
c- the character to be appended, represented as a char.- Returns:
- this buffer after the character has been appended.
-
append
public ByteBuffer append(byte b)
Appends a single byte to the ByteBuffer.- Parameters:
b- the byte to be appended to the ByteBuffer- Returns:
- the updated ByteBuffer instance with the new byte added
-
append
public ByteBuffer append(byte[] buffer)
Appends the specified byte array to the buffer.- Parameters:
buffer- the byte array to append- Returns:
- the updated ByteBuffer with the appended data
-
append
public ByteBuffer append(String str)
Appends the content of the provided string to the ByteBuffer using ISO-8859-1 encoding.- Parameters:
str- the string whose content is to be appended- Returns:
- the ByteBuffer instance with the appended content
-
append
public ByteBuffer append(String str, Charset charset)
Appends the specified string to the buffer using the given charset for encoding.- Parameters:
str- the string to append to the buffercharset- the charset to be used for encoding the string- Returns:
- the updated ByteBuffer after appending the encoded string
-
append
public ByteBuffer append(ValuePtr value)
Appends the given value to the buffer. The value must be properly set and not null, otherwise aNullPointerExceptionorIllegalArgumentExceptionwill be thrown.- Parameters:
value- theValuePtrobject containing the data to append. Must not be null and must be set.- Returns:
- the
ByteBufferafter appending the value. - Throws:
NullPointerException- if the value is null.IllegalArgumentException- if the value is not set.
-
append
public ByteBuffer append(byte[] buffer, int length)
Appends the specified portion of the byte array to the buffer.- Parameters:
buffer- the byte array containing the data to be appendedlength- the number of bytes to append from the beginning of the array- Returns:
- the modified ByteBuffer after appending the specified bytes
-
append
public ByteBuffer append(byte[] buffer, int offset, int length)
Appends a portion of the given byte array to the current storage buffer.- Parameters:
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 array- Returns:
- the updated ByteBuffer instance with the appended data
- Throws:
NullPointerException- ifbufferis nullIllegalArgumentException- ifoffsetorlengthis negative, ifoffset + lengthexceedsbuffer.length, or if resulting buffer size overflows int range
-
appendBase64
public ByteBuffer appendBase64(byte[] buffer)
Appends Base64 representation of all bytes from the provided byte array.- Parameters:
buffer- the source byte array- Returns:
- the updated ByteBuffer after appending Base64 content
-
appendBase64
public ByteBuffer appendBase64(byte[] buffer, int offset, int length)
Appends Base64 representation of a byte array range.- Parameters:
buffer- the source byte arrayoffset- the start position in the source byte arraylength- number of source bytes to encode- Returns:
- the updated ByteBuffer after appending Base64 content
- Throws:
NullPointerException- ifbufferis nullIllegalArgumentException- ifoffsetorlengthis negative, ifoffset + lengthexceedsbuffer.length, or if encoded/resulting size overflows int range
-
append
public ByteBuffer append(long value)
Appends the string representation of a specified long value to the current ByteBuffer.- Parameters:
value- the long value to be appended- Returns:
- the updated ByteBuffer instance
-
appendCheckSum
public ByteBuffer appendCheckSum(int checkSum)
Appends a checksum value to the current buffer by converting it into three bytes. Each digit of the checksum is stored as its ASCII character representation.- Parameters:
checkSum- the checksum value to append, represented as an integer- Returns:
- the updated ByteBuffer instance with the checksum appended
-
insert
public ByteBuffer insert(ByteBuffer buffer, int startPsn)
Inserts content from the given ByteBuffer into a specific position.- Parameters:
buffer- the ByteBuffer containing the data to be inserted; must not be nullstartPsn- the starting position where the content is to be inserted- Returns:
- a ByteBuffer representing the modified state after insertion
- Throws:
NullPointerException- if the provided buffer is null
-
insert
public ByteBuffer insert(char c, int pos)
Inserts the given character at the specified position in the ByteBuffer.- Parameters:
c- the character to be inserted, provided as a charpos- the position at which the character should be inserted- Returns:
- the updated ByteBuffer after the character has been inserted
-
insert
public ByteBuffer insert(byte b, int pos)
Inserts a byte at the specified position in the buffer.- Parameters:
b- the byte to insertpos- the position at which the byte should be inserted- Returns:
- the current buffer after the byte has been inserted
- Throws:
IllegalArgumentException- ifposis negative or greater than current size
-
insert
public ByteBuffer insert(byte b, int pos, int times)
Inserts the specified byte into the buffer multiple times at the given position.- Parameters:
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 inserted- Returns:
- the updated ByteBuffer instance with the new bytes inserted
- Throws:
IllegalArgumentException- iftimesis negative, ifposis outside[0, size], or if resulting size overflows int range
-
insert
public ByteBuffer insert(long l, int pos)
Inserts the string representation of the specified long value at the given position.- Parameters:
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 inserted- Returns:
- a ByteBuffer after inserting the string representation of the specified long value
-
insert
public 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.- Parameters:
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 bytes- Returns:
- a ByteBuffer containing the updated buffer after the insertion
-
insert
public ByteBuffer insert(String s, int pos)
Inserts the specified string into a byte buffer at the given position.- Parameters:
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 buffer- Returns:
- a ByteBuffer containing the updated content after the insertion
-
insert
public ByteBuffer insert(byte[] bytes, int pos)
Inserts the specified byte array into a buffer at the given position.- Parameters:
bytes- the byte array to be insertedpos- the position at which the byte array should be inserted- Returns:
- a ByteBuffer containing the modified buffer with the inserted byte array
-
insert
public 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.- Parameters:
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 occur- Returns:
- the resulting ByteBuffer after the insertion
-
insert
public 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.- Parameters:
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 inserted- Returns:
- a ByteBuffer instance after the insertion operation
- Throws:
NullPointerException- ifbufferis nullIllegalArgumentException- ifstartPsnis outside[0, size], ifoffsetorlengthis negative, ifoffset + lengthexceedsbuffer.length, or if resulting size overflows int range
-
capacity
public int capacity()
Returns the maximum capacity of the underlying storage.- Returns:
- the total number of elements that can be stored.
-
clear
public ByteBuffer clear()
Clears the current buffer by resetting its size to zero. Allows the buffer to be reused for new data.- Returns:
- The current ByteBuffer instance after being cleared.
-
erase
public ByteBuffer erase(int length)
Erases a portion of the buffer by specifying the length to erase. This method delegates to another erase method with a default offset of 0.- Parameters:
length- the number of bytes to erase from the buffer- Returns:
- a ByteBuffer instance after the specified portion has been erased
-
erase
public ByteBuffer erase(int length, int startPsn)
Erases a portion of the ByteBuffer starting from the specified position and of the specified length. The content after the erased portion is shifted accordingly to fill the gap.- Parameters:
length- the number of bytes to be erased from the bufferstartPsn- the starting position in the buffer from where the erasing begins- Returns:
- the modified ByteBuffer after the specified range has been erased
- Throws:
IllegalArgumentException- iflengthis negative, ifstartPsnis outside[0, size], or iflengthexceeds available data fromstartPsn
-
get
public byte[] get()
Retrieves the stored byte array.- Returns:
- a byte array representing the stored data.
-
getCopy
public byte[] getCopy()
Creates and returns a copy of the underlying storage array up to the current size.- Returns:
- a new byte array containing the copied elements from the storage array
-
getCheckSum
public int getCheckSum()
Computes the checksum of the elements stored in the 'storage' array. The checksum is calculated as the sum of the unsigned byte values of all elements in the array, modulo 256.- Returns:
- the checksum value as an integer in the range 0 to 255
-
reserveSpace
public ByteBuffer reserveSpace(int space)
Reserves additional space in the ByteBuffer.- Parameters:
space- the amount of additional space to reserve in the ByteBuffer- Returns:
- a ByteBuffer instance with the updated reserved space
- Throws:
IllegalArgumentException- ifspaceis negative or if resulting size overflows int range
-
reserve
public ByteBuffer reserve(int expectedCapacity)
Reserves the specified capacity in the storage.- Parameters:
expectedCapacity- the desired capacity to be reserved in the storage- Returns:
- the updated ByteBuffer instance with the adjusted storage capacity
- Throws:
IllegalArgumentException- ifexpectedCapacityis negative or exceeds internal growth limits
-
size
public int size()
Returns the current size.- Returns:
- the number of elements or the current size as an integer
-
length
public int length()
Returns the current size.- Returns:
- the number of elements or the current size as an integer
-
resize
public ByteBuffer resize(int newSize)
Resizes the ByteBuffer to the specified new size. Allocates additional space if required.- Parameters:
newSize- the new size to which the ByteBuffer should be resized- Returns:
- the current instance of ByteBuffer after resizing
- Throws:
IllegalArgumentException- ifnewSizeis negative
-
incLength
public ByteBuffer incLength(int size)
Increases the length of the current buffer by the specified size. Updates the internal size and ensures sufficient capacity by reserving space if necessary.- Parameters:
size- the amount by which to increase the length; must be non-negative- Returns:
- the updated ByteBuffer instance
- Throws:
IllegalArgumentException- ifsizeis negative or if resulting length overflows int range
-
reverse
public ByteBuffer reverse()
Reverses the order of the elements in the buffer within the specified range, starting from the beginning to the current size.- Returns:
- a ByteBuffer object with the elements reversed in the specified range
-
reverse
public ByteBuffer reverse(int startIdx)
Reverses the elements in the specified range within this byte buffer.- Parameters:
startIdx- the starting index of the range to reverse, inclusive.- Returns:
- a ByteBuffer with the elements in the specified range reversed.
-
reverse
public ByteBuffer reverse(int startIdx, int endIdx)
Reverses the bytes in the specified range of the storage array.- Parameters:
startIdx- the starting index of the range to reverse (inclusive)endIdx- the ending index of the range to reverse (inclusive)- Returns:
- the current ByteBuffer instance with the specified range reversed
-
findIndex
public int findIndex(int startPsn, byte[] subBuffer)Searches for the first occurrence of the specified sub-buffer starting from the given position.- Parameters:
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.- Returns:
- the index of the first occurrence of the specified sub-buffer, or -1 if the sub-buffer is not found or if the sub-buffer's length exceeds the size of the buffer.
- Throws:
IllegalArgumentException- if the starting position is negative or the sub-buffer is emptyNullPointerException- if the sub-buffer is null.
-
write
public ByteBuffer write(OutputStream outputStream) throws IOException
Writes the contents of the internal storage buffer to the specified OutputStream.- Parameters:
outputStream- the OutputStream to which the data will be written- Returns:
- the current ByteBuffer instance after writing the data
- Throws:
IOException- if an I/O error occurs during writing
-
compareTo
public int compareTo(ByteBuffer o)
- Specified by:
compareToin interfaceComparable<ByteBuffer>
-
-