Finally, an official Java Base64 encoder/decoder....

Remember the old days of using sun.misc.BASE64Encoder and sun.misc.BASE64Decoder? Later, you went on the internet and found out that you should not use the sun.* package and BASE64Encoder/BASE64Decoder were not part of the public interface so they were not recommended. The interim solution was to either use the org.apache.commons.codec.binary.Base64 Codec (released by Apache on Apache Commons Codec library), found alternative solutions or write your own (which I did anyway).

Now, you don't have to fret anymore. In Java 8 JDK, The Oracle team decided to release a Base64 encoder/decoder on the java.util package. The java.util.Base64 class provides static methods to get a Decoder and Encoder for Base 64 decoding and encoding respectively.

The beauty of this Base64 class is that it provides Base 64 that conforms to 3 Base64 encoding formats:
  • Basic, as stipulated on RFC4648.
  • URL safe encoding, and
  • MIME format, as stipulated in RFC2045.

The following example shows how to do a Basic Base64 encoding:


And the reverse is simply as follows:


That's all there is to it and, though Oracle took years and tons of releases, it's about time (it's long overdue).

Comments