JBundle Thin Base Utilities Base64 Base64 Code framework in Java Library Analysis
JBundle Thin Base Utilities is an open source project for the Java class library, which contains various commonly used tool classes and functions, including Base64 codec.This article will analyze the working principle of the Base64 Code framework in JBUNDLE Thin Base Utilities in the Java library and provide relevant Java code examples.
## 1. Base64 Code decoding principle
Base64 is a encoding method for converting binary data into ASCII characters.It divides the binary data of three bytes into four -byte ASCII characters, and the output characters are concentrated in a fixed character.The advantage of Base64 encoding is that binary data can be transmitted in text, which is suitable for transmitting binary data in the text protocol.
Base64 coding working principle is as follows:
1. Divide the input binary data according to a group of bytes per 3 bytes. If there are less than 3 bytes in the end, add 0 bytes or use specific characters as needed.
2. Convert the binary data of each group of 3 bytes into 4 bytes of ASCII characters.The conversion rule is to split each byte into 6 binary positions, and map it to the character concentrated by the Base64 character in order.
3. If the binary data input is not a multiples of 3, add an appropriate amount of filling characters (usually using '=' symbols) at the end of the output character.
The principle of Base64 decoding is opposite to encoding, and 4 characters are converted back to binary data of 3 bytes.
## 2. Use JBUNDLE Thin Base Utilities for BASE64 Code
JBundle Thin Base Utilities provides support for Base64 encoding, which can easily convert Base64 in the Java library.
Below is an example code for using JBUNDLE Thin Base Utilities for BASE64 Code:
import org.thin.base64.Base64;
public class Base64Example {
public static void main(String[] args) {
// String to be coded
String Originalstring = "Hello, World!";
// base64 encoding
String encodedString = Base64.encodeByte(originalString.getBytes());
System.out.println ("Base64 encoding result:" + EncodedString);
// base64 decoding
byte[] decodedBytes = Base64.decode(encodedString);
String decodedString = new String(decodedBytes);
System.out.println ("Base64 decoding result:" + decodedstring);
}
}
Output results:
Base64 encoding result: SGVSBG8SIOS4LUEVJCE =
Base64 decoding result: Hello, World!
In the above example code, first of all, we convert the string to be encoded into byte array, and then use the `Base64.encodebyte ()" method to obtain the encoded Base64 string.Then use the `Base64.DEcode ()" method to decode the encoded string, and finally convert the decoding byte array into a string to get the original string.
By using the Base64 function provided by the JBUNDLE Thin Base Utilities, we can easily perform the coding operation of Base64 to easily process binary data in the Java library.