Java Base64 Encoding and Decoding: A Comprehensive Guide

Learn about the Base64 utility class introduced in Java 8, which offers built-in methods for encoding and decoding data in Base64 format. Explore the three types of Base64 encoding and decoding available, and understand how to efficiently handle binary data in your Java applications.



Java - Base64 Encoding and Decoding

The Base64 utility class introduced in Java 8 provides inbuilt methods for encoding and decoding data in Base64 format. There are three types of Base64 encoding and decoding available:

Types of Base64 Encoding

  • Basic − Output is mapped to characters in the set A-Za-z0-9+/. No line feed is added in the output, and the decoder rejects any character outside this set.
  • URL − Output is mapped to characters in the set A-Za-z0-9+_. This type is safe for URLs and filenames.
  • MIME − Output is represented in lines of up to 76 characters, with a carriage return (\r) followed by a line feed (\n) as the line separator. There is no separator at the end of the encoded output.

Basic Base64 Encoding and Decoding

The basic Base64 encoder encodes the provided string using characters from the A-Za-z0-9+/ set without adding line feeds. Below is an example:

Syntax

String stringToEncode = "LearnBase64Java";
// Encode using basic encoder
String base64encodedString = Base64.getEncoder().encodeToString(stringToEncode.getBytes("utf-8"));
Output

Encoded String: TGVhcm5CYXNlNjRKYXZh

The basic Base64 decoder can decode the encoded string as follows:

Syntax

// Decode the Base64 encoded string using basic decoder
byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
// Print the decoded string
System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
Output

Original String: LearnBase64Java

Base64 Encoding and Decoding for URL

The URL Base64 encoder encodes data in a URL- and filename-safe manner. Below is an example:

Syntax

String stringToEncode = "LearnBase64Java";
// Encode using URL encoder
String base64encodedString = Base64.getUrlEncoder().encodeToString(stringToEncode.getBytes("utf-8"));
Output

Encoded String: TGVhcm5CYXNlNjRKYXZh

The URL Base64 decoder decodes the string as shown below:

Syntax

// Decode the Base64 encoded string using URL decoder
byte[] base64decodedBytes = Base64.getUrlDecoder().decode(base64encodedString);
// Print the decoded string
System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
Output

Original String: LearnBase64Java

Base64 Encoding and Decoding for MIME Type Content

The MIME Base64 encoder is designed for encoding MIME-friendly content. The output is formatted into lines of no more than 76 characters. Below is an example:

Syntax

String stringToEncode = "Base64MIMEContent";
// Encode using MIME encoder
String base64encodedString = Base64.getMimeEncoder().encodeToString(stringToEncode.getBytes("utf-8"));
Output

Encoded String (MIME): QmFzZTY0TUlNRUNvbnRlbnQ=

The MIME Base64 decoder decodes the string as shown below:

Syntax

// Decode the Base64 encoded string using MIME decoder
byte[] base64decodedBytes = Base64.getMimeDecoder().decode(base64encodedString);
// Print the decoded string
System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
Output

Original String: Base64MIMEContent