public class BytesUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static byte |
boolToByte(boolean x) |
static byte[] |
boolToBytes(boolean x)
convert boolean to byte[1].
|
static byte[] |
boolToBytes(boolean x,
byte[] desc,
int offset)
boolean convert to byte array, then write four bytes to parameter desc start from index:offset.
|
static boolean |
bytesToBool(byte[] b)
byte array to boolean.
|
static boolean |
bytesToBool(byte[] b,
int offset)
convert one-bytes byte array cut from parameters to boolean.
|
static double |
bytesToDouble(byte[] bytes)
convert byte array to double.
|
static double |
bytesToDouble(byte[] bytes,
int offset)
convert eight-bytes byte array cut from parameters to double.
|
static float |
bytesToFloat(byte[] b)
convert byte[4] to float.
|
static float |
bytesToFloat(byte[] b,
int offset)
convert four-bytes byte array cut from parameters to float.
|
static int |
bytesToInt(byte[] bytes)
byte[4] convert to integer.
|
static int |
bytesToInt(byte[] bytes,
int offset)
convert four-bytes byte array cut from parameters to integer.
|
static int |
bytesToInt(byte[] result,
int pos,
int width)
given a byte array, read width bits from specified position bits and convert it to an integer.
|
static long |
bytesToLong(byte[] byteNum)
convert byte array to long with default length 8.
|
static long |
bytesToLong(byte[] byteNum,
int len)
specify the input byte array length.
|
static long |
bytesToLong(byte[] result,
int pos,
int width)
given a byte array, read width bits from specified pos bits and convert it to an long.
|
static long |
bytesToLongFromOffset(byte[] byteNum,
int len,
int offset)
convert eight-bytes byte array cut from parameters to long.
|
static short |
bytesToShort(byte[] b)
we modify the order of serialization for fitting ByteBuffer.getShort()
|
static String |
bytesToString(byte[] byteStr)
convert byte array to string using UTF-8 encoding.
|
static boolean |
byteToBool(byte b) |
static byte[] |
concatByteArray(byte[] a,
byte[] b)
join two byte arrays to one.
|
static byte[] |
concatByteArrayList(List<byte[]> list)
join a list of byte arrays into one array.
|
static byte[] |
doubleToBytes(double data)
convert double to byte array.
|
static void |
doubleToBytes(double d,
byte[] bytes,
int offset)
convert double to byte into the given byte array started from offset.
|
static byte[] |
floatToBytes(float x)
convert float to byte array.
|
static void |
floatToBytes(float x,
byte[] desc,
int offset)
float convert to boolean, then write four bytes to parameter desc start from index:offset.
|
static int |
getByteN(byte data,
int offset)
get one bit in input byte.
|
static int |
getIntN(int data,
int offset)
get one bit in input integer.
|
static int |
getLongN(long data,
int offset)
get one bit in input long.
|
static byte[] |
intToBytes(int i)
integer convert to byte[4].
|
static byte[] |
intToBytes(int i,
byte[] desc,
int offset)
integer convert to byte array, then write four bytes to parameter desc start from index:offset.
|
static void |
intToBytes(int srcNum,
byte[] result,
int pos,
int width)
convert an integer to a byte array which length is width, then copy this array to the parameter
result from pos.
|
static byte[] |
intToTwoBytes(int i)
divide int to two bytes.
|
static byte[] |
longToBytes(long num)
long to byte array with default converting length 8.
|
static byte[] |
longToBytes(long num,
byte[] desc,
int offset)
long convert to byte array, then write four bytes to parameter desc start from index:offset.
|
static void |
longToBytes(long srcNum,
byte[] result,
int pos,
int width)
convert a long to a byte array which length is width, then copy this array to the parameter
result from pos.
|
static byte[] |
longToBytes(long num,
int len)
specify the result array length.
|
static boolean |
readBool(InputStream in)
read 1-byte array from an InputStream and convert it to a integer number.
|
static double |
readDouble(InputStream in)
read 8-byte array from an InputStream and convert it to a double number.
|
static float |
readFloat(InputStream in)
read 4-byte array from an InputStream and convert it to a float number.
|
static int |
readInt(InputStream in)
read 4-byte array from an InputStream and convert it to a integer number.
|
static long |
readLong(InputStream in)
read 8-byte array from an InputStream and convert it to a long number.
|
static byte[] |
safeReadInputStreamToBytes(int count,
InputStream in)
read bytes specified length from InputStream safely.
|
static byte |
setByteN(byte data,
int offset,
int value)
set one bit in input byte.
|
static int |
setIntN(int data,
int offset,
int value)
set one bit in input integer.
|
static long |
setLongN(long data,
int offset,
int value)
set one bit in input long.
|
static byte[] |
shortToBytes(short number)
we modify the order of serialization for fitting ByteBuffer.putShort()
|
static byte[] |
stringToBytes(String str)
convert string to byte array using UTF-8 encoding.
|
static byte[] |
subBytes(byte[] src,
int start,
int length)
cut out specified length byte array from parameter start from input byte array src and return.
|
static int |
twoBytesToInt(byte[] ret)
concatenate two bytes to int.
|
public static byte[] intToBytes(int i)
i - integer to convertpublic static byte[] intToBytes(int i,
byte[] desc,
int offset)
i - integer to convertdesc - byte array be writtenoffset - position in desc byte array that conversion result should startpublic static void intToBytes(int srcNum,
byte[] result,
int pos,
int width)
srcNum - input integer variable. All but the lowest width bits are 0.result - byte array to convertpos - start positionwidth - bit-widthpublic static byte[] intToTwoBytes(int i)
i - intpublic static int twoBytesToInt(byte[] ret)
ret - byte[]public static int bytesToInt(byte[] bytes)
bytes - input byte[]public static int bytesToInt(byte[] bytes,
int offset)
bytes - source bytes which length should be greater than 4offset - position in parameter byte array that conversion result should startpublic static int bytesToInt(byte[] result,
int pos,
int width)
result - input byte arraypos - bit offset rather than byte offsetwidth - bit-widthpublic static byte[] floatToBytes(float x)
x - floatpublic static void floatToBytes(float x,
byte[] desc,
int offset)
x - floatdesc - byte array be writtenoffset - position in desc byte array that conversion result should startpublic static float bytesToFloat(byte[] b)
b - byte[4]public static float bytesToFloat(byte[] b,
int offset)
b - source bytes which length should be greater than 4offset - position in parameter byte array that conversion result should startpublic static byte[] doubleToBytes(double data)
data - doublepublic static void doubleToBytes(double d,
byte[] bytes,
int offset)
d - input doublebytes - target byte[]offset - start pospublic static double bytesToDouble(byte[] bytes)
bytes - byte[8]public static double bytesToDouble(byte[] bytes,
int offset)
bytes - source bytes which length should be greater than 8offset - position in parameter byte array that conversion result should startpublic static byte[] boolToBytes(boolean x)
x - booleanpublic static byte boolToByte(boolean x)
public static boolean byteToBool(byte b)
public static byte[] boolToBytes(boolean x,
byte[] desc,
int offset)
x - input booleandesc - byte array be writtenoffset - position in desc byte array that conversion result should startpublic static boolean bytesToBool(byte[] b)
b - input byte[1]public static boolean bytesToBool(byte[] b,
int offset)
b - source bytes which length should be greater than 1offset - position in parameter byte array that conversion result should startpublic static byte[] longToBytes(long num)
num - long variable to be convertedpublic static byte[] longToBytes(long num,
int len)
num - long variable to be convertedlen - length of result byte arraypublic static byte[] longToBytes(long num,
byte[] desc,
int offset)
num - input long variabledesc - byte array be writtenoffset - position in desc byte array that conversion result should startpublic static void longToBytes(long srcNum,
byte[] result,
int pos,
int width)
srcNum - input long variable. All but the lowest width bits are 0.result - byte array to convertpos - start positionwidth - bit-widthpublic static long bytesToLong(byte[] byteNum)
byteNum - input byte arraypublic static long bytesToLong(byte[] byteNum,
int len)
byteNum - byte array to be convertedlen - length of input byte array to be convertedpublic static long bytesToLong(byte[] result,
int pos,
int width)
result - input byte arraypos - bit offset rather than byte offsetwidth - bit-widthpublic static long bytesToLongFromOffset(byte[] byteNum,
int len,
int offset)
byteNum - source bytes which length should be greater than 8len - length of input byte array to be convertedoffset - position in parameter byte array that conversion result should startpublic static byte[] stringToBytes(String str)
str - input stringpublic static String bytesToString(byte[] byteStr)
byteStr - input byte arraypublic static byte[] concatByteArray(byte[] a,
byte[] b)
a - one of byte arrayb - another byte arraypublic static byte[] concatByteArrayList(List<byte[]> list)
list - a list of byte array to joinpublic static byte[] subBytes(byte[] src,
int start,
int length)
src - input byte arraystart - start index of srclength - cut off lengthpublic static int getIntN(int data,
int offset)
data - input int variableoffset - bit offsetpublic static int setIntN(int data,
int offset,
int value)
data - input int variableoffset - bit offsetvalue - value to setpublic static int getByteN(byte data,
int offset)
data - input byte variableoffset - bit offsetpublic static byte setByteN(byte data,
int offset,
int value)
data - input byte variableoffset - bit offsetvalue - value to setpublic static int getLongN(long data,
int offset)
data - input long variableoffset - bit offsetpublic static long setLongN(long data,
int offset,
int value)
data - input long variableoffset - bit offsetvalue - value to setpublic static double readDouble(InputStream in) throws IOException
in - InputStreamIOException - cannot read double from InputStreampublic static float readFloat(InputStream in) throws IOException
in - InputStreamIOException - cannot read float from InputStreampublic static boolean readBool(InputStream in) throws IOException
in - InputStreamIOException - cannot read boolean from InputStreampublic static int readInt(InputStream in) throws IOException
in - InputStreamIOException - cannot read int from InputStreampublic static long readLong(InputStream in) throws IOException
in - InputStreamIOException - cannot read long from InputStreampublic static byte[] safeReadInputStreamToBytes(int count,
InputStream in)
throws IOException
count - number of byte to readin - InputStreamIOException - cannot read from InputStreampublic static byte[] shortToBytes(short number)
number - input short numberpublic static short bytesToShort(byte[] b)
b - bytesCopyright © 2022 The Apache Software Foundation. All rights reserved.