In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to implement the base64 encoder in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to implement the base64 encoder in Java"!
Brief introduction
What is Base64 coding? Before we answer this question, we need to understand the classification of files in the computer. For computers, files can be divided into two categories, one is text files, the other is binary files.
For binary files, their contents are expressed in binary and cannot be understood immediately by human beings. If you try to open a binary file with a text editor, you may see garbled code. This is because binary files are encoded differently from text files, so garbled occurs when a text editor tries to translate binary files into text content.
There are also many encoding methods for text files, such as the earliest ASCII encoding and the commonly used encoding methods such as UTF-8 and UTF-16. Even if you open a text file using a different encoding, you may see garbled code.
Therefore, whether it is a text file or a binary file, it is necessary to unify the coding format. That is to say, what the written encoding looks like, then the data read encoding should also match it.
Base64 coding is actually a way to encode binary data into visual ASCII characters.
Why is there such a requirement?
We know that the development of the computer world is not achieved overnight, it is a process of slow growth, for character coding, only ASCII coding is supported at first, and then extended to Unicode and so on. So for many applications, coding formats other than ASCII coding are not supported, so how to show non-ASCII code in these systems?
The solution is to do coding mapping, mapping non-ASCII characters into ASCII characters. And base64 is such a coding method.
The common place to use Base64 is in web pages. Sometimes we need to display pictures on the page, so we can encode the pictures in base64 and fill them into html.
Another application is to encode the file in base64 and send it as an attachment to the message.
JAVA support for base64
Now that base64 coding is so easy to use, 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 utility class for Base64 and was introduced by JDK in version 1.8.
Three getEncoder and getDecoder methods are provided in Base64. By getting the corresponding Encoder and Decoder, you can call Encoder's encode and decode methods to encode and decode the data, which is very convenient.
Let's first take a look at a basic example of using Base64:
/ / use encoder to encode String encodedString = Base64.getEncoder (). EncodeToString ("what is your name baby?" .getBytes ("utf-8")); System.out.println ("Base64 encoded string:" + encodedString); / / use encoder to decode byte [] decodedBytes = Base64.getDecoder (). Decode (encodedString); System.out.println ("decoded string:" + new String (decodedBytes, "utf-8"))
As a utility class, the Base64 utility class provided in JDK is very useful.
I won't explain its use in detail here. This article mainly analyzes how to implement Base64 in JDK.
Classification and implementation of Base64 in JDK
The Base64 class in JDK provides three encoder methods, which are getEncoder,getUrlEncoder and getMimeEncoder:
Public static Encoder getEncoder () {return Encoder.RFC4648;} public static Encoder getUrlEncoder () {return Encoder.RFC4648_URLSAFE;} public static Encoder getMimeEncoder () {return Encoder.RFC2045;}
Similarly, it also provides three corresponding decoder, which are 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;}
As you can see from the code, these three codes correspond to RFC4648,RFC4648_URLSAFE and RFC2045, respectively.
All three are variants of base64 coding, so let's see how they differ:
Code name Encoding character 62 bit 63 complement RFC 2045: Base64 transfer encoding for MIME+/= mandatoryRFC 4648: base64 (standard) + / = optionalRFC 4648: base64url (URL- and filename-safe standard)-_ = optional
You can see that the difference between base64 and Base64url is that the encoded characters of bit 62 and bit 63 are different, while the difference between base64 for MIME and base64 is whether the complement is mandatory.
In addition, for Basic and base64url, no line separator characters are added, while base64 for MIME adds'\ r 'and'\ n'as line separator after a line exceeds 76 characters.
Finally, if, in the process of decoding, it is found that characters that are not stored in the Base64 mapping table are handled differently, base64 and Base64url will reject them directly, while base64 for MIME will ignore them.
The difference between base64 and Base64url can be seen in the following 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' 'Wells, 'Xrays,' Yee, 'Zhuan,' axed, 'baked,' clocked, 'dashed,' eyed, 'faded,' grubbed, 'hacked,' ified, 'jaded,' kitted, 'lumped,' masked, 'nicked, oiled,' paired, 'qrabbit,' rusted, 'sworn,' t' 'upright,' vain, 'wicked,' x, 'yearly,' zoned,'0,'1,'2, 3, 4, 5, 6, 7, 8, 9, +,'/'} 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' 'Vail, 'Walt,' Xbox, 'Yee,' Zhuan, 'Atoll,' baked, 'clocked,' dashed, 'eyed,' faded, 'grubbed,' hacked, 'ified,' jaded, 'Kwon,' lumped, 'masked,' nicked, 'oiled,' paired, 'qflux,' ringing,'s' 'tween,' upright, 'vain,' wicked, 'xxed,' yawned, 'zoned,' 0percent, '1percent,' 2percent, '3percent,' 4percent, '5percent,' 6percent, 'seven percent,' eight percent, 'nine percent,' -','_'}
For MIME, the maximum number of characters per line is defined, and the newline character is defined:
Private static final int MIMELINEMAX = 76; private static final byte [] CRLF = new byte [] {'\ rpm,'\ n'}; Advanced usage of Base64
In general, the length of the object we encode in Base64 is fixed, and we only need to convert the input object to an byte array to call the encode or decode method.
However, in some cases, we need to convert the streaming data, so we can use the two methods provided in Base64 to wrap Stream:
Public OutputStream wrap (OutputStream os) {Objects.requireNonNull (os); return new EncOutputStream (os, isURL? ToBase64URL: toBase64, newline, linemax, doPadding);} public InputStream wrap (InputStream is) {Objects.requireNonNull (is); return new DecInputStream (is, isURL? FromBase64URL: fromBase64, isMIME);}
These two methods correspond to encoder and decoder, respectively.
At this point, I believe you have a deeper understanding of "how to implement the base64 encoder in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.