How to implement base64 encoder in Java
Introduction
What is Base64 encoding? Before answering this question, we need to understand the classification of files in computers. For computers, files can be divided into two categories, one is text files and the other is binary files.
For binary files, their content is represented in binary, which is not immediately understandable to humans. If you try to open a binary file with a text editor, you may see gibberish. This is because the encoding method of binary files is different from the encoding method of text files, so when the text editor tries to translate the binary files into text content, garbled characters will appear.
For text files, there are many encoding methods, such as the earliest ASCII encoding and the currently commonly used encoding methods such as UTF-8 and UTF-16. Even text files may see garbled characters if you open them using a different encoding.
So whether it is a text file or a binary file, the encoding format needs to be unified. In other words, what the encoding of writing looks like, then the encoding of data reading should also match it.
Base64 encoding is actually an encoding method that encodes binary data into visual ASCII characters.
Why is there such a requirement?
We know that the development of the computer world does not happen overnight. It is a process of slow growth. For character encoding, it only supports ASCII encoding at first, and later it was expanded to Unicode and so on. Therefore, for many applications, encoding formats other than ASCII encoding are not supported. So how to display non-ASCII code in these systems?
The solution is to perform encoding mapping to map non-ASCII characters to ASCII characters. Base64 is such an encoding method.
The common place to use Base64 is in web pages. Sometimes we need to display images on web pages, so we can base64 encode the images and then fill them into html.
Another application is to base64 encode the file and then send it as an email attachment.
JAVA’s support for base64
Since base64 encoding 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 tool class for Base64, which was introduced by JDK in version 1.8.
Base64 provides three getEncoder and getDecoder methods. By obtaining the corresponding Encoder and Decoder, you can then call the encoder's encode and decode methods to encode and decode the data, which is very convenient.
Let’s first take a look at the basic usage examples of Base64:
// 使用encoder进行编码 String encodedString = Base64.getEncoder().encodeToString("what is your name baby?".getBytes("utf-8")); System.out.println("Base64编码过后的字符串 :" + encodedString); // 使用encoder进行解码 byte[] decodedBytes = Base64.getDecoder().decode(encodedString); System.out.println("解码过后的字符串: " + new String(decodedBytes, "utf-8"));
As a tool class, the Base64 tool class provided in the JDK is still very useful.
I won’t explain its use in detail here. This article mainly analyzes how Base64 is implemented in JDK.
Classification and implementation of Base64 in JDK
The Base64 class in JDK provides three encoder methods, namely 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 Three corresponding decoder are also provided, namely 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 can be seen from the code, these three encodings correspond to RFC4648, RFC4648_URLSAFE and RFC2045 respectively.
These three are variants of base64 encoding. Let’s take a look at their differences:
Encoding name | Encoded characters | Encoded characters | Encoded characters |
---|---|---|---|
The 62nd digit | The 63rd digit | Complete character | |
RFC 2045: Base64 transfer encoding for MIME | | / | = mandatory |
RFC 4648: base64 (standard) | | / | = optional |
-
| _
| = optional |
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', '+', '/' };
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', '-', '_' };
private static final int MIMELINEMAX = 76; private static final byte[] CRLF = new byte[] {'\r', '\n'};
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); }
The above is the detailed content of How to implement base64 encoder in Java. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
