Base64Utils Tutorial
This tutorial provides an overview of the Base64Utils class, which is part of the lightweight-component/aj-util
library. The Base64Utils class provides a fluent interface for Base64 encoding and decoding
operations.
Introduction
The Base64Utils 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. Construction
new Base64Utils(String input)- Use UTF-8 for string inputnew Base64Utils(String input, Charset charset)- Use a specified charsetnew Base64Utils(byte[] input)- Use binary input
3. Configuration
setWithoutPadding(true)- Remove padding from encoded outputsetUrlSafe(true)- Use the URL-safe Base64 variant
4. Output Methods
encode()/decode()- Return a byte arrayencodeAsString()- Return encoded ASCII textdecodeAsString()/decodeAsString(Charset)- Decode text
Usage Examples
Basic Encoding
String encoded = new Base64Utils("Hello World").encodeAsString();
String decoded = new Base64Utils(encoded).decodeAsString();
URL-Safe Encoding
String encoded = new Base64Utils("data to encode").setUrlSafe(true).encodeAsString();
Without Padding
String encoded = new Base64Utils("data").setWithoutPadding(true).encodeAsString();
Custom Charset
String encoded = new Base64Utils("数据", StandardCharsets.UTF_16).encodeAsString();
Byte Array Handling
byte[] data = {1, 2, 3, 4};
byte[] encoded = new Base64Utils(data).encode();
Conclusion
The Base64Utils class provides a flexible and type-safe way to perform Base64 encoding and
decoding operations with support for various configurations and output formats.