logo

Base64


Show

Base64 has finally got its due with Java 8. There is an inbuilt encoder and decoder for encoding in Java 8. In Java 8, we will use three sorts of Base64 encoding.

  • Simple − Output is mapped to a group of characters lying in A-Za-z0-9+/. The encoder doesn't add any printing operation in output, and therefore the decoder rejects any character aside from A-Za-z0-9+/.
  • URL − Output is mapped to a line of characters lying in A-Za-z0-9+_. Output is URL and filename safe.
  • MIME − Its Output is mapped to MIME-friendly format easily. Output is acted in lines of no quite 76 characters each and uses a printing operation '\r' followed by a linefeed '\n' because of the line separator. No line separator is present at the top of the encoded output.

Nested Classes

Sr.No.

Nested class & Description

1

static class Base64.Decoder

This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045.

2

static class Base64.Encoder

This 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

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 decoding 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 specified line length and line separators.

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.

Base64 Example

Create the subsequent Java program using any editor of your choice in say C:/> JAVA.

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

public class HelloWorld {

   public static void main(String args[]) {

      try {
         // Encode using basic encoder
         String base64encodedString = Base64.getEncoder().encodeToString(
            "TutorialsPoint?java8".getBytes("utf-8"));
         System.out.println("Base64 Encoded String (Basic) :" + base64encodedString);
         // Decode
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
         System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
         base64encodedString = Base64.getUrlEncoder().encodeToString(
            "TutorialsPoint?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());
      }
   }
}

Check Your Result By Using:

Assemble the whole lesson by using javac compile as given:

C:\JAVA>javac Java8Tester.java

Run the Java8Tester same as given below-

C:\JAVA>java Java8Tester

Your Outcome must be:

Base64 Encoded String (Basic) :VHV0b3JpYWxzUG9pbnQ/amF2YTg=
Original String: TutorialsPoint?java8
Base64 Encoded String (URL) :VHV0b3JpYWxzUG9pbnQ_amF2YTg=
Base64 Encoded String (MIME) :YmU3NWY2ODktNGM5YS00ODlmLWI2MTUtZTVkOTk2YzQ1Njk1Y2EwZTg2OTEtMmRiZC00YTQ1LWJl
NTctMTI1MWUwMTk0ZWQyNDE0NDAwYjgtYTYxOS00NDY5LTllYTctNjc1YzE3YWJhZTk1MTQ2MDQz
NDItOTAyOC00ZWI0LThlOTYtZWU5YzcwNWQyYzVhMTQxMWRjYTMtY2MwNi00MzU0LTg0MTgtNGQ1
MDkwYjdiMzg2ZTY0OWU5MmUtZmNkYS00YWEwLTg0MjQtYThiOTQxNDQ2YzhhNTVhYWExZjItNjU2
Mi00YmM4LTk2ZGYtMDE4YmY5ZDZhMjkwMzM3MWUzNDMtMmQ3MS00MDczLWI0Y2UtMTQxODE0MGU5
YjdmYTVlODUxYzItN2NmOS00N2UyLWIyODQtMThlMWVkYTY4M2Q1YjE3YTMyYmItZjllMS00MTFk
LWJiM2UtM2JhYzUxYzI5OWI4