Class Base64


  • public final class Base64
    extends Object
    Provides RFC 4648 / RFC 2045 compatible Base64 encoding and decoding. The implementation is based on Base64. The normal methods use the MIME decoder for compatibility with the permissive decoding of the previous implementation. All invalid characters and newlines in the input are ignored. Use the new decodeStrict(byte[]) or decodeStrict(String) methods if strict decoding is preferred, for example in security-sensitive uses.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static byte[] decode​(byte[] sArr)
      Decodes a BASE64 encoded byte array.
      static byte[] decode​(char[] sArr)
      Decodes a BASE64 encoded char array.
      static byte[] decode​(String str)
      Decodes a BASE64 encoded String.
      static byte[] decodeFast​(byte[] sArr)
      Deprecated.
      Use decode(byte[]) instead.
      static byte[] decodeFast​(char[] sArr)
      Deprecated.
      Use decode(char[]) instead.
      static byte[] decodeFast​(String s)
      Deprecated.
      Use decode(String) instead.
      static byte[] decodeStrict​(byte[] encoded)
      Decodes the input using a strict decoder that rejects any invalid characters or line separators.
      static byte[] decodeStrict​(char[] encoded)
      Decodes the input using a strict decoder that rejects any invalid characters or line separators.
      static byte[] decodeStrict​(String encoded)
      Decodes the input using a strict decoder that rejects any invalid characters or line separators.
      static String encode​(byte[] content)
      This method is using encode(byte[], boolean), and it only exists so we don't break the API.
      static String encode​(byte[] data, boolean lineSep)
      Encodes a raw byte array into a BASE64 String representation i accordance with RFC 2045.
      static byte[] encodeToByte​(byte[] data, boolean lineSep)
      Encodes a raw byte array into a BASE64 byte[] representation i accordance with RFC 2045.
      static char[] encodeToChar​(byte[] sArr, boolean lineSep)
      Encodes a raw byte array into a BASE64 char[] representation i accordance with RFC 2045.
    • Method Detail

      • decode

        public static byte[] decode​(byte[] sArr)
        Decodes a BASE64 encoded byte array. All illegal characters will be ignored and can handle both arrays with and without line separators. Use decodeStrict(byte[]) if rejection of invalid characters is required.
        Parameters:
        sArr - The source array. Length 0 will return an empty array. null will throw an exception. Returns null if the padding is incorrect.
        Returns:
        The decoded array of bytes. May be of length 0.
      • decode

        public static byte[] decode​(char[] sArr)
        Decodes a BASE64 encoded char array. All illegal characters will be ignored and can handle both arrays with and without line separators. Use decodeStrict(char[]) if rejection of invalid characters is required.
        Parameters:
        sArr - The source array. null or length 0 will return an empty array.
        Returns:
        The decoded array of bytes. May be of length 0. Returns null if the padding is incorrect.
      • decode

        public static byte[] decode​(String str)
        Decodes a BASE64 encoded String. All illegal characters will be ignored and can handle both strings with and without line separators. Use decodeStrict(String) if rejection of invalid characters is required.
        Parameters:
        str - The source string. null or length 0 will return an empty array.
        Returns:
        The decoded array of bytes. May be of length 0. Returns null if the padding is incorrect.
      • decodeFast

        @Deprecated
        public static byte[] decodeFast​(byte[] sArr)
        Deprecated.
        Use decode(byte[]) instead.
        This method is now a direct synonym for decode(byte[]).
        Parameters:
        sArr - The source array. Length 0 will return an empty array. null will throw an exception.
        Returns:
        The decoded array of bytes. May be of length 0.
      • decodeFast

        @Deprecated
        public static byte[] decodeFast​(char[] sArr)
        Deprecated.
        Use decode(char[]) instead.
        This method is now a direct synonym for decode(char[]).
        Parameters:
        sArr - The source array. Length 0 will return an empty array. null will throw an exception.
        Returns:
        The decoded array of bytes. May be of length 0.
      • decodeFast

        @Deprecated
        public static byte[] decodeFast​(String s)
        Deprecated.
        Use decode(String) instead.
        This method is now a direct synonym for decode(String).
        Parameters:
        s - The source string. Length 0 will return an empty array. null will throw an exception.
        Returns:
        The decoded array of bytes. May be of length 0.
      • decodeStrict

        public static byte[] decodeStrict​(byte[] encoded)
        Decodes the input using a strict decoder that rejects any invalid characters or line separators.
        Parameters:
        encoded - the encoded data.
        Returns:
        the decoded data.
        Throws:
        IllegalArgumentException - if the data is malformed in any way.
      • decodeStrict

        public static byte[] decodeStrict​(char[] encoded)
        Decodes the input using a strict decoder that rejects any invalid characters or line separators.
        Parameters:
        encoded - the encoded data.
        Returns:
        the decoded data.
        Throws:
        IllegalArgumentException - if the data is malformed in any way.
      • decodeStrict

        public static byte[] decodeStrict​(String encoded)
        Decodes the input using a strict decoder that rejects any invalid characters or line separators.
        Parameters:
        encoded - the encoded data.
        Returns:
        the decoded data.
        Throws:
        IllegalArgumentException - if the data is malformed in any way.
      • encode

        public static String encode​(byte[] content)
        This method is using encode(byte[], boolean), and it only exists so we don't break the API.
        Parameters:
        content - The bytearray that needs to be Base64 encoded
        Returns:
        the Base64 encoded
      • encode

        public static String encode​(byte[] data,
                                    boolean lineSep)
        Encodes a raw byte array into a BASE64 String representation i accordance with RFC 2045.
        Parameters:
        data - The bytes to convert. If null or length 0 an empty string will be returned.
        lineSep - Optional "\r\n" after 76 characters, unless end of file.
        No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a little faster.
        Returns:
        A BASE64 encoded array. Never null.
      • encodeToByte

        public static byte[] encodeToByte​(byte[] data,
                                          boolean lineSep)
        Encodes a raw byte array into a BASE64 byte[] representation i accordance with RFC 2045.
        Parameters:
        data - The bytes to convert. If null or length 0 an empty array will be returned.
        lineSep - Optional "\r\n" after 76 characters, unless end of file.
        No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a little faster.
        Returns:
        A BASE64 encoded array. Never null.
      • encodeToChar

        public static char[] encodeToChar​(byte[] sArr,
                                          boolean lineSep)
        Encodes a raw byte array into a BASE64 char[] representation i accordance with RFC 2045.
        Parameters:
        sArr - The bytes to convert. If null or length 0 an empty array will be returned.
        lineSep - Optional "\r\n" after 76 characters, unless end of file.
        No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a little faster.
        Returns:
        A BASE64 encoded array. Never null.