base64 encode and decode using JDK

Ever asked yourself why the JDK isn’t shipped with a builtin base64 encoder/decoder? Well, since JDK 6 there is one – although it’s quite buried: javax.xml.bind.DatatypeConverter.

Usage is quite simple:

// encode byte[] to base64 String
String myBase64String = DatatypeConverter.printBase64Binary(myByteData);
// decode base64 String to byte[]
byte[] myByteData = DatatypeConverter.parseBase64Binary(myBase64String);

So no need to add a thirdparty library just for decoding/encoding base64 anymore.