Sun Implementation of Base64 in Java - Test

This section provides a test program for the Java implementation of the Base64 encoding algorithm by the Sun.

To test Java implementation of the Base64 encoding algorithm listed in the previous section, I wrote this simple testing program:

/* SunBase64Test.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import sunlabs.brazil.util.*;
class SunBase64Test {
   public static void main(String[] a) {
      if (a.length<1) {
         System.out.println("Usage:");
         System.out.println("java SunBase64Test 1/2/3");
         return;
      }
      String test = a[0];
      String theInput = null;
      String theExpected = null;
      if (test.equals("1")) {
         theInput = "A";
         theExpected = "QQ==";
      } else if (test.equals("2")) {
         theInput = "AB";
         theExpected = "QUI=";
      } else if (test.equals("3")) {
         theInput = "ABC";
         theExpected = "QUJD";
      } else {
         System.out.println("Usage:");
         System.out.println("java SunBase64Test 1/2/3");
         return;
      }
      String theEncoded = Base64.encode(theInput.getBytes());
      byte[] theDecoded = Base64.decode(theEncoded);
      System.out.println("Input   : "+theInput);
      System.out.println("Encoded : "+theEncoded);
      System.out.println("Expected: "+theExpected);
      System.out.println("Decoded : "+new String(theDecoded));
      return;
   }
}

Here is the test result:

herong> javac SunBase64Test.java

herong> java SunBase64Test 1
Input   : A
Encoded : QQ==
Expected: QQ==
Decoded : A

herong> java SunBase64Test 2
Input   : AB
Encoded : QUI=
Expected: QUI=
Decoded : AB

herong> java SunBase64Test 3
Input   : ABC
Encoded : QUJD
Expected: QUJD
Decoded : ABC

The result matches my expectation perfectly.

Table of Contents

 About This Book

Base64 Encoding

 Base64 Encoding Algorithm

 RFC 1421 - Privacy Enhancement for Email

 RFC 1521 - MIME (Multipurpose Internet Mail Extensions)

 W3C Implementation of Base64 in Java

 Sun Implementation of Base64 in Java

Sun Implementation of Base64 in Java - Test

 Goetz' Implementation of Base64 in JavaScript

 Goetz' Implementation of Base64 in JavaScript - Test

 Base64 Encoding and Decoding Tools

 Base64URL - URL Safe Base64 Encoding

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 UUEncode Encoding

 References

 Full Version in PDF/EPUB