Java Base64 URL Safe Encoding


When it comes to common tooling in Java, Guava is your best friend. If you are still using Apache Commons, stop now. Below is the gradle dependency information for guava as of this writing.

compile 'com.google.guava:guava:18.0'
Base64 URL Safe Encoding
Here is a simple example of encoding text with Base64 with Guava.
import com.google.common.io.BaseEncoding
import com.google.common.base.Preconditions

public class UrlSafeEncodingExample {

  public static void main(String[] args) {
    Preconditions.checkNotNull(args);
    Preconditions.checkNotNull(args[0]);

    String decodedString = args[0];

    System.out.println("Decoded String: " + decodedString);

    String encodedString = BaseEncoding.base64Url().encode(decodedString.getBytes('UTF-8'));

    System.out.println("Encoded String: " + encodedString);
  }

}
Check out more about Guava in their git repository.

Comments

Post a Comment

Popular posts from this blog

Atmosphere Websockets & Comet with Spring MVC

Microservices Tech Stack with Spring and Vert.X