Class Base64


  • public class Base64
    extends java.lang.Object
    Minimal Base64 utility. Will always encode with standard base64 encoding, no padding, and no wrapping, but will parse both URL_SAFE and standard base64 data (or even a mismatched mix).

    The code is optimized to have a low stack count, but be call safe using IllegalArgumentException on errors.

    It is a minimal port of the iharder Base64 utility class. Note: This library should only be used when you have a controlled environment where you use base64 encoding, and it needs to be as fast as possible. If you need more functionality around base64, use the native encoder or decoder.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] decode​(byte[] source)
      Decode the Base64-encoded data in input and return the data in a new byte array.
      static byte[] decode​(byte[] source, int off, int len)
      Decode the Base64-encoded data in input and return the data in a new byte array.
      static byte[] decode​(java.lang.String s)
      Decodes data from Base64 notation.
      static byte[] encode​(byte[] source)
      Encodes a byte array into Base64 string.
      static byte[] encode​(byte[] source, int off, int len)
      Encodes a byte array into Base64 data.
      static java.lang.String encodeToString​(byte[] source)
      Encodes a byte array into Base64 notation.
      static java.lang.String encodeToString​(byte[] source, int off, int len)
      Encodes a byte array into Base64 string.
      • Methods inherited from class java.lang.Object

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

      • encodeToString

        public static java.lang.String encodeToString​(byte[] source)
        Encodes a byte array into Base64 notation.
        Parameters:
        source - The data to convert
        Returns:
        The Base64-encoded data as a String
        Throws:
        java.lang.NullPointerException - if source array is null
        Since:
        2.0
      • encodeToString

        public static java.lang.String encodeToString​(byte[] source,
                                                      int off,
                                                      int len)
        Encodes a byte array into Base64 string.
        Parameters:
        source - The data to convert
        off - The data offset of what to encode.
        len - The number of bytes to encode.
        Returns:
        The Base64-encoded data as a String
        Throws:
        java.lang.NullPointerException - if source array is null
        Since:
        2.0
      • encode

        public static byte[] encode​(byte[] source,
                                    int off,
                                    int len)
        Encodes a byte array into Base64 data.
        Parameters:
        source - The data to convert
        off - The data offset of what to encode.
        len - The number of bytes to encode.
        Returns:
        The Base64-encoded data as a String
        Throws:
        java.lang.NullPointerException - if source array is null
        Since:
        2.0
      • encode

        public static byte[] encode​(byte[] source)
        Encodes a byte array into Base64 string.
        Parameters:
        source - The data to convert
        Returns:
        The Base64-encoded data.
        Throws:
        java.lang.NullPointerException - if source array is null
        Since:
        2.0
      • decode

        public static byte[] decode​(byte[] source)
        Decode the Base64-encoded data in input and return the data in a new byte array. The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.
        Parameters:
        source - The data to decode
        Returns:
        decoded data
      • decode

        public static byte[] decode​(byte[] source,
                                    int off,
                                    int len)
        Decode the Base64-encoded data in input and return the data in a new byte array. The padding '=' characters at the end are considered optional, but if any are present, there must be the correct number of them.
        Parameters:
        source - The data to decode
        off - The offset within the input array at which to start
        len - The number of byte sof input to decode.
        Returns:
        decoded data
      • decode

        public static byte[] decode​(java.lang.String s)
        Decodes data from Base64 notation.
        Parameters:
        s - the string to decode
        Returns:
        the decoded data
        Throws:
        java.lang.NullPointerException - if s is null