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 HerongYang.com. All Rights Reserved.
 */
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

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