Package com.github.toolarium.common.util
Class ByteUtil
- java.lang.Object
-
- com.github.toolarium.common.util.ByteUtil
-
public final class ByteUtil extends java.lang.ObjectByte utitlity.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcopyBlock(byte[] src, byte[] dst)Copies one byte block to another.voidcopyBlock(byte[] src, int srcOff, byte[] dst, int dstOff, int len)Copies the specified byte sequence of the given source array to the specified destination array.voidcopyBlock(int[] src, int[] dst)Copies one int block to another.voidcopyBlock(int[] src, int srcOff, int[] dst, int dstOff, int len)Copies the specified int sequence of the given source array to the specified destination array.booleanequalsBlock(byte[] a, byte[] b)Check two blocks for equality.booleanequalsBlock(byte[] dataA, int offsetDataA, byte[] dataB, int offsetDataB, int len)Check two blocks for equality.static ByteUtilgetInstance()Get the instanceintindexOf(byte[] source, int sourceOffset, int sourceCount, byte[] target, int targetOffset, int targetCount, int index)The source is the byte array being searched, and the target is the string being searched for.inttoByte(char c, int radix)Convert the given char to an int.byte[]toByteArray(java.lang.String s)Convert the given string with hex values to a byte array.byte[]toBytes(byte[] src, int len)Prepare a byte array to a byte array of a defined sizebyte[]toBytes(int inputSrc)Convert an integer value to a 4 byte arraybyte[]toBytes(long src)Convert from a long value to a 8 byte arrayinttoInteger(byte[] src)Build an integer from first 4 bytes of the array.inttoInteger(ByteArray src)Build an integer from first 4 bytes of the array.longtoLong(byte[] src)Build a long from first 8 bytes of the array.longtoLong(ByteArray src)Build a long from first 8 bytes of the array.
-
-
-
Method Detail
-
getInstance
public static ByteUtil getInstance()
Get the instance- Returns:
- the instance
-
equalsBlock
public boolean equalsBlock(byte[] dataA, int offsetDataA, byte[] dataB, int offsetDataB, int len)Check two blocks for equality. The specified sub blocks of the given byte arrays are checked for equality.- Parameters:
dataA- the first byte array to be comparedoffsetDataA- the offset indicating the start position of the sub-block ofadataB- the second byte array to be comparedoffsetDataB- the offset indicating the start position of the sub-block ofblen- the number of bytes to be compared- Returns:
true, if the two sub-blocks are equal,falseotherwise
-
equalsBlock
public boolean equalsBlock(byte[] a, byte[] b)Check two blocks for equality.- Parameters:
a- the first byte array to be comparedb- the second byte array to be compared- Returns:
true, if the two blocks are equal,falseotherwise
-
toByteArray
public byte[] toByteArray(java.lang.String s)
Convert the given string with hex values to a byte array. For example "001122" is turned into {0, 0x11, 0x22}. All characters outside the range of '0'-'9', 'a'-'z', and 'A'-'Z' or simply ignored.- Parameters:
s- the string to convert- Returns:
- the byte array
-
toByte
public int toByte(char c, int radix)Convert the given char to an int. '0' maps to 0, 'a' and 'A' to 10, 'z' and 'Z' to 36, and so on. For all other characters and for resulting values equal to or larger than radix -1 is returned.- Parameters:
c- the string to convertradix- the radix- Returns:
- the converted byte
-
copyBlock
public void copyBlock(byte[] src, int srcOff, byte[] dst, int dstOff, int len)Copies the specified byte sequence of the given source array to the specified destination array. Beginning at the specifiedsrcOffposition,lenbytes of the source array are copied to the given destination array, starting atdstOff.- Parameters:
src- the source byte arraysrcOff- the offset indicating the start position within the first byte array; the followinglenbytes will be copied to the destination arraydst- the destination array to which to copy the bytesdstOff- the offset indicating the start position within the destination byte array, to which the bytes shall be copiedlen- the number of bytes to be copied
-
copyBlock
public void copyBlock(byte[] src, byte[] dst)Copies one byte block to another.- Parameters:
src- the source byte arraydst- the destination array to which to copy the bytes
-
copyBlock
public void copyBlock(int[] src, int srcOff, int[] dst, int dstOff, int len)Copies the specified int sequence of the given source array to the specified destination array. Beginning at the specifiedsrcOffposition,lenints of the source array are copied to the given destination array, starting atdstOff.- Parameters:
src- the source int arraysrcOff- the offset indicating the start position within the first int array; the followinglenints will be copied to the destination arraydst- the destination array to which to copy the intsdstOff- the offset indicating the start position within the destination int array, to which the ints shall be copiedlen- the number of ints to be copied
-
copyBlock
public void copyBlock(int[] src, int[] dst)Copies one int block to another.- Parameters:
src- the source int arraydst- the destination array to which to copy the ints
-
toBytes
public byte[] toBytes(int inputSrc)
Convert an integer value to a 4 byte array- Parameters:
inputSrc- the array to convert (must be of size 4)- Returns:
- the converted byte array
-
toBytes
public byte[] toBytes(long src)
Convert from a long value to a 8 byte array- Parameters:
src- the array to convert (must be of size 8)- Returns:
- the converted byte array
-
toBytes
public byte[] toBytes(byte[] src, int len)Prepare a byte array to a byte array of a defined size- Parameters:
src- the array to preparelen- the size of the returned byte array- Returns:
- the prepared byte array with the defined size
-
toInteger
public int toInteger(ByteArray src)
Build an integer from first 4 bytes of the array.- Parameters:
src- The byte[] to convert.- Returns:
- the integer value.
-
toInteger
public int toInteger(byte[] src)
Build an integer from first 4 bytes of the array.- Parameters:
src- The byte[] to convert.- Returns:
- the integer value.
-
toLong
public long toLong(byte[] src)
Build a long from first 8 bytes of the array.- Parameters:
src- The byte[] to convert.- Returns:
- the long value.
-
toLong
public long toLong(ByteArray src)
Build a long from first 8 bytes of the array.- Parameters:
src- The byte[] to convert.- Returns:
- the long value.
-
indexOf
public int indexOf(byte[] source, int sourceOffset, int sourceCount, byte[] target, int targetOffset, int targetCount, int index)The source is the byte array being searched, and the target is the string being searched for.- Parameters:
source- the bytes being searched.sourceOffset- offset of the source array.sourceCount- count of the source array.target- the bytes being searched for.targetOffset- offset of the target array.targetCount- count of the target array.index- the index to begin searching from.- Returns:
- the index within this array of the first occurrence of the specified sub array, starting at the specified index.
-
-