Package com.apple.foundationdb.tuple
Class ByteArrayUtil
java.lang.Object
com.apple.foundationdb.tuple.ByteArrayUtil
Utility functions for operating on byte arrays. Although built for
the FoundationDB tuple layer, some functions may be useful otherwise, such as use of
printable(byte[])
for debugging non-text keys and values.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic Comparator<byte[]>
static int
compareTo
(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) Lexicographically compare two byte arrays.static int
compareUnsigned
(byte[] l, byte[] r) Compare byte arrays for equality and ordering purposes.static long
decodeInt
(byte[] src) Decode a little-endian encoded long integer from an 8-byte array.static byte[]
encodeInt
(long i) Encode an 64-bit integer (long) into a byte array.static byte[]
interludeJoin
(byte[] interlude, byte[][] parts) Joins a set of byte arrays into a larger array.static byte[]
join
(byte[]... parts) Joins a variable number of byte arrays into one larger array.static byte[]
Joins a set of byte arrays into a larger array.static byte[]
keyAfter
(byte[] key) Computes the key that would sort immediately afterkey
.static String
printable
(byte[] val) Gets a human readable version of a byte array.static byte[]
replace
(byte[] src, byte[] pattern, byte[] replacement) Replaces occurrences of a pattern in a byte array.static byte[]
replace
(byte[] src, int offset, int length, byte[] pattern, byte[] replacement) Replaces occurrences of a pattern in a byte array.static List<byte[]>
split
(byte[] src, byte[] delimiter) Splits a byte array at each occurrence of a pattern.static List<byte[]>
split
(byte[] src, int offset, int length, byte[] delimiter) Splits a byte array at each occurrence of a pattern.static boolean
startsWith
(byte[] array, byte[] prefix) Check if a byte array starts with another byte array.static byte[]
strinc
(byte[] key) Computes the first key that would sort outside the range prefixed bykey
.
-
Field Details
-
EMPTY_BYTES
public static final byte[] EMPTY_BYTESA zero-length byte array.
-
-
Method Details
-
join
Joins a set of byte arrays into a larger array. Theinterlude
is placed between each of the elements, but not at the beginning or end. In the case that the list is empty ornull
, a zero-length byte array will be returned.- Parameters:
interlude
- can benull
or zero length. Placed internally between concatenated elements.parts
- the pieces to be joined. May benull
, but does not allow for elements in the list to benull
.- Returns:
- a newly created concatenation of the input
-
interludeJoin
public static byte[] interludeJoin(byte[] interlude, byte[][] parts) Joins a set of byte arrays into a larger array. Theinterlude
is placed between each of the elements, but not at the beginning or end. In the case that the list is empty ornull
, a zero-length byte array will be returned.- Parameters:
interlude
- can benull
or zero length. Placed internally between concatenated elements.parts
- the pieces to be joined. May benull
, but does not allow for elements in the array to benull
.- Returns:
- a newly created concatenation of the input
-
join
public static byte[] join(byte[]... parts) Joins a variable number of byte arrays into one larger array.- Parameters:
parts
- the elements to join.null
elements are not allowed.- Returns:
- a newly created concatenation of the input
-
replace
public static byte[] replace(byte[] src, byte[] pattern, byte[] replacement) Replaces occurrences of a pattern in a byte array. Does not mutate the contents of the parametersrc
.- Parameters:
src
- the source to search forpattern
pattern
- the pattern for which to searchreplacement
- the sequence of bytes to replacepattern
with.- Returns:
- a newly created array where
pattern
replaced withreplacement
-
replace
public static byte[] replace(byte[] src, int offset, int length, byte[] pattern, byte[] replacement) Replaces occurrences of a pattern in a byte array. Does not mutate the contents of the parametersrc
.- Parameters:
src
- the source to search forpattern
offset
- the location insrc
at which to start the operationlength
- the number of bytes pastoffset
to search forpattern
pattern
- the pattern for which to searchreplacement
- the sequence of bytes to replacepattern
with.- Returns:
- a newly created array where
pattern
replaced withreplacement
-
split
Splits a byte array at each occurrence of a pattern. If the pattern is found at the beginning or end of the array the result will have a leading or trailing zero-length array. The delimiter is not included in the output array. Does not mutate the contents the source array.- Parameters:
src
- the array to splitdelimiter
- the byte pattern on which to split- Returns:
- a list of byte arrays from
src
now not containingdelimiter
-
split
Splits a byte array at each occurrence of a pattern. If the pattern is found at the beginning or end of the array the result will have a leading or trailing zero-length array. The delimiter is not included in the output array. Does not mutate the contents the source array.- Parameters:
src
- the array to splitoffset
- the location in the array at which to start the operationlength
- the number of bytes to search, must not extend past the end ofsrc
delimiter
- the byte pattern on which to split- Returns:
- a list of byte arrays from
src
now not containingdelimiter
-
compareUnsigned
public static int compareUnsigned(byte[] l, byte[] r) Compare byte arrays for equality and ordering purposes. Elements in the array are interpreted and compared as unsigned bytes. Neither parameter may benull
.- Parameters:
l
- byte array on the left-hand side of the inequalityr
- byte array on the right-hand side of the inequality- Returns:
- return -1, 0, or 1 if
l
is less than, equal to, or greater thanr
.
-
comparator
- Returns:
- a
Comparator
instance which can be used for Java-level sorting of byte arrays.
-
startsWith
public static boolean startsWith(byte[] array, byte[] prefix) Check if a byte array starts with another byte array.- Parameters:
array
- the source byte arrayprefix
- the byte array that we are checking ifsrc
starts with.- Returns:
true
ifarray
starts withprefix
-
strinc
public static byte[] strinc(byte[] key) Computes the first key that would sort outside the range prefixed bykey
.key
must be non-null, and contain at least some character this is not\xFF
(255).- Parameters:
key
- prefix key- Returns:
- a newly created byte array
-
keyAfter
public static byte[] keyAfter(byte[] key) Computes the key that would sort immediately afterkey
.key
must be non-null.- Parameters:
key
- byte array for which next key is to be computed- Returns:
- a newly created byte array that would sort immediately after
key
-
encodeInt
public static byte[] encodeInt(long i) Encode an 64-bit integer (long) into a byte array. Encodes the integer in little endian byte order. The result is valid for use withTransaction.mutate(...)
.- Parameters:
i
- the number to encode- Returns:
- an 8-byte array containing the
- See Also:
-
decodeInt
public static long decodeInt(byte[] src) Decode a little-endian encoded long integer from an 8-byte array.- Parameters:
src
- the non-null, 8-element byte array from which to decode- Returns:
- a decoded 64-bit integer
-
printable
Gets a human readable version of a byte array. The bytes that correspond with ASCII printable characters [32-127) are passed through. Other bytes are replaced with\x
followed by a two character zero-padded hex code for the byte.- Parameters:
val
- the byte array for which to create a human readable form- Returns:
- a modification of the byte array with unprintable characters replaced.
-
compareTo
public static int compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2) Lexicographically compare two byte arrays.- Parameters:
buffer1
- left operand, expected to not be nullbuffer2
- right operand, expected to not be nulloffset1
- Where to start comparing in the left buffer, expected to be >= 0offset2
- Where to start comparing in the right buffer, expected to be >= 0length1
- How much to compare from the left buffer, expected to be >= 0length2
- How much to compare from the right buffer, expected to be >= 0- Returns:
- 0 if equal, < 0 if left is less than right, etc.
-