public class ByteUtils
extends java.lang.Object
Constructor and Description |
---|
ByteUtils() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
asAscii(byte[] data)
Gets an ASCII representation of an array of bytes.
|
static java.lang.String |
asAscii(byte[] buf,
int off,
int len)
Gets an ASCII representation of an array of bytes.
|
static java.lang.String |
asBinary(byte[] data)
Gets a binary representation of an array of bytes.
|
static java.lang.String |
asBinary(byte[] buf,
int off,
int len)
Gets a binary representation of an array of bytes.
|
static java.lang.String |
asHex(byte[] data)
Gets a hexadecimal representation of an array of bytes.
|
static java.lang.String |
asHex(byte[] buf,
int off,
int len)
Gets a hexadecimal representation of an array of bytes.
|
static java.lang.String |
bytesToAsciiString(byte[] data)
Gets an ASCII representation of an array of bytes.
|
static java.lang.String |
bytesToAsciiString(byte[] buf,
int off,
int len)
Gets an ASCII representation of an array of bytes.
|
static java.lang.String |
bytesToBinString(byte[] data)
Converts a byte array into a binary string.
|
static java.lang.String |
bytesToBinString(byte[] buf,
int off,
int len)
Converts a byte array into a binary string.
|
static java.lang.String |
bytesToFormattedHexString(byte[] data,
char c,
int num)
Gets a formatted (e.g. colon-separated) hexadecimal representation of an array of bytes.
|
static java.lang.String |
bytesToFormattedHexString(byte[] buf,
int off,
int len,
char c,
int num)
Gets a formatted (e.g. colon-separated) hexadecimal representation of an array of bytes.
|
static java.lang.String |
bytesToHexString(byte[] data)
Gets a hexadecimal representation of an array of bytes.
|
static java.lang.String |
bytesToHexString(byte[] buf,
int off,
int len)
Gets a hexadecimal representation of an array of bytes.
|
static int |
compare(byte[] a1,
byte[] a2)
Compares two arrays of bytes.
|
static int |
compare(byte[] buf1,
int off1,
int len1,
byte[] buf2,
int off2,
int len2)
Compares two arrays of bytes.
|
static byte[] |
concat(byte[] src1,
byte[] src2)
Concatenates two arrays of bytes.
|
static int |
concat(byte[] src1,
byte[] src2,
byte[] dst3)
Concatenates two arrays of bytes.
|
static byte[] |
concat(byte[] src1,
int off1,
int len1,
byte[] src2,
int off2,
int len2)
Concatenates two arrays of bytes.
|
static int |
concat(byte[] src1,
int off1,
int len1,
byte[] src2,
int off2,
int len2,
byte[] dst3,
int off3)
Concatenates two arrays of bytes.
|
static byte[] |
copy(byte[] src)
Gets a new array containing a copy of an array of bytes.
|
static void |
copy(byte[] src,
byte[] dst)
Copies an array of bytes into another array.
|
static void |
copy(byte[] src,
byte[] dst,
int dst_off)
Copies an array of bytes into another array.
|
static void |
copy(byte[] src,
int src_off,
byte[] dst,
int dst_off,
int len)
Copies an array of bytes into another array.
|
static byte[] |
copy(byte[] src,
int off,
int len)
Gets a new array containing a copy of an array of bytes.
|
static long |
eightBytesToInt(byte[] src)
Transforms a 8-byte array into a 64-bit integer; the 8-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static long |
eightBytesToInt(byte[] src,
int off)
Transforms a 8-byte array into a 64-bit integer; the 8-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static void |
fill(byte[] a,
byte val)
Assigns the specified byte value to each element of the array of bytes.
|
static void |
fill(byte[] buf,
int off,
int len,
byte val)
Assigns the specified byte value to each element of an array of bytes.
|
static byte[] |
formattedHexStringToBytes(java.lang.String str,
char c)
Converts a formatted hexadecimal string into an array of bytes.
|
static int |
formattedHexStringToBytes(java.lang.String str,
char c,
byte[] buf,
int off)
Converts a formatted hexadecimal string into an array of bytes.
|
static byte[] |
formattedHexStringToBytes(java.lang.String str,
char c,
int len)
Converts a formatted hexadecimal string into an array of bytes.
|
static long |
fourBytesToInt(byte[] src)
Transforms a 4-byte array into a 32-bit integer; the 4-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static long |
fourBytesToInt(byte[] src,
int off)
Transforms a 4-byte array into a 32-bit integer; the 4-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static long |
fourBytesToIntLittleEndian(byte[] src)
Transforms a four-byte array into a 32-bit integer; the four-byte array is in little-endian byte order (the little end, least significant byte, is stored first, at the lowest storage address).
|
static long |
fourBytesToIntLittleEndian(byte[] src,
int off)
Transforms a four-byte array into a 32-bit integer; the four-byte array is in little-endian byte order (the little end, least significant byte, is stored first, at the lowest storage address).
|
static long |
fourBytesToLong(byte b3,
byte b2,
byte b1,
byte b0)
Gets a 32-bit integer (as long) from four bytes.
|
static byte[] |
hexStringToBytes(java.lang.String str)
Converts a hexadecimal string into an array of bytes.
|
static int |
hexStringToBytes(java.lang.String str,
byte[] buf,
int off)
Converts a hexadecimal string into an array of bytes.
|
static byte[] |
hexToBytes(java.lang.String str)
Converts a hexadecimal string into an array of bytes.
|
static int |
hexToBytes(java.lang.String str,
byte[] buf,
int off)
Converts a hexadecimal string into an array of bytes.
|
static byte[] |
hexToBytes(java.lang.String str,
int len)
Converts a hexadecimal string into an array of bytes.
|
static void |
inc(byte[] buf)
Adds 1 to the number represented by the given array of bytes, module 2^n, where n is the length (in bits) of the array.
|
static int |
indexOf(byte[] a1,
byte[] buf2,
int off2,
int len2)
Finds the first occurrence of one array of bytes within a second array of bytes.
|
static int |
indexOf(byte[] buf1,
int off1,
int len1,
byte[] buf2,
int off2,
int len2)
Finds the first occurrence of one array of bytes within a second array of bytes.
|
static byte[] |
intToEightBytes(long val)
Transforms a 64-bit signed integer (as long) into an eight-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static void |
intToEightBytes(long val,
byte[] dst,
int off)
Transforms a 64-bit signed integer (as long) into an eight-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static byte[] |
intToFourBytes(long val)
Transforms a 32-bit integer into a four-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static void |
intToFourBytes(long val,
byte[] dst,
int off)
Transforms a 32-bit integer into a four-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static byte[] |
intToFourBytesLittleEndian(long n)
Transforms a 32-bit integer into a four-byte array in little-endian byte order (the little end, least significant byte, is stored first, at the lowest storage address).
|
static void |
intToFourBytesLittleEndian(long n,
byte[] dst,
int off)
Transforms a 32-bit integer into a four-byte array copied into a given buffer; the 4-bytes array is in little-endian byte order (the little end, least significant byte, is stored first, at the lowest storage address).
|
static void |
intToNBytes(long val,
byte[] dst,
int off,
int len)
Transforms an integer into an n-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static byte[] |
intToNBytes(long val,
int len)
Transforms an integer into an n-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static byte[] |
intToSixBytes(long val)
Transforms a 48-bit integer into a six-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static void |
intToSixBytes(long val,
byte[] dst,
int off)
Transforms a 48-bit integer into a six-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static byte[] |
intToTwoBytes(int val)
Transforms a 16-bit integer into a two-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static void |
intToTwoBytes(int val,
byte[] dst,
int off)
Writes a 16-bit integer into a two-byte array in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static void |
invertByteOrder(byte[] buf)
Modifies the given array inverting the byte order.
|
static boolean |
match(byte[] a1,
byte[] a2)
Compares two arrays of bytes.
|
static boolean |
match(byte[] buf1,
int off1,
int len1,
byte[] buf2,
int off2,
int len2)
Compares two arrays of bytes.
|
static long |
nBytesToInt(byte[] src)
Transforms a n-byte array into an integer; the n-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static long |
nBytesToInt(byte[] src,
int off,
int len)
Transforms a n-byte array into an integer; the n-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static java.lang.String |
trimHexString(java.lang.String str)
Gets an unformatted hexadecimal string.
|
static int |
twoBytesToInt(byte[] src)
Transforms a two-byte array into a 16-bit integer; the 2-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static int |
twoBytesToInt(byte[] src,
int off)
Reads a 16-bit integer from a two-byte array; the two-byte array is in big-endian byte order (the big end, most significant byte, is stored first, at the lowest storage address).
|
static int |
twoBytesToInt(byte b_high,
byte b_low)
Gets a 16-bit integer from two bytes.
|
static short |
uByte(byte b)
Gets the unsigned representation of a byte, returned as a short.
|
static long |
uInt(int n)
Gets the unsigned representation of a 32-bit integer, returned as a long.
|
public static void copy(byte[] src, byte[] dst)
src
- the source array to be copieddst
- the destination arraypublic static void copy(byte[] src, byte[] dst, int dst_off)
src
- the source array to be copieddst
- the destination buffer where the array has to be copieddst_off
- the offset within the bufferpublic static void copy(byte[] src, int src_off, byte[] dst, int dst_off, int len)
src
- the buffer containing the source array to be copiedsrc_off
- the offset within the bufferdst
- the destination buffer where the array has to be copieddst_off
- the offset within the bufferlen
- the number of bytes to be copiedpublic static byte[] copy(byte[] src)
src
- the array to be copiedpublic static byte[] copy(byte[] src, int off, int len)
src
- the buffer containing the array to be copiedoff
- the offset within the bufferlen
- the length of the arraypublic static boolean match(byte[] a1, byte[] a2)
a1
- one arraya2
- the other arraypublic static boolean match(byte[] buf1, int off1, int len1, byte[] buf2, int off2, int len2)
buf1
- the buffer containing the first arrayoff1
- the offset within the bufferlen1
- the length of the first arraybuf2
- the buffer containing the second arrayoff2
- the offset within the bufferlen2
- the length of the second arraypublic static int compare(byte[] a1, byte[] a2)
a1
- one arraya2
- the other arraypublic static int compare(byte[] buf1, int off1, int len1, byte[] buf2, int off2, int len2)
buf1
- the buffer containing the first arrayoff1
- the offset within the bufferlen1
- the length of the first arraybuf2
- the buffer containing the second arrayoff2
- the offset within the bufferlen2
- the length of the second arraypublic static int indexOf(byte[] a1, byte[] buf2, int off2, int len2)
a1
- the array to be searchedbuf2
- the buffer containing the second arrayoff2
- the offset within the second arraylen2
- the length of the second arraypublic static int indexOf(byte[] buf1, int off1, int len1, byte[] buf2, int off2, int len2)
buf1
- the buffer containing the array to be searchedoff1
- the offset within the bufferlen1
- the length of the array to be searchedbuf2
- the buffer containing the second arrayoff2
- the offset within the bufferlen2
- the length of the second arraypublic static void fill(byte[] a, byte val)
a
- the array of bytesval
- the value to be assignedpublic static void fill(byte[] buf, int off, int len, byte val)
buf
- the buffer containing the arrayoff
- the offset within the bufferlen
- the length of the arrayval
- the value to be assignedpublic static byte[] concat(byte[] src1, byte[] src2)
src1
- buffer containing the 1st arraysrc2
- buffer containing the 2nd arraypublic static int concat(byte[] src1, byte[] src2, byte[] dst3)
src1
- buffer containing the first arraysrc2
- buffer containing the second arraydst3
- buffer for the resulting concatenation arraypublic static byte[] concat(byte[] src1, int off1, int len1, byte[] src2, int off2, int len2)
src1
- buffer containing the first arrayoff1
- offset of the first arraylen1
- length of the first arraysrc2
- buffer containing the second arrayoff2
- offset of the second arraylen2
- length of the second arraypublic static int concat(byte[] src1, int off1, int len1, byte[] src2, int off2, int len2, byte[] dst3, int off3)
src1
- buffer containing the first arrayoff1
- offset of the first arraylen1
- length of the first arraysrc2
- buffer containing the second arrayoff2
- offset of the second arraylen2
- length of the second arraydst3
- buffer for the resulting concatenation arrayoff3
- offset of the resulting arraypublic static short uByte(byte b)
b
- the byte valuepublic static long uInt(int n)
n
- the integer valuepublic static java.lang.String bytesToHexString(byte[] data)
data
- the byte arraypublic static java.lang.String bytesToHexString(byte[] buf, int off, int len)
buf
- buffer containing the byte arrayoff
- the offset within the arraylen
- the number of bytespublic static java.lang.String bytesToFormattedHexString(byte[] data, char c, int num)
data
- the byte arrayc
- the block separatornum
- the number of bytes within each blockpublic static java.lang.String bytesToFormattedHexString(byte[] buf, int off, int len, char c, int num)
buf
- the buffer containing the byte arrayoff
- the offset within the arraylen
- the length of the arrayc
- the block separatornum
- the number of bytes within each blockpublic static java.lang.String trimHexString(java.lang.String str)
str
- the formatted hexadecimal stringpublic static byte[] hexStringToBytes(java.lang.String str)
str
- the hexadecimal stringpublic static int hexStringToBytes(java.lang.String str, byte[] buf, int off)
str
- the hexadecimal stringbuf
- the buffer where the array of bytes will be writtenoff
- the offset within the bufferpublic static byte[] formattedHexStringToBytes(java.lang.String str, char c)
str
- the string with hexadecimal characters possibly separated by the given characterc
- the separator characterpublic static byte[] formattedHexStringToBytes(java.lang.String str, char c, int len)
str
- the string with hexadecimal characters possibly separated by the given characterc
- the separator characterlen
- the length of the array of bytes, or -1 if unknownpublic static int formattedHexStringToBytes(java.lang.String str, char c, byte[] buf, int off)
str
- the string with hexadecimal characters possibly separated by the given characterc
- the separator characterbuf
- the buffer where the array of bytes will be writtenoff
- the offset within the bufferpublic static java.lang.String asHex(byte[] data)
The same as bytesToHexString(byte[])
data
- the byte arraypublic static java.lang.String asHex(byte[] buf, int off, int len)
The same as bytesToHexString(byte[], int, int)
buf
- buffer containing the byte arrayoff
- the offset within the arraylen
- the number of bytespublic static byte[] hexToBytes(java.lang.String str)
The same as formattedHexStringToBytes(String, char, int)
with c=':' and len=-1
str
- the string with hexadecimal characters possibly separated by colonpublic static byte[] hexToBytes(java.lang.String str, int len)
The same as formattedHexStringToBytes(String, char, int)
with c=':'
str
- the string with hexadecimal characters possibly separated by colonlen
- the length of the array of bytes, or -1 if unknownpublic static int hexToBytes(java.lang.String str, byte[] buf, int off)
The same as formattedHexStringToBytes(String, char, byte[], int)
with c=':'
str
- the string with hexadecimal characters possibly separated by colonbuf
- the buffer where the array of bytes will be writtenoff
- the offset within the bufferpublic static java.lang.String bytesToAsciiString(byte[] data)
data
- the array of bytespublic static java.lang.String bytesToAsciiString(byte[] buf, int off, int len)
buf
- buffer containing the array of bytesoff
- the offset within the bufferlen
- the number of bytespublic static java.lang.String asAscii(byte[] data)
The same as bytesToAsciiString(byte[])
data
- the array of bytespublic static java.lang.String asAscii(byte[] buf, int off, int len)
The same as bytesToAsciiString(byte[], int, int)
buf
- buffer containing the array of bytesoff
- the offset within the bufferlen
- the number of bytespublic static java.lang.String bytesToBinString(byte[] data)
data
- the byte arraypublic static java.lang.String bytesToBinString(byte[] buf, int off, int len)
buf
- buffer containing the byte arrayoff
- the offset within the bufferlen
- the length of the arraypublic static java.lang.String asBinary(byte[] data)
The same as bytesToBinString(byte[])
data
- the array of bytespublic static java.lang.String asBinary(byte[] buf, int off, int len)
The same as bytesToBinString(byte[], int, int)
buf
- buffer containing the array of bytesoff
- the offset within the bufferlen
- the number of bytespublic static int twoBytesToInt(byte[] src)
src
- the two-byte array representing an integer in big-endian byte orderpublic static int twoBytesToInt(byte[] src, int off)
src
- a buffer containing the two-byte array representing an integer in big-endian byte orderoff
- the offset within the bufferpublic static long fourBytesToInt(byte[] src)
src
- the 4-byte array representing an integer in big-endian byte orderpublic static long fourBytesToInt(byte[] src, int off)
src
- a buffer containing the 4-byte array representing an integer in big-endian byte orderoff
- the offset within the bufferpublic static long eightBytesToInt(byte[] src)
src
- the 8-byte array representing an integer in big-endian byte orderpublic static long eightBytesToInt(byte[] src, int off)
src
- a buffer containing the 8-byte array representing an integer in big-endian byte orderoff
- the offset within the bufferpublic static long nBytesToInt(byte[] src)
src
- the n-byte array representing an integer in big-endian byte orderpublic static long nBytesToInt(byte[] src, int off, int len)
src
- a buffer containing the n-byte array representing an integer in big-endian byte orderoff
- the offset within the bufferlen
- the number of bytespublic static byte[] intToTwoBytes(int val)
val
- the 16-bit integerpublic static void intToTwoBytes(int val, byte[] dst, int off)
val
- the 16-bit integerdst
- a buffer for the two-byte big-endian representation of the given integeroff
- the offset within the bufferpublic static byte[] intToFourBytes(long val)
val
- the 32-bit integerpublic static void intToFourBytes(long val, byte[] dst, int off)
val
- the 32-bit integerdst
- a buffer for the four-byte big-endian representation of the given integeroff
- the offset within the bufferpublic static byte[] intToSixBytes(long val)
val
- the 48-bit integerpublic static void intToSixBytes(long val, byte[] dst, int off)
val
- the 48-bit integerdst
- a buffer for the six-byte big-endian representation of the given integeroff
- the offset within the bufferpublic static byte[] intToEightBytes(long val)
val
- the 64-bit integerpublic static void intToEightBytes(long val, byte[] dst, int off)
val
- the 64-bit integerdst
- a buffer for the eight-byte big-endian representation of the given integeroff
- the offset within the bufferpublic static byte[] intToNBytes(long val, int len)
val
- the integerlen
- the number of bytespublic static void intToNBytes(long val, byte[] dst, int off, int len)
val
- the integerdst
- a buffer for the n-byte big-endian representation of the given integeroff
- the offset within the bufferlen
- the number of bytespublic static long fourBytesToIntLittleEndian(byte[] src)
src
- the four-byte array representing an integer in little-endian byte orderpublic static long fourBytesToIntLittleEndian(byte[] src, int off)
src
- a buffer containing the four-byte array representing an integer in little-endian byte orderoff
- the offset within the bufferpublic static byte[] intToFourBytesLittleEndian(long n)
n
- the 32-bit integerpublic static void intToFourBytesLittleEndian(long n, byte[] dst, int off)
n
- the 32-bit integerdst
- a buffer for the four-byte little-endian representation of the given integeroff
- the offset within the bufferpublic static void invertByteOrder(byte[] buf)
buf
- the buffer containing the arraypublic static void inc(byte[] buf)
buf
- the buffer containing the arraypublic static int twoBytesToInt(byte b_high, byte b_low)
b_high
- high byteb_low
- low bytepublic static long fourBytesToLong(byte b3, byte b2, byte b1, byte b0)
b3
- the fourth byteb2
- the third byteb1
- the second byteb0
- the first byte