Encode or Decode a Base64 String using native Java

This tutorials shows 3 ways how to encode or decode a Base64 String. Prior to Java 8 it was not possible with standard java library so we had to use a 3th party library like Apache Commons-Codec or Google Guava. But since the release of Java 8 there is (Finally) a new java.util.Base64 class introduced.

We compare 3 ways to encode or decode a Base64 string.

  1. Using Apache Commons-Codec
  2. Using Google Guava
  3. Using native Base64 from Java 8
Info:
When converting String to byte[] it’s a best practice to always provide a java.nio.charset.Charset.

Using Apache Commons-Codec

To use apache commons codec we need to add the dependency to our project, if using maven add the following:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.10</version>
</dependency>

Apache Commons-Codec provides convenient static utility methods Base64.encodeBase64(byte[]) and Base64.decodeBase64(byte[]) for converting binary data into Base64 encoded byte arrays.

package com.memorynotfound.base64;

import org.apache.commons.codec.binary.Base64;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Base64CommonsCodec {

    private static final Charset UTF_8 = StandardCharsets.UTF_8;

    public static void main(String... args){

        String text = "Hello, world!";

        byte[] encodedBytes = Base64.encodeBase64(text.getBytes(UTF_8));
        System.out.println("encodedBytes: " + new String(encodedBytes, UTF_8));

        byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
        System.out.println("decodedBytes: " + new String(decodedBytes, UTF_8));
    }

}

Using Google Guava

To use Google Guava we need to add the dependency to our project, if using maven add the following:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

Google Guava provides convenient static utility methods BaseEncoding.base64().encode(byte[]) and BaseEncoding.base64().decode(String) for converting binary data into Base64 encoded byte arrays.

package com.memorynotfound.base64;

import com.google.common.io.BaseEncoding;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Base64Guava {

    private static final Charset UTF_8 = StandardCharsets.UTF_8;

    public static void main(String... args){

        String text = "Hello, world!";

        String encodedString = BaseEncoding.base64().encode(text.getBytes(UTF_8));
        System.out.println("encodedString: " + encodedString);

        byte[] decodedString = BaseEncoding.base64().decode(encodedString);
        System.out.println("decodedString: " + new String(decodedString, UTF_8));
    }

}

Using native Base64 from Java 8

After about 20 years we finally have native java support for converting a string to base64 format. Just like the other libraries there are convenient methods to encode a string to a base64 byte array and decode a base64 byte array back to a string.

package com.memorynotfound.base64;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Java8 {

    private static final Charset UTF_8 = StandardCharsets.UTF_8;

    public static void main(String... args){

        String text = "Hello, world!";

        String encodedString = Base64.getEncoder().encodeToString(text.getBytes(UTF_8));
        System.out.println("encodedString: " + encodedString);

        byte[] decodedString = Base64.getDecoder().decode(encodedString.getBytes(UTF_8));
        System.out.println("decodedString: " + new String(decodedString, UTF_8));
    }

}

References

You may also like...