Package com.apple.foundationdb.tuple
Class ByteArrayUtil
- java.lang.Object
-
- com.apple.foundationdb.tuple.ByteArrayUtil
-
public class ByteArrayUtil extends java.lang.ObjectUtility functions for operating on byte arrays. Although built for the FoundationDB tuple layer, some functions may be useful otherwise, such as use ofprintable(byte[])for debugging non-text keys and values.
-
-
Field Summary
Fields Modifier and Type Field Description static byte[]EMPTY_BYTESA zero-length byte array.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.Comparator<byte[]>comparator()static intcompareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)Lexicographically compare two byte arrays.static intcompareUnsigned(byte[] l, byte[] r)Compare byte arrays for equality and ordering purposes.static longdecodeInt(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[]join(byte[] interlude, java.util.List<byte[]> parts)Joins a set of byte arrays into a larger array.static byte[]keyAfter(byte[] key)Computes the key that would sort immediately afterkey.static java.lang.Stringprintable(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 java.util.List<byte[]>split(byte[] src, byte[] delimiter)Splits a byte array at each occurrence of a pattern.static java.util.List<byte[]>split(byte[] src, int offset, int length, byte[] delimiter)Splits a byte array at each occurrence of a pattern.static booleanstartsWith(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.
-
-
-
Method Detail
-
join
public static byte[] join(byte[] interlude, java.util.List<byte[]> parts)Joins a set of byte arrays into a larger array. Theinterludeis 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 benullor 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. Theinterludeis 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 benullor 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.nullelements 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 forpatternpattern- the pattern for which to searchreplacement- the sequence of bytes to replacepatternwith.- Returns:
- a newly created array where
patternreplaced 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 forpatternoffset- the location insrcat which to start the operationlength- the number of bytes pastoffsetto search forpatternpattern- the pattern for which to searchreplacement- the sequence of bytes to replacepatternwith.- Returns:
- a newly created array where
patternreplaced withreplacement
-
split
public static java.util.List<byte[]> split(byte[] src, byte[] delimiter)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
srcnow not containingdelimiter
-
split
public static java.util.List<byte[]> split(byte[] src, int offset, int length, byte[] delimiter)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 ofsrcdelimiter- the byte pattern on which to split- Returns:
- a list of byte arrays from
srcnow 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
lis less than, equal to, or greater thanr.
-
comparator
public static java.util.Comparator<byte[]> comparator()
- Returns:
- a
Comparatorinstance 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 ifsrcstarts with.- Returns:
trueifarraystarts withprefix
-
strinc
public static byte[] strinc(byte[] key)
Computes the first key that would sort outside the range prefixed bykey.keymust 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.keymust 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:
Transaction.mutate(com.apple.foundationdb.MutationType, byte[], byte[])
-
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
public static java.lang.String printable(byte[] val)
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\xfollowed 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.
-
-