Class ByteUtils
java.lang.Object
org.springframework.data.redis.util.ByteUtils
Some handy methods for dealing with
byte arrays.- Since:
- 1.7
- Author:
- Christoph Strobl, Mark Paluch, Guy Korland
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]concat(byte[] array1, byte[] array2) Concatenate the givenbytearrays into one.static byte[]concatAll(byte[]... arrays) Concatenate the givenbytearrays into one.static byte[]extractBytes(ByteBuffer buffer) Extract/Transfer bytes from the givenByteBufferinto an array by duplicating the buffer and fetching its content.static ByteBuffergetByteBuffer(String theString) static ByteBuffergetByteBuffer(String theString, Charset charset) static byte[]getBytes(ByteBuffer byteBuffer) Extract a byte array fromByteBufferwithout consuming it.static intindexOf(byte[] haystack, byte needle) Searches the specified array of bytes for the specified value.static byte[][]mergeArrays(byte[] firstArray, byte[]... additionalArrays) Merge multiplebytearrays into one arraystatic byte[][]split(byte[] source, int c) Splitsourceinto partitioned arrays using delimiterc.static booleanstartsWith(byte[] haystack, byte[] prefix) Tests if thehaystackstarts with the givenprefix.static booleanstartsWith(byte[] haystack, byte[] prefix, int offset) Tests if thehaystackbeginning at the specifiedoffsetstarts with the givenprefix.
-
Method Details
-
concat
public static byte[] concat(byte[] array1, byte[] array2) Concatenate the givenbytearrays into one.The order of elements in the original arrays is preserved.
- Parameters:
array1- the first array.array2- the second array.- Returns:
- the new array.
-
concatAll
public static byte[] concatAll(byte[]... arrays) Concatenate the givenbytearrays into one. Returns a new, empty array ifarrayswas empty and returns the first array ifarrayscontains only a single array.The order of elements in the original arrays is preserved.
- Parameters:
arrays- the arrays.- Returns:
- the new array.
-
mergeArrays
public static byte[][] mergeArrays(byte[] firstArray, byte[]... additionalArrays) Merge multiplebytearrays into one array- Parameters:
firstArray- must not be nulladditionalArrays- must not be null- Returns:
- the merged array.
-
split
public static byte[][] split(byte[] source, int c) Splitsourceinto partitioned arrays using delimiterc.- Parameters:
source- the source array.c- delimiter.- Returns:
- the partitioned arrays.
-
getBytes
Extract a byte array fromByteBufferwithout consuming it. The resultingbyte[]is a copy of the buffer's contents and not updated upon changes within the buffer.- Parameters:
byteBuffer- must not be null.- Returns:
- Since:
- 2.0
-
startsWith
public static boolean startsWith(byte[] haystack, byte[] prefix) Tests if thehaystackstarts with the givenprefix.- Parameters:
haystack- the source to scan.prefix- the prefix to find.- Returns:
- true if
haystackat positionoffsetstarts withprefix. - Since:
- 1.8.10
- See Also:
-
startsWith
public static boolean startsWith(byte[] haystack, byte[] prefix, int offset) Tests if thehaystackbeginning at the specifiedoffsetstarts with the givenprefix.- Parameters:
haystack- the source to scan.prefix- the prefix to find.offset- the offset to start at.- Returns:
- true if
haystackat positionoffsetstarts withprefix. - Since:
- 1.8.10
-
indexOf
public static int indexOf(byte[] haystack, byte needle) Searches the specified array of bytes for the specified value. Returns the index of the first matching value in thehaystacks natural order or-1ofneedlecould not be found.- Parameters:
haystack- the source to scan.needle- the value to scan for.- Returns:
- index of first appearance, or -1 if not found.
- Since:
- 1.8.10
-
getByteBuffer
- Parameters:
theString- must not be null.- Returns:
- Since:
- 2.1
-
getByteBuffer
- Parameters:
theString- must not be null.charset- must not be null.- Returns:
- Since:
- 2.1
-
extractBytes
Extract/Transfer bytes from the givenByteBufferinto an array by duplicating the buffer and fetching its content.- Parameters:
buffer- must not be null.- Returns:
- the extracted bytes.
- Since:
- 2.1
-