public class ByteArrayUtils extends java.lang.Object
Constructor and Description |
---|
ByteArrayUtils() |
Modifier and Type | Method and Description |
---|---|
static double |
byteArrayToDouble(byte[] value)
Converts a byte array to a double value.
|
static int |
byteArrayToInt(byte[] bytes)
Converts a byte array to an integer value.
|
static long |
byteArrayToLong(byte[] bytes)
Converts a byte array to a long value.
|
static java.util.OptionalInt |
byteArrayToOptionalInt(byte[] bytes)
Converts a byte array to an optional int value, by mapping a null input array
to empty.
|
static java.util.OptionalLong |
byteArrayToOptionalLong(byte[] bytes)
Converts a byte array to an optional long value, by mapping a null input
array to empty.
|
static byte[] |
compress(byte[] value)
Compresses a single byte[] using GZIP.
|
static byte[] |
concat(byte[]... arrays)
Returns the concatenation of the given arrays.
|
static byte[] |
concat(java.lang.Iterable<byte[]> arrays)
Returns the concatenation of the given arrays.
|
static byte[] |
decompress(byte[] value)
Decompresses a single byte[] using GZIP.
|
static byte[] |
doubleToByteArray(double value)
Converts a double value to a byte array.
|
static java.lang.String |
hexDump(byte[] data)
Creates a hex dump of the provided bytes.
|
static java.lang.String |
hexDump(byte[] data,
int width)
Creates a hex dump of the provided bytes.
|
static int |
indexOf(byte[] searchFor,
byte[] searchIn,
int startIndex)
Returns the first index in
searchIn at or after the start index
containing searchFor (or -1 if not found). |
static int |
indexOf(byte[] searchFor,
byte[] searchIn,
int startIndex,
int endIndex)
Returns the first index in
searchIn at or after the start index
containing searchFor (or -1 if not found). |
static byte[] |
intToByteArray(int value)
Converts an integer value to a byte array.
|
static boolean |
isLess(byte[] a1,
byte[] a2,
boolean resultIfEqual)
Returns true if a1 is (lexicographically) less than a2.
|
static boolean |
isPrefix(byte[] prefix,
byte[] key)
Returns whether the prefix is a prefix of the given key.
|
static boolean |
isPrefix(byte[] prefix,
byte[] key,
int startIndex)
Returns whether the
prefix is a prefix of the given
key when only looking at the part of key starting
at startIndex . |
static byte[] |
longToByteArray(long value)
Converts a long value to a byte array.
|
static int |
readIntFromStartOfArray(byte[] bytes)
Reads an int stored with
storeIntInStartOfArray(int, byte[]) from
the first 4 bytes of the array. |
static java.util.List<byte[]> |
split(byte[] bytes,
byte[] separatorBytes)
Perform a split with no limit according to
split(byte[], byte[], int) . |
static java.util.List<byte[]> |
split(byte[] bytes,
byte[] separatorBytes,
int maxSplits)
Splits the byte array at the separator bytes given maximum split amount of
times.
|
static boolean |
startsWithZipMagicBytes(byte[] data)
Returns whether the given bytes start with the
magic bytes that mark a ZIP file.
|
static void |
storeIntInStartOfArray(int value,
byte[] bytes)
Stores the given int at the first 4 bytes of the array.
|
static int |
unsignedByte(byte b)
Returns the unsigned byte interpretation of the parameter.
|
static long |
unsignedByteAsLong(byte b)
Returns the unsigned byte interpretation of the parameter as long.
|
public ByteArrayUtils()
public static byte[] intToByteArray(int value)
Integer.BYTES
.public static void storeIntInStartOfArray(int value, byte[] bytes)
public static byte[] doubleToByteArray(double value)
Double.BYTES
public static byte[] longToByteArray(long value)
Long.BYTES
public static int byteArrayToInt(byte[] bytes)
intToByteArray(int)
.public static java.util.OptionalInt byteArrayToOptionalInt(byte[] bytes)
public static int readIntFromStartOfArray(byte[] bytes)
storeIntInStartOfArray(int, byte[])
from
the first 4 bytes of the array.public static double byteArrayToDouble(byte[] value)
doubleToByteArray(double)
.public static long byteArrayToLong(byte[] bytes)
longToByteArray(long)
.public static java.util.OptionalLong byteArrayToOptionalLong(byte[] bytes)
public static byte[] decompress(byte[] value) throws java.io.IOException
java.io.IOException
- if the input array is not valid GZIP compressed data (as created
by compress(byte[])
).public static byte[] compress(byte[] value)
public static boolean isPrefix(byte[] prefix, byte[] key)
public static boolean isPrefix(byte[] prefix, byte[] key, int startIndex)
prefix
is a prefix of the given
key
when only looking at the part of key
starting
at startIndex
.public static boolean isLess(byte[] a1, byte[] a2, boolean resultIfEqual)
public static int unsignedByte(byte b)
public static long unsignedByteAsLong(byte b)
public static byte[] concat(byte[]... arrays)
public static byte[] concat(java.lang.Iterable<byte[]> arrays)
public static java.lang.String hexDump(byte[] data)
public static java.lang.String hexDump(byte[] data, int width)
width
bytes of data first printed as hex
numbers and then as a string interpretation. Each line is also prefixed with
an offset.public static boolean startsWithZipMagicBytes(byte[] data)
public static int indexOf(byte[] searchFor, byte[] searchIn, int startIndex)
searchIn
at or after the start index
containing searchFor
(or -1 if not found).public static int indexOf(byte[] searchFor, byte[] searchIn, int startIndex, int endIndex)
searchIn
at or after the start index
containing searchFor
(or -1 if not found). endIndex is the index
of the first byte that is not considered in the match (exclusive).public static java.util.List<byte[]> split(byte[] bytes, byte[] separatorBytes)
split(byte[], byte[], int)
.public static java.util.List<byte[]> split(byte[] bytes, byte[] separatorBytes, int maxSplits)
ArrayList
is never longer than the maximum split.bytes
- The bytes to split. An empty list is returned if this is null.separatorBytes
- Non null array of bytes to split at. Must have positive length.maxSplits
- the maximum number of splits to perform starting at the beginning
of the bytes array. An empty list is returned if this is 0 or
negative.