Latest web development tutorials

Java8 Base64

Java 8 new features Java 8 new features


In the Java 8, Base64 encoding has become the standard Java class libraries.

Java 8 built Base64 encoded encoder and decoder.

Base64 utility class provides a static method to obtain the following three BASE64 codec:

  • Basic: output is mapped to a set of characters A-Za-z0-9 + /, do not add any line coding standard, decoded output supports only A-Za-z0-9 + /.
  • URL: output is mapped to a set of characters A-Za-z0-9 + _, output is a URL and file.
  • MIME: Output hidden incident MIME-friendly format.Each output line is not more than 76 characters, and '\ r' and follow the '\ n' as the split. Finally, no code output line segmentation.

Embedded class

No. Nested classes & Description
1 static class Base64.Decoder

This class implements a decoder for use Base64 encoding to decode the bytes of data.

2 static class Base64.Encoder

This class implements an encoder, use Base64 encoding to encode bytes of data.

method

No. Method Name & Description
1 static Base64.Decoder getDecoder ()

Returns a Base64.Decoder, decoding using the basic base64 encoding scheme.

2 static Base64.Encoder getEncoder ()

Returns a Base64.Encoder, base64 encoded using basic coding scheme.

3 static Base64.Decoder getMimeDecoder ()

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

4

static Base64.Encoder getMimeEncoder ()

Returns a Base64.Encoder, base64 encoded using MIME type coding scheme.

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

Returns a Base64.Encoder, base64 encoded using MIME type coding scheme, you can specify the length of each line and a line separator through parameters.

6 static Base64.Decoder getUrlDecoder ()

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

7 static Base64.Encoder getUrlEncoder ()

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

Note: Many methods Base64 class inherits from java.lang.Objectclass.


Base64 examples

The following example demonstrates the use Base64:

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

public class Java8Tester {
   public static void main(String args[]){
      try {
		
         // 使用基本编码
         String base64encodedString = Base64.getEncoder().encodeToString("w3big?java8".getBytes("utf-8"));
         System.out.println("Base64 比那么字符串 (基本) :" + base64encodedString);
		
         // 解码
         byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
		
         System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8"));
         base64encodedString = Base64.getUrlEncoder().encodeToString("TutorialsPoint?java8".getBytes("utf-8"));
         System.out.println("Base64 编码字符串 (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 编码字符串 (MIME) :" + mimeEncodedString);
         
      }catch(UnsupportedEncodingException e){
         System.out.println("Error :" + e.getMessage());
      }
   }
}

Implementation of the above script, output is:

$ javac Java8Tester.java 
$ java Java8Tester
原始字符串: w3big?java8
Base64 编码字符串 (URL) :VHV0b3JpYWxzUG9pbnQ_amF2YTg=
Base64 编码字符串 (MIME) :MjY5OGRlYmEtZDU0ZS00MjY0LWE3NmUtNzFiNTYwY2E4YjM1NmFmMDFlNzQtZDE2NC00MDk3LTlh
ZjItYzNkNGJjNmQwOWE2OWM0NDJiN2YtOGM4Ny00MjhkLWJkMzgtMGVlZjFkZjkyYjJhZDUwYzk0
ZWMtNDE5ZC00MTliLWEyMTAtZGMyMjVkYjZiOTE3ZTkxMjljMTgtNjJiZC00YTFiLTg3MzAtOTA0
YzdjYjgxYjQ0YTUxOWNkMTAtNjgxZi00YjQ0LWFkZGMtMzk1YzRkZjIwMjcyMzA0MTQzN2ItYzBk
My00MmQyLWJiZTUtOGM0MTlmMWIxM2MxYTY4NmNiOGEtNTkxZS00NDk1LThlN2EtM2RjMTZjMWJk
ZWQyZTdhNmZiNDgtNjdiYy00ZmFlLThjNTYtMjcyNDNhMTRhZTkyYjNiNWY2MmEtNTZhYS00ZDhk
LWEwZDYtY2I5ZTUwNzJhNGE1

Java 8 new features Java 8 new features