Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java8 Base64

Java8 Base64

Java 8 New Features Java 8 New Features


In Java 8, Base64 encoding has become a standard of 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 BASE64 codecs:

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.

Method

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 base64 encoding scheme.

4

static Base64.Encoder getMimeEncoder()

Returns a Base64.Encoder that encodes using the MIME base64 encoding scheme.

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

Returns a Base64.Encoder that encodes using the MIME base64 encoding scheme, with the line length and line separator specifiable 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 from the java.lang.Object 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

(

"

runoob?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

(

"

runoob?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 produces the following output:


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

Java 8 New Features Java 8 New Features