Class ByteUtils

java.lang.Object
org.springframework.data.redis.util.ByteUtils

public final class ByteUtils extends Object
Some handy methods for dealing with byte arrays.
Since:
1.7
Author:
Christoph Strobl, Mark Paluch
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    concat(byte[] array1, byte[] array2)
    Concatenate the given byte arrays into one, with overlapping array elements included twice.
    static byte[]
    concatAll(byte[]... arrays)
    Concatenate the given byte arrays into one, with overlapping array elements included twice.
    static byte[]
    Extract/Transfer bytes from the given ByteBuffer into an array by duplicating the buffer and fetching its content.
    static ByteBuffer
    getByteBuffer(String theString)
    Convert a String into a ByteBuffer using StandardCharsets.UTF_8.
    static ByteBuffer
    getByteBuffer(String theString, Charset charset)
    Convert a String into a ByteBuffer using the given Charset.
    static byte[]
    getBytes(ByteBuffer byteBuffer)
    Extract a byte array from ByteBuffer 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 multiple byte arrays into one array
    static byte[][]
    split(byte[] source, int c)
    Split source into partitioned arrays using delimiter c.
    static boolean
    startsWith(byte[] haystack, byte[] prefix)
    Tests if the haystack starts with the given prefix.
    static boolean
    startsWith(byte[] haystack, byte[] prefix, int offset)
    Tests if the haystack beginning at the specified offset starts with the given prefix.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • concat

      public static byte[] concat(byte[] array1, byte[] array2)
      Concatenate the given byte 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 given byte arrays into one, with overlapping array elements included twice. Returns a new, empty array if arrays was empty and returns the first array if arrays 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)
      Split source into partitioned arrays using delimiter c.
      Parameters:
      source - the source array.
      c - delimiter.
      Returns:
      the partitioned arrays.
    • mergeArrays

      public static byte[][] mergeArrays(byte[] firstArray, byte[]... additionalArrays)
      Merge multiple byte arrays into one array
      Parameters:
      firstArray - must not be null
      additionalArrays - must not be null
      Returns:
    • getBytes

      public static byte[] getBytes(ByteBuffer byteBuffer)
      Extract a byte array from ByteBuffer without consuming it. The resulting byte[] 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 the haystack starts with the given prefix.
      Parameters:
      haystack - the source to scan.
      prefix - the prefix to find.
      Returns:
      true if haystack at position offset starts with prefix.
      Since:
      1.8.10
      See Also:
    • startsWith

      public static boolean startsWith(byte[] haystack, byte[] prefix, int offset)
      Tests if the haystack beginning at the specified offset starts with the given prefix.
      Parameters:
      haystack - the source to scan.
      prefix - the prefix to find.
      offset - the offset to start at.
      Returns:
      true if haystack at position offset starts with prefix.
      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 the haystacks natural order or -1 of needle 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

      public static ByteBuffer getByteBuffer(String theString)
      Convert a String into a ByteBuffer using StandardCharsets.UTF_8.
      Parameters:
      theString - must not be null.
      Returns:
      Since:
      2.1
    • getByteBuffer

      public static ByteBuffer getByteBuffer(String theString, Charset charset)
      Convert a String into a ByteBuffer using the given Charset.
      Parameters:
      theString - must not be null.
      charset - must not be null.
      Returns:
      Since:
      2.1
    • extractBytes

      public static byte[] extractBytes(ByteBuffer buffer)
      Extract/Transfer bytes from the given ByteBuffer into an array by duplicating the buffer and fetching its content.
      Parameters:
      buffer - must not be null.
      Returns:
      the extracted bytes.
      Since:
      2.1