Package io.permazen.util
Class ByteUtil
java.lang.Object
io.permazen.util.ByteUtil
Byte manipulation utilities.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Comparator<byte[]>
Comparator
that compares two byte arrays lexicographically using unsigned values.static final byte[]
An empty byte array. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
compare
(byte[] b1, byte[] b2) Compare two byte arrays lexicographically using unsigned values.static byte[]
fromDouble
(double value) Performs the inverse oftoDouble()
.static byte[]
getKeyAfterPrefix
(byte[] prefix) Get the first key that would be greater than the given key in unsigned lexicographic ordering and that does not have the given key as a prefix.static byte[]
getNextKey
(byte[] key) Get the next key greater than the given key in unsigned lexicographic ordering.static boolean
isConsecutive
(byte[] key1, byte[] key2) Determine whetherkey2
is the next key afterkey1
.static boolean
isPrefixOf
(byte[] prefix, byte[] value) Determine if the first of twobyte[]
arrays is a prefix of the second.static byte[]
max
(byte[] b1, byte[] b2) Determine the larger of two byte arrays when compared lexicographically using unsigned values.static byte[]
min
(byte[] b1, byte[] b2) Determine the smaller of two byte arrays when compared lexicographically using unsigned values.static byte[]
Decode a hexadecimalString
into abyte[]
array.static int
readInt
(ByteReader reader) Read anint
as four big-endian bytes.static long
readLong
(ByteReader reader) Read along
as eight big-endian bytes.static double
toDouble
(byte[] key) Map abyte[]
array into the range[0.0, 1.0)
in a way that preserves order.static String
toString
(byte[] buf) Convert a byte array into a string of hex digits, or"null"
ifbuf
is null.static void
writeInt
(ByteWriter writer, int value) Write anint
as four big-endian bytes.static void
writeLong
(ByteWriter writer, long value) Write along
as eight big-endian bytes.
-
Field Details
-
EMPTY
public static final byte[] EMPTYAn empty byte array. This is the minimum value according toCOMPARATOR
. -
COMPARATOR
Comparator
that compares two byte arrays lexicographically using unsigned values. -
STRING_CONVERTER
-
-
Method Details
-
compare
public static int compare(byte[] b1, byte[] b2) Compare two byte arrays lexicographically using unsigned values.- Parameters:
b1
- first byte arrayb2
- second byte array- Returns:
- -1 if
b1 < b2
, 1 ifb1 > b2
, or zero ifb1 = b2
- Throws:
NullPointerException
- ifb1
orb2
is null
-
min
public static byte[] min(byte[] b1, byte[] b2) Determine the smaller of two byte arrays when compared lexicographically using unsigned values.- Parameters:
b1
- first byte arrayb2
- second byte array- Returns:
b1
ifb1 <= b2
, otherwiseb2
- Throws:
NullPointerException
- ifb1
orb2
is null
-
max
public static byte[] max(byte[] b1, byte[] b2) Determine the larger of two byte arrays when compared lexicographically using unsigned values.- Parameters:
b1
- first byte arrayb2
- second byte array- Returns:
b1
ifb1 >= b2
, otherwiseb2
- Throws:
NullPointerException
- ifb1
orb2
is null
-
isPrefixOf
public static boolean isPrefixOf(byte[] prefix, byte[] value) Determine if the first of twobyte[]
arrays is a prefix of the second.- Parameters:
prefix
- prefix to checkvalue
- value to check for havingprefix
as a prefix- Returns:
- true if
prefix
is a prefix ofvalue
- Throws:
NullPointerException
- ifprefix
orvalue
is null
-
getNextKey
public static byte[] getNextKey(byte[] key) Get the next key greater than the given key in unsigned lexicographic ordering. This creates a new key simply by appending a0x00
byte to the data contained in the given key.- Parameters:
key
- previous key- Returns:
- next key after
key
- Throws:
NullPointerException
- ifkey
is null
-
isConsecutive
public static boolean isConsecutive(byte[] key1, byte[] key2) Determine whetherkey2
is the next key afterkey1
.- Parameters:
key1
- first keykey2
- second key- Returns:
- true if
key2
immediately followskey1
- Throws:
NullPointerException
- if either parameter is null
-
getKeyAfterPrefix
public static byte[] getKeyAfterPrefix(byte[] prefix) Get the first key that would be greater than the given key in unsigned lexicographic ordering and that does not have the given key as a prefix.- Parameters:
prefix
- lower bound prefix key- Returns:
- next key not having
prefix
as a prefix - Throws:
IllegalArgumentException
- ifprefix
has zero lengthIllegalArgumentException
- ifprefix
contains only0xff
bytesNullPointerException
- ifprefix
is null
-
toString
Convert a byte array into a string of hex digits, or"null"
ifbuf
is null.- Parameters:
buf
- bytes- Returns:
- string encoding of
buf
- See Also:
-
parse
Decode a hexadecimalString
into abyte[]
array. The string must have an even number of digits and contain no other characters (e.g., whitespace).- Parameters:
text
- string previously encoded bytoString(byte[])
- Returns:
byte[]
decoding oftext
- Throws:
IllegalArgumentException
- if any non-hexadecimal characters are found or the number of characters is oddNullPointerException
- iftext
is null- See Also:
-
readInt
Read anint
as four big-endian bytes.- Parameters:
reader
- input- Returns:
- decoded integer
- Throws:
IndexOutOfBoundsException
- if less than four bytes remain inreader
NullPointerException
- ifreader
is null- See Also:
-
writeInt
Write anint
as four big-endian bytes.- Parameters:
writer
- byte destinationvalue
- value to write- Throws:
NullPointerException
- ifwriter
is null- See Also:
-
readLong
Read along
as eight big-endian bytes.- Parameters:
reader
- input- Returns:
- decoded long
- Throws:
IndexOutOfBoundsException
- if less than eight bytes remain inreader
- See Also:
-
writeLong
Write along
as eight big-endian bytes.- Parameters:
writer
- byte destinationvalue
- value to write- Throws:
NullPointerException
- ifwriter
is null- See Also:
-
toDouble
public static double toDouble(byte[] key) Map abyte[]
array into the range[0.0, 1.0)
in a way that preserves order.This allows calculations that require a notion of "distance" between two keys.
This method simply maps the first 6.5 bytes of
key
into the 52 mantissa bits of adouble
value. Obviously, the mapping is not reversible: some keys will map equaldouble
values, but otherwise the mapping is order-preserving.- Parameters:
key
- input key- Returns:
- nearest corresponding value between zero (inclusive) and one (exclusive)
- Throws:
IllegalArgumentException
- ifkey
is null
-
fromDouble
public static byte[] fromDouble(double value) Performs the inverse oftoDouble()
.The mapping of
toDouble()
is not reversible: some keys will map to the samedouble
value, so this method does not always return the originalbyte[]
key.- Parameters:
value
- input value; must be >= 0.0 and < 1.0- Returns:
- a
byte[]
key that maps tovalue
- Throws:
IllegalArgumentException
- ifvalue
is not a number between zero (inclusive) and one (exclusive)
-