Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Base64 URL

farenda 2015-09-25 1

Problem:

How to use Java Base64 to encode data using URL and Filename safe Base64 Alphabet? In the following example we’ll show how to use Base64 util class, available since Java 8.

Solution:

In last two posts we’ve shown how to use java.util.Base64 class to work with basic and MIME types of Base64. Here we’ll show how to encode safely for URL and Filename purposes. To do this we’ll use Base64.getUrlEncoder() to create an URL/Filename safe encoder and Base64.getUrlDecoder() for decoder:

package com.farenda.java.util;

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

public class Base64URLEncoder {

    public static void main(String[] args) {
        String message = "This will be encoded using URL and Filename safe Base64 Alphabet!";
        System.out.println("Original: " + message);

        byte[] bytesToEncode = message.getBytes(StandardCharsets.UTF_8);

        byte[] encoded = encode(bytesToEncode);

        decode(encoded);
    }

    private static byte[] encode(byte[] bytesToEncode) {
        Base64.Encoder encoder = Base64.getUrlEncoder();
        byte[] encoded = encoder.encode(bytesToEncode);
        System.out.println("Encoded: " + new String(encoded));
        return encoded;
    }

    private static void decode(byte[] encoded) {
        Base64.Decoder decoder = Base64.getUrlDecoder();
        byte[] decoded = decoder.decode(encoded);
        System.out.println("Decoded: " + new String(decoded));
    }
}

And here’s the result of running the above code:

Original: This will be encoded using URL and Filename safe Base64 Alphabet!
Encoded: VGhpcyB3aWxsIGJlIGVuY29kZWQgdXNpbmcgVVJMIGFuZCBGaWxlbmFtZSBzYWZlIEJhc2U2NCBBbHBoYWJldCE=
Decoded: This will be encoded using URL and Filename safe Base64 Alphabet!

As you can see UrlEncoder adds no newline at the end. It also rejects data that contains characters outside of the Base64 Alphabet.

Share with the World!
Categories Java Tags java, java-util
Previous: Java Base64 MIME
Next: Java Calendar

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok