public class ByteUtils extends Object
Modifier and Type | Field and Description |
---|---|
static int |
LONG_IN_BYTES |
Constructor and Description |
---|
ByteUtils() |
Modifier and Type | Method and Description |
---|---|
static long |
bytesToLong(byte[] bytes)
Convert a byte array into a long using
ByteOrder.LITTLE_ENDIAN as the byte order. |
static long |
bytesToLong(byte[] bytes,
int offset)
Convert a byte array into a long from the given offset using
ByteOrder.LITTLE_ENDIAN as the byte order. |
static long |
getLong(InputStream input)
Read a long out of the given input stream (reading out the number bytes in a long and converting).
|
static byte[] |
getRandomBytes(int length)
Creates a byte array 'length' long and populates it with random data.
|
static boolean |
isAllZeros(byte[] bytes)
Checks if a byte array contains all zero bytes.
|
static byte[] |
longToBytes(long x)
Convert a long into a byte array using
ByteOrder.LITTLE_ENDIAN as the byte order. |
static byte[] |
readBytes(InputStream input)
Read all the bytes from the input stream and return as a single byte array.
|
static byte[] |
readBytes(InputStream input,
int length)
Read 'length' bytes from the input stream and return as a single byte array 'length' long.
|
static byte[] |
readLengthPrefixedData(InputStream input)
This will read a long from the input stream.
|
static void |
writeLengthPrefixedData(byte[] data,
OutputStream outputStream)
This will first write out the length of the data as a long (in bytes) followed by the data itself.
|
public static final int LONG_IN_BYTES
public static long bytesToLong(byte[] bytes)
ByteOrder.LITTLE_ENDIAN
as the byte order.bytes
- the bytes to convert into a longpublic static long bytesToLong(byte[] bytes, int offset)
ByteOrder.LITTLE_ENDIAN
as the byte order.bytes
- the bytes to convert into a longoffset
- the offset in the byte arraypublic static long getLong(InputStream input) throws IOException
input
- the input stream to read fromIOException
- thrown if there was an error reading the bytespublic static byte[] getRandomBytes(int length)
SecureRandom
).length
- the size of the random byte arraypublic static boolean isAllZeros(byte[] bytes)
bytes
- public static byte[] longToBytes(long x)
ByteOrder.LITTLE_ENDIAN
as the byte order.x
- the long to convert into a byte arraypublic static byte[] readBytes(InputStream input, int length) throws IOException
input
- the input stream to read bytes fromlength
- the number of bytes to read from the input streamIOException
- thrown if there are any issues reading the bytes from the input streampublic static byte[] readBytes(InputStream input) throws IOException
input
- the input stream to read bytes fromIOException
- thrown if there are any issues reading the bytes from the input streampublic static byte[] readLengthPrefixedData(InputStream input) throws IOException
input
- the stream to read fromIOException
- thrown if there are any errors reading the data from the streampublic static void writeLengthPrefixedData(byte[] data, OutputStream outputStream) throws IOException
data
- the data to write outoutputStream
- the stream to write toIOException
- thrown if there are any errors writing the data to the streamCopyright © 2021. All rights reserved.