public class ByteUtils
extends java.lang.Object
| 限定符和类型 | 字段和说明 |
|---|---|
static byte[] |
EMPTY_BYTE_ARRAY |
static byte[] |
ZERO_BYTE_ARRAY |
| 构造器和说明 |
|---|
ByteUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static byte[] |
and(byte[] b1,
byte[] b2) |
static byte[] |
appendByte(byte[] bytes,
byte b)
Creates a copy of bytes and appends b to the end of it
|
static short |
bigEndianToShort(byte[] bs) |
static short |
bigEndianToShort(byte[] bs,
int off) |
static byte[] |
bigIntegerToBytes(java.math.BigInteger value) |
static byte[] |
bigIntegerToBytes(java.math.BigInteger b,
int numBytes)
The regular
BigInteger.toByteArray() method isn't quite what we often need: it
appends a leading zero to indicate that the number is positive and may need padding. |
static byte[] |
bigIntegerToBytesSigned(java.math.BigInteger b,
int numBytes) |
static int |
byteArrayToInt(byte[] b)
Cast hex encoded value from byte[] to int null is parsed like byte[0]
Limited to Integer.MAX_VALUE: 2^32-1 (4 bytes)
|
static long |
byteArrayToLong(byte[] b)
Cast hex encoded value from byte[] to long null is parsed like byte[0]
Limited to Long.MAX_VALUE: 263-1 (8 bytes)
|
static java.math.BigInteger |
bytesToBigInteger(byte[] bb)
Cast hex encoded value from byte[] to BigInteger null is parsed like byte[0]
|
static int[] |
bytesToInts(byte[] arr,
boolean bigEndian) |
static void |
bytesToInts(byte[] b,
int[] arr,
boolean bigEndian) |
static java.lang.String |
bytesToIp(byte[] bytesIp)
Converts 4 bytes IPv4 IP to String representation
|
static byte[] |
calcPacketLength(byte[] msg)
Calculate packet length
|
static byte[] |
copyToArray(java.math.BigInteger value)
Utility function to copy a byte array into a new byte array with given size.
|
static java.util.Set<byte[]> |
difference(java.util.Set<byte[]> setA,
java.util.Set<byte[]> setB) |
static byte[] |
encodeDataList(java.lang.Object... args)
encode the values and concatenate together
|
static int |
firstNonZeroByte(byte[] data) |
static int |
getBit(byte[] data,
int pos) |
static byte[] |
hexStringToBytes(java.lang.String data)
Converts string hex representation to data bytes Accepts following hex: - with or without 0x
prefix
|
static byte[] |
hostToBytes(java.lang.String ip)
Converts string representation of host/ip to 4-bytes byte[] IPv4
|
static boolean |
increment(byte[] bytes)
increment byte array as a number until max is reached
|
static byte[] |
intsToBytes(int[] arr,
boolean bigEndian) |
static void |
intsToBytes(int[] arr,
byte[] b,
boolean bigEndian) |
static byte[] |
intToBytes(int val)
Converts int value into a byte array.
|
static byte[] |
intToBytesNoLeadZeroes(int val)
Converts a int value into a byte array.
|
static boolean |
isNullOrZeroArray(byte[] array) |
static boolean |
isSingleZero(byte[] array) |
static int |
length(byte[]... bytes) |
static byte[] |
longToBytes(long val)
Converts a long value into a byte array.
|
static byte[] |
longToBytesNoLeadZeroes(long val)
Converts a long value into a byte array.
|
static int |
matchingNibbleLength(byte[] a,
byte[] b)
Returns the amount of nibbles that match each other from 0 ... amount will never be larger
than smallest input
|
static byte[] |
merge(byte[]... arrays) |
static java.lang.String |
nibblesToPrettyString(byte[] nibbles)
Turn nibbles to a pretty looking output string
Example. [ 1, 2, 3, 4, 5 ] becomes '\x11\x23\x45'
|
static int |
numberOfLeadingZeros(byte[] bytes)
Returns a number of zero bits preceding the highest-order ("leftmost") one-bit interpreting
input array as a big-endian integer value
|
static int |
numBytes(java.lang.String val)
Calculate the number of bytes need to encode the number
|
static java.lang.String |
oneByteToHexString(byte value) |
static byte[] |
or(byte[] b1,
byte[] b2) |
static byte[] |
parseBytes(byte[] input,
int offset,
int len)
Parses fixed number of bytes starting from
offset in input array. |
static byte[] |
parseWord(byte[] input,
int idx)
Parses 32-bytes word from given input.
|
static byte[] |
parseWord(byte[] input,
int offset,
int idx)
Parses 32-bytes word from given input.
|
static byte[] |
setBit(byte[] data,
int pos,
int val) |
static byte[] |
shortToBytes(short n) |
static byte[] |
stripLeadingZeroes(byte[] data) |
static byte[] |
trimLeadingBytes(byte[] bytes,
byte b) |
static byte[] |
trimLeadingZeroes(byte[] bytes) |
static byte[] |
xor(byte[] b1,
byte[] b2) |
static byte[] |
xorAlignRight(byte[] b1,
byte[] b2)
XORs byte arrays of different lengths by aligning length of the shortest via adding zeros at
beginning
|
public static final byte[] EMPTY_BYTE_ARRAY
public static final byte[] ZERO_BYTE_ARRAY
public static byte[] appendByte(byte[] bytes,
byte b)
bytes - the original bytesb - the appended bytepublic static byte[] bigIntegerToBytes(java.math.BigInteger b,
int numBytes)
BigInteger.toByteArray() method isn't quite what we often need: it
appends a leading zero to indicate that the number is positive and may need padding.b - the integer to format into a byte arraynumBytes - the desired size of the resulting byte arraypublic static byte[] bigIntegerToBytes(java.math.BigInteger value)
public static byte[] bigIntegerToBytesSigned(java.math.BigInteger b,
int numBytes)
public static java.math.BigInteger bytesToBigInteger(byte[] bb)
bb - byte array contains the valuespublic static int matchingNibbleLength(byte[] a,
byte[] b)
a - - first inputb - - second inputpublic static byte[] longToBytes(long val)
val - - long value to convertbyte[] of length 8, representing the long valuepublic static byte[] longToBytesNoLeadZeroes(long val)
val - - long value to convertpublic static byte[] intToBytes(int val)
val - - int value to convertbyte[] of length 4, representing the int valuepublic static byte[] intToBytesNoLeadZeroes(int val)
val - - int value to convertpublic static byte[] calcPacketLength(byte[] msg)
msg - byte[]public static int byteArrayToInt(byte[] b)
Limited to Integer.MAX_VALUE: 2^32-1 (4 bytes)
b - array contains the valuespublic static long byteArrayToLong(byte[] b)
Limited to Long.MAX_VALUE: 263-1 (8 bytes)
b - array contains the valuespublic static java.lang.String nibblesToPrettyString(byte[] nibbles)
Example. [ 1, 2, 3, 4, 5 ] becomes '\x11\x23\x45'
nibbles - - getting byte of data [ 04 ] and turning it to a '\x04' representationpublic static java.lang.String oneByteToHexString(byte value)
public static int numBytes(java.lang.String val)
val - - numberpublic static byte[] encodeDataList(java.lang.Object... args)
args - Objectpublic static int firstNonZeroByte(byte[] data)
public static byte[] stripLeadingZeroes(byte[] data)
public static boolean increment(byte[] bytes)
bytes - byte[]public static byte[] copyToArray(java.math.BigInteger value)
value - - a BigInteger with a maximum value of 2^256-1srcpublic static byte[] setBit(byte[] data,
int pos,
int val)
public static int getBit(byte[] data,
int pos)
public static byte[] and(byte[] b1,
byte[] b2)
public static byte[] or(byte[] b1,
byte[] b2)
public static byte[] xor(byte[] b1,
byte[] b2)
public static byte[] xorAlignRight(byte[] b1,
byte[] b2)
b1 - the first byte arrayb2 - the second byte arraypublic static byte[] merge(byte[]... arrays)
arrays - - arrays to mergepublic static boolean isNullOrZeroArray(byte[] array)
public static boolean isSingleZero(byte[] array)
public static java.util.Set<byte[]> difference(java.util.Set<byte[]> setA,
java.util.Set<byte[]> setB)
public static int length(byte[]... bytes)
public static int[] bytesToInts(byte[] arr,
boolean bigEndian)
public static void bytesToInts(byte[] b,
int[] arr,
boolean bigEndian)
public static byte[] intsToBytes(int[] arr,
boolean bigEndian)
public static void intsToBytes(int[] arr,
byte[] b,
boolean bigEndian)
public static short bigEndianToShort(byte[] bs)
public static short bigEndianToShort(byte[] bs,
int off)
public static byte[] shortToBytes(short n)
public static byte[] hexStringToBytes(java.lang.String data)
data - String like '0xa5e..' or just 'a5e..'public static byte[] hostToBytes(java.lang.String ip)
ip - the ip string of hostpublic static java.lang.String bytesToIp(byte[] bytesIp)
bytesIp - the 4 bytes IPv4 IPpublic static int numberOfLeadingZeros(byte[] bytes)
bytes - the byte arraypublic static byte[] parseBytes(byte[] input,
int offset,
int len)
offset in input array. If input has not enough bytes return array will be right padded with zero bytes. I.e. if offset is higher than input.length then zero byte array of length len will
be returnedinput - the input bytes arrayoffset - an offset in input array to start parsing fromlen - the length of zero byte arraypublic static byte[] parseWord(byte[] input,
int idx)
parseBytes(byte[], int, int) method,
thus, result will be right-padded with zero bytes if there is not enough bytes in inputinput - the input bytes arrayidx - an index of the word starting from 0public static byte[] parseWord(byte[] input,
int offset,
int idx)
parseBytes(byte[], int, int) method,
thus, result will be right-padded with zero bytes if there is not enough bytes in inputinput - the input bytes arrayidx - an index of the word starting from 0offset - an offset in input array to start parsing frompublic static byte[] trimLeadingBytes(byte[] bytes,
byte b)
public static byte[] trimLeadingZeroes(byte[] bytes)