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
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
concat
(byte[] array1, byte[] array2) Concatenate the givenbyte
arrays into one, with overlapping array elements included twice.static byte[]
concatAll
(byte[]... arrays) Concatenate the givenbyte
arrays into one, with overlapping array elements included twice.static byte[]
extractBytes
(ByteBuffer buffer) Extract/Transfer bytes from the givenByteBuffer
into an array by duplicating the buffer and fetching its content.static ByteBuffer
getByteBuffer
(String theString) static ByteBuffer
getByteBuffer
(String theString, Charset charset) static byte[]
getBytes
(ByteBuffer byteBuffer) Extract a byte array fromByteBuffer
without consuming it.static int
indexOf
(byte[] haystack, byte needle) Searches the specified array of bytes for the specified value.static byte[][]
mergeArrays
(byte[] firstArray, byte[]... additionalArrays) Merge multiplebyte
arrays into one arraystatic byte[][]
split
(byte[] source, int c) Splitsource
into partitioned arrays using delimiterc
.static boolean
startsWith
(byte[] haystack, byte[] prefix) Tests if thehaystack
starts with the givenprefix
.static boolean
startsWith
(byte[] haystack, byte[] prefix, int offset) Tests if thehaystack
beginning at the specifiedoffset
starts with the givenprefix
.
-
Method Details
-
concat
public static byte[] concat(byte[] array1, byte[] array2) Concatenate the givenbyte
arrays into one, with overlapping array elements included twice.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 givenbyte
arrays into one, with overlapping array elements included twice. Returns a new, empty array ifarrays
was empty and returns the first array ifarrays
contains only a single array.The order of elements in the original arrays is preserved.
- Parameters:
arrays
- the arrays.- Returns:
- the new array.
-
split
public static byte[][] split(byte[] source, int c) Splitsource
into partitioned arrays using delimiterc
.- Parameters:
source
- the source array.c
- delimiter.- Returns:
- the partitioned arrays.
-
mergeArrays
public static byte[][] mergeArrays(byte[] firstArray, byte[]... additionalArrays) Merge multiplebyte
arrays into one array- Parameters:
firstArray
- must not be nulladditionalArrays
- must not be null- Returns:
-
getBytes
Extract a byte array fromByteBuffer
without 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 thehaystack
starts with the givenprefix
.- Parameters:
haystack
- the source to scan.prefix
- the prefix to find.- Returns:
- true if
haystack
at positionoffset
starts withprefix
. - Since:
- 1.8.10
- See Also:
-
startsWith
public static boolean startsWith(byte[] haystack, byte[] prefix, int offset) Tests if thehaystack
beginning at the specifiedoffset
starts with the givenprefix
.- Parameters:
haystack
- the source to scan.prefix
- the prefix to find.offset
- the offset to start at.- Returns:
- true if
haystack
at positionoffset
starts 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 thehaystack
s natural order or-1
ofneedle
could 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 givenByteBuffer
into an array by duplicating the buffer and fetching its content.- Parameters:
buffer
- must not be null.- Returns:
- the extracted bytes.
- Since:
- 2.1
-