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) 2007 by Dr. Herong Yang
 */
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:

C:\herong\base64>javac SunBase64Test.java

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

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

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

The result matches my expectation perfectly.

Table of Contents

 About This Book

 UUEncode Encoding

Base64 Encoding

 Base64 Encoding Algorithm

 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

 Implementation of Base64 in PHP - Test

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 References

 PDF Printing Version