iHarder.net

Base64

Something Sun Should Have Included Long Ago

Download now! (v2.2.2 48KB)

Alert me to new releases...

Now supports GZip-(de)compressing data before/after encoding!

This is a Public Domain Java class providing very fast Base64 encoding and decoding in the form of convenience methods and input/output streams.

There are other Base64 utilities on the Internet, some part of proprietary packages, some with various open source licenses. In any event, I hope with one or more of these Base64 tools, you won't have to write your own like I did.

Thanks to Brian Burton for providing this Base64Test.java test class for use with JUnit.org.

Changes

Examples

The easiest way to convert some data is with the convenience methods:

String result1 = Base64.encodeObject( mySerializableObject );
String result2 = Base64.encodeBytes( new byte[]{ 3, 34, 116, 9 } );

Or you can use the very efficient streams:

OutputStream out = new Base64.OutputStream( 
                    new FileOutputStream( "out.txt" ) );
// Go on about your outputting...
// ...

InputStream in = new Base64.InputStream( 
                  new FileInputStream( "in.txt" ) );
// Go on about your inputting...
// ...

There are defaults (OutputStream encodes, InputStream decodes), but you can easily override that:

OutputStream out = new Base64.OutputStream( 
                    new FileOutputStream( "out.txt" ), Base64.DECODE );
// Go on about your outputting...
// ...

A Note About Public Domain

I have released this software into the Public Domain. That means you can do whatever you want with it. Really. You don't have to match it up with any other open source license &em; just use it. You can rename the files, move the Java packages, whatever you want. If your lawyers say you have to have a license, contact me, and I'll make a special release to you under whatever reasonable license you desire: MIT, BSD, GPL, whatever.