Class Base64


  • public class Base64
    extends java.lang.Object
    Provides Base64 encoding and decoding as defined by RFC 2045.

    This class implements section 6.8. Base64 Content-Transfer-Encoding from RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies by Freed and Borenstein.

    Since:
    1.0-dev
    Version:
    $Id$
    See Also:
    RFC 2045
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static byte[] base64Alphabet
      Contains the Base64 values 0 through 63 accessed by using character encodings as indices.
      (package private) static int BASELENGTH
      The base length.
      (package private) static byte[] CHUNK_SEPARATOR
      Chunk separator per RFC 2045 section 2.1.
      (package private) static int CHUNK_SIZE
      Chunk size per RFC 2045 section 6.8.
      (package private) static int EIGHTBIT
      Used to calculate the number of bits in a byte.
      (package private) static int FOURBYTE
      Used to get the number of Quadruples.
      private static byte[] lookUpBase64Alphabet
      Contains the Base64 encodings A through Z, followed by a through z, followed by 0 through 9, followed by +, and /.
      (package private) static int LOOKUPLENGTH
      Lookup length.
      (package private) static byte PAD
      Byte used to pad output.
      (package private) static int SIGN
      Used to test the sign of a byte.
      (package private) static int SIXTEENBIT
      Used when encoding something which has fewer than 24 bits.
      (package private) static int TWENTYFOURBITGROUP
      Used to determine how many bits data contains.
    • Constructor Summary

      Constructors 
      Constructor Description
      Base64()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] decode​(byte[] pArray)
      Decodes a byte[] containing containing characters in the Base64 alphabet.
      java.lang.Object decode​(java.lang.Object pObject)
      Decodes an Object using the base64 algorithm.
      static byte[] decodeBase64​(byte[] base64Data)
      Decodes Base64 data into octects
      (package private) static byte[] discardNonBase64​(byte[] data)
      Discards any characters outside of the base64 alphabet, per the requirements on page 25 of RFC 2045 - "Any characters outside of the base64 alphabet are to be ignored in base64 encoded data."
      (package private) static byte[] discardWhitespace​(byte[] data)
      Discards any whitespace from a base-64 encoded block.
      byte[] encode​(byte[] pArray)
      Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
      java.lang.Object encode​(java.lang.Object pObject)
      Encodes an Object using the base64 algorithm.
      static byte[] encodeBase64​(byte[] binaryData)
      Encodes binary data using the base64 algorithm but does not chunk the output.
      static byte[] encodeBase64​(byte[] binaryData, boolean isChunked)
      Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
      static byte[] encodeBase64Chunked​(byte[] binaryData)
      Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
      static boolean isArrayByteBase64​(byte[] arrayOctect)
      Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
      static boolean isBase64​(byte b)
      Returns whether or not the octect is in the base 64 alphabet.
      • Methods inherited from class java.lang.Object

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

      • CHUNK_SIZE

        static final int CHUNK_SIZE
        Chunk size per RFC 2045 section 6.8.

        The character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.

        See Also:
        RFC 2045 section 6.8, Constant Field Values
      • CHUNK_SEPARATOR

        static final byte[] CHUNK_SEPARATOR
        Chunk separator per RFC 2045 section 2.1.
        See Also:
        RFC 2045 section 2.1
      • EIGHTBIT

        static final int EIGHTBIT
        Used to calculate the number of bits in a byte.
        See Also:
        Constant Field Values
      • SIXTEENBIT

        static final int SIXTEENBIT
        Used when encoding something which has fewer than 24 bits.
        See Also:
        Constant Field Values
      • TWENTYFOURBITGROUP

        static final int TWENTYFOURBITGROUP
        Used to determine how many bits data contains.
        See Also:
        Constant Field Values
      • FOURBYTE

        static final int FOURBYTE
        Used to get the number of Quadruples.
        See Also:
        Constant Field Values
      • base64Alphabet

        private static byte[] base64Alphabet
        Contains the Base64 values 0 through 63 accessed by using character encodings as indices.

        For example, base64Alphabet['+'] returns 62.

        The value of undefined encodings is -1.

      • lookUpBase64Alphabet

        private static byte[] lookUpBase64Alphabet

        Contains the Base64 encodings A through Z, followed by a through z, followed by 0 through 9, followed by +, and /.

        This array is accessed by using character values as indices.

        For example, lookUpBase64Alphabet[62] returns '+'.

    • Constructor Detail

      • Base64

        public Base64()
    • Method Detail

      • isBase64

        public static boolean isBase64​(byte b)
        Returns whether or not the octect is in the base 64 alphabet.
        Parameters:
        b - The value to test
        Returns:
        true if the value is defined in the the base 64 alphabet, false otherwise.
      • isArrayByteBase64

        public static boolean isArrayByteBase64​(byte[] arrayOctect)
        Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
        Parameters:
        arrayOctect - byte array to test
        Returns:
        true if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; false, otherwise
      • encodeBase64

        public static byte[] encodeBase64​(byte[] binaryData)
        Encodes binary data using the base64 algorithm but does not chunk the output.
        Parameters:
        binaryData - binary data to encode
        Returns:
        Base64 characters
      • encodeBase64Chunked

        public static byte[] encodeBase64Chunked​(byte[] binaryData)
        Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
        Parameters:
        binaryData - binary data to encode
        Returns:
        Base64 characters chunked in 76 character blocks
      • decode

        public java.lang.Object decode​(java.lang.Object pObject)
                                throws java.lang.IllegalArgumentException
        Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[].
        Parameters:
        pObject - Object to decode
        Returns:
        An object (of type byte[]) containing the binary data which corresponds to the byte[] supplied.
        Throws:
        java.lang.IllegalArgumentException - if the parameter supplied is not of type byte[]
      • decode

        public byte[] decode​(byte[] pArray)
        Decodes a byte[] containing containing characters in the Base64 alphabet.
        Parameters:
        pArray - A byte array containing Base64 character data
        Returns:
        a byte array containing binary data
      • encodeBase64

        public static byte[] encodeBase64​(byte[] binaryData,
                                          boolean isChunked)
        Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
        Parameters:
        binaryData - Array containing binary data to encode.
        isChunked - if true this encoder will chunk the base64 output into 76 character blocks
        Returns:
        Base64-encoded data.
      • decodeBase64

        public static byte[] decodeBase64​(byte[] base64Data)
        Decodes Base64 data into octects
        Parameters:
        base64Data - Byte array containing Base64 data
        Returns:
        Array containing decoded data.
      • discardWhitespace

        static byte[] discardWhitespace​(byte[] data)
        Discards any whitespace from a base-64 encoded block.
        Parameters:
        data - The base-64 encoded data to discard the whitespace from.
        Returns:
        The data, less whitespace (see RFC 2045).
      • discardNonBase64

        static byte[] discardNonBase64​(byte[] data)
        Discards any characters outside of the base64 alphabet, per the requirements on page 25 of RFC 2045 - "Any characters outside of the base64 alphabet are to be ignored in base64 encoded data."
        Parameters:
        data - The base-64 encoded data to groom
        Returns:
        The data, less non-base64 characters (see RFC 2045).
      • encode

        public java.lang.Object encode​(java.lang.Object pObject)
                                throws java.lang.IllegalArgumentException
        Encodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the Encoder interface, and will throw an EncoderException if the supplied object is not of type byte[].
        Parameters:
        pObject - Object to encode
        Returns:
        An object (of type byte[]) containing the base64 encoded data which corresponds to the byte[] supplied.
        Throws:
        java.lang.IllegalArgumentException - if the parameter supplied is not of type byte[]
      • encode

        public byte[] encode​(byte[] pArray)
        Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
        Parameters:
        pArray - a byte array containing binary data
        Returns:
        A byte array containing only Base64 character data