English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input and output (I/O)

Java Reader/Java Writer

Java other topics

Java8 Base64

Java 8 New Features

In Java 8In, Base64Encoding has become a standard of Java class library.

Java 8 Built-in Base64 Encoding encoder and decoder.

Base64The utility class provides a set of static methods to obtain the following three BASE64Encoder/Decoder:

  • Basic:Output is mapped to a set of characters A-Za-z0-9+/, encoding does not add any line indicators, and the decoded output only supports A-Za-z0-9+/.

  • URL:Output is mapped to a set of characters A-Za-z0-9+_, the output is URL and file.

  • MIME:Output is mapped to MIME-friendly format. Each line does not exceed76Characters, and uses '\r' followed by '\n' as separators. The encoded output does not have line separators at the end.

Nested class

NumberNested class & description
1static class Base64.Decoder

.Decoder to decode byte data.64 an encoder that uses Base

2static class Base64.Encoder

This class implements an encoder that uses Base64 Encodes byte data using encoding.

Method

NumberMethod name & description
1static Base64.Decoder getDecoder()

Returns a Base64.Decoder, decoding uses basic type base64 encoding scheme.

2static Base64.Encoder getEncoder()

Returns a Base64.Encoder, encoding uses basic type base64 encoding scheme.

3static Base64.Decoder getMimeDecoder()

Returns a Base64.Decoder, decoding uses MIME type base64 encoding scheme.

4

static Base64.Encoder getMimeEncoder()

Returns a Base64.Encoder, encoding uses MIME type base64 encoding scheme.

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

Returns a Base64.Encoder, encoding uses MIME type base64 Encoding scheme can be specified by parameters to specify the length of each line and the line separator.

6static Base64.Decoder getUrlDecoder()

Returns a Base64.Decoder, decoding uses URL and filename safe base64 encoding scheme.

7static Base64.Encoder getUrlEncoder()

Returns a Base64.Encoder, encoding uses URL and filename safe base64 encoding scheme.

Note:Base64 Many methods of the class are inherited from java.lang.Object class inheritance.

Base64 Example

The following examples demonstrate the use of Base64 Usage:

import java.util.Base64;
import java.util.UUID;
import java.io.UnsupportedEncodingException;
 
public class Java8Tester {
   public static void main(String args[]){
      try {
        
         // Using basic encoding
         String base64encodedString = Base64.getEncoder().encodeToString("w3codebox?java8".getBytes("utf-8"));
         System.out.println("Base64 Encoding string (Basic): " + base64encodedString);
        
         // Decoding
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
        
         System.out.println("Original string: "); + new String(base64decodedBytes, "utf-8"));
         base64encodedString = Base64.getUrlEncoder().encodeToString("w3codebox?java8".getBytes("utf-8"));
         System.out.println("Base64 Encoding 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 Encoding string (MIME): " + mimeEncodedString);
         
      }catch(UnsupportedEncodingException e){
         System.out.println("Error: "); + e.getMessage());
      }
   }
}

Execute the above script, the output is:

$ javac Java8Tester.java 
$ java Java8Tester
Original string: w3codebox?java8
Base64 Encoding string (URL): VHV0b3JpYWxzUG9pbnQ_amF2YTg=
Base64 Encoding string (MIME): M2Q4YmUxMTEtYWRkZi00NzBlLTgyZDgtN2MwNjgzOGY2NGFlOTQ3NDYyMWEtZDM4ZS00YWVhLTkz
OTYtY2ZjMzZiMzFhNmZmOGJmOGI2OTYtMzkxZi00OTJiLWEyMTQtMjgwN2RjOGI0MTBmZWUwMGNk
NTktY2ZiZS00MTMxLTgzODctNDRjMjFkYmZmNGM4Njg1NDc3OGItNzNlMC00ZWM4LTgxNzAtNjY3
NTgyMGY3YzVhZWQyMmNiZGItOTIwZi00NGUzLTlkMjAtOTkzZTI1MjUwMDU5ZjdkYjg2M2UtZTJm
YS00Y2Y2LWIwNDYtNWQ2MGRiOWQyZjFiMzJhMzYxOWQtNDE0ZS00MmRiLTk3NDgtNmM4NTczYjMx
ZDIzNGRhOWU4NDAtNTBiMi00ZmE2LWE0M2ItZjU3MWFiNTI2NmQ2NTlmMTFmZjctYjg1NC00NmE1
LWEzMWItYjk3MmEwZTYyNTdk

Java 8 New Features