Java 8 Base64 class

In Java 8, A new class Base64 is added that contains static methods for obtaining encoders and decoders for the Base64 encoding scheme. It supports three types of encoding and decoding as specified in RFC 4648 and RFC 2045. For more about RFC visit this link.

  • Basic Encoding and Decoding

    In this type of encoding and decoding, Base64 uses "The Base64 Alphabet" specified in RFC 4648 and RFC 2045. The encoder does not add any extra line feed character. While decoding, decoder rejects data that contains characters outside the base64 alphabet.

  • URL Encoding and Decoding

    Here, Base64 uses the "URL and Filename safe Base64 Alphabet" as specified in RFC 4648. The encoder does not add any extra line feed character. while decoder rejects data that contains characters that are outside the base64 alphabet.

  • MIME Encoding and Decoding

    In this encoding and decoding, Base64 uses the "The Base64 Alphabet" as specified in RFC 2045. The encoded output must be represented in lines of no more than 76 characters. No line separator is added to the end of the encoded output.

Base64 Declaration

public class Base64 extends Object

Base64 class Methods

The following are the methods of the Base64 class that is used to encoding and decoding for Basic, Url, and Mime type.

Modifier and Type

Method and Description

static Base64.Decoder getDecoder()

It returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme.

static Base64.Encoder getEncoder()

It returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme.

static Base64.Decoder getMimeDecoder()

It returns a Base64.Decoder that decodes using the MIME type base64 decoding scheme.

static Base64.Encoder getMimeEncoder()

It returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme.

static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator)

It returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators.

static Base64.Decoder getUrlDecoder()

It returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme.

static Base64.Encoder getUrlEncoder()

It returns a Base64.Encoder that encodes using the URL and Filename safe type base64 encoding scheme.

Example: Basic Encoding and Decoding

In this example, we are using basic type encoding and decoding operations to encode and decode a string message. We use a StudyToNight word to encode using encodeToString() method and decode() method to decode the encoded message.

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

public class STDemo {
	public static void main(String[] args) throws UnsupportedEncodingException {
		
		// Encoding
		Base64.Encoder be = Base64.getEncoder();
		byte[] msg = "StudyToNight".getBytes();
		String encodeMsg = be.encodeToString(msg);
		System.out.println(encodeMsg);
		// Decoding
		Base64.Decoder bd = Base64.getDecoder();
		byte[] decodeMsg = bd.decode(encodeMsg);
        String dStr = new String(decodeMsg);  
        System.out.println(dStr);
	}
}


U3R1ZHlUb05pZ2h0
StudyToNight

Example: Url Encoding and Decoding

In this example, we are using Url encoding and decoding operations to encode a Url. Here, we used home Url of studytonight website to encode using the getUrlEncoder() method and getUrlDecoder() to decode the Url.

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

public class STDemo {
	public static void main(String[] args) throws UnsupportedEncodingException {
		
		// Encoding
		Base64.Encoder be = Base64.getUrlEncoder();
		byte[] url = "https://www.studytonight.com".getBytes();
		String encodeUrl = be.encodeToString(url);
		System.out.println(encodeUrl);
		// Decoding
		Base64.Decoder bd = Base64.getUrlDecoder();
		byte[] decodeUrl = bd.decode(encodeUrl);
        String dUrl = new String(decodeUrl);  
        System.out.println(dUrl);
	}
}


aHR0cHM6Ly93d3cuc3R1ZHl0b25pZ2h0LmNvbQ==
https://www.studytonight.com

Example: MIME Encoding and Decoding

In this example, we are encoding and decoding a text file content by using the getMimeEncoder() and getMimeDecoder() methods.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

public class STDemo {
	public static void main(String[] args) throws IOException {
		
		byte[] bytes = Files.readAllBytes(Paths.get("abc.txt"));
		// Encoding
		Base64.Encoder be = Base64.getMimeEncoder();
		String encodeFile = be.encodeToString(bytes);
		System.out.println(encodeFile);
		// Decoding
		Base64.Decoder bd = Base64.getUrlDecoder();
		byte[] decodeFile = bd.decode(encodeFile);
        String dFile = new String(decodeFile);  
        System.out.println(dFile);
	}
}


VGhpcyBpcyBlbmNyeXB0ZWQgZmlsZQ==
This is encrypted file