Java Base64 Encoding and Decoding
In this post, we will explore various options of Java Base64 Encoding and Decoding. We will cover mainly new Base64 API introduced in Java 8.
Introduction
Base64 encoding was missing from standard JDK before Java 8. Java 8 introduced a simple yet powerful API under java.util package or specifically a utility class java.util.Base64. In this post, we will cover Java Base64 encoding and decoding.
The Base64 class comprises static factory methods for getting Base64 based encoder and decoder. We can get following 3 types of Base64 encoder/ decoder
- Basic
- URL and FileName
- MIME
1. Java 8 Basic Encoder
Java 8 Basic Base64 encoder is a simple encoder. It will encode adding no line feed/line separator characters.
void basicEncoder(final String input) {
String simpleBase64 = Base64.getEncoder().encodeToString(input.getBytes(StandardCharsets.UTF_8));
System.out.println(simpleBase64);
}
It will map the output of the encoder to characters in the Base64 Alphabet: A-Za-z0-9+/ and it will reject data is outside this base64 alphabet.
1.1 Java 8 Basic Decoder
Decoding encoded String back to original form is simple with new API, all we have to do it to get encode and pass decoded data to it.
void basicDecoder(final String encodedData) {
byte[] decodeData = Base64.getDecoder().decode(encodedData);
System.out.println(new String(decodeData, StandardCharsets.UTF_8));
}
We are no longer required to use those external dependencies (e.g. Sun classes etc) to do encoding and decoding.
2. URL Encoding
URL encoding works similarly to Basic encoding and it uses “URL and Filename safe Base64 Alphabet”.
String input= "search?_base64";
void urlEncoding(final String input) {
String urlEncode= Base64.getUrlEncoder().encodeToString(input.getBytes(StandardCharsets.UTF_8));
System.out.println("Encoding using URL Encoder "+urlEncode);
}
2.1 URL Decoder
URL decoder work in a similar way as encoder work, we need to get UrlDecoder()
and pass the encoded data to it.
void urlDecoding(final String encodedData) {
byte[] decodeData = Base64.getUrlDecoder().decode(encodedData);
System.out.println(new String(decodeData, StandardCharsets.UTF_8));
}
3. MIME Encoding
MIME encoding will use Base64 encoding using Basic Alphabet and encoded output will be converted into MIME format (only 76 characters each and uses a carriage return ‘\r’ followed immediately by a linefeed ‘\n’ as the line separator).
private static void mimeEncoder(){
StringBuilder stringBuffer = new StringBuilder();
for (int t = 0; t < 10; ++t) {
stringBuffer.append(UUID.randomUUID().toString());
}
String mimeEncoding = Base64.getMimeEncoder().
encodeToString(stringBuffer.toString().getBytes(StandardCharsets.UTF_8));
System.out.println("MIME Encoding is " +mimeEncoding);
}
Output
MIME Encoding is:: ZjY5NzBkYTctMDFmNy00YmY3LTk4YjAtMmYxZGUzYzNhM2QwZTNmM2I4M2EtODg0Yy00ZjlkLTlm
YTgtMjIzZWY1ZTMzNGQ2ODVlZDA0ZDQtMjJjMy00NzUxLWEwYTYtYzM2ZTNhOTFjNzk0MmEwY2Iz
ZDYtYmRmNC00MmUzLTllMzQtZjIzMDRkMmMxMzgyMmIxNTExZjEtZDdkYS00ZjIwLTlhMzUtYjYy
ZTFiNTc3ZmQ1NzM3NDViM2ItNTRkMC00MTM4LTgxMjMtNmQ4ZDgyMzRlYTI4YTA1NjFhMGEtNWY3
NC00NDdmLTlhNWItOWIxNTgxOWY4NzJlMTRhZThmN2MtNjdiOS00ZDcwLWEwZmYtOGEyNjM3MDBm
NzNlMTRiNzdiY2YtYWI2ZS00MmNmLWI5NjAtMjFmZThmZjZmMmJjYWU0MmQ0MmUtZDE1ZC00NTdk
LThmMzUtNTUyMTlhMzRiMmYx
2.1 MIME Decoder
byte[] decodeData = Base64.getMimeDecoder().decode(encodedData);
System.out.println(new String(decodeData, StandardCharsets.UTF_8));
4. Conclusion
In this post, we explored Java Base64 Encoding and Decoding API introduced in Java 8. This new API is simple and powerful and will be available within the JDK with no external dependencies (commons-codec or sun.misc.BASE64Decoder).
All the code of this article is available Over on Github. This is a Maven-based project.
References