iHarder.net

Base64

Support This Project

Something Sun Should Have Included Long Ago

Download now! (v2.3.7 48KB)

Follow the file release RSS feed...

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.

If you use Maven, thank Owen O'Malley from Apache and Matthew from Sensible Development for working up a Base64 Maven Repository.

Thanks to Brian Burton for providing this Base64Test.java test class for use with JUnit.org. (The test file hasn't been updated in a while.)

You may view the Base64 javadoc API online, if you like.

Version 2.3 is not a drop-in replacement to earlier versions. I finally improved the error handling such that the code will throw exceptions when there is a problem instead of just quietly returning null. Sorry if you like the old style (I did too for a long time), but I changed it and think it's better form this way. There are also some helper methods for encoding data without creating a String object; that should help with very large amounts of data or when memory is tight.

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.

Alternatives

There are alternative Base64 classes out there. For various reasons you might prefer them. It's OK; you won't hurt my feelings. Keep in mind particularly that there are a lot of "helper functions" in the Base64.java class available on this page, and if you're doing embedded work, you'll either want to strip out the fluff or just grab a more minimal implementation. Here are some that I've found:

Changes


blog comments powered by Disqus