-
- All Implemented Interfaces:
-
java.io.Serializable,java.lang.Cloneable
public class ByteArray implements Serializable, Cloneable
The ByteArray class provides a flexible and efficient way of managing and manipulating byte arrays. It includes various constructors, methods for appending, prepending, copying, and resetting byte data, and functionalities for equality checks, hashing, and string representation. This class supports both read-only and writable modes and ensures proper handling of capacity and bounds.
-
-
Field Summary
Fields Modifier and Type Field Description public final static intDEFAULT_CAPACITYpublic Array<byte>arrpublic intoffsetpublic intlength
-
Constructor Summary
Constructors Constructor Description ByteArray(Array<byte> p_arr, int p_offset, int p_length)Constructs a ByteArray object with the specified byte array, offset, and length. ByteArray(Array<byte> p_arr)Constructs a ByteArray object with the specified byte array.Initializes the byte array, offset, length, and read-only state based on the input. ByteArray(int capacity)Constructs a ByteArray with the specified initial capacity. ByteArray()Default constructor for creating a ByteArray with the default capacity.It initializes the internal byte array to the predefined default size. ByteArray(String str)Constructor that initializes the ByteArray object using the given string. ByteArray(ByteArray b)Constructs a new ByteArray object by copying the contents of the specified ByteArray.
-
Method Summary
Modifier and Type Method Description intgetLength()Retrieves the length value. ByteArraycopy(ByteArray b)Creates a copy of the given ByteArray and appends its contents to the current ByteArray. ByteArraycopy(Array<byte> b)Creates a copy of the provided byte array, resets the offset and length to zero,and sets the object to be writable. ByteArraycopy(Array<byte> b, int offset, int len)Copies a specified portion of the given byte array into a ByteArray object. voidreset()Resets the state of the object to its initial values.This method sets the `offset` and `length` to 0, fills the array with zero bytes,and sets the `readOnly` flag to false. synchronized inthashCode()Computes a hash code for the object based on its internal state.The method ensures thread-safety through synchronization and usesa buffer to calculate the hash code value iteratively over an array. booleanequals(Object obj)Compares this ByteArray object to the specified object for equality. StringtoString()Returns a string representation of the specified portion of the array. ByteArrayappend(Array<byte> a)Appends the given byte array to the current ByteArray instance. ByteArrayappendIPAddress(Array<byte> a)Appends an IP address represented as a byte array to the underlying data structure.Each byte in the array is interpreted as a segment of the IP address, separated by dots. ByteArrayappend(Array<byte> a, int p_offset, int p_length)Appends a portion of the given byte array to the current ByteArray object. ByteArrayappend(ByteArray p)Appends the specified ByteArray to the current ByteArray instance. ByteArrayappend(char a)Appends a single character to the current ByteArray instance. ByteArrayappend(char a, char b)Appends two characters to the current ByteArray instance.Converts the provided characters to bytes and stores them sequentiallyin the underlying byte array. ByteArrayappend(char a, char b, char c)Appends three characters to the current ByteArray.The characters provided are cast to bytes and added sequentiallyat the end of the current ByteArray. ByteArrayappend(char a, char b, char c, char d)Appends four characters to the ByteArray. ByteArrayappend(int n)Appends the byte representation of the provided integer to the current byte array. ByteArrayappend(long n)Appends the string representation of the specified long valueto the current ByteArray. booleanisEmpty()Checks if the collection or data structure is empty. Array<byte>getBytes()Copies a portion of the source array into a new byte array and returns it. ByteArrayprepend(ByteArray b)Prepends the content of the specified ByteArray to this ByteArray. ByteArrayprepend(Array<byte> inputData, int inputOffset, int inputLen)Prepends a specified portion of the given byte array to the beginning of the current ByteArray. ByteArrayappendByte(int i)Appends a single byte, represented as an integer, to the current ByteArray. ByteArrayappendByte(long l)Appends a byte represented by the specified long value to the current ByteArray. ByteArrayappend(String string)Appends the byte representation of the given string to the ByteArray. byteendCharacter()Retrieves the last character in the byte array based on the current offset and length. bytestartCharacter()Retrieves the starting character from the byte array at a specific offset. ByteArrayreadOnlyCopy()Creates a read-only copy of the current ByteArray instance. ByteArrayincrement()Increments the offset by one and decrements the length by one. bytegetCharacterAt(int index)Retrieves the byte value of a character at the specified index in the internal byte array. ByteArrayappend(ByteArray b, int length)Appends a portion of the given ByteArray to the current ByteArray. booleangetStrUptoSlashR(String searchStr, ByteArray buffer, int maxLength)Extracts a string up to the occurrence of a carriage return character ('\r')from the specified input string and stores it in the provided buffer. booleangetStrUptoSlashR(ByteArray searchStr, ByteArray buffer, int maxLength)Extracts a substring from the given search string up to the occurrence of a carriage return ('\r') or until the specified maximum length. booleangetStrUptoSpace(ByteArray searchStr, ByteArray buffer, int maxLength)Extracts a substring from the current instance of a ByteArray until a spaceor carriage return is encountered, or until the specified maximum length is reached. booleangetStrValueUptoSlashR(String searchStr, ByteArray buffer, int maxLength)Retrieves a value from the buffer up to the occurrence of a '\r' or maxLength in the given search string,with a restriction on the maximum length of the value extracted. booleangetStrValueUptoSlashR(ByteArray searchStr, ByteArray buffer, int maxLength)Searches for a specific string within a byte array and extracts characters up to a carriage return ('\r')or newline ('\n'), appending them to a buffer. booleangetStrValueUptoSlashRFromBalance(ByteArray searchStr, ByteArray buffer, int maxLength)Searches for the specified byte array within the object's buffer, retrievesvalues up to the first occurrence of a carriage return ('\r'), and appendsthem to a provided buffer. intiTelStrstr(String searchStr)Searches for the first occurrence of a substring within a byte arraycreated from the given string. intiTelStrstr(ByteArray searchStr)Searches for the first occurrence of a substring within the current byte array,ignoring case differences in ASCII characters. booleanstartsWith(String startStr)Checks if the current byte array starts with the specified string.This method internally converts the string to its corresponding byte arrayand then performs the comparison. booleanstartsWith(Array<byte> startStr)Checks if the current byte array starts with the specified byte arrayprefix. voidforward(int length)Advances the internal offset of the byte array by the specified length and reducesthe available length accordingly. voiddropBytes(int index, int length)Removes a specified number of bytes from the byte array, starting at the given index.This shifts all subsequent bytes to fill the removed space and adjusts the length of the array. bytecharAt(int i)Returns the byte value at the specified index in the byte array.The index is computed relative to the internal offset and must bewithin the bounds of the array length. inttoInteger()Converts a sequence of numeric characters within the underlying byte array (`arr`)to an integer. inttoIntegerWhenValueIsInteger()Converts the first two bytes of the underlying byte array (`arr`) into an integer.The method interprets the first byte (`arr[0]`) as the higher-order byteand the second byte (`arr[1]`) as the lower-order byte, combining them using bitwise operations. booleangetStrValueUptoSpace(String searchStr, ByteArray buffer, int maxLength)Searches for a specified substring within the given string and extractsa sequence of characters from the underlying data buffer starting after theoccurrence of the specified substring. booleangetStrValueUptoSpace(ByteArray searchStr, ByteArray buffer, int maxLength)Searches for a specified substring within the current ByteArray and extractsa sequence of characters from the underlying byte array starting after theoccurrence of the specified substring. Objectclone()Creates and returns a copy of this object.The method performs a shallow copy of the current objectby invoking the `clone` method from the `Object` class. -
-
Constructor Detail
-
ByteArray
ByteArray(Array<byte> p_arr, int p_offset, int p_length)
Constructs a ByteArray object with the specified byte array, offset, and length.- Parameters:
p_arr- the byte array from which data will be usedp_offset- the starting offset within the byte arrayp_length- the number of bytes to include from the specified offset
-
ByteArray
ByteArray(Array<byte> p_arr)
Constructs a ByteArray object with the specified byte array.Initializes the byte array, offset, length, and read-only state based on the input.- Parameters:
p_arr- the byte array to initialize the ByteArray object with.If null, a default capacity array will be created, and the object will be writable.If non-null, the object will be initialized with the given array and set to read-only.
-
ByteArray
ByteArray(int capacity)
Constructs a ByteArray with the specified initial capacity.- Parameters:
capacity- the initial size of the byte array to be created
-
ByteArray
ByteArray()
Default constructor for creating a ByteArray with the default capacity.It initializes the internal byte array to the predefined default size.
-
ByteArray
ByteArray(String str)
Constructor that initializes the ByteArray object using the given string.- Parameters:
str- the input string to be converted into a byte array
-
ByteArray
ByteArray(ByteArray b)
Constructs a new ByteArray object by copying the contents of the specified ByteArray.- Parameters:
b- the ByteArray to be copied
-
-
Method Detail
-
getLength
int getLength()
Retrieves the length value.
-
copy
ByteArray copy(ByteArray b)
Creates a copy of the given ByteArray and appends its contents to the current ByteArray.
- Parameters:
b- the ByteArray to be copied and appended
-
copy
ByteArray copy(Array<byte> b)
Creates a copy of the provided byte array, resets the offset and length to zero,and sets the object to be writable. The byte array is appended to the current data.
- Parameters:
b- The byte array to copy and append.
-
copy
ByteArray copy(Array<byte> b, int offset, int len)
Copies a specified portion of the given byte array into a ByteArray object.
- Parameters:
b- the byte array to be copied fromoffset- the starting position in the array to begin copyinglen- the number of bytes to copy from the array
-
reset
void reset()
Resets the state of the object to its initial values.This method sets the `offset` and `length` to 0, fills the array with zero bytes,and sets the `readOnly` flag to false. Use this method to clear the current stateand prepare the object for reuse.
-
hashCode
synchronized int hashCode()
Computes a hash code for the object based on its internal state.The method ensures thread-safety through synchronization and usesa buffer to calculate the hash code value iteratively over an array.
-
equals
boolean equals(Object obj)
Compares this ByteArray object to the specified object for equality.
- Parameters:
obj- the object to be compared for equality with this ByteArray
-
toString
@NotNull() String toString()
Returns a string representation of the specified portion of the array.
-
append
ByteArray append(Array<byte> a)
Appends the given byte array to the current ByteArray instance.
- Parameters:
a- the byte array to append; if null, the method performs no operation
-
appendIPAddress
ByteArray appendIPAddress(Array<byte> a)
Appends an IP address represented as a byte array to the underlying data structure.Each byte in the array is interpreted as a segment of the IP address, separated by dots.
- Parameters:
a- the byte array representing the IP address; each byte corresponds to a segment of the address
-
append
ByteArray append(Array<byte> a, int p_offset, int p_length)
Appends a portion of the given byte array to the current ByteArray object.
- Parameters:
a- the byte array to append fromp_offset- the starting offset in the given byte arrayp_length- the number of bytes to append
-
append
ByteArray append(ByteArray p)
Appends the specified ByteArray to the current ByteArray instance.
- Parameters:
p- the ByteArray to append to the current instance; can be null
-
append
ByteArray append(char a)
Appends a single character to the current ByteArray instance.
- Parameters:
a- the character to append
-
append
ByteArray append(char a, char b)
Appends two characters to the current ByteArray instance.Converts the provided characters to bytes and stores them sequentiallyin the underlying byte array.
- Parameters:
a- the first character to appendb- the second character to append
-
append
ByteArray append(char a, char b, char c)
Appends three characters to the current ByteArray.The characters provided are cast to bytes and added sequentiallyat the end of the current ByteArray. The method verifies if thearray has sufficient capacity for the new bytes and if modificationsare allowed. Throws an exception if any of these conditions are not met.
- Parameters:
a- the first character to appendb- the second character to appendc- the third character to append
-
append
ByteArray append(char a, char b, char c, char d)
Appends four characters to the ByteArray. The characters are cast to bytesand stored in sequence within the internal byte array.
- Parameters:
a- the first character to append, cast to a byte before storingb- the second character to append, cast to a byte before storingc- the third character to append, cast to a byte before storingd- the fourth character to append, cast to a byte before storing
-
append
ByteArray append(int n)
Appends the byte representation of the provided integer to the current byte array.
- Parameters:
n- the integer value to be converted to bytes and appended
-
append
ByteArray append(long n)
Appends the string representation of the specified long valueto the current ByteArray.
- Parameters:
n- the long value to be converted to a string and appended
-
isEmpty
boolean isEmpty()
Checks if the collection or data structure is empty.
-
getBytes
Array<byte> getBytes()
Copies a portion of the source array into a new byte array and returns it.
-
prepend
ByteArray prepend(ByteArray b)
Prepends the content of the specified ByteArray to this ByteArray.
- Parameters:
b- the ByteArray to be prepended to this ByteArray.
-
prepend
ByteArray prepend(Array<byte> inputData, int inputOffset, int inputLen)
Prepends a specified portion of the given byte array to the beginning of the current ByteArray.
- Parameters:
inputData- The byte array containing the data to prepend.inputOffset- The starting offset in the inputData array from where the data should be read.inputLen- The number of bytes to read from the inputData array starting at inputOffset.
-
appendByte
ByteArray appendByte(int i)
Appends a single byte, represented as an integer, to the current ByteArray.
- Parameters:
i- the byte value to be appended, represented as an integer (0-255)
-
appendByte
ByteArray appendByte(long l)
Appends a byte represented by the specified long value to the current ByteArray.
- Parameters:
l- the long value whose lower 8 bits are to be appended as a byte
-
append
ByteArray append(String string)
Appends the byte representation of the given string to the ByteArray.
- Parameters:
string- the string to be converted to bytes and appended; if null, an empty byte array is appended
-
endCharacter
byte endCharacter()
Retrieves the last character in the byte array based on the current offset and length.
-
startCharacter
byte startCharacter()
Retrieves the starting character from the byte array at a specific offset.
-
readOnlyCopy
ByteArray readOnlyCopy()
Creates a read-only copy of the current ByteArray instance.
-
getCharacterAt
byte getCharacterAt(int index)
Retrieves the byte value of a character at the specified index in the internal byte array.
- Parameters:
index- the position in the array to retrieve the character from, where 0 is the starting index.
-
append
ByteArray append(ByteArray b, int length)
Appends a portion of the given ByteArray to the current ByteArray.
- Parameters:
b- the ByteArray to append to the current ByteArraylength- the number of bytes to append; if negative, the full length of the given ByteArray is appended
-
getStrUptoSlashR
boolean getStrUptoSlashR(String searchStr, ByteArray buffer, int maxLength)
Extracts a string up to the occurrence of a carriage return character ('\r')from the specified input string and stores it in the provided buffer.
- Parameters:
searchStr- the input string to search for a carriage return characterbuffer- the buffer where the extracted string will be storedmaxLength- the maximum number of characters to extract and store in the buffer
-
getStrUptoSlashR
boolean getStrUptoSlashR(ByteArray searchStr, ByteArray buffer, int maxLength)
Extracts a substring from the given search string up to the occurrence of a carriage return ('\r') or until the specified maximum length.
- Parameters:
searchStr- The string in which to search for the substring.buffer- The buffer to store the extracted substring.maxLength- The maximum length to extract, regardless of whether a carriage return is found.
-
getStrUptoSpace
boolean getStrUptoSpace(ByteArray searchStr, ByteArray buffer, int maxLength)
Extracts a substring from the current instance of a ByteArray until a spaceor carriage return is encountered, or until the specified maximum length is reached.
- Parameters:
searchStr- the ByteArray representing the substring to search for.buffer- the ByteArray to store the extracted substring.maxLength- the maximum number of characters to extract from the substring.
-
getStrValueUptoSlashR
boolean getStrValueUptoSlashR(String searchStr, ByteArray buffer, int maxLength)
Retrieves a value from the buffer up to the occurrence of a '\r' or maxLength in the given search string,with a restriction on the maximum length of the value extracted. This method delegates the operation toanother overloaded method using a ByteArray representation of the search string.
- Parameters:
searchStr- the string to search for within the bufferbuffer- the ByteArray buffer from which the value will be extractedmaxLength- the maximum allowed length of the string value to be retrieved
-
getStrValueUptoSlashR
boolean getStrValueUptoSlashR(ByteArray searchStr, ByteArray buffer, int maxLength)
Searches for a specific string within a byte array and extracts characters up to a carriage return ('\r')or newline ('\n'), appending them to a buffer. If the target string is not found, the buffer is cleared.
- Parameters:
searchStr- The target string to search for within the byte array.buffer- The buffer to store the extracted characters up to a carriage return or newline.maxLength- The maximum length of characters to extract and append to the buffer.
-
getStrValueUptoSlashRFromBalance
boolean getStrValueUptoSlashRFromBalance(ByteArray searchStr, ByteArray buffer, int maxLength)
Searches for the specified byte array within the object's buffer, retrievesvalues up to the first occurrence of a carriage return ('\r'), and appendsthem to a provided buffer. The method stops at either the maximum specifiedlength or a carriage return, whichever occurs first.
- Parameters:
searchStr- The ByteArray to search for within the object's buffer.buffer- The ByteArray where the retrieved values will be appended.maxLength- The maximum number of bytes to read and append to the buffer.
-
iTelStrstr
int iTelStrstr(String searchStr)
Searches for the first occurrence of a substring within a byte arraycreated from the given string.
- Parameters:
searchStr- the string to search for within the current byte array.This string is converted into a ByteArray internally.
-
iTelStrstr
int iTelStrstr(ByteArray searchStr)
Searches for the first occurrence of a substring within the current byte array,ignoring case differences in ASCII characters.
- Parameters:
searchStr- the ByteArray to search for within the current byte array.
-
startsWith
boolean startsWith(String startStr)
Checks if the current byte array starts with the specified string.This method internally converts the string to its corresponding byte arrayand then performs the comparison.
- Parameters:
startStr- the string to check as the prefix of the current byte array.
-
startsWith
boolean startsWith(Array<byte> startStr)
Checks if the current byte array starts with the specified byte arrayprefix. The comparison is case-insensitive for characters withinthe ASCII range.
- Parameters:
startStr- the byte array to check as the prefix of the currentbyte array.
-
forward
void forward(int length)
Advances the internal offset of the byte array by the specified length and reducesthe available length accordingly. This is typically used to skip over a portionof the byte array.
- Parameters:
length- the number of bytes to advance the offset by.
-
dropBytes
void dropBytes(int index, int length)
Removes a specified number of bytes from the byte array, starting at the given index.This shifts all subsequent bytes to fill the removed space and adjusts the length of the array.
- Parameters:
index- the starting position from which bytes are to be removed.length- the number of bytes to be removed.
-
charAt
byte charAt(int i)
Returns the byte value at the specified index in the byte array.The index is computed relative to the internal offset and must bewithin the bounds of the array length.
- Parameters:
i- the index of the byte to be returned, relative to the offset.
-
toInteger
int toInteger()
Converts a sequence of numeric characters within the underlying byte array (`arr`)to an integer. The conversion begins at the specified `offset` and continuesup to the `length` of the array or until a non-numeric character (e.g., '\r') is encountered.Non-numeric characters are ignored, and the method processes only valid digit characters ('0' to '9').
-
toIntegerWhenValueIsInteger
int toIntegerWhenValueIsInteger()
Converts the first two bytes of the underlying byte array (`arr`) into an integer.The method interprets the first byte (`arr[0]`) as the higher-order byteand the second byte (`arr[1]`) as the lower-order byte, combining them using bitwise operations.
-
getStrValueUptoSpace
boolean getStrValueUptoSpace(String searchStr, ByteArray buffer, int maxLength)
Searches for a specified substring within the given string and extractsa sequence of characters from the underlying data buffer starting after theoccurrence of the specified substring. The characters are appended to theprovided buffer until a space, carriage return, newline, or the maximumlength is reached.
- Parameters:
searchStr- the string to search for within the current byte array.buffer- the ByteArray to which the extracted sequence of characters willbe appended.maxLength- the maximum number of characters to extract and append intothe buffer.
-
getStrValueUptoSpace
boolean getStrValueUptoSpace(ByteArray searchStr, ByteArray buffer, int maxLength)
Searches for a specified substring within the current ByteArray and extractsa sequence of characters from the underlying byte array starting after theoccurrence of the specified substring. The characters are appended to theprovided buffer until a space, carriage return, newline, or the maximumlength is reached.
- Parameters:
searchStr- the ByteArray to search for within the current ByteArray.buffer- the ByteArray to which the extracted sequence of characters willbe appended.maxLength- the maximum number of characters to extract and append intothe buffer.
-
-
-
-