Explain Codes LogoExplain Codes Logo

Decode Base64 data in Java

java
base64-decoding
java-8
base64-encoding
Anton ShumikhinbyAnton Shumikhin·Dec 10, 2024
TLDR

In Java, decode Base64 using the Base64.getDecoder().decode(). This method turns a Base64 string into a byte array, then new string is constructed out of it. Here's the punch line:

String base64Encoded = "SGVsbG8gV29ybGQh"; // "Hello World!" in disguise byte[] decodedBytes = Base64.getDecoder().decode(base64Encoded); String decodedString = new String(decodedBytes); // surprise! System.out.println(decodedString); // "Hello World!"

With blazing speed, it transforms Base64 encoded text back into its original string form. Neat, huh?

Science behind java.util.Base64

In Java 8, we were blessed with java.util.Base64 for handling all our Base64 encoding and decoding needs. Plus, it's built into Java, meaning you don't have to deal with external dependencies.

This wonder class also offers three types of encoding schemes: basic, URL, and MIME. Each with a specific use case.

Coding in the past: Prior to Java 8

Java 8 or higher being a luxury you don't have? No worries. For those bonding with older Java versions, javax.xml.bind.DatatypeConverter.parseBase64Binary(), part of Java 6's JAXB, is here to save the day.

Embarking on a project that's bonded with Sun Java 6? Stick with officially supported classes to avoid any compatibility shenanigans.

Living in the future, past Java 11? Keep in mind that you might need to handle dependencies using tools like Maven due to module changes in the JDK.

Remember, though: sun.misc.BASE64Decoder may seem like an option, but it's a trap! It's no longer supported and it hides in the shadows of your JDK.

Not just one tool in the box: Considering alternatives

While java.util.Base64 is indeed the belle of the ball, there are other players worth considering.

The Apache way: Apache Commons Codec

Apache Commons Codec library provides some serious competition. It plays well with older Java versions and has a battle-hardened API:

String base64Encoded = "SGVsbG8gV29ybGQh"; // "Hello World!" in costume byte[] decodedBytes = org.apache.commons.codec.binary.Base64.decodeBase64(base64Encoded); // Presto! It's a classic.

Powered by Google: Google Guava

Google's Guava also offers BaseEncoding.base64().decode(), which has been a star player on the team:

String base64Encoded = "SGVsbG8gV29ybGQh"; // Another "Hello World!" in disguise byte[] decodedBytes = BaseEncoding.base64().decode(base64Encoded); // Ta-da! Hello, World!

The old faithful: JAXB

For those with Java 6, JAXB offers built-in support. It comes pre-loaded with Java 6, ensuring compliance and compatibility. Moreover, xjc.exe is an additional tool that can help rock JAXB.

Remember, after Java 11, JAXB membership isn't complementary. You'll need to manually add it to your squad.

Making the smart choice

When looking to decode Base64, make your selection based on project specifics, Java version, and policy around external dependencies. Keeping it official and supported is the best defense against future code malaise.