Java Built-In Implementation of Base64URL

This section provides a tutorial example on how to perform Base64URL encoding on binary files using the default Java implementation of the Base64URL encoding algorithm.

The Base64URL encoding algorithm bas been implemented in Java language since Java 8 with two methods, Base64.getUrlEncoder() and Base64.getUrlDecoder().

However, the Java implementation of Base64URL does not remove "=" paddings. You may need to do it yourself.

Here is a sample program that allows you to perform Base64URL encoding/decoding and other variations.

/* Base64Tool.java
 * Copyright (c) 2002 HerongYang.com. All Rights Reserved.
 */
import java.io.*;
import java.util.Base64;
class Base64Tool {
  public static void main(String[] args) {
    
    if (args.length<4) {
       System.out.println("Usage:");
       System.out.println(
          "java Base64Tool.java mode standard input output");
       return;
    }
    String mode = args[0];
    String standard = args[1];
    String inputFile = args[2];
    String outputFile = args[3];
    
    try {
      File file = new File(inputFile);
      long length = file.length();
      FileInputStream inputStream = new FileInputStream(file);
      byte[] inputBytes = new byte[(int)length];
      int l = inputStream.read(inputBytes);
      inputStream.close();
      
      byte[] outputBytes = null;
      if (mode.equals("decode")) {
        Base64.Decoder decoder = null;      
        if (standard.equals("mime")) {
          decoder = Base64.getMimeDecoder();
        } else if (standard.equals("url")) {
          decoder = Base64.getUrlDecoder();
        } else {
          decoder = Base64.getDecoder();
        }
        outputBytes = decoder.decode(inputBytes);
        
      } else {
        Base64.Encoder encoder = null;    
        if (standard.equals("mime")) {
          encoder = Base64.getMimeEncoder();
        } else if (standard.equals("url")) {
          encoder = Base64.getUrlEncoder();
        } else {
          encoder = Base64.getEncoder();
        }
        outputBytes = encoder.encode(inputBytes);
      }
      
      System.out.println(new String(outputBytes));
      
      FileOutputStream outputStream = new FileOutputStream(outputFile);
      outputStream.write(outputBytes);
      outputStream.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Let's try it with different test cases:

herong$ java Base64Tool.java encode url A.txt encoded.txt 
QQ==

herong$ java Base64Tool.java decode url encoded.txt decoded.txt   
A

herong$ java Base64Tool.java encode url lazy_dog.txt encoded.txt
VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=

herong$ java Base64Tool.java decode url encoded.txt decoded.txt   
The quick brown fox jumps over the lazy dog.
  
herong$ java Base64Tool.java encode url url.txt encoded.txt 
aS5odG0_NjQ-NjM=

herong$ java Base64Tool.java decode url encoded.txt decoded.txt   
i.htm?64>63

herong$ java Base64Tool.java encode standard url.txt encoded.txt 
aS5odG0/NjQ+NjM=

herong$ java Base64Tool.java decode standard encoded.txt decoded.txt   
i.htm?64>63

herong$ java Base64Tool.java encode url icon.png encoded.txt 
iVBORw0KGgoAAAANSUhEUgAAABgAAAAY ... HwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC

herong$ java Base64Tool.java decode url encoded.txt decoded.txt   
?PNG...       

As you can see, Java implementation of Base64URL works as expected. The only issue is the "=" padding, which can be removed easily.

Table of Contents

 About This Book

 Base64 Encoding

 Base64 Encoding and Decoding Tools

Base64URL - URL Safe Base64 Encoding

 Base64URL Encoding Algorithm

Java Built-In Implementation of Base64URL

 Python Built-In Implementation of Base64URL

 PHP Implementation of Base64URL Encoding

 Perl Implementation of Base64URL Encoding

 Base32 Encoding

 URL Encoding, URI Encoding, or Percent Encoding

 UUEncode Encoding

 References

 Full Version in PDF/EPUB