Java8 Base64

Java 8 新特性Java 8 New Features


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

Java 8 has built-in Base64 encoders and decoders.

The Base64 utility class provides a set of static methods to obtain the following three types of BASE64 encoders/decoders:

  • Basic:The output is mapped to a set of characters A-Za-z0-9+/, the encoding does not add any line breaks, and the decoding of the output only supports A-Za-z0-9+/.
  • URL:The output is mapped to a set of characters A-Za-z0-9+_, and the output is URL and filename safe.
  • MIME:The output is mapped to MIME-friendly format. Each line of output is no more than 76 characters, and uses '\r' followed by '\n' as the separator. The encoded output has no line separator at the end.

Nested Classes

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

This class implements a decoder for decoding byte data using Base64 encoding.

2 static class Base64.Encoder

This class implements an encoder for encoding byte data using Base64 encoding.

Methods

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

Returns a Base64.Decoder that decodes using the basic Base64 encoding scheme.

2 static Base64.Encoder getEncoder()

Returns a Base64.Encoder that encodes using the basic Base64 encoding scheme.

3 static Base64.Decoder getMimeDecoder()

Returns a Base64.Decoder that decodes using the MIME type Base64 encoding 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, and the line length and line separator can be specified via parameters.

6 static Base64.Decoder getUrlDecoder()

Returns a Base64.Decoder that decodes using the URL and filename safe Base64 encoding scheme.

7 static Base64.Encoder getUrlEncoder()

Returns a Base64.Encoder that encodes using the URL and filename safe Base64 encoding scheme.

Note:Many methods of the Base64 class are inherited fromjava.lang.Objectthe class.


Base64 Example

The following example demonstrates the use of Base64:

Java8Tester.java file

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("example?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("example?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()); } } }

Executing the above script, the output is:

$ javac Java8Tester.java 
$ java Java8Tester
原始字符串: example?java8
Base64 编码字符串 (URL) :VHV0b3JpYWxzUG9pbnQ_amF2YTg=
Base64 编码字符串 (MIME) :M2Q4YmUxMTEtYWRkZi00NzBlLTgyZDgtN2MwNjgzOGY2NGFlOTQ3NDYyMWEtZDM4ZS00YWVhLTkz
OTYtY2ZjMzZiMzFhNmZmOGJmOGI2OTYtMzkxZi00OTJiLWEyMTQtMjgwN2RjOGI0MTBmZWUwMGNk
NTktY2ZiZS00MTMxLTgzODctNDRjMjFkYmZmNGM4Njg1NDc3OGItNzNlMC00ZWM4LTgxNzAtNjY3
NTgyMGY3YzVhZWQyMmNiZGItOTIwZi00NGUzLTlkMjAtOTkzZTI1MjUwMDU5ZjdkYjg2M2UtZTJm
YS00Y2Y2LWIwNDYtNWQ2MGRiOWQyZjFiMzJhMzYxOWQtNDE0ZS00MmRiLTk3NDgtNmM4NTczYjMx
ZDIzNGRhOWU4NDAtNTBiMi00ZmE2LWE0M2ItZjU3MWFiNTI2NmQ2NTlmMTFmZjctYjg1NC00NmE1
LWEzMWItYjk3MmEwZTYyNTdk

Java 8 新特性Java 8 New Features

Other Extensions