Java8 Base64
In Java 8, Base64 encoding has become a standard part of the Java class library.
Java 8 includes built-in encoders and decoders for Base64 encoding.
The Base64 utility class provides a set of static methods to obtain the following three BASE64 encoders and decoders:
- Basic: The output is mapped to a set of characters A-Za-z0-9+/; no line feeds are added to the output, and the output is decoded only with A-Za-z0-9+/.
- URL: The output is mapped to a set of characters A-Za-z0-9+_; the output is URL and filename safe.
- MIME: The output is mapped to a MIME-friendly format; each line does not exceed 76 characters, and uses '\r' followed by '\n' as the line separator; no line separators are in the encoded output.
Nested Classes
Number | Nested Class & Description |
---|---|
1 | static class Base64.Decoder This class implements a decoder for decoding byte data using Base64 encoding. |
2 | static class Base64.Encoder This class implements an encoder for encoding byte data using Base64 encoding. |
Methods
| 1 | static Base64.Decoder getDecoder() Returns a Base64.Decoder that decodes using the basic Base64 encoding scheme. | | 2 | static Base64.Encoder getEncoder() Returns a Base64.Encoder that encodes using the basic Base64 encoding scheme. | | 3 | static Base64.Decoder getMimeDecoder() Returns a Base64.Decoder that decodes using the MIME Base64 encoding scheme. | | 4 | static Base64.Encoder getMimeEncoder() Returns a Base64.Encoder that encodes using the MIME Base64 encoding scheme. | | 5 | static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) Returns a Base64.Encoder that encodes using the MIME Base64 encoding scheme, with specified line length and line separators. | | 6 | static Base64.Decoder getUrlDecoder() Returns a Base64.Decoder that decodes using the URL and filename-safe Base64 encoding scheme. | | 7 | static Base64.Encoder getUrlEncoder() Returns a Base64.Encoder that encodes using the URL and filename-safe Base64 encoding scheme. |
Note: Many methods of the Base64 class are inherited from the java.lang.Object class.
Base64 Example
The following example demonstrates the use of Base64:
Java8Tester.java File
import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;
public class Java8Tester {
public static void main(String args[]){
try {
// Using basic encoding
String base64encodedString = Base64.getEncoder().encodeToString("tutorialpro?java8".getBytes("utf-8"));
System.out.println("Base64 Encoded String (Basic) :" + base64encodedString);
// Decoding
byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
base64encodedString = Base64.getUrlEncoder().encodeToString("tutorialpro?java8".getBytes("utf-8"));
System.out.println("Base64 Encoded String (URL) :" + base64encodedString);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 10; ++i) {
stringBuilder.append(UUID.randomUUID().toString());
}
byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8");
String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
System.out.println("Base64 Encoded String (MIME) :" + mimeEncodedString);
} catch(UnsupportedEncodingException e) {
System.out.println("Error :" + e.getMessage());
}
}
}
Executing the script above, the output is:
$ javac Java8Tester.java
$ java Java8Tester
Original String: tutorialpro?java8
Base64 Encoded String (URL) :VHV0b3JpYWxzUG9pbnQ_amF2YTg=
Base64 Encoded String (MIME) :M2Q4YmUxMTEtYWRkZi00NzBlLTgyZDgtN2MwNjgzOGY2NGFlOTQ3NDYyMWEtZDM4ZS00YWVhLTkz
OTYtY2ZjMzZiMzFhNmZmOGJmOGI2OTYtMzkxZi00OTJiLWEyMTQtMjgwN2RjOGI0MTBmZWUwMGNk
NTktY2ZiZS00MTMxLTgzODctNDRjMjFkYmZmNGM4Njg1NDc3OGItNzNlMC00ZWM4LTgxNzAtNjY3
NTgyMGY3YzVhZWQyMmNiZGItOTIwZi00NGUzLTlkMjAtOTkzZTI1MjUwMDU5ZjdkYjg2M2UtZTJm
YS00Y2Y2LWIwNDYtNWQ2MGRiOWQyZjFiMzJhMzYxOWQtNDE0ZS00MmRiLTk3NDgtNmM4NTczYjMx
ZDIzNGRhOWU4NDAtNTBiMi00ZmE2LWE0M2ItZjU3MWFiNTI2NmQ2NTlmMTFmZjctYjg1NC00NmE1
LWEzMWItYjk3MmEwZTYyNTdk