Package com.github.toolarium.common
Class ByteArray
- java.lang.Object
-
- com.github.toolarium.common.ByteArray
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Serializable,java.lang.AutoCloseable,java.lang.Cloneable,java.nio.channels.Channel,java.nio.channels.ReadableByteChannel,java.nio.channels.WritableByteChannel
public class ByteArray extends java.lang.Object implements java.io.Serializable, java.nio.channels.WritableByteChannel, java.nio.channels.ReadableByteChannel, java.lang.CloneableClass used to represent an array of bytes as an Object.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static ByteArrayCRThe cr character as byte arraystatic intDEFAULT_BLOCK_SIZEDefines the default byte array sizestatic ByteArrayDOTThe dot character as byte arraystatic ByteArrayEMPTYThe dot character as byte arraystatic ByteArrayNLThe new line character as byte arraystatic ByteArraySPACEThe space as byte arraystatic ByteArrayTABThe tab character as byte array
-
Constructor Summary
Constructors Constructor Description ByteArray()Create a ByteArray with the default sizeByteArray(byte[] data)Create a ByteArray from an array of bytesByteArray(ByteArray data)Copy constructorByteArray(java.lang.String data)Create a ByteArray from a StringByteArray(java.lang.StringBuffer data)Create a ByteArray from a StringByteArray(java.lang.StringBuilder data)Create a ByteArray from a String
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteArrayappend(byte ch)Append a byteByteArrayappend(byte[] data)Append an array of bytesByteArrayappend(byte[] data, int off, int len)Append an array of bytesByteArrayappend(ByteArray data)Append a ByteArrayByteArrayappend(java.lang.String data)Append a StringByteArrayappend(java.lang.StringBuffer data)Append a StringBufferByteArrayappend(java.lang.StringBuilder data)Append a StringBuilderByteArrayappend(java.nio.ByteBuffer data)Append a bufferByteArraychopLeft()Chop a byteByteArraychopLeft(int num)Chop some bytesByteArraychopRight()Chop a byteByteArraychopRight(int num)Chop some bytesByteArrayclone()voidclose()ByteArrayconcat(byte b)Concat a given byte and return a new byte arrayByteArrayconcat(ByteArray b)Concat 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(java.nio.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 prefixbooleanendsWith(ByteArray prefix)Test if the array starts with the given prefixbooleanequals(java.lang.Object other)Implements the default functionality of the equals method.ByteArrayerase()Erase the byte arraybyteget(int pos)Return a specific byte from the byte arrayByteArraygetBytes()Return the bytes as a copyByteArraygetBytes(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 dataintindexOf(ByteArray toFind)Returns the index within this byte array of the first occurrence of the specified substring, starting at the specified index.intindexOf(ByteArray toFind, int fromIndex)Returns 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(java.nio.ByteBuffer dst)ByteArrayreplace(byte toReplace, byte newByte)Replaces a given byte with anotherByteArrayreplace(ByteArray replaceData, ByteArray newData)Replaces data in a given byte array.ByteArrayreverse()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 prefixByteArraytoByteArray(int startPos, int endPos)Return a sub part of the original bytesjava.nio.ByteBuffertoByteBuffer()Return a byte buffer of the databyte[]toBytes()Return the bytes as a copy of the originaljava.lang.StringtoHex()Convert to hex stringlongtoLong()Build a long from the array.java.lang.StringtoString()Convert to StringByteArraytrim(byte ch)Trims the data with the given byteByteArraytrimLeft(byte ch)Trims the data left with the given byteByteArraytrimRight(byte ch)Trims the data right with the given byteintwrite(java.nio.ByteBuffer src)
-
-
-
Field Detail
-
NL
public static final ByteArray NL
The new line character as byte array
-
CR
public static final ByteArray CR
The cr character as byte array
-
TAB
public static final ByteArray TAB
The tab character as byte array
-
SPACE
public static final ByteArray SPACE
The space as byte array
-
DOT
public static final ByteArray DOT
The dot character as byte array
-
EMPTY
public static final ByteArray EMPTY
The dot character as byte array
-
DEFAULT_BLOCK_SIZE
public static final int DEFAULT_BLOCK_SIZE
Defines the default byte array size- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ByteArray
public ByteArray()
Create a ByteArray with the default size
-
ByteArray
public ByteArray(java.lang.String data)
Create a ByteArray from a String- Parameters:
data- initial data
-
ByteArray
public ByteArray(java.lang.StringBuilder data)
Create a ByteArray from a String- Parameters:
data- initial data
-
ByteArray
public ByteArray(java.lang.StringBuffer data)
Create a ByteArray from a String- Parameters:
data- initial data
-
ByteArray
public ByteArray(ByteArray data)
Copy constructor- Parameters:
data- initial data
-
ByteArray
public ByteArray(byte[] data)
Create a ByteArray from an array of bytes- Parameters:
data- initial data
-
-
Method Detail
-
createByteArrayWithSize
public static ByteArray createByteArrayWithSize(int size)
Creates a byte array with the given size- Parameters:
size- the size- Returns:
- the byte array with the given size
-
createByteArray
public static ByteArray createByteArray(byte b, int size)
Creates a byte array with a simple byte- Parameters:
b- the bytesize- the size of the byte array- Returns:
- the created byte array
-
createByteArray
public static ByteArray createByteArray(java.nio.ByteBuffer buffer)
Creates a byte array- Parameters:
buffer- the byte buffer- Returns:
- the created byte array
-
append
public ByteArray append(byte ch)
Append a byte- Parameters:
ch- the data- Returns:
- the byte array (no copy)
-
append
public ByteArray append(ByteArray data)
Append a ByteArray- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
public ByteArray append(byte[] data)
Append an array of bytes- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
public ByteArray append(byte[] data, int off, int len)
Append an array of bytes- Parameters:
data- the dataoff- the offsetlen- the length- Returns:
- the byte array (no copy)
-
append
public ByteArray append(java.lang.String data)
Append a String- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
public ByteArray append(java.lang.StringBuilder data)
Append a StringBuilder- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
public ByteArray append(java.lang.StringBuffer data)
Append a StringBuffer- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
append
public ByteArray append(java.nio.ByteBuffer data)
Append a buffer- Parameters:
data- the data- Returns:
- the byte array (no copy)
-
reverse
public ByteArray reverse()
Reverse the internal array- Returns:
- the reversed byte array (no copy)
-
trim
public ByteArray trim(byte ch)
Trims the data with the given byte- Parameters:
ch- the trim byte- Returns:
- the byte array (no copy)
-
trimRight
public ByteArray trimRight(byte ch)
Trims the data right with the given byte- Parameters:
ch- the trim byte- Returns:
- the byte array (no copy)
-
trimLeft
public ByteArray trimLeft(byte ch)
Trims the data left with the given byte- Parameters:
ch- the trim byte- Returns:
- the byte array (no copy)
-
replace
public ByteArray replace(byte toReplace, byte newByte)
Replaces a given byte with another- Parameters:
toReplace- the byte to replacenewByte- the new byte- Returns:
- the byte array (no copy)
-
replace
public ByteArray replace(ByteArray replaceData, ByteArray newData)
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
public int indexOf(ByteArray toFind)
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
public int indexOf(ByteArray toFind, int fromIndex)
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
public boolean startsWith(ByteArray prefix)
Test if the array starts with the given prefix- Parameters:
prefix- the bytes to test- Returns:
- true if it starts with the prefix
-
startsWith
public boolean startsWith(ByteArray prefix, int toffset)
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
public int countStartsWith(ByteArray prefix)
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
public boolean endsWith(ByteArray prefix)
Test if the array starts with the given prefix- Parameters:
prefix- the bytes to test- Returns:
- true if it starts with the prefix
-
countEndsWith
public int countEndsWith(ByteArray prefix)
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
public java.lang.String toHex()
Convert to hex string- Returns:
- the byte array as hex string
-
toString
public java.lang.String toString()
Convert to String- Overrides:
toStringin classjava.lang.Object- Returns:
- the byte array as 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
public java.nio.ByteBuffer toByteBuffer()
Return a byte buffer of the data- Returns:
- the buffer
-
toByteArray
public ByteArray toByteArray(int startPos, int endPos)
Return a sub part of the original bytes- Parameters:
startPos- the start positionendPos- the end position- Returns:
- the bytes (copy)
-
getBytes
public ByteArray getBytes()
Return the bytes as a copy- Returns:
- the bytes (copy)
-
getBytes
public ByteArray getBytes(int startPos, int endPos)
Return the bytes as a copy- Parameters:
startPos- the start positionendPos- the end position- Returns:
- the bytes (copy)
-
concat
public ByteArray concat(byte b)
Concat a given byte and return a new byte array- Parameters:
b- the byte to append- Returns:
- the concated byte array (copy)
-
concat
public ByteArray concat(ByteArray b)
Concat a given byte and return a new byte array- Parameters:
b- the byte to append- Returns:
- the concated byte array (copy)
-
get
public byte get(int pos) throws java.lang.ArrayIndexOutOfBoundsExceptionReturn a specific byte from the byte array- Parameters:
pos- the position in the byte array- Returns:
- a byte
- Throws:
java.lang.ArrayIndexOutOfBoundsException- in case of error
-
set
public byte set(int pos, byte b) throws java.lang.ArrayIndexOutOfBoundsExceptionSet 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:
java.lang.ArrayIndexOutOfBoundsException- in case of error
-
length
public int length()
Return the number of bytes- Returns:
- the number of bytes
-
erase
public ByteArray erase()
Erase the byte array- Returns:
- the byte array (no copy)
-
chopRight
public ByteArray chopRight()
Chop a byte- Returns:
- the byte array (no copy)
-
chopRight
public ByteArray chopRight(int num)
Chop some bytes- Parameters:
num- number of bytes to chop- Returns:
- the byte array (no copy)
-
chopLeft
public ByteArray chopLeft()
Chop a byte- Returns:
- the byte array (no copy)
-
chopLeft
public ByteArray chopLeft(int num)
Chop some bytes- Parameters:
num- number of bytes to chop- Returns:
- the byte array (no copy)
-
write
public int write(java.nio.ByteBuffer src)
- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- See Also:
WritableByteChannel.write(java.nio.ByteBuffer)
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable- See Also:
Channel.close()
-
isOpen
public boolean isOpen()
- Specified by:
isOpenin interfacejava.nio.channels.Channel- See Also:
Channel.isOpen()
-
read
public int read(java.nio.ByteBuffer dst)
- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- See Also:
ReadableByteChannel.read(java.nio.ByteBuffer)
-
equals
public boolean equals(java.lang.Object other)
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; }- Overrides:
equalsin classjava.lang.Object- Parameters:
other- the object to compare- Returns:
- true if the objects are equals
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object- See Also:
Object.hashCode()
-
clone
public ByteArray clone()
- Overrides:
clonein classjava.lang.Object- See Also:
Object.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
-
-