Java8 Base64

In Java 8, Base64 encoding has become the standard for Java class libraries.
Java 8 has built-in Base64-encoded encoders and decoders.
The Base64 utility class provides a static set of methods for obtaining the following three BASE64 codecs:
  • Basic: The output is mapped to a set of characters A-Za-z0-9+/. The encoding does not add any line markers and the output decoding only supports A-Za-z0-9+/.
  • URL: The output maps to a set of characters A-Za-z0-9+_ and the output is a URL and a file.
  • MIME: Output is implicitly shot into MIME-friendly format. Output no more than 76 characters per line and use '\r' followed by '\n' as a split. There is no line segmentation at the end of the encoding output.

Embedded class

No.Embedded Classes & Descriptions
1static class Base64.Decoder
This class implements a decoder for decoding byte data using Base64 encoding.
2static class Base64.Encoder
This class implements an encoder that encodes byte data using Base64 encoding.

method

No.Method Name & Description
1static Base64.Decoder getDecoder()
Returns a Base64.Decoder that decodes using the basic base64 encoding scheme.
2static Base64.Encoder getEncoder()
Returns a Base64.Encoder encoding using the basic base64 encoding scheme.
3static Base64.Decoder getMimeDecoder()
Returns a Base64.Decoder that decodes using a MIME-type base64 encoding scheme.
4
static Base64.Encoder getMimeEncoder()
Returns a Base64.Encoder encoding using a MIME-type base64 encoding scheme.
5static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)
Returns a Base64.Encoder encoding using a MIME-type base64 encoding scheme that specifies the length of each line and the delimiter of the line.
6static Base64.Decoder getUrlDecoder()
Returns a Base64.Decoder that decodes the base64 encoding scheme using URL and file name security.
7static Base64.Encoder getUrlEncoder()
Returns a Base64.Encoder encoding using a base64 encoding scheme with URL and filename security.
Note: Many methods of the Base64 class inherit from the java.lang.Object class.

Base64 instance

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 { // Use basic encoding String base64encodedString = Base64.getEncoder().encodeToString("bubble?java8".getBytes("utf-8")); System.out.println("Base64 Encoded String (Basic) :" + base64encodedString); // decode byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString); System.out.println("raw string: " + new String(base64decodedBytes, "utf-8")); base64encodedString = Base64.getUrlEncoder().encodeToString("bubble?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()); } } }
Execute the above script, the output is:
$ javac Java8Tester.java 
$ java Java8Tester 
Base64 Encoded String (Basic) :YnViYmxlP2phdmE4
raw string: bubble?java8
Base64 Encoded String (URL) :YnViYmxlP2phdmE4
Base64 Encoded String (MIME) :MjdkNjg1M2MtOWMyMy00MTU0LTk0NTAtNDgyZjJiYTIwOT
MwZTM5NGZmZDEtZWRkMi00YmNhLTk2YjctZjJhM2FlZjQwN2M2ZWJlMTViNGYtMzVhNS00MWM3LTl
mNTItNDkwZWY3ZjE1Y2Q5YmFiOTM4MmYtYTUyMS00N2I5LTkwY2EtNTM4OWU1ZjY2Mzc1NDI0YzUx
YzQtOTVjMC00YWQ1LThmOTUtOTIzMGVhNzlhYjU2MTNkNDFiMGYtOWIyYi00MzA5LWExOGYtZDY5Z
mUyMTA1NjJhZGI3MGJmZWUtY2ZiZC00OTUyLThjZjMtN2ZmN2I2MWQ1MjVmOWYwMWMxOWEtNGI1Ny
00ZmYyLWI3MzctYmQyZmI4Zjk0MzdmNWQzNmIzNjQtZjZkOC00ZDgwLTlkMmYtMzljZmQxZDNmNWF
iNjU5MzQ2NDktMGNkNi00ZjhjLWE3ZmQtZmMzZWViZjI2YTQy

Comments