Introduction to the

What is Base64 encoding? Before we answer this question, we need to understand the classification of files on computers. For computers, files can be divided into two types: text files and binary files.

For binary files, the contents are represented in binary and are not immediately understandable to humans. If you try to open a binary with a text editor, you may see garbled characters. This is because binaries are not encoded the same way as text files, so when a text editor tries to translate binaries into text, garbled characters appear.

For text files, there are many encoding methods, such as the original ASCII encoding and the current common encoding methods such as UTF-8 and UTF-16. Even text files can be garbled if you open them using a different encoding.

So whether it is text file or binary file, all need to carry on the unification of encoding format. That is, the code that is written should match the code that is read.

Base64 encoding is actually a way to encode binary data into visual ASCII characters.

Why the requirement?

We know that the development of the computer world is not an overnight move, it is a slow growth process, for character encoding, at first only ASCII encoding, later extended to Unicode and so on. So for many applications, other encodings than ASCII are not supported, so how do you display non-ASCII code on these systems?

The solution is to do encoding mapping, which maps non-ASCII characters to ASCII characters. Base64 is such an encoding method.

A common place to use Base64 is in Web pages. Sometimes you need to display images on a web page, so you can Base64 encode the images and populate them with HTML.

Another application is to base64 encode a file and send it as an attachment to an email.

JAVA support for Base64

Since base64 encoding works so well, let’s take a look at the Base64 implementation in JAVA.

There is a corresponding base64 implementation in Java called java.util.base64. This class is a Base64 utility class introduced by the JDK in version 1.8.

Base64 provides three getEncoder and getDecoder methods, by obtaining the corresponding Encoder and Decoder, and then you can call Encoder encode and decode methods for data encoding and decoding, very convenient.

Let’s start with a basic Base64 use example:

String encodedString = base64. getEncoder(). EncodeToString (" What is your name baby?") .getBytes("utf-8")); Println ("Base64 encodedString :" + encodedString); system.out.println ("Base64 encodedString :" + encodedString); Byte [] decodedBytes = base64.getdecoder ().decode(encodedString); byte[] decodedBytes = base64.getdecoder (). System.out.println(" decoded String: "+ new String(decodedBytes," utF-8 "));Copy the code

As a utility class, the Base64 utility class provided in the JDK works quite well.

This article will not explain its use in detail, the main analysis of the JDK Base64 is how to achieve.

Classification and implementation of Base64 in JDK

GetEncoder; getUrlEncoder; getMimeEncoder; getEncoder; getUrlEncoder; getMimeEncoder

    public static Encoder getEncoder() {
         return Encoder.RFC4648;
    }

    public static Encoder getUrlEncoder() {
         return Encoder.RFC4648_URLSAFE;
    }

    public static Encoder getMimeEncoder() {
        return Encoder.RFC2045;
    }
Copy the code

Similarly, it also provides three corresponding decoder, respectively is getDecoder, getUrlDecoder, getMimeDecoder:

    public static Decoder getDecoder() {
         return Decoder.RFC4648;
    }

    public static Decoder getUrlDecoder() {
         return Decoder.RFC4648_URLSAFE;
    }

    public static Decoder getMimeDecoder() {
         return Decoder.RFC2045;
    }
Copy the code

The codes correspond to RFC4648, RFC4648_URLSAFE, and RFC2045 respectively.

All three are base64 encoding variants, so let’s see how they differ:

Code name Coded character Coded character Coded character
The 62th The 63th Completion operator
RFC 2045: Base64 transfer encoding for MIME + / = mandatory
RFC 4648: base64 (standard) + / = optional
RFC 4648: base64url (URL- and filename-safe standard) - _ = optional

As you can see, the difference between Base64 and Base64 URL is that bit 62nd and bit 63rd encoding characters are different, and the difference between Base64 for MIME and Base64 is whether the completion character is mandatory.

Additionally, for Basic and Base64 urls, the Line separator character is not added, while Base64 for MIME adds ‘\r’ and ‘\n’ as line separator after a line of 76 characters or more.

Finally, if characters that do not exist in the Base64 mapping table are found to be processed differently during decoding, Base64 and Base64 URLS will reject them directly, while Base64 for MIME will ignore them.

The difference between base64 and Base64url can be seen in two ways:

private static final char[] toBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};Copy the code
private static final char[] toBase64URL = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'};Copy the code

For MIME, it defines the maximum number of characters on a line, and a newline character:

        private static final int MIMELINEMAX = 76;
        private static final byte[] CRLF = new byte[] {'\r', '\n'};
Copy the code

Advanced use of Base64

In general, the length of the Base64 encoded object is fixed. We only need to convert the input object into byte array to call the encode or decode method.

However, in some cases we need to convert Stream data. In this case, we can use the two methods provided in Base64 to wrap a Stream:

        public OutputStream wrap(OutputStream os) {
            Objects.requireNonNull(os);
            return new EncOutputStream(os, isURL ? toBase64URL : toBase64,
                                       newline, linemax, doPadding);
        }
Copy the code
        public InputStream wrap(InputStream is) {
            Objects.requireNonNull(is);
            return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME);
        }
Copy the code

These two methods correspond to encoder and decoder respectively.

conclusion

Above is the IMPLEMENTATION and use of Base64 in JDK, although there are many varieties of Base64, but the JDK Base64 only implemented the most widely used three. You must distinguish between the specific Base64 implementation when using, in order to avoid problems.

This article is available at www.flydean.com/14-1-1-java…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!