Java 8

Java8 – Base64

With Java 8, Base64 has finally got its due. Java 8 now has inbuilt encoder and decoder for Base64 encoding. In Java 8, we can use three types of Base64 encoding.

  • Simple − Output is mapped to a set of characters lying in A-Za-z0-9+/. The encoder does not add any line feed in output, and the decoder rejects any character other than A-Za-z0-9+/.
  • URL − Output is mapped to set of characters lying in A-Za-z0-9+_. Output is URL and filename safe.
  • MIME − Output is mapped to MIME friendly format. Output is represented in lines of no more than 76 characters each, and uses a carriage return ‘\r’ followed by a linefeed ‘\n’ as the line separator. No line separator is present to the end of the encoded output.

Nested Classes

Sr.No.Nested class & Description
1static class Base64.DecoderThis class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.
2static class Base64.EncoderThis class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

Methods

Sr.No.Method Name & Description
1static Base64.Decoder getDecoder()Returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.
2static Base64.Encoder getEncoder()Returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme.
3static Base64.Decoder getMimeDecoder()Returns a Base64.Decoder that decodes using the MIME type base64 decoding scheme.
4static Base64.Encoder getMimeEncoder()Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme.
5static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators.
6static Base64.Decoder getUrlDecoder()Returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.
7static Base64.Encoder getUrlEncoder()Returns a Base64.Encoder that encodes using the URL and Filename safe type base64 encoding scheme.

Methods Inherited

This class inherits methods from the following class −

  • java.lang.Object

Base64 Example

Create the following Java program using any editor of your choice in say C:\hitechpoints\workspace\java8\src>.

Java8Base64.java

import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;

public class Java8Base64 {

   public static void main(String args[]) {

      try {
		String originalStr = "hitechpoints?version=java8";
    	  
         // 1. Encode using basic encoder
         String base64encodedString = Base64.getEncoder().encodeToString(
        		 originalStr.getBytes("utf-8"));
         System.out.println("Base64 Encoded String (Basic) :" + base64encodedString);
		
         // 2. Decode the encoded string using basic decoder
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);	
         System.out.println("Original String - Decode using Basic: " + new String(base64decodedBytes, "utf-8"));
         
         // 3. Encode using url encoder
         base64encodedString = Base64.getUrlEncoder().encodeToString(
        		 originalStr.getBytes("utf-8"));
         System.out.println("Base64 Encoded String (URL) :" + base64encodedString);
         
         // 4. Decode the encoded string using URL decoder
         base64decodedBytes = Base64.getUrlDecoder().decode(base64encodedString);
         System.out.println("Original String - Decode using URL: " + new String(base64decodedBytes, "utf-8"));
		
         // 5. Encode using MIME encoder
         StringBuilder stringBuilder = new StringBuilder();
         for (int i = 0; i < 10; ++i) {
            stringBuilder.append(UUID.randomUUID().toString());
         }
		 System.out.println("Original string for MIME encoder: " + stringBuilder.toString());
         byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8");
         String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes);
         System.out.println("Base64 Encoded String (MIME) :" + mimeEncodedString);
         
         // 6. Decode the encoded string using MIME decoder
         base64decodedBytes = Base64.getMimeDecoder().decode(mimeEncodedString);
         System.out.println("Original String - Decode using MIME: " + new String(base64decodedBytes, "utf-8"));
		
      } catch(UnsupportedEncodingException e) {
         System.out.println("Error :" + e.getMessage());
      }
   }
}

Output

Compile the class using javac compiler as follows −

C:\hitechpoints\workspace\java8\src>javac Java8Base64.java

Now run the Java8Base64 as follows −

C:\hitechpoints\workspace\java8\src>java Java8Base64

It should produce the following output −

Base64 Encoded String (Basic) :aGl0ZWNocG9pbnRzP3ZlcnNpb249amF2YTg=
Original String - Decoded using Basic: hitechpoints?version=java8
Base64 Encoded String (URL) :aGl0ZWNocG9pbnRzP3ZlcnNpb249amF2YTg=
Original String - Decoded using URL: hitechpoints?version=java8
Original string for MIME encoder: 6e8ddb51-0846-46de-8282-03d047e18eb1b1820184-1f37-428e-8207-b1923045755d1d33f716-fcd8-4f71-9df1-bfd1dbe3c722a057822c-b225-46e0-9ca8-4c485af75402daa35fcb-8f32-4e2d-9e12-f232145c761976d94766-4184-4250-861d-f2e1a3777627d558a276-29fd-4d32-9999-3d87f664744263384c92-9bfa-461a-8cc4-649b6e7a09454b705e42-3750-45de-ad8c-e46f729c3351b8cdfdcc-0d04-490f-8b33-e99944c9c7a6
Base64 Encoded String (MIME) :NmU4ZGRiNTEtMDg0Ni00NmRlLTgyODItMDNkMDQ3ZTE4ZWIxYjE4MjAxODQtMWYzNy00MjhlLTgy
MDctYjE5MjMwNDU3NTVkMWQzM2Y3MTYtZmNkOC00ZjcxLTlkZjEtYmZkMWRiZTNjNzIyYTA1Nzgy
MmMtYjIyNS00NmUwLTljYTgtNGM0ODVhZjc1NDAyZGFhMzVmY2ItOGYzMi00ZTJkLTllMTItZjIz
MjE0NWM3NjE5NzZkOTQ3NjYtNDE4NC00MjUwLTg2MWQtZjJlMWEzNzc3NjI3ZDU1OGEyNzYtMjlm
ZC00ZDMyLTk5OTktM2Q4N2Y2NjQ3NDQyNjMzODRjOTItOWJmYS00NjFhLThjYzQtNjQ5YjZlN2Ew
OTQ1NGI3MDVlNDItMzc1MC00NWRlLWFkOGMtZTQ2ZjcyOWMzMzUxYjhjZGZkY2MtMGQwNC00OTBm
LThiMzMtZTk5OTQ0YzljN2E2
Original String - Decoded using MIME: 6e8ddb51-0846-46de-8282-03d047e18eb1b1820184-1f37-428e-8207-b1923045755d1d33f716-fcd8-4f71-9df1-bfd1dbe3c722a057822c-b225-46e0-9ca8-4c485af75402daa35fcb-8f32-4e2d-9e12-f232145c761976d94766-4184-4250-861d-f2e1a3777627d558a276-29fd-4d32-9999-3d87f664744263384c92-9bfa-461a-8cc4-649b6e7a09454b705e42-3750-45de-ad8c-e46f729c3351b8cdfdcc-0d04-490f-8b33-e99944c9c7a6

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs