Quick 'n dirty Base64 encoder/decoder in Java
Today, someone on a mailing list I was reading asked about how to do Base64 encoding and decoding in Java. Surprisingly, it's not part of the core libraries and although there are a couple of them hidden away they're either private or not recommend for use. Usually I end up googling for an implementation I can use (which may be what you've done to find this page).
I thought that I ought to be able to make my own, so here's the result of a couple of hours of hacking around. Consider it freeware. Use it wherever you like. No warranties. If it doesn't work for you either let me know or just grab another one from the Internet...
Example usage:
String x = "To the batmobile! Atomic batteries to power. Turbines to speed."; // Encode, no linefeeds Base64.encode(x.getBytes()); // Same as the above Base64.encode(x.getBytes(), false)); // Encode, linefeed at 76 chars (MIME encoding) Base64.encode(x.getBytes(), true)); // Decode Base64.decode( Base64.encode(x.getBytes()) );
There are also freely usable (Apache license) implementations available as part of JXTA at :
https://jxta-jxse.dev.java.net/source/browse/jxta-jxse/trunk/impl/src/net/jxta/impl/util/BASE64OutputStream.java
https://jxta-jxse.dev.java.net/source/browse/jxta-jxse/trunk/impl/src/net/jxta/impl/util/BASE64InputStream.java
Posted by Mike Duigou on July 11, 2008 at 01:56 AM BST #