W3C Implementation of Base64 in Java

This section provides a test program for the Java implementation of Base64 by W3C. Tests of both encoder and decoder are provided.

One of the Java implementations of Base64 algorithm available on the Internet is from the Jigsaw project at w3c.org. The Base64 algorithm is implemented into 2 classes, Base64Encoder and Base64Decoder in the org.w3c.tools.codec package. Here is how to download this package, and make it available to your Java environment.

The following program, W3CBase64.java, shows how to use the W3C implementation.

/**
 * W3CBase64.java
 * Copyright (c) 2002 by Dr. Herong Yang
 */
import java.io.*;
import org.w3c.tools.codec.*;
class W3CBase64 {
   public static void main(String[] a) {
      String action = a[0];
      String inFile = a[1];
      String outFile = a[2];
      if (action.equals("encode")) {
         encode(inFile,outFile);
      } else if (action.equals("decode")) {
         decode(inFile,outFile);
      } else {
        System.out.println("Please invoke the program with:");
        System.out.println("   java W3CBase64 encode/decode in out");
      }
   }
   private static void encode(String inFile, String outFile) {
      try {
         InputStream in = new FileInputStream(inFile);
         OutputStream out = new FileOutputStream(outFile);
         Base64Encoder encoder = new Base64Encoder(in,out);
         encoder.process();
         in.close();
         out.close();
      } catch (IOException e) {
         System.out.println(e.toString());
      }
   }
   private static void decode(String inFile, String outFile) {
      try {
         InputStream in = new FileInputStream(inFile);
         OutputStream out = new FileOutputStream(outFile);
         Base64Decoder decoder = new Base64Decoder(in,out);
         decoder.process();
         in.close();
         out.close();
      } catch (IOException e) {
         System.out.println(e.toString());
      } catch (Base64FormatException e) {
         System.out.println(e.toString());
      }
   }
}

Let's try this program by encoding and decoding the Java class file produced by the Java compiler:

javac W3CBase64.java
java W3CBase64 encode W3CBase64.class W3CBase64.b64
type W3CBase64.b64
java W3CBase64 decode W3CBase64.b64 W3CBase64.cls
del W3CBase64.class
ren W3CBase64.cls W3CBase64.class
java W3CBase64 abc in out

As you can see from the "type W3CBase64.b64" command, the encoded output is nicely formatted with 76 characters per line.

By running the decoded version of the class file at the end, you know that the encoding and decoding implementation works fine.

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