Class Base64


  • public final class Base64
    extends java.lang.Object

    Lets encoding and decoding String with the Base64.

    Note: To encode/decode stream of data, you can also use Base64InputStream and Base64OutputStream.

    Examples:

     public final static void main(final String[] args) throws Exception {
            String message = "Hi ! If you can read this, the Base64 encoding/decoding has completely worked ! Well done ;-) !";
     
            System.out.println("ORIGINAL MESSAGE:\n\""+message+"\"");
     
            String encoded, decoded;
     
            System.out.println("\nEncoding....");
            encoded = Base64.encodeStr(message);
            System.out.println("ENCODED MESSAGE:\n\""+encoded+"\"");
     
            System.out.println("\nDecoding....");
            decoded = Base64.decodeStr(encoded);
            System.out.println("DECODED MESSAGE:\n\""+decoded+"\"");
     }
     
    Since:
    09/2011
    Author:
    Gregory Mantelet (CDS)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] decode​(char[] encoded)
      Decodes the given string which is supposed to be encoded in Base64.
      static byte[] decode​(java.lang.String encoded)
      Decodes the given string which is supposed to be encoded in Base64.
      static java.lang.String decodeStr​(java.lang.String encoded)
      Decodes the given string which is supposed to be encoded in Base64.
      static java.lang.String decodeStr​(java.lang.String encoded, java.lang.String charset)
      Decodes the given string which is supposed to be encoded in Base64.
      static java.lang.String encode​(byte[] byteArray)
      Encodes the given bytes array in Base64 characters.
      static java.lang.String encodeStr​(java.lang.String string)
      Encodes the given string in Base64.
      static java.lang.String encodeStr​(java.lang.String string, java.lang.String charset)
      Encodes the given string in Base64.
      • Methods inherited from class java.lang.Object

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

      • encodeStr

        public static java.lang.String encodeStr​(java.lang.String string)
        Encodes the given string in Base64.
        Parameters:
        string - The string to encode.
        Returns:
        Its Base64 encoding.
        See Also:
        encodeStr(String, String)
      • encodeStr

        public static java.lang.String encodeStr​(java.lang.String string,
                                                 java.lang.String charset)
        Encodes the given string in Base64.
        Parameters:
        string - The string to encode (string supposed to be encoded with the given charset).
        charset - The name of a supported charset (i.e. 'UTF-8').
        Returns:
        Its Base64 encoding.
        See Also:
        encode(byte[])
      • encode

        public static java.lang.String encode​(byte[] byteArray)
        Encodes the given bytes array in Base64 characters.
        Parameters:
        byteArray - Data to encode.
        Returns:
        The encoded data.
      • decodeStr

        public static java.lang.String decodeStr​(java.lang.String encoded)
                                          throws Base64Exception
        Decodes the given string which is supposed to be encoded in Base64.
        Parameters:
        encoded - Message to decode.
        Returns:
        The decoded message.
        Throws:
        Base64Exception - If the encoded message is corrupted (that's to say: not conform to the Base64 encoding).
        See Also:
        decodeStr(String, String)
      • decodeStr

        public static java.lang.String decodeStr​(java.lang.String encoded,
                                                 java.lang.String charset)
                                          throws Base64Exception
        Decodes the given string which is supposed to be encoded in Base64.
        Parameters:
        encoded - Message to decode.
        charset - The name of a supported charset.
        Returns:
        The decoded message (string encoded using the given charset).
        Throws:
        Base64Exception - If the encoded message is corrupted (that's to say: not conform to the Base64 encoding).
        See Also:
        decode(String)
      • decode

        public static byte[] decode​(java.lang.String encoded)
                             throws Base64Exception
        Decodes the given string which is supposed to be encoded in Base64.
        Parameters:
        encoded - Data to decode.
        Returns:
        The decoded data.
        Throws:
        Base64Exception - If the encoded data are corrupted (that's to say: not conform to the Base64 encoding).
        See Also:
        decode(char[])
      • decode

        public static byte[] decode​(char[] encoded)
                             throws Base64Exception
        Decodes the given string which is supposed to be encoded in Base64.
        Parameters:
        encoded - Data to decode.
        Returns:
        The decoded data.
        Throws:
        Base64Exception - If the encoded data are corrupted (that's to say: not conform to the Base64 encoding).