Saturday, 16 July 2011

Base64 Encoding with Apache and Sun Libraries

Base64 is a encoding scheme that is commonly used to create a textual representation of binary data. This is usually needed for either transmission of that data via a text based medium such as email, or storage in a text base repository. I don't intend to go into great depth on this as there is a full explanation on wikipedia.

There are currently large number of open source libraries that offer Base64 encoding; the first one I ever used was written by Sun and was from their undocumented sun.misc package. It still comes with a warning that you shouldn’t rely in it as it may be removed at any time; however, it has been around since Java 1.1 (I think) and is still there. It’s a very simple API to use as the code below demonstrates:

public class SunMiscBase64Tester {

 
/**
   * Main method
   *
   *
@param args Not used.
   */
 
public static void main(String[] args) throws IOException {

   
System.out.println("== Start ==");
    System.out.println
("Sun Codec Base 64 tester...");

    String toCode =
"{This is my test string}";
    String coded = encoder
(toCode);
    String decoded = decoder
(coded);

    System.out.println
("Coded: " + toCode + " with value " + coded
        +
" ad was decoded to: " + decoded);
    System.out.println
("== End ==");
 
}

 
public static String encoder(String param) throws UnsupportedEncodingException {

   
// Convert the string to UTF8 bytes for md5 input
   
byte[] inputBytes = param.getBytes("UTF8");

    BASE64Encoder encoder =
new BASE64Encoder();
   
return encoder.encode(inputBytes);
 
}

 
public static String decoder(String param) throws IOException {

   
BASE64Decoder decoder = new BASE64Decoder();
   
byte[] decoded = decoder.decodeBuffer(param);
   
return new String(decoded);
 
}
}

According to the Apache Commons people, the one that you really should use can be found in the Apache Commons libraries under the Apache Commons Codec project, the impetus for which is to try to focus development on one definitive implementation of Base64. This sounds like a good idea.

If you want to use this library, then you’ll need to add the following dependency to your Maven POM file:

<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.5</version>
 </dependency>

At the time of writing this blog, the latest version is 1.5, though check Maven Central for updates.

Like the sun.misc package, it too is very simple to use. In this case, the base64 translation is done using the org.apache.commons.codec.binary.Base64 static utility class.


public class ApacheBase64Tester {

 
/**
   * Main method
   *
   *
@param args Not used.
   */
 
public static void main(String[] args) throws UnsupportedEncodingException {

   
System.out.println("== Start ==");
    System.out.println
("Apache Codec Base 64 tester...");

    String toCode =
"{This is my test string}";
    String coded = encoder
(toCode);
    String decoded = decoder
(coded);

    System.out.println
("Coded: " + toCode + " with value " + coded
        +
" ad was decoded to: " + decoded);
    System.out.println
("== End ==");
 
}

 
public static String encoder(String param) throws UnsupportedEncodingException {

   
// Convert the string to UTF8 bytes for md5 input
   
byte[] binaryData = param.getBytes("UTF8");
   
return new String(Base64.encodeBase64(binaryData));
 
}

 
public static String decoder(String encoded) throws UnsupportedEncodingException {

   
byte[] encodedBytes = encoded.getBytes("UTF-8");
   
byte[] decodeBase64 = Base64.decodeBase64(encodedBytes);
   
return new String(decodeBase64);
 
}
}

...and it's no coincidence that both these snippets of code give the same output:

== Start ==
Apache Codec Base 64 tester...
Coded: {This is my test string} with value e1RoaXMgaXMgbXkgdGVzdCBzdHJpbmd9 ad was decoded to: {This is my test string}
== End ==

1 comment:

The Price of A Chulk said...

If the encoded String is too long, please use Base64.encodeBase64(binarydata, true) instead.