Class Base64

java.lang.Object
cds.savot.binary.Base64

public final class Base64 extends 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 Details

    • encodeStr

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

      public static String encodeStr(String string, 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

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

      public static String decodeStr(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

      public static String decodeStr(String encoded, 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

      public static byte[] decode(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

      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).