Java Base64 Encode and Decode

Java 8 introduced Base64 support to the IO operations. Let’s learn to encode and decode the inputs and outputs in Java 8.

1. What is Base 64 Encoding?

When we have some binary data that we want to send across a network, we generally don’t do it by just converting data into a stream of bits in a raw format.

Why? Because some media types are designed for streaming text only. These associated protocols may interpret our binary data as control characters which they are not.

Base-64 encoding converts our binary data into a string containing characters out of 64 printable characters. Generally, this encoding is done for binary data in email messages and "basic" HTTP authentication.

These 64 printable characters are:

  • 26 uppercase letters [A…Z]
  • 26 lowercase letters [a…z]
  • 10 digits [0…9]
  • 2 symbols [Read more]

The base-64 encoded string containing the above characters is safe to be transferred over the network supporting text data without fear of losing data in a confusion of control characters.

2. Base64 Support before Java 8

For many years, Java has provided support for base-64 via a non-public class (therefore non-usable) java.util.prefs.Base64 and an undocumented class sun.misc.BASE64Encoder. This class has also very limited information in the public domain.

3. Base64 Support since Java 8

Java 8 has added java.util.Base64 for Base-64 encoding and decoding purposes. It has two nested classes to work with the encoding and decoding process.

  • Base64.Encoder
  • Base64.Decoder

3.1. Encoding a String to Base64

This is as simple as getting an instance of the Base64.Encoder and input the string as bytes to encode it.

Base64.Encoder encoder = Base64.getEncoder();
String normalString = "username:password";

String encodedString = encoder.encodeToString(
        normalString.getBytes(StandardCharsets.UTF_8) );

3.2. Decoding a Base64 Encoded String

This is also very simple. Just get the instance of Base64.Decoder and use it to decode the base 64 encoded string.

String encodedString = "dXNlcm5hbWU6cGFzc3dvcmQ=";
Base64.Decoder decoder = Base64.getDecoder();

byte[] decodedByteArray = decoder.decode(encodedString);

//Verify the decoded string
System.out.println(new String(decodedByteArray));

3.3. Base64 Encoded Streams

If you don’t want to directly work with data and rather prefer to work with streams, you can wrap the output stream such that all data written to this output stream will be automatically base 64 encoded.

Path originalPath = Paths.get("c:/temp", "mail.txt");
Path targetPath = Paths.get("c:/temp", "encoded.txt");

Base64.Encoder mimeEncoder = Base64.getMimeEncoder();

try(OutputStream output = Files.newOutputStream(targetPath))
    //Copy the encoded file content to target file
    Files.copy(originalPath, mimeEncoder.wrap(output));

    //Or simply use the encoded output stream
    OutputStream encodedStrem = mimeEncoder.wrap(output);

3.4. Base64 Encoded URLs

The Base64.getUrlEncoder() Uses the “URL and Filename safe Base64 Alphabet” as specified in Table 2 of RFC 4648 for encoding and decoding. The encoder does not add any line feed (line separator) character.

String originalUrl = "";
String encodedUrl = Base64.getUrlEncoder()

The use of the decoder is almost the same. The decoder rejects data that contains characters outside the base64 alphabet.

byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedUrl);
String decodedUrl = new String(decodedBytes);

That’s all for this topic. This is already simple enough.

Happy Learning !!


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode