Java8 Base64
[ Java 8 New Features](#)
* * *
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.
The Base64 utility class provides a set of static methods to obtain the following three types of Base64 encoders and decoders:
* **Basic:** The output is mapped to a set of characters A-Za-z0-9+/. The encoding does not add any line breaks, and the decoded output only supports A-Za-z0-9+/.
* **URL:** The output is mapped to a set of characters A-Za-z0-9-_. The output is URL and file safe.
* **MIME:** The output is mapped to a MIME-friendly format. The output does not exceed 76 characters per line, and uses 'r' followed by 'n' as line separators. The encoded output does not have line separators at the end.
### Inner Classes
| No. | Inner Class & Description |
| --- | --- |
| 1 | **static class Base64.Decoder** This class implements a decoder for decoding byte data using the Base64 encoding scheme. |
| 2 | **static class Base64.Encoder** This class implements an encoder for encoding byte data using the Base64 encoding scheme. |
### Methods
| No. | Method Name & Description |
| --- | --- |
| 1 | **static Base64.Decoder getDecoder()** Returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme. |
| 2 | **static Base64.Encoder getEncoder()** Returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme. |
| 3 | **static Base64.Decoder getMimeDecoder()** Returns a Base64.Decoder that decodes using the MIME type base64 encoding scheme. |
| 4 | **static Base64.Encoder getMimeEncoder()** Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme. |
| 5 | **static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)** Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme, with the specified line length and line separator. |
| 6 | **static Base64.Decoder getUrlDecoder()** Returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme. |
| 7 | **static Base64.Encoder getUrlEncoder()** Returns a Base64.Encoder that encodes using the URL and Filename safe type 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{String base64encodedString = Base64.getEncoder().encodeToString("tutorial?java8".getBytes("utf-8")); System.out.println("Base64 Encoded String (Basic) :" + base64encodedString); byte[]base64decodedBytes = Base64.getDecoder().decode(base64encodedString); System.out.println("Original String: " + new String(base64decodedBytes, "utf-8")); base64encodedString = Base64.getUrlEncoder().encodeToString("tutorial?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 above script, the output result is:
$ javac Java8Tester.java $ java Java8TesterOriginal String: tutorial?java8 Base64 Encoded String (URL) :VHV0b3JpYWxzUG9pbnQ_amF2YTg=Base64 Encoded String (MIME) :M2Q4YmUxMTEtYWRkZi00NzBlLTgyZDgtN2MwNjgzOGY2NGFlOTQ3NDYyMWEtZDM4ZS00YWVhLTkz OTYtY2ZjMzZiMzFhNmZmOGJmOGI2OTYtMzkxZi00OTJiLWEyMTQtMjgwN2RjOGI0MTBmZWUwMGNkNTktY2ZiZS00MTMxLTgzODctNDRjMjFkYmZmNGM4Njg1NDc3OGItNzNlMC00ZWM4LTgxNzAtNjY3NTgyMGY3YzVhZWQyMmNiZGItOTIwZi00NGUzLTlkMjAtOTkzZTI1MjUwMDU5ZjdkYjg2M2UtZTJm YS00Y2Y2LWIwNDYtNWQ2MGRiOWQyZjFiMzJhMzYxOWQtNDE0ZS00MmRiLTk3NDgtNmM4NTczYjMx ZDIzNGRhOWU4NDAtNTBiMi00ZmE2LWE0M2ItZjU3MWFiNTI2NmQ2NTlmMTFmZjctYjg1NC00NmE1LWEzMWItYjk3MmEwZTYyNTdk
* * Java 8 New Features](#)
YouTip