Class ByteArrayHelper


  • @InternalApi
    public final class ByteArrayHelper
    extends java.lang.Object
    Helper methods for byte arrays.
    Since:
    3.0
    Author:
    Mark Rotteveel
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] emptyByteArray()  
      static byte[] fromBase64String​(java.lang.String base64)
      Decodes a base64 encoded string to a byte array.
      static byte[] fromBase64urlString​(java.lang.String base64url)
      Decodes a base64url encoded string to a byte array.
      static byte[] fromHexString​(java.lang.String hexString)
      Converts the provided hexadecimal string to a byte array.
      static int indexOf​(byte[] array, byte b)
      Returns the index of the first occurrence of b in array.
      static java.lang.String toHexString​(byte[] bytes)
      Converts the provided byte array to a hexadecimal string
      static void validateBufferLength​(byte[] b, int off, int len)
      Validates requested offset (off) and length (len) against the array (b).
      • Methods inherited from class java.lang.Object

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

      • toHexString

        public static java.lang.String toHexString​(byte[] bytes)
        Converts the provided byte array to a hexadecimal string

        Adapted from https://stackoverflow.com/a/21429909/466862 by higginse

        Parameters:
        bytes - byte array (not null
        Returns:
        String with the content of the byte array in hexadecimal.
      • fromHexString

        public static byte[] fromHexString​(java.lang.String hexString)
        Converts the provided hexadecimal string to a byte array.
        Parameters:
        hexString - Hexadecimal string
        Returns:
        byte array
        Since:
        4.0
      • fromBase64String

        public static byte[] fromBase64String​(java.lang.String base64)
        Decodes a base64 encoded string to a byte array.
        Parameters:
        base64 - Base64 encoded data
        Returns:
        byte array after decoding
      • fromBase64urlString

        public static byte[] fromBase64urlString​(java.lang.String base64url)
        Decodes a base64url encoded string to a byte array.

        Base64url applies the "URL and Filename safe" Base 64 Alphabet.

        Parameters:
        base64url - Base64url encoded data
        Returns:
        byte array after decoding
        Since:
        5
      • indexOf

        public static int indexOf​(byte[] array,
                                  byte b)
        Returns the index of the first occurrence of b in array.
        Parameters:
        array - Array to search
        b - byte to find
        Returns:
        the index of the first occurrence of b, or -1 if b is not in the array
        Since:
        5
      • emptyByteArray

        public static byte[] emptyByteArray()
        Returns:
        an empty array (length == 0)
      • validateBufferLength

        public static void validateBufferLength​(byte[] b,
                                                int off,
                                                int len)
                                         throws java.sql.SQLException
        Validates requested offset (off) and length (len) against the array (b).
        Parameters:
        b - array
        off - position in array
        len - length from off
        Throws:
        java.sql.SQLException - if off < 0, len < 0, or if off + len > b.length
        Since:
        5.0.8