Java 8 will be remembered for introducing lambdas, streams, the new date/time model and the Nashorn JavaScript engine to Java. Some will remember Java 8 because it introduced various small but useful features, such as the Base64 API. What is Base64 and how do you use this API? This article answers these questions.

What is Base64?

Base64

ASCII

Base64 requests comment files

Base64 was first described (but not named) in RFC 1421: Privacy Enhancement for Internet E-mail: Part 1: Message encryption and authentication Processes. It was later formally presented in RFC 2045 as Base64: Multipurpose Internet Mail Extensions (MIME) Part I: The format of the Body of an Internet message, and subsequently reaccessed in RFC 4648: Base16, Base32, and Base64 data encodings.

Base64 is used to prevent data from being modified during transmission by information systems, such as E-mail, that may not be 8-bit clean (they may be 8-bit values). For example, you attach an image to an E-mail message and want it to arrive at the other end without garbled characters. Your email software Base64 encodes the image and inserts the equivalent text into the message, as shown below:

Content-Disposition: inline;
	filename=IMG_0006.JPG
Content-Transfer-Encoding: base64

/9j/4R/+RXhpZgAATU0AKgAAAAgACgEPAAIAAAAGAAAAhgEQAAIAAAAKAAAAjAESAAMAAAABAAYA
AAEaAAUAAAABAAAAlgEbAAUAAAABAAAAngEoAAMAAAABAAIAAAExAAIAAAAHAAAApgEyAAIAAAAU
AAAArgITAAMAAAABAAEAAIdpAAQAAAABAAAAwgAABCRBcHBsZQBpUGhvbmUgNnMAAAAASAAAAAEA
...
NOMbnDUk2bGh26x2yiJcsoBIrvtPe3muBbTRGMdeufmH+Nct4chUXpwSPk/qK9GtJRMWWVFbZ0JH
I4rf2dkZSbOjt7hhEzwcujA4I7Gust75pYVwAPpXn+kzNLOVYD7xFegWEKPkHsM/pU1F0NKbNS32
o24sSCOlaaFYLUhjky4x9PSsKL5bJsdWkAz3xirH2dZLy1DM2C44zx1FZqL2PTXY/9k=
Copy the code

The illustration shows this encoded image beginning with/and ending with =. In the… Indicates undisplayed text. Note that the entire encoding for this or any other example is approximately 33% larger than the original binary data.

The recipient’s E-mail software will Base64 decode the encoded text image to restore the original binary image. For this example, the image is displayed along with the rest of the message.

Base64 encoding and decoding

Base64 relies on simple encoding and decoding algorithms. They use a us-ASCII subset of 65 characters, where each of the first 64 characters is mapped to an equivalent 6-bit binary sequence. Here’s the alphabet:

Value Encoding Value Encoding Value Encoding Value Encoding 0 A 17 R 34 i 51 z 1 B 18 S 35 j 52 0 2 C 19 T 36 k 53 1 3 D  20 U 37 l 54 2 4 E 21 V 38 m 55 3 5 F 22 W 39 n 56 4 6 G 23 X 40 o 57 5 7 H 24 Y 41 p 58 6 8 I 25 Z 42 q 59 7 9 J 26 a 43 r 60 8 10 K 27 b 44 s 61 9 11 L 28 c 45 t 62 + 12 M 29 d 46 u 63 / 13 N 30 e 47 v 14 O 31 f 48 w (pad) = 15 P 32 g 49  x 16 Q 33 h 50 yCopy the code

The 65th character (=) is used to fill base64-encoded text to an integer size, as described below.

The encoding algorithm receives an 8-byte input stream. Assume that the stream is first sorted by the most significant bit: the first bit is the highest in the first byte, the eighth bit is the lowest in the byte, and so on.

From left to right, the bytes are organized into 24-bit groups. Each group is considered a 6-bit group of four connected. Each 6-bit group is indexed as an array of 64 printable characters; Output result character.

When there are fewer than 24 bits available at the end of the encoded data, zero bits are added (on the right) to form integer 6-bit groups. You can then print one or two = fill characters. There are two cases to consider:

  • One remaining byte: Four zero bits are appended to this byte to form two 6-bit groups. Each group indexes the array and outputs the resulting character. After these two characters, print two=Padding characters.
  • The remaining two bytes: two zero bits are appended to the second byte, forming three 6-bit groups. Each group indexes the array and outputs the resulting character. After these three characters, print one=Padding characters.

Let’s consider three examples to see how the coding algorithm works. First, suppose we want to code @! * :

Source ASCII bit sequences with prepended 0 bits to form 8-bit bytes:

@        !        *
01000000 00100001 00101010

Dividing this 24-bit group into four 6-bit groups yields the following:

010000 | 000010 | 000100 | 101010

These bit patterns equate to the following indexes:

16 2 4 42

Indexing into the Base64 alphabet shown earlier yields the following encoding:

QCEq
Copy the code

We will continue to shorten the input sequence to @! :

Source ASCII bit sequences with prepended 0 bits to form 8-bit bytes:

@        !       
01000000 00100001

Two zero bits are appended to make three 6-bit groups:

010000 | 000010 | 000100

These bit patterns equate to the following indexes:

16 2 4

Indexing into the Base64 alphabet shown earlier yields the following encoding:

QCE

An = pad character is output, yielding the following final encoding:

QCE=
Copy the code

The final example shortens the input sequence to @ :

Source ASCII bit sequence with prepended 0 bits to form 8-bit byte:

@       
01000000

Four zero bits are appended to make two 6-bit groups:

010000 | 000000

These bit patterns equate to the following indexes:

16 0

Indexing into the Base64 alphabet shown earlier yields the following encoding:

QA

Two = pad characters are output, yielding the following final encoding:

QA==
Copy the code

The decoding algorithm is the inverse of the encoding algorithm. However, if a character that is not in the Base64 alphabet or the padding character number is incorrect is detected, you are free to take appropriate action.

Base64 variant

Several Base64 variants have been designed. Some variations require that the encoded output stream be divided into multiple lines of fixed length, each line not exceeding a certain length limit, and (except for the last line) separated from the next line by a line separator (carriage return \r followed by a line newline \n). I described three variants of Java 8’s Base64 API support. See Wikipedia’s Base64 entry for a complete list of variations.

Basic

RFC 4648 describes a system called

Basic

Base64 variant. This variant is encoded and decoded using the Base64 alphabet shown in Table 1 of RFC 4648 and RFC 2045 (and shown earlier in this article). The encoder treats the encoded output stream as a line; No output line delimiters. The decoder rejects encodings that contain characters other than the Base64 alphabet. Note that these and other provisions can be overridden.

MIME

RFC 2045 describes a system called

MIME

Base64 variant. This variant is encoded and decoded using the Base64 alphabet provided in Table 1 of RFC 2045. The encoded output stream is organized into lines of no more than 76 characters; Each line (except the last) is separated from the next by a line separator. All line delimiters or other characters not found in the Base64 alphabet are ignored during decoding.

URL and Filename Safe

RFC 4648 describes a system called

URL and file name security

Base64 variant. This variant is encoded and decoded using the Base64 alphabet provided in Table 2 of RFC 4648. The alphabet is the same as the letters shown above, except that -replaces + and _ replaces /. No line delimiters are printed. The decoder rejects encodings that contain characters other than the Base64 alphabet.

Base64 encoding is useful in the context of lengthy binary data and HTTP GET requests. The idea is to encode this data and then attach it to the HTTP GET URL. If Basic or MIME variants are used, any + or/characters in the encoded data must be urL-encoded into a hexadecimal sequence (+ becomes %2B and/becomes %2F). The generated URL string is slightly longer. By replacing + same – and/same _, URL and filename security eliminates the need for URL encoders/decoders (and the length effects of their encoded values). In addition, this variant is useful when encoding data for filenames, because Unix and Windows filenames cannot contain slashes.

Use Java’s Base64 API

Java 8 introduces a Base64 API, including the java.util.Base64 class and its nested static classes Encoder and Decoder. Base64 has several static methods for obtaining encoders and decoders:

  • Base64.Encoder getEncoder(): Returns the encoder for the Basic variant.
  • Base64.Decoder getDecoder(): Returns a decoder for the Basic variant.
  • Base64.Encoder getMimeEncoder(): Returns the encoder for the MIME variant.
  • Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator): returns with a givenlineLengthThe encoder for the modified MIME variant (rounded down to the nearest multiple of 4 – output inlineLength<= 0 does not divide into lines) andlineSeparator. whenlineSeparatorIt throws when it contains any of the Base64 alphabetic characters listed in Table 1 of RFC 2045java.lang.IllegalArgumentException.getMimeEncoder()The RFC 2045 encoder returned by the method is quite strict. For example, the encoder creates encoded text with a fixed line length of 76 characters, except for the last line. This is required if you want the encoder to support RFC 1421, which specifies a fixed line length of 64 charactersgetMimeEncoder(int lineLength, byte[] lineSeparator).
  • Base64.Decoder getMimeDecoder(): Returns a decoder for the MIME variant.
  • Base64.Encoder getUrlEncoder(): returns the encoder for the URL and Filename Safe variant.
  • Base64.Decoder getUrlDecoder(): returns the decoder for the URL and Filename Safe variant.

. Base64 Encoder is proposed several thread-safe instance methods for coding sequence of bytes Pass null reference to one of the following methods will lead to Java. Lang. NullPointerException:

  • byte[] encode(byte[] src)Will:srcAll bytes are encoded into the newly allocated byte array and the result is returned.
  • int encode(byte[] src, byte[] dst): encodingsrcAll bytes todst(starts at offset 0). ifdstNot enough to save the encodingIllegalArgumentException. Otherwise, return to writedstNumber of bytes.
  • ByteBuffer encode(ByteBuffer buffer)Will:bufferAll remaining bytes are encoded into the newly allocatedjava.nio.ByteBufferIn the object. After return,bufferPosition is updated to its limit; Its limit doesn’t change. The position of the returned output buffer will be zero, and its limit will be the number of resulting encoded bytes.
  • String encodeToString(byte[] src)Will:srcAll bytes are encoded as a string and the string is returned. Calling this method is equivalent to executingnew String(encode(src), StandardCharsets.ISO_8859_1).
  • Base64.Encoder withoutPadding(): Returns the encoder equivalent to this encoder, but without adding any padding characters to the end of the encoded byte data.
  • OutputStream wrap(OutputStream os): wraps the output stream to encode byte data. It is recommended that the returned output stream be closed immediately after use, during which time it flusher all possible remaining bytes to the underlying output stream. Closing the returned output stream closes the underlying output stream.

Base64.Decoder proposes several thread-safe instance methods to decode byte sequences. Passing an empty reference to one of the following methods results in a NullPointerException:

  • byte[] decode(byte[] src)Will:srcAll bytes are decoded into the newly allocated byte array and returned. Thrown if Base64 is invalidIllegalArgumentException.
  • int decode(byte[] src, byte[] dst): decodingsrcAll bytes todst(starting at offset 0). ifdstInsufficient to save decoding, or thrown if Base64 is invalidIllegalArgumentException. Otherwise, return to writedstNumber of bytes.
  • byte[] decode(String src)Will:srcAll bytes are decoded into the newly allocated byte array and that byte array is returned. Calling this method is equivalent to callingdecode(src.getBytes(StandardCharsets.ISO_8859_1)). Thrown if Base64 is invalidIllegalArgumentException.
  • ByteBuffer decode(ByteBuffer buffer)Will:bufferAll bytes decoded as newly allocatedjava.nio.ByteBufferObject. After return,bufferIts position will be updated to its limit; Its limit doesn’t change. The position of the returned output buffer will be zero, and its limit will be the number of decoded bytes generated. Thrown if Base64 is invalidIllegalArgumentException. In this case,bufferThe location is not updated.
  • InputStream wrap(InputStream is): wraps the input stream to decode bytes of data. When the Base64 input is invalid,isThe object’sread()Method to throw ajava.io.IOException. Closing the returned output stream closes the underlying output stream.

Hello, Base64

Java’s Base64 API is easy to use. Consider a “Hello, World” style program that encodes Base64 using a Basic encoder and then Base64 decoding of encoded text using a Basic decoder. Listing 1 shows the source code.

Listing 1. HelloBase64.java

import java.util.Base64;

public class HelloBase64
{
   public static void main(String[] args)
   {
      String msg = "Hello, Base64!";
      Base64.Encoder enc = Base64.getEncoder();
      byte[] encbytes = enc.encode(msg.getBytes());
      for (int i = 0; i < encbytes.length; i++)
      {
         System.out.printf("%c", (char) encbytes[i]);
         if(i ! = 0 && i % 4 == 0) System.out.print(' '); } System.out.println(); Base64.Decoder dec = Base64.getDecoder(); byte[] decbytes = dec.decode(encbytes); System.out.println(new String(decbytes)); }}Copy the code

Compile Listing 1 as follows:

javac HelloBase64.javaCopy the code

Run the generated application as follows:

java HelloBase64Copy the code

You should observe the following output:

SGVsb G8sI EJhc 2U2N CE=
Hello, Base64!Copy the code

File encoding and decoding

Base64 is more useful for encoding files. I’ve created a second application that demonstrates this usefulness and more Base64 apis. Listing 2 shows the source code for the application.

Listing 2. FileEncDec.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;

import java.util.Base64;

public class FileEncDec
{
   public static void main(String[] args)
   {
      if(args.length ! = 1) { System.err.println("usage: java FileEncDec filename");
         return;
      }
      try (FileInputStream fis = new FileInputStream(args[0]))
      {
         Base64.Encoder enc1 = Base64.getEncoder();
         Base64.Encoder enc2 = Base64.getMimeEncoder();
         Base64.Encoder enc3 = Base64.getUrlEncoder();
         OutputStream os1 = enc1.wrap(new FileOutputStream(args[0] + "1.enc"));
         OutputStream os2 = enc2.wrap(new FileOutputStream(args[0] + "2.enc"));
         OutputStream os3 = enc3.wrap(new FileOutputStream(args[0] + "3.enc"));
         int _byte;
         while((_byte = fis.read()) ! = -1) { os1.write(_byte); os2.write(_byte); os3.write(_byte); } os1.close(); os2.close(); os3.close(); } catch (IOException ioe) { System.err.printf("I/O error: %s%n", ioe.getMessage());
      }
      try (FileOutputStream fos1 = new FileOutputStream("1" + args[0]);
           FileOutputStream fos2 = new FileOutputStream("2" + args[0]);
           FileOutputStream fos3 = new FileOutputStream("3" + args[0]))
      {
         Base64.Decoder dec1 = Base64.getDecoder();
         Base64.Decoder dec2 = Base64.getMimeDecoder();
         Base64.Decoder dec3 = Base64.getUrlDecoder();
         InputStream is1 = dec1.wrap(new FileInputStream(args[0] + "1.enc"));
         InputStream is2 = dec2.wrap(new FileInputStream(args[0] + "2.enc"));
         InputStream is3 = dec3.wrap(new FileInputStream(args[0] + "3.enc"));
         int _byte;
         while((_byte = is1.read()) ! = -1) fos1.write(_byte);while((_byte = is2.read()) ! = -1) fos2.write(_byte);while((_byte = is3.read()) ! = -1) fos3.write(_byte); is1.close(); is2.close(); is3.close(); } catch (IOException ioe) { System.err.printf("I/O error: %s%n", ioe.getMessage()); }}}Copy the code

The FileEncDec application needs a file as the name of its isolated command-line argument. It continues to open the file and read its contents. Each read byte is written to another file via a different encoder and wrapped output stream. These files are then opened and read through different decoders and wrapped input streams. The results are stored in three separate files.

Compile Listing 2 as follows:

javac FileEncDec.javaCopy the code

Run the generated application as follows (assuming a file named JPEG image.jpg- see the code archive for the post) :

java FileEncDec image.jpgCopy the code

You should look at the image.jpg1.enc, image.jpg2.enc, and image.jpg3.enc files in the current directory.

Image.jpg1.enc stores the Basic encoding on a long line. Here is the output prefix, broken into two lines for ease of reading (… Sequence indicates content not displayed) :

/9j/4AAQSkZJRgABAQEASABIAAD/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD/
   4QM6aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+...Copy the code

Image.jpg2. enc stores the MIME encoding in multiple lines:

/9j/4AAQSkZJRgABAQEASABIAAD/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD/
4QM6aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9
Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/Pg0KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpu
czptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4wLWMwNjAgNjEuMTM0Nzc3LCAyMDEw
LzAyLzEyLTE3OjMyOjAwICAgICAgICAiPg0KCTxyZGY6UkRGIHh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J2v1Wj0b8/yR1f2DOurxut92vlvul5H0xofxB8Eaf8HZobj4c6dp
uqarp8unwa3DK97I43qXxHK5SKbB/wBafmCnCAKeOB8Pado91dTxpazpBJC6nMMV3M4xyFZ02xnq
fMVdy449R4V8X/E0PhT4kRtZ2Xju+tlhMUE0OoJHLDKjgMojKbGiMSbxlVGSoyONvK3H7Rnia90O
2l0OG8WeztVW5kJE7pN5SLvXAHLSuxbhjwNuxQRV4XiqlVp+0hCXvaq7Xyvdt+W2+yM8Vw/UpT5J
zj7ujsn+iS8/zZ+h91qGi3t7Lpsmi3CR+SLi3+y3kkIjTgZYBDhlG7bxkZPLFhWh47+EmgzeDdQ8
UeEdYbTdPUL9p0xtUuJyJtuxWEsbxnBOThwuArAsVznFstCaLxjpuk7bdYLi5+ybQhYIpcxEg8cj
jHsPUkn16Pwf/wAIHqL2d5cR6ppdx+7Wze2CQuihTyqsNh28gglg3RgBz+FYOpDBzi5RUodU0ndd
d1v1Wq1P0DETnibtVGpdNWrfd/wT520DRfEV1eyWt3cXek/2ZCF3Tt9str8RxKCVZozI0rKFwC6Y
L84GBUC+GfE2sQ6nNc3Gl6T/AGezSodT0mOX7RIxxGEEbqFkxuHqSVXJJBEn7YNvJ8Dvit9ttbi+
uNH1WITC3FwI7i33gSbFYJt2j7v3eRk4ycjwo/F6bTPOjT7bI0gJLtPy+75lDDoccE+9fquG4Syv
F044mnTSUkmrJrR+V/0PjqnE2OoN0ZTbs3e7PbdHbXtGvlSbS/Ccmm3Nq7/aLN73TWluFXhgD5q5
BZeuMbQAVANN8feJYfBuoLqFj4L1jUrW1t45ZpV8SIJbNiqyNuD5Dqm6TDA/KcggEYrF8M/FTQ9c
+HurfZLfV7jVNJ0sag320oIBH5yqyoUbcrfMT6HnIGa53Qvi9b+WGkspp47hWQo7jKj2YflyDkVn
/qPgJXlFyT23dvuTX5ov/WrGxsna3or/AHtM/9k=Copy the code

Image.jpg3.enc stores the URL and filename safe encoding on a long line. Here is the output prefix, broken into two lines for easy reading:

_9j_4AAQSkZJRgABAQEASABIAAD_4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAEAAAAAAAD_
   4QM6aHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu-...Copy the code

The difference between image.jpg1.enc and image.jpg3.enc is that each/is replaced with _ and each + is replaced with -.

You should also look at the 1image.jpg, 2image.jpg, and 3image.jpg files in the current directory. Each of these files contains the same content, image.jpg.

conclusion

The Base64 API is one of the various little gems introduced in Java 8. If you must use Base64, you will find this API very handy. I encourage you to try Base64, starting with methods not covered in this article.

The original link: www.javaworld.com/article/324…

More articles are welcome to visit http://www.apexyun.com

Public id: Galaxy 1

Contact email: [email protected]

(Please do not reprint without permission)