Differences between getEncoder() and getUrlEncoder() method of java.util.Base64 class

The java.util.Base64.getEncoder() and java.util.Base64.getUrlEncoder() methods in Java both return a Base64 encoder. However, they exhibit different behaviors mainly due to the encoding scheme they follow for handling URL and filename safe characters.

  • java.util.Base64.getEncoder(): This method returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme as per RFC 4648 Section 4.

Basic Base64 encoding does not handle URL and filename safe characters well. It uses + for 62, / for 63, and = for padding. These characters can cause problems in URLs.

An example of its usage.

package org.kodejava.util;

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

public class Base64EncoderExample {
    public static void main(String[] args) {
        Base64.Encoder encoder = Base64.getEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String encodedString = encoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + encodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g/cT1IZWxsbytXb3JsZA==
  • java.util.Base64.getUrlEncoder(): This method returns a Base64.Encoder that encodes in URL and filename safe type base64 scheme as per RFC 4648 Section 5.

URL and filename safe Base64 encoding replaces + with - , and / with _. It also omits padding characters. This makes it safer for use in URLs or file-system paths.

An example of its usage.

package org.kodejava.util;

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

public class Base64UrlEncoderExample {
    public static void main(String[] args) {
        Base64.Encoder urlEncoder = Base64.getUrlEncoder();
        String str = "https://www.google.com/search?q=Hello+World";
        String urlEncodedString = urlEncoder.encodeToString(str.getBytes(StandardCharsets.UTF_8));
        System.out.println("encodedString = " + urlEncodedString);
    }
}

Output:

encodedString = aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9zZWFyY2g_cT1IZWxsbytXb3JsZA==

So the choice between these two would depend on where you intend to use the encoded bytes. If it’s to be in a URL or a filepath, it’s recommended to use the getUrlEncoder() due to its url safe encoding scheme. For other use cases, the getEncoder() method should suffice.

Similar to the getEncoder() and getUrlEncoder() methods, the java.util.Base64 class provides the getDecoder() and getUrlDecoder() methods for Base64 decoding.

  • java.util.Base64.getDecoder(): This method returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.

Example:

package org.kodejava.util;

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

public class Base64DecodeExample {
    public static void main(String[] args) {
        Base64.Decoder decoder = Base64.getDecoder();

        // "Hello, World!" in Base64
        String str = "SGVsbG8sIFdvcmxkIQ==";
        String decodedString = new String(decoder.decode(str), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!
  • java.util.Base64.getUrlDecoder(): This method returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

Example:

package org.kodejava.util;

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

public class Base64UrlDecoderExample {
    public static void main(String[] args) {
        Base64.Decoder urlDecoder = Base64.getUrlDecoder();

        // "Hello, World!" in URL-safe Base64
        String urlSafeStr = "SGVsbG8sIFdvcmxkIQ";
        String decodedString = new String(urlDecoder.decode(urlSafeStr), StandardCharsets.UTF_8);
        System.out.println("decodedString = " + decodedString);
    }
}

Output:

decodedString = Hello, World!

The choice between these two would again depend on from where you are getting the encoded bytes. If it’s from a URL or a filepath, you would want to use getUrlDecoder(). For other use cases, the getDecoder() method should suffice.

Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.