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.

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

Konstantin Pavlov

Software Engineer working with Java, Kotlin, Swift, and AI. Focusing on software architecture and building AI-infused apps. Passionate about testing and Open-Source projects.