Class OpenBase64
Based on JDK java.util.Base64, provides three encoding modes: standard, URL-safe and MIME.
基于 JDK java.util.Base64,提供标准、URL 安全和 MIME 三种编码方式。
Features | 主要功能:
- Standard encoding (encode, decode) - 标准编码
- URL-safe encoding (encodeUrlSafe, decodeUrlSafe) - URL 安全编码
- MIME encoding (encodeMime, decodeMime) - MIME 编码
- No-padding encoding (encodeNoPadding) - 无填充编码
- Validation (isBase64, isBase64UrlSafe) - 验证
- Stream wrapping (encodingWrap, decodingWrap) - 流包装
Usage Examples | 使用示例:
// Standard encoding - 标准编码
String encoded = OpenBase64.encode("Hello");
String decoded = OpenBase64.decodeToString(encoded);
// URL-safe encoding - URL 安全编码
String urlSafe = OpenBase64.encodeUrlSafe(bytes);
byte[] data = OpenBase64.decodeUrlSafe(urlSafe);
// Validation - 验证
boolean valid = OpenBase64.isBase64(str);
Security | 安全性:
- Thread-safe: Yes (stateless, uses JDK encoders) - 线程安全: 是 (无状态, 使用 JDK 编码器)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]decode(byte[] base64Bytes) Decodes a Base64 byte array 将 Base64 字节数组解码static byte[]Decodes a Base64 string to a byte array 将 Base64 字符串解码为字节数组static byte[]decodeMime(String base64) MIME 解码static StringdecodeToString(String base64) Decodes a Base64 string to a string (using UTF-8) 将 Base64 字符串解码为字符串(使用 UTF-8)static StringdecodeToString(String base64, Charset charset) Decodes a Base64 string to a string (with specified charset) 将 Base64 字符串解码为字符串(指定字符集)static byte[]decodeUrlSafe(String base64) URL 安全解码static StringdecodeUrlSafeToString(String base64) URL 安全解码为字符串static InputStreamCreates 创建 Base64 解码输入流包装器static InputStreamCreates 创建 URL 安全 Base64 解码输入流包装器static Stringencode(byte[] data) Encodes a byte array to a Base64 string 将字节数组编码为 Base64 字符串static StringEncodes a string to Base64 (using UTF-8) 将字符串编码为 Base64(使用 UTF-8)static StringEncodes a string to Base64 (with specified charset) 将字符串编码为 Base64(指定字符集)static StringencodeMime(byte[] data) MIME 编码(每 76 字符换行)static StringencodeNoPadding(byte[] data) Encodes 编码但不添加填充字符 =static byte[]encodeToBytes(byte[] data) Encodes a byte array to a Base64 byte array 将字节数组编码为 Base64 字节数组static StringencodeUrlSafe(byte[] data) URL 安全编码(使用 - 和 _ 替代 + 和 /,无填充)static StringencodeUrlSafe(String data) URL 安全编码字符串static StringencodeUrlSafeNoPadding(byte[] data) URL 安全编码且无填充static OutputStreamencodingWrap(OutputStream out) Creates 创建 Base64 编码输出流包装器static OutputStreamCreates 创建 URL 安全 Base64 编码输出流包装器static booleanChecks if the string is valid standard Base64 encoding (without decoding).static booleanisBase64UrlSafe(String str) Checks if the string is valid URL-safe Base64 encoding (without decoding).
-
Method Details
-
encode
Encodes a byte array to a Base64 string 将字节数组编码为 Base64 字符串 -
encode
-
encode
-
encodeToBytes
public static byte[] encodeToBytes(byte[] data) Encodes a byte array to a Base64 byte array 将字节数组编码为 Base64 字节数组 -
decode
Decodes a Base64 string to a byte array 将 Base64 字符串解码为字节数组 -
decodeToString
-
decodeToString
-
decode
public static byte[] decode(byte[] base64Bytes) Decodes a Base64 byte array 将 Base64 字节数组解码 -
encodeUrlSafe
URL 安全编码(使用 - 和 _ 替代 + 和 /,无填充) -
encodeUrlSafe
-
decodeUrlSafe
URL 安全解码 -
decodeUrlSafeToString
-
encodeMime
MIME 编码(每 76 字符换行) -
decodeMime
MIME 解码 -
encodeNoPadding
Encodes 编码但不添加填充字符 = -
encodeUrlSafeNoPadding
URL 安全编码且无填充 -
isBase64
Checks if the string is valid standard Base64 encoding (without decoding). 检查字符串是否为有效的标准 Base64 编码(不执行解码)。Validates that the string contains only valid Base64 characters (A-Z, a-z, 0-9, +, /, =) and that the non-whitespace content length is a multiple of 4. Whitespace characters (spaces, tabs, CR, LF) are ignored to support MIME-formatted input.
验证字符串仅包含有效的 Base64 字符(A-Z、a-z、0-9、+、/、=), 且去除空白后的内容长度为 4 的倍数。忽略空白字符(空格、制表符、CR、LF)以支持 MIME 格式输入。
- Parameters:
str- the string to check | 要检查的字符串- Returns:
- true if the string is valid Base64 | 如果字符串是有效的 Base64 则返回 true
- See Also:
-
isBase64UrlSafe
Checks if the string is valid URL-safe Base64 encoding (without decoding). 检查字符串是否为有效的 URL 安全 Base64 编码(不执行解码)。Validates that the string contains only valid URL-safe Base64 characters (A-Z, a-z, 0-9, -, _). URL-safe Base64 typically omits padding ('='), so length is not required to be a multiple of 4.
验证字符串仅包含有效的 URL 安全 Base64 字符(A-Z、a-z、0-9、-、_)。 URL 安全 Base64 通常省略填充字符('='),因此长度不要求为 4 的倍数。
- Parameters:
str- the string to check | 要检查的字符串- Returns:
- true if the string is valid URL-safe Base64 | 如果字符串是有效的 URL 安全 Base64 则返回 true
- See Also:
-
encodingWrap
Creates 创建 Base64 编码输出流包装器 -
decodingWrap
Creates 创建 Base64 解码输入流包装器 -
encodingWrapUrlSafe
Creates 创建 URL 安全 Base64 编码输出流包装器 -
decodingWrapUrlSafe
Creates 创建 URL 安全 Base64 解码输入流包装器
-