Package com.github.toolarium.common
Class ByteArray
java.lang.Object
com.github.toolarium.common.ByteArray
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable,Cloneable,Channel,ReadableByteChannel,WritableByteChannel
public class ByteArray
extends Object
implements Serializable, WritableByteChannel, ReadableByteChannel, Cloneable
Class used to represent an array of bytes as an Object.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ByteArrayThe cr character as byte arraystatic final intDefines the default byte array sizestatic final ByteArrayThe dot character as byte arraystatic final ByteArrayThe dot character as byte arraystatic final ByteArrayThe new line character as byte arraystatic final ByteArrayThe space as byte arraystatic final ByteArrayThe tab character as byte array -
Constructor Summary
ConstructorsConstructorDescriptionCreate a ByteArray with the default sizeByteArray(byte[] data) Create a ByteArray from an array of bytesCopy constructorCreate a ByteArray from a StringByteArray(StringBuffer data) Create a ByteArray from a StringByteArray(StringBuilder data) Create a ByteArray from a String -
Method Summary
Modifier and TypeMethodDescriptionappend(byte ch) Append a byteappend(byte[] data) Append an array of bytesappend(byte[] data, int off, int len) Append an array of bytesAppend a ByteArrayAppend a Stringappend(StringBuffer data) Append a StringBufferappend(StringBuilder data) Append a StringBuilderappend(ByteBuffer data) Append a bufferchopLeft()Chop a bytechopLeft(int num) Chop some bytesChop a bytechopRight(int num) Chop some bytesclone()voidclose()concat(byte b) Concat a given byte and return a new byte arrayConcat a given byte and return a new byte arrayintcountEndsWith(ByteArray prefix) Test how many times the given prefix is contained at the endintcountStartsWith(ByteArray prefix) Test how many times the given prefix is contained at the beginningstatic ByteArraycreateByteArray(byte b, int size) Creates a byte array with a simple bytestatic ByteArraycreateByteArray(ByteBuffer buffer) Creates a byte arraystatic ByteArraycreateByteArrayWithSize(int size) Creates a byte array with the given sizeprotected voidcrowUpDataSize(int requestedSpace) Gets the additional sizebooleanendsWith(byte prefix) Test if the array ends with the given prefixbooleanTest if the array starts with the given prefixbooleanImplements the default functionality of the equals method.erase()Erase the byte arraybyteget(int pos) Return a specific byte from the byte arraygetBytes()Return the bytes as a copygetBytes(int startPos, int endPos) Return the bytes as a copyprotected intgetNewDataSize(int requestedSpace) Gets the new data sizeinthashCode()intindexOf(byte toFind) Finds a byte in the dataintReturns the index within this byte array of the first occurrence of the specified substring, starting at the specified index.intReturns the index within this byte array of the first occurrence of the specified substring, starting at the specified index.booleanisOpen()intlastIndexOf(byte toFind) Finds a byte in the dataintlength()Return the number of bytesintread(ByteBuffer dst) replace(byte toReplace, byte newByte) Replaces a given byte with anotherReplaces data in a given byte array.reverse()Reverse the internal arraybyteset(int pos, byte b) Set a specific byte in the byte arraybooleanstartsWith(byte prefix) Test if the array starts with the given prefixbooleanstartsWith(ByteArray prefix) Test if the array starts with the given prefixbooleanstartsWith(ByteArray prefix, int toffset) Test if the array starts with the given prefixtoByteArray(int startPos, int endPos) Return a sub part of the original bytesReturn a byte buffer of the databyte[]toBytes()Return the bytes as a copy of the originaltoHex()Convert to hex stringlongtoLong()Build a long from the array.toString()Convert to Stringtrim(byte ch) Trims the data with the given bytetrimLeft(byte ch) Trims the data left with the given bytetrimRight(byte ch) Trims the data right with the given byteintwrite(ByteBuffer src)
-
Field Details
-
NL
The new line character as byte array -
CR
The cr character as byte array -
TAB
The tab character as byte array -
SPACE
The space as byte array -
DOT
The dot character as byte array -
EMPTY
The dot character as byte array -
DEFAULT_BLOCK_SIZE
public static final int DEFAULT_BLOCK_SIZEDefines the default byte array size- See Also:
-
-
Constructor Details
-
ByteArray
public ByteArray()Create a ByteArray with the default size -
ByteArray
Create a ByteArray from a String- Parameters:
data- initial data
-
ByteArray
Create a ByteArray from a String- Parameters:
data- initial data
-
ByteArray
Create a ByteArray from a String- Parameters:
data- initial data
-
ByteArray
Copy constructor- Parameters:
data- initial data
-
ByteArray
public ByteArray(byte[] data) Create a ByteArray from an array of bytes- Parameters:
data- initial data
-
-
Method Details
-
createByteArrayWithSize
Creates a byte array with the given size- Parameters:
size- the size- Returns:
- the byte array with the given size
-
createByteArray
Creates a byte array with a simple byte- Parameters:
b- the bytesize- the size of the byte array- Returns:
- the created byte array
-
createByteArray
Creates a byte array- Parameters:
buffer- the byte buffer- Returns:
- the created byte array
-
append
Append a byte- Parameters:
ch- the data- Returns:
- the byte array (no copy)
-
append
Append a ByteArray- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
Append an array of bytes- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
Append an array of bytes- Parameters:
data- the dataoff- the offsetlen- the length- Returns:
- the byte array (no copy)
-
append
Append a String- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
Append a StringBuilder- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
Append a StringBuffer- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
Append a buffer- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
reverse
Reverse the internal array- Returns:
- the reversed byte array (no copy)
-
trim
Trims the data with the given byte- Parameters:
ch- the trim byte- Returns:
- the byte array (no copy)
-
trimRight
Trims the data right with the given byte- Parameters:
ch- the trim byte- Returns:
- the byte array (no copy)
-
trimLeft
Trims the data left with the given byte- Parameters:
ch- the trim byte- Returns:
- the byte array (no copy)
-
replace
Replaces a given byte with another- Parameters:
toReplace- the byte to replacenewByte- the new byte- Returns:
- the byte array (no copy)
-
replace
Replaces data in a given byte array.- Parameters:
replaceData- the data to replacenewData- the new data- Returns:
- the replaced byte array as a copy (copy)
-
indexOf
public int indexOf(byte toFind) Finds a byte in the data- Parameters:
toFind- the byte to find- Returns:
- the position in the data
-
indexOf
Returns the index within this byte array of the first occurrence of the specified substring, starting at the specified index.- Parameters:
toFind- the sub array for which to search.- Returns:
- the index within this array of the first occurrence of the specified sub array, starting at the specified index.
-
indexOf
Returns the index within this byte array of the first occurrence of the specified substring, starting at the specified index.- Parameters:
toFind- the sub array for which to search.fromIndex- the index from which to start the search.- Returns:
- the index within this array of the first occurrence of the specified sub array, starting at the specified index.
-
lastIndexOf
public int lastIndexOf(byte toFind) Finds a byte in the data- Parameters:
toFind- the byte to find- Returns:
- the position in the data
-
startsWith
Test if the array starts with the given prefix- Parameters:
prefix- the bytes to test- Returns:
- true if it starts with the prefix
-
startsWith
Test if the array starts with the given prefix- Parameters:
prefix- the bytes to testtoffset- the offset to start- Returns:
- true if it starts with the prefix
-
startsWith
public boolean startsWith(byte prefix) Test if the array starts with the given prefix- Parameters:
prefix- the bytes to test- Returns:
- true if it starts with the prefix
-
countStartsWith
Test how many times the given prefix is contained at the beginning- Parameters:
prefix- the bytes to test- Returns:
- how many times the prefix was found at the beginning
-
endsWith
public boolean endsWith(byte prefix) Test if the array ends with the given prefix- Parameters:
prefix- the bytes to test- Returns:
- true if it ends with the prefix
-
endsWith
Test if the array starts with the given prefix- Parameters:
prefix- the bytes to test- Returns:
- true if it starts with the prefix
-
countEndsWith
Test how many times the given prefix is contained at the end- Parameters:
prefix- the bytes to test- Returns:
- how many times the prefix was found at the end
-
toHex
Convert to hex string- Returns:
- the byte array as hex string
-
toString
Convert to String -
toBytes
public byte[] toBytes()Return the bytes as a copy of the original- Returns:
- the bytes (copy)
-
toLong
public long toLong()Build a long from the array.- Returns:
- the long value.
-
toByteBuffer
Return a byte buffer of the data- Returns:
- the buffer
-
toByteArray
Return a sub part of the original bytes- Parameters:
startPos- the start positionendPos- the end position- Returns:
- the bytes (copy)
-
getBytes
Return the bytes as a copy- Returns:
- the bytes (copy)
-
getBytes
Return the bytes as a copy- Parameters:
startPos- the start positionendPos- the end position- Returns:
- the bytes (copy)
-
concat
Concat a given byte and return a new byte array- Parameters:
b- the byte to append- Returns:
- the concated byte array (copy)
-
concat
Concat a given byte and return a new byte array- Parameters:
b- the byte to append- Returns:
- the concated byte array (copy)
-
get
Return a specific byte from the byte array- Parameters:
pos- the position in the byte array- Returns:
- a byte
- Throws:
ArrayIndexOutOfBoundsException- in case of error
-
set
Set a specific byte in the byte array- Parameters:
pos- the position in the byte arrayb- the byte to set in the byte array- Returns:
- the replaced byte
- Throws:
ArrayIndexOutOfBoundsException- in case of error
-
length
public int length()Return the number of bytes- Returns:
- the number of bytes
-
erase
Erase the byte array- Returns:
- the byte array (no copy)
-
chopRight
Chop a byte- Returns:
- the byte array (no copy)
-
chopRight
Chop some bytes- Parameters:
num- number of bytes to chop- Returns:
- the byte array (no copy)
-
chopLeft
Chop a byte- Returns:
- the byte array (no copy)
-
chopLeft
Chop some bytes- Parameters:
num- number of bytes to chop- Returns:
- the byte array (no copy)
-
write
- Specified by:
writein interfaceWritableByteChannel- See Also:
-
close
public void close() -
isOpen
public boolean isOpen() -
read
- Specified by:
readin interfaceReadableByteChannel- See Also:
-
equals
Implements the default functionality of the equals method. Subclasses should implements their own equals method like:
public boolean equals( MyObject other ) { if( this==other ) return true; if( !super.equals( other ) ) return false; if( x !=((MyObject)other).x ) return false; ... return true; } -
hashCode
public int hashCode() -
clone
-
crowUpDataSize
protected void crowUpDataSize(int requestedSpace) Gets the additional size- Parameters:
requestedSpace- the requested space
-
getNewDataSize
protected int getNewDataSize(int requestedSpace) Gets the new data size- Parameters:
requestedSpace- the requested space- Returns:
- the new size
-