Base64Helper Tutorial
This tutorial provides an overview of the Base64Helper class, which is part of the lightweight-component/aj-util library. The Base64Helper class provides a fluent interface for Base64 encoding and decoding operations.
Introduction
The Base64Helper class offers a type-safe, configurable way to perform Base64 encoding and decoding, with support for URL-safe encoding and padding control.
Main Features
- Fluent/chained method calls
- Support for both standard and URL-safe Base64
- Optional padding removal
- Type-safe output (String or byte[])
- UTF-8 by default with custom charset support
Methods
1. Initialization
encode()- Create instance in encode mode (factory method)decode()- Create instance in decode mode (factory method)encoder()- Set to encode modedecoder()- Set to decode mode
2. Input Methods
input(String input)- Set input string (UTF-8)input(String input, Charset charset)- Set input string with custom charsetinput(byte[] input)- Set input bytes
3. Configuration
withoutPadding()- Remove padding from encoded outputurlSafe()- Use URL-safe Base64 variant
4. Output Methods
getString()- Get result as UTF-8 stringgetString(Charset charset)- Get result as string with custom charsetgetBytes()- Get result as byte array
Usage Examples
Basic Encoding
String encoded = Base64Helper.encode()
.input("Hello World")
.getString();
String decoded = Base64Helper.decode()
.input(encoded)
.getString();
URL-Safe Encoding
String encoded = Base64Helper.encode()
.input("data to encode")
.urlSafe()
.getString();
Without Padding
String encoded = Base64Helper.encode()
.input("data")
.withoutPadding()
.getString();
Custom Charset
String encoded = Base64Helper.encode()
.input("数据", StandardCharsets.UTF_16)
.getString(StandardCharsets.UTF_16);
Byte Array Handling
byte[] data = {1, 2, 3, 4};
byte[] encoded = Base64Helper.encode()
.input(data)
.getBytes();
Conclusion
The Base64Helper class provides a flexible and type-safe way to perform Base64 encoding and decoding operations with support for various configurations and output formats.