Class Base64Util


  • public class Base64Util
    extends Object
    The Base64Util class provides a number of Base-64 related utility functions.
    Author:
    Robert Harder, Marcus Portmann
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte BAD_ENCODING
      Indicates error in encoding.
      static boolean COMPRESS
      Specify that data should be compressed (value is true).
      static boolean DECODE
      Specify decoding (value is false).
      static boolean DONT_COMPRESS
      Specify that data should not be compressed (value is false).
      static boolean ENCODE
      Specify encoding (value is true).
      static byte EQUALS_SIGN_ENC
      Indicates equals sign in encoding.
      static byte WHITE_SPACE_ENC
      Indicates white space in encoding.
    • Field Detail

      • BAD_ENCODING

        public static final byte BAD_ENCODING
        Indicates error in encoding.
      • COMPRESS

        public static final boolean COMPRESS
        Specify that data should be compressed (value is true).
        See Also:
        Constant Field Values
      • DECODE

        public static final boolean DECODE
        Specify decoding (value is false).
        See Also:
        Constant Field Values
      • DONT_COMPRESS

        public static final boolean DONT_COMPRESS
        Specify that data should not be compressed (value is false).
        See Also:
        Constant Field Values
      • ENCODE

        public static final boolean ENCODE
        Specify encoding (value is true).
        See Also:
        Constant Field Values
      • EQUALS_SIGN_ENC

        public static final byte EQUALS_SIGN_ENC
        Indicates equals sign in encoding.
      • WHITE_SPACE_ENC

        public static final byte WHITE_SPACE_ENC
        Indicates white space in encoding.
    • Method Detail

      • decode

        public static byte[] decode​(String s)
        Decodes data from Base64Util notation.
        Parameters:
        s - the string to decode
        Returns:
        the decoded data
        Since:
        1.4
      • decode

        public static byte[] decode​(byte[] source,
                                    int off,
                                    int len)
        Decodes Base64Util content in byte array format and returns the decoded byte array.
        Parameters:
        source - the Base64Util encoded data
        off - the offset of where to begin decoding
        len - the length of characters to decode
        Returns:
        decoded data
        Since:
        1.3
      • decodeToObject

        public static Optional<Object> decodeToObject​(String encodedObject)
        Attempts to decode Base64Util data and deserialize a Java Object within. Returns null if there was an error.
        Parameters:
        encodedObject - the Base64Util data to decode
        Returns:
        an Optional containing the decoded and deserialized object or an empty Optional if there was an error
        Since:
        1.4
      • decodeToString

        public static String decodeToString​(String s)
        Decodes data from Base64Util notation and returns it as a string. Equivlaent to calling new String(decode(s))
        Parameters:
        s - the string to decode
        Returns:
        the data as a string
        Since:
        1.4
      • encodeBytes

        public static String encodeBytes​(byte[] source)
        Encodes a byte array into Base64Util notation. Equivalent to calling encodeBytes(source, 0, source.length)
        Parameters:
        source - the data to convert
        Returns:
        the Base64Util encoded string
        Since:
        1.4
      • encodeBytes

        public static String encodeBytes​(byte[] source,
                                         boolean breakLines)
        Encodes a byte array into Base64Util notation. Equivalent to calling encodeBytes(source, 0, source.length)
        Parameters:
        source - the data to convert
        breakLines - break lines at 80 characters or less
        Returns:
        the Base64Util encoded bytes
        Since:
        1.4
      • encodeBytes

        public static String encodeBytes​(byte[] source,
                                         int off,
                                         int len)
        Encodes a byte array into Base64Util notation.
        Parameters:
        source - the data to convert
        off - offset in array where conversion should begin
        len - length of data to convert
        Returns:
        the Base64Util encoded bytes
        Since:
        1.4
      • encodeBytes

        public static String encodeBytes​(byte[] source,
                                         int off,
                                         int len,
                                         boolean breakLines)
        Encodes a byte array into Base64Util notation.
        Parameters:
        source - the data to convert
        off - offset in array where conversion should begin
        len - length of data to convert
        breakLines - break lines at 80 characters or less
        Returns:
        the Base64Util encoded bytes
        Since:
        1.4
      • encodeString

        public static String encodeString​(String s)
        Encodes a string in Base64Util notation with line breaks after every 75 Base64Util characters. Of course you probably only need to encode a string if there are non-ASCII characters in it such as many non-English languages.
        Parameters:
        s - the string to encode
        Returns:
        the encoded string
        Since:
        1.3
      • encodeString

        public static String encodeString​(String s,
                                          boolean breakLines)
        Encodes a string in Base64Util notation with line breaks after every 75 Base64Util characters. Of course you probably only need to encode a string if there are non-ASCII characters in it such as many non-English languages.
        Parameters:
        s - the string to encode
        breakLines - break lines at 80 characters or less
        Returns:
        the encoded string
        Since:
        1.3