Base64 Encoding and Decoding Examples in Java using Google Guava

Base64 Encoding and Decoding Examples in Java using Google Guava

In this tutorial we will discuss how to Encode and Decode using Base64 using Google’s Guava Project Open Source library. In our other tutorials, we discuss how to perform “Base64 Encoding and Decoding Examples in Java using Apache Commons”. If you are using Java 8, you may be aware that it now natively supports Base64 encoding and decoding. Please review our Java 8 tutorial called “Base64 Encoding and Decoding Examples in Java 8”.

What’s Covered

  1. Getting Started
  2. Required Libraries
  3. What is Base64 Encoding
  4. Base64 Alphabet Characters Used for Encoding
  5. Base64 Encoding/Decoding using Google Guava Library
  6. Base64 Encoding/Decoding Google Guava Output

Getting Started

In order to run this tutorial yourself, you will need the following:

  • Java JDK 1.6 or greater
  • Favorite IDE Spring Tool Suite (STS), Eclipse IDE or NetBeans (I happen to be using STS because it comes with a Tomcat server built-in)
  • Tomcat 7 or greater or other popular container (Weblogic, Websphere, Glassfish, JBoss, VMWare vFabric, etc). For this tutorial I am using VMware vFabric tc Server Developer Edition which is essentially an enhanced Tomcat instance integrated with Spring STS
  • Google Guava – 18.0 Guava is a suite of core and expanded libraries that include utility classes, google’s collections, base64, io classes, and much much more.

Required Libraries

guava-18.0.jar

You will then configure your libraries in the Libraries tab on Java Build Path Dialog Screen (shown below).

base64_guava_lib_setup

Complete Project Overview

I have added the project overview to give you a full view of the structure and show you all files contained in this sample project.

base64_guava_proj_struct

What is Base64 Encoding

Base64 is one of several encoding schemes used to encode binary data in a text based representation (ASCII) using a radix-64 representation. Although there are several other variants among them Base16 and Base32, it is Base64 which is the most prevalent and popular. The need for Base64 arose during the advent of email. During which time folks began to speculate with the possibility of using attachments with things like images, videos or other binary data. Since STMP (Simple Mail Transfer Protocol) only supported 7-bit ASCII characters within the messages, there was a need to be able to encode this binary data and convert it into a format that was universally supported without having to affect the current infrastructure of email servers and the SMTP protocol.

This led the industry into standards like MIME (Multipurpose Internet Mail Extensions). The MIME specification supports two binary-to-text encoding schemes (Base64 and quotable-printable).

Base64 Alphabet Characters Used for Encoding

Base64 makes use of the following characters:

CharactersDescription
[a-z]26 Characters
[A-Z]26 Characters
[0-9]10 Characters
[+]1 Character (filler)
[/]1 Character (filler)
[=]1 Character (padding)

Base64 Encoding/Decoding using Google Guava Library

In this example, we will encode a String called sampleText using the Base64 encoding algorithm. We will be using the com.google.common.io.BaseEncoding class from the Google Guava library. This class implements the Base64 encoding specified by RFC 4648 section 4.

To encode our sampleText String we will use BaseEncoding.base64().encode(byte[] bytes) method which returns a String with the encoded data.

When decoding a Base64 encoded string we use the BaseEncoding.base64().decode(String encodedText) method and pass the encoded text as a String parameter. This method returns a byte[] array of decodedText.

  • Create a String containing the encoded text
  • Call the BaseEncoding.base64().decode method
  • Use the String as parameter, method returns byte[] array
  • Output the String to our console
package com.avaldes.tutorial;

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

import com.google.common.io.BaseEncoding;

public class Base64EncodeDecodeGuava {

  private static String sampleText = "Do, or do Not. There is no try.";
  private static final Charset UTF_8 = StandardCharsets.UTF_8;
  
  public static void main(String[] args) {  
    encodingDecodingGuava();
    encodingDecodingGuavaUTF();
  }
  
  public static void encodingDecodingGuava() {
    // --- Encode Data---
    String encodedText = BaseEncoding.base64()
                                 .encode(sampleText.getBytes());
    
    // ---Decode Data---
    byte[] decoded =  BaseEncoding.base64().decode(encodedText);
    String decodedText =  new String(decoded);
    
    System.out.println("Base64 Encoding/Decoding - Guava");
    System.out.println("--------------------------------");
    System.out.println("SampleText......: " + sampleText);
    System.out.println("EncodedText.....: " + encodedText);
    System.out.println("DecodedText.....: " + decodedText);
    System.out.println();
  }
  
  public static void encodingDecodingGuavaUTF() {
  // --- Encode Data---
    String encodedText = BaseEncoding.base64()
                                 .encode(sampleText.getBytes(UTF_8));
    
    // ---Decode Data---
    byte[] decoded =  BaseEncoding.base64().decode(encodedText);
    String decodedText =  new String(decoded, UTF_8);
    
    System.out.println("Base64 Encoding/Decoding - Guava (UTF-8)");
    System.out.println("----------------------------------------");
    System.out.println("SampleText......: " + sampleText);
    System.out.println("EncodedText.....: " + encodedText);
    System.out.println("DecodedText.....: " + decodedText);
    System.out.println();
  }
}

Base64 Encoding/Decoding Google Guava Output

Base64 Encoding/Decoding - Guava
--------------------------------
SampleText......: Do, or do Not. There is no try.
EncodedText.....: RG8sIG9yIGRvIE5vdC4gVGhlcmUgaXMgbm8gdHJ5Lg==
DecodedText.....: Do, or do Not. There is no try.

Base64 Encoding/Decoding - Guava (UTF-8)
----------------------------------------
SampleText......: Do, or do Not. There is no try.
EncodedText.....: RG8sIG9yIGRvIE5vdC4gVGhlcmUgaXMgbm8gdHJ5Lg==
DecodedText.....: Do, or do Not. There is no try.

References

Download the Complete Source Code

That’s It!

I hope you enjoyed this tutorial. It was certainly a lot of fun putting it together and testing it out. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!

base64_google_guava

Core Java Related Tutorials

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *