Base64 encoding and decoding in Java 8 and above
This post will discuss Base64 encoding and decoding in Java.
Java 8 finally extended support for Base64 Encoding and Decoding capabilities by providing Base64, Base64.Encoder and Base64.Decoder utility class in the java.util package. The Base64 class consists of static methods for obtaining instances of encoders (Base64.Encoder) and decoders (Base64.Decoder) for the Base64 encoding scheme.
The implementation of the Base64 class supports the following types of encoding:
1. Basic Encoding
The basic encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet set, which is A-Za-z0-9+/.
The following program uses Base64.Encoder.encodeToString() method for encoding the specified byte array into a Base64 encoded string and Base64.Decoder.decode() method for decoding back the Base64 encoded string into a newly allocated byte array.
We can also use the Base64.Encoder.encode() method for writing the resulting bytes to a byte array instead of a string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Base64; // Base64 encoding and decoding in Java 8 and above class Main { public static void main(String[] args) { String string = "Techie Delight"; // encode a string using `Base64` encoder Base64.Encoder encoder = Base64.getEncoder(); String encoded = encoder.encodeToString(string.getBytes()); // byte[] encodedBytes = encoder.encode(string.getBytes()); System.out.println("Encoded Data: " + encoded); // decode the encoded data Base64.Decoder decoder = Base64.getDecoder(); String decoded = new String(decoder.decode(encoded)); System.out.println("Decoded Data: " + decoded); } } |
Output:
Encoded Data: VGVjaGllIERlbGlnaHQ=
Decoded Data: Techie Delight
The basic encoding adds padding character at the end of the encoded byte data by default if the length of the encoded string is not a multiple of 3. These padding characters are then discarded on decoding. To get an encoder without padding, we can call the withoutPadding() method:
|
1 |
Base64.Encoder encoder = Base64.getEncoder().withoutPadding(); |
2. URL and Filename safe encoding
To encode an URL, we can’t use a basic encoding. We should use the “URL and Filename safe Base64 Alphabet” for encoding and decoding. The encoder does not add any line feed (line separator) character, and the decoder rejects data that contains characters outside the base64 alphabet.
The following example encodes a URL using the URL encoder:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; // Base64 encoding and decoding in Java 8 and above class Main { public static void main(String[] args) throws IOException { String url = "https://techiedelight.com/?v=1"; String encoded = Base64.getUrlEncoder().encodeToString(url.getBytes()); System.out.println(encoded); String decoded = new String(Base64.getUrlDecoder().decode(encoded)); System.out.println(decoded); } } |
Output:
Using URL Alphabet: aHR0cDovL3d3dy50ZWNoaWVkZWxpZ2h0LmNvbS8_dj0x
https://techiedelight.com/?v=1
3. MIME Encoding
The MIME Encoding uses the “The Base64 Alphabet” for encoding and decoding operation, but the encoded output is represented in lines of no more than 76 characters each and uses a carriage return, followed by a linefeed '\r\n' as the line separator.
The following example encodes a large block of text (having more than 76 characters) using the MIME encoder:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.IOException; import java.util.Base64; import java.util.UUID; // Base64 encoding and decoding in Java 8 and above class Main { public static void main(String[] args) throws IOException { StringBuilder buffer = new StringBuilder(); for (int i = 0; i < 5; i++) { buffer.append(UUID.randomUUID().toString()); } String string = buffer.toString(); String mimeEncoded = Base64.getMimeEncoder() .encodeToString(string.getBytes()); String mimeDecoded = new String(Base64.getMimeDecoder() .decode(mimeEncoded)); System.out.println(mimeDecoded); } } |
Output (may vary):
e2317f97-47d0-4799-b383-2a89070d5c1ef57d1659-3153-47d6-88a1-be3d5cca8cf526afb83c-c0db-4ef6-bd41-edc7ca4a691db744a484-f6fd-4f80-a7e4-4d8d3d7d3153309b3b8c-d4b3-4027-821b-85450c735616
That’s all about base64 encoding and decoding in Java.
Reference: Base64 (Java Platform SE 8)
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)