The Culture of Code
  1. Posts/

Base64 Variants in Java 8

You most likely used Base64 encoding. It’s about encoding any sequence of data as a printable string (digits, lower case and upper case letters). But Base64 has variations. E.g., not every Base64 variant allows safe transfer of any data as URL parameters. For that purpose there is a special dialect of Base64: Url-safe encoding.

Since Java 8 you may use class java.util.Base64 for encoding and decoding. It supports Basic, URL and Filename Safe and MIME-encoded variants, as specified in RFC 4648 and RFC 2045.

Base64.Encoder encoder = java.util.Base64.getUrlEncoder().withoutPadding();
String base64String = encoder.encodeToString(byteArray);