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, Guy Korland, John Blum
  • Method Details

    • concat

      public static byte[] concat(byte[] array1, byte[] array2)
      Concatenate the given byte arrays 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 given byte arrays into one. 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.
    • 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:
      the merged 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.
    • 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

      @Deprecated(since="3.2") public static byte[] extractBytes(ByteBuffer buffer)
      Deprecated.
      Since 3.2. Use getBytes(ByteBuffer) instead.
      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