public final class ByteUtils extends Object
Modifier and Type | Method and Description |
---|---|
static void |
and(byte[] b,
byte[] other)
This method performs a logical AND of the bits in byte array
b with the other array. |
static void |
and(byte[] b,
byte[] other,
int offset)
This method performs a logical AND of the bits in byte array
b with the other array, from the specified offset to
the end of byte array b . |
static void |
and(byte[] b,
byte[] other,
int offset,
int length)
This method performs a logical AND of the bits in byte array
b with the other array, for the specified region of
byte array b . |
static String |
byteToHex(byte b)
Returns a two character string representation of the given byte,
in hex.
|
static int |
byteToInt(byte b)
Converts a byte (-128 ..
|
static int |
countBits(byte[] b)
Counts and returns the number of 1 bits within the specified byte array.
|
static int |
countBits(byte[] b,
int offset)
Counts and returns the number of 1 bits within the specified region of
a byte array, starting at the given offset, to the end of the array.
|
static int |
countBits(byte[] b,
int offset,
int length)
Counts and returns the number of 1 bits within the specified region
of a byte array, starting at the given offset, for 'length' bytes.
|
static String |
getByteArrayString(byte[] b)
Returns a string representation of the given byte array.
|
static String |
getByteArrayString(byte[] b,
int len)
Returns a string representation of the given byte array.
|
static String |
getDebugString(String msg,
byte[] b)
Helper method to create log string of a byte array.
|
static String |
getDebugString(String msg,
byte[] b,
int len)
Creates a string of a byte array suitable for logging.
|
static String |
getFilteredString(byte[] b)
Returns a 'filtered' string representation of the given byte array:
|
static int |
getInteger(byte[] b,
int offset)
Extracts the integer value from four bytes of a byte array
starting at the given offset.
|
static long |
getLong(byte[] b,
int offset)
Extracts the long value from eight bytes of a byte array
starting at the given offset.
|
static String |
getNullTerminatedAscii(byte[] bytes)
Interprets the given array of bytes as a null-terminated
ASCII string, and returns that string.
|
static byte[] |
getPadded64BitBytes(byte[] b)
Examines the given byte array and returns an array that is padded out
to a 64 bit boundary.
|
static byte[] |
getRawBytes(String s)
Helper method to convert the string to an array of bytes
(without local replacement).
|
static short |
getShort(byte[] b,
int offset)
Extracts and returns the short value from two bytes of a byte array
starting at the given offset.
|
static String |
getStringFromRawBytes(byte[] bytes)
Interprets the given array of bytes as a UTF-8 encoded string,
and returns that string.
|
static int |
getU16(byte[] b,
int offset)
Reads two bytes from the array (at the given offset),
interpreting them as an unsigned 16-bit value.
|
static long |
getU32(byte[] b,
int offset)
Reads four bytes from the array (at the given offset),
interpreting them as an unsigned 32-bit value.
|
static BigInteger |
getU64(byte[] b,
int offset)
Reads eight bytes from the array (at the given offset),
interpreting them as an unsigned 64-bit value.
|
static short |
getU8(byte[] b,
int offset)
Reads a single byte from the array (at the given offset),
interpreting it as an unsigned 8-bit value.
|
static String |
hex(byte[] b)
Generates a hex dump string from the given bytes.
|
static String |
hex(byte[] b,
int offset)
Generates a hex dump string from the specified offset to the end of
the given byte array.
|
static String |
hex(byte[] b,
int offset,
int length)
Generates a hex dump string from the specified region of the
given byte array.
|
static String |
hexLookupLower(byte b)
Provides a fast lookup of the 2-digit hex string representation for
the given byte, using lowercase alpha digits.
|
static String |
hexLookupUpper(byte b)
Provides a fast lookup of the 2-digit hex string representation for
the given byte, using uppercase alpha digits.
|
static String |
hexWithPrefix(byte[] bytes)
Returns a string representation of a byte array with "0x" prefix.
|
static int |
indexOf(byte[] b,
byte match)
Scans through the specified byte array
returning the index of the first byte that matches the given value,
or -1 if no match is found.
|
static int |
indexOf(byte[] b,
byte match,
int startIndex)
Scans through the specified byte array starting at the given index,
returning the index of the first byte that matches the given value,
or -1 if no match is found.
|
static int |
indexOf(byte[] b,
byte match,
int startIndex,
int endIndex)
Scans through the specified byte array starting at the given index,
and ending before the given end index, returning the index of the
first byte that matches the given value, or -1 if no match is found.
|
static byte |
intToByte(int i)
Converts an int (0 ..
|
static void |
not(byte[] b)
This method reverses all the bits (logical NOT) in the given array.
|
static void |
not(byte[] b,
int offset)
This method reverses all the bits (logical NOT) in the given array,
from offset to the end of the array.
|
static void |
not(byte[] b,
int offset,
int length)
This method reverses all the bits (logical NOT) in the given array
for the specified region.
|
static void |
or(byte[] b,
byte[] other)
This method performs a logical OR of the bits in byte array
b
with the other array. |
static void |
or(byte[] b,
byte[] other,
int offset)
This method performs a logical OR of the bits in byte array
b
with the other array, from the specified offset to the end of
byte array b . |
static void |
or(byte[] b,
byte[] other,
int offset,
int length)
This method performs a logical OR of the bits in byte array
b
with the other array, for the specified region of byte array
b . |
static byte[] |
parseHex(String asciiHex)
Generates a byte array by parsing the given ascii string of hex
characters.
|
static byte[] |
parseHexWithPrefix(String s)
Helper method to convert the hex string with "0x" prefix
to an array of bytes.
|
static void |
setInteger(byte[] b,
int offset,
int value)
Populates four bytes of a byte array with the given integer value.
|
static void |
setInteger(byte[] b,
int offset,
long value)
Populates four bytes of a byte array with the lower four
bytes of the given long value.
|
static void |
setLong(byte[] b,
int offset,
long value)
Populates eight bytes of a byte array with the given long value.
|
static void |
setShort(byte[] b,
int offset,
int value)
Populates two bytes of a byte array with the lower two bytes
of the given integer value.
|
static void |
setShort(byte[] b,
int offset,
short value)
Populates two bytes of a byte array with the given short value.
|
static void |
setU16(byte[] b,
int offset,
int value)
Populates two bytes of a byte array (at the given offset),
with the supplied equivalent unsigned 16-bit value.
|
static void |
setU32(byte[] b,
int offset,
long value)
Populates four bytes of a byte array (at the given offset),
with the supplied equivalent unsigned 32-bit value.
|
static void |
setU64(byte[] b,
int offset,
BigInteger value)
Populates eight bytes of a byte array (at the given offset),
with the supplied equivalent unsigned 64-bit value.
|
static void |
setU8(byte[] b,
int offset,
short value)
Populates a single byte of a byte array (at the given offset),
with the supplied equivalent unsigned 8-bit value.
|
static byte[] |
slurpBytesFromHexFile(Class<?> c,
String baseName)
Reads in a human readable file of hex data and returns it
as a byte array.
|
static byte[] |
slurpBytesFromHexFile(String path,
ClassLoader cl)
Reads in a human readable file of hex data and returns it
as a byte array.
|
static String |
toHexArrayString(byte[] bytes)
Returns a string representation of a byte array.
|
static String |
toHexString(byte[] bytes)
Returns a string representation of the byte array as a hex dump,
with spaces inserted to help reading the string.
|
static void |
xor(byte[] b,
byte[] other)
This method performs a logical EXCLUSIVE-OR of the bits in byte array
b with the other array. |
static void |
xor(byte[] b,
byte[] other,
int offset)
This method performs a logical EXCLUSIVE-OR of the bits in byte array
b with the other array, from the specified offset to
the end of byte array b . |
static void |
xor(byte[] b,
byte[] other,
int offset,
int length)
This method performs a logical EXCLUSIVE-OR of the bits in byte array
b with the other array, for the specified region of
byte array b . |
public static int byteToInt(byte b)
b
- the bytepublic static byte intToByte(int i)
i
- the intpublic static short getShort(byte[] b, int offset)
b
- the byte array from which to extract the short valueoffset
- the start offset into the byte arraypublic static void setShort(byte[] b, int offset, short value)
b
- the byte array into which to put the short valueoffset
- the start offset into the byte arrayvalue
- the value to be represented by the two bytes at the
given offsetpublic static void setShort(byte[] b, int offset, int value)
b
- the byte array into which to put the lower half of the
integer valueoffset
- the start offset into the byte arrayvalue
- the value to be represented by the two bytes at the
given offsetpublic static int getInteger(byte[] b, int offset)
b
- the byte array from which to extract the integer valueoffset
- the start offset into the byte arraypublic static void setInteger(byte[] b, int offset, int value)
b
- the byte array into which to put the integer valueoffset
- the start offset into the byte arrayvalue
- the integer value to be represented by the four bytes at
the given offsetpublic static void setInteger(byte[] b, int offset, long value)
b
- the byte array into which to put the lower half of the
long valueoffset
- the start offset into the byte arrayvalue
- the value to be represented by the four bytes at the
given offsetpublic static long getLong(byte[] b, int offset)
b
- the byte array from which to extract the long valueoffset
- the start offset into the byte arraypublic static void setLong(byte[] b, int offset, long value)
b
- the byte array into which to put the long valueoffset
- the start offset into the byte arrayvalue
- the long value to be represented by the eight bytes at
the given offsetpublic static short getU8(byte[] b, int offset)
b
- the byte array from which to read the byteoffset
- the start offset into the byte arraypublic static void setU8(byte[] b, int offset, short value)
b
- the byte array into which to write the u8 valueoffset
- the start offset into the byte arrayvalue
- the unsigned value to be converted and insertedIllegalArgumentException
- if value is not 0..2^8-1public static int getU16(byte[] b, int offset)
b
- the byte array from which to readoffset
- the start offset into the byte arraypublic static void setU16(byte[] b, int offset, int value)
b
- the byte array into which to write the u16 valueoffset
- the start offset into the byte arrayvalue
- the unsigned value to be converted and insertedIllegalArgumentException
- if uv is not 0..2^16-1public static long getU32(byte[] b, int offset)
b
- the byte array from which to readoffset
- the start offset into the byte arraypublic static void setU32(byte[] b, int offset, long value)
b
- the byte array into which to write the u32 valueoffset
- the start offset into the byte arrayvalue
- the unsigned value to be converted and insertedIllegalArgumentException
- if uv is not 0..2^32-1public static BigInteger getU64(byte[] b, int offset)
b
- the byte array from which to readoffset
- the start offset into the byte arraypublic static void setU64(byte[] b, int offset, BigInteger value)
b
- the byte array into which to write the u64 valueoffset
- the start offset into the byte arrayvalue
- the unsigned value to be converted and insertedIllegalArgumentException
- if uv is not 0..2^64-1public static int indexOf(byte[] b, byte match)
b
- the byte array to searchmatch
- the byte value to matchpublic static int indexOf(byte[] b, byte match, int startIndex)
b
- the byte array to searchmatch
- the byte value to matchstartIndex
- the index into the array from which to start the
search (inclusive)ArrayIndexOutOfBoundsException
- if startIndex is not within
array boundspublic static int indexOf(byte[] b, byte match, int startIndex, int endIndex)
b
- the byte array to searchmatch
- the byte value to matchstartIndex
- the index into the array from which to start the
search (inclusive)endIndex
- the index which designates the end of the searchable
area (exclusive)ArrayIndexOutOfBoundsException
- if startIndex/endIndex
attempts access outside the array boundspublic static int countBits(byte[] b)
b
- the byte array from which to count the 1 bitspublic static int countBits(byte[] b, int offset)
b
- the byte array from which to count the 1 bitsoffset
- the start offset into the byte arrayArrayIndexOutOfBoundsException
- if offset is not within
array boundspublic static int countBits(byte[] b, int offset, int length)
b
- the byte array from which to count the 1 bitsoffset
- the start offset into the byte arraylength
- the number of bytes to include in the countArrayIndexOutOfBoundsException
- if offset/length attempts
access outside the array boundspublic static void not(byte[] b)
b
- the byte arraypublic static void not(byte[] b, int offset)
b
- the byte arrayoffset
- the start offset into the byte arrayArrayIndexOutOfBoundsException
- if offset is not within
array boundspublic static void not(byte[] b, int offset, int length)
b
- the byte arrayoffset
- the start offset into the byte arraylength
- the number of bytes to include in the operationArrayIndexOutOfBoundsException
- if offset/length attempts
access outside the array boundspublic static void and(byte[] b, byte[] other)
b
with the other
array. The result is left in byte
array b
. Note that if the other
array is shorter than
byte array b
, the "missing" bytes are assumed to be 0xff
resulting in no change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to ANDpublic static void and(byte[] b, byte[] other, int offset)
b
with the other
array, from the specified offset to
the end of byte array b
. The result is left in byte array
b
. Note that if the other
array is shorter than the
region specified, the "missing" bytes are assumed to be 0xff
resulting in no change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to ANDoffset
- the start offset into the byte arrayArrayIndexOutOfBoundsException
- if offset is not within
array boundspublic static void and(byte[] b, byte[] other, int offset, int length)
b
with the other
array, for the specified region of
byte array b
. The result is left in byte array b
. Note
that if the other
array is shorter than the region specified,
the "missing" bytes are assumed to be 0xff
resulting in no
change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to ANDoffset
- the start offset into the byte arraylength
- the number of bytes to include in the operationArrayIndexOutOfBoundsException
- if offset/length attempts access
outside the array boundspublic static void or(byte[] b, byte[] other)
b
with the other
array. The result is left in byte array b
.
Note that if the other
array is shorter than byte array
b
, the "missing" bytes are assumed to be 0x00
resulting
in no change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to ORpublic static void or(byte[] b, byte[] other, int offset)
b
with the other
array, from the specified offset to the end of
byte array b
. The result is left in byte array b
. Note
that if the other
array is shorter than the region specified,
the "missing" bytes are assumed to be 0x00
resulting in no
change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to ORoffset
- the start offset into the byte arrayArrayIndexOutOfBoundsException
- if offset is not within
array boundspublic static void or(byte[] b, byte[] other, int offset, int length)
b
with the other
array, for the specified region of byte array
b
. The result is left in byte array b
. Note that if the
other
array is shorter than the region specified, the "missing"
bytes are assumed to be 0x00
resulting in no change to the
original array for those byte positions.b
- the byte arrayother
- the operand with which to ORoffset
- the start offset into the byte arraylength
- the number of bytes to include in the operationArrayIndexOutOfBoundsException
- if offset/length attempts access
outside the array boundspublic static void xor(byte[] b, byte[] other)
b
with the other
array. The result is left in byte array
b
. Note that if the other
array is shorter than byte
array b
, the "missing" bytes are assumed to be 0x00
resulting in no change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to EXCLUSIVE-ORpublic static void xor(byte[] b, byte[] other, int offset)
b
with the other
array, from the specified offset to
the end of byte array b
. The result is left in byte array
b
. Note that if the other
array is shorter than the
region specified, the "missing" bytes are assumed to be 0x00
resulting in no change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to EXCLUSIVE-ORoffset
- the start offset into the byte arrayArrayIndexOutOfBoundsException
- if offset is not within
array boundspublic static void xor(byte[] b, byte[] other, int offset, int length)
b
with the other
array, for the specified region of
byte array b
. The result is left in byte array b
. Note
that if the other
array is shorter than the region specified,
the "missing" bytes are assumed to be 0x00
resulting in no
change to the original array for those byte positions.b
- the byte arrayother
- the operand with which to EXCLUSIVE-ORoffset
- the start offset into the byte arraylength
- the number of bytes to include in the operationArrayIndexOutOfBoundsException
- if offset/length attempts
access outside the array boundspublic static String hex(byte[] b)
b
- the byte arraypublic static String hex(byte[] b, int offset)
b
- the byte arrayoffset
- the start offset into the byte arraypublic static String hex(byte[] b, int offset, int length)
b
- the byte arrayoffset
- the start offset into the byte arraylength
- the number of bytes to outputpublic static String hexLookupLower(byte b)
b
- the bytepublic static String hexLookupUpper(byte b)
b
- the bytepublic static String byteToHex(byte b)
b
- the bytepublic static String toHexString(byte[] bytes)
bytes
- the byte arraypublic static String toHexArrayString(byte[] bytes)
"0x[ 00 2e ff b7 ]"
bytes
- the byte arraypublic static String hexWithPrefix(byte[] bytes)
"0x002effb7"
bytes
- the byte arraypublic static byte[] parseHexWithPrefix(String s)
s
- string with "0x" prefixpublic static byte[] parseHex(String asciiHex)
hex(byte[])
method, since it will happily deal with whitespace etc. between
hex digits.
This method ignores any non-word characters (including new-lines).
asciiHex
- a string of ascii hex digits; must be an even lengthIllegalArgumentException
- if the string cannot be parsed as hexpublic static byte[] getPadded64BitBytes(byte[] b)
b
- the byte arraypublic static byte[] getRawBytes(String s)
s
- string to convertpublic static String getStringFromRawBytes(byte[] bytes)
bytes
- array of the string characterspublic static String getNullTerminatedAscii(byte[] bytes)
bytes
- the null-terminated ASCII string encoded as bytespublic static String getDebugString(String msg, byte[] b, int len)
msg
- a header message to displayb
- an array of byteslen
- number of bytes in arraypublic static String getDebugString(String msg, byte[] b)
msg
- header message to displayb
- an array of bytespublic static String getByteArrayString(byte[] b)
b
- an array of bytespublic static String getByteArrayString(byte[] b, int len)
b
- an array of byteslen
- the number of bytes in arraypublic static String getFilteredString(byte[] b)
"[<chars>] [<bytes>]"where all control chars in <chars> are replaced by '.' and <bytes> is of the form "00 ab ..".
b
- byte arraypublic static byte[] slurpBytesFromHexFile(String path, ClassLoader cl) throws IOException
For example, given the file contents:
# comment ff ab 00 # another comment 11 22 33invoking the method will return the following byte array:
[ 0xff, 0xab, 0x00, 0x11, 0x22, 0x33 ]
path
- the path of the file resourcecl
- the classloader to use to locate the file resourceIOException
- if there is a problem reading the fileslurpBytesFromHexFile(Class, String)
public static byte[] slurpBytesFromHexFile(Class<?> c, String baseName) throws IOException
This form locates the .hex file by looking in the same package as the given class, and using the given base name. Note that both the class and the .hex file need to be in the same jar file.
For example, supposing the jar file contains:
com/hp/sdn/app/example/SomeObject.class com/hp/sdn/app/example/MyPacketData.hexThen, to correctly load the hex file data, call:
byte[] pktData = ByteUtils.slurpBytesFromHexFile(SomeObject.class, "MyPacketData");
c
- the class used to locate the hex filebaseName
- the hex file base nameIOException
- if there is a problem reading the fileslurpBytesFromHexFile(String, ClassLoader)
Copyright © 2015. All Rights Reserved.