Class OpenBase64

java.lang.Object
cloud.opencode.base.core.OpenBase64

public final class OpenBase64 extends Object
Base64 Encoding/Decoding Utility Class - Standard, URL-safe and MIME encodings Base64 编解码工具类 - 标准、URL 安全和 MIME 编码

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 Details

    • encode

      public static String encode(byte[] data)
      Encodes a byte array to a Base64 string 将字节数组编码为 Base64 字符串
    • encode

      public static String encode(String data)
      Encodes a string to Base64 (using UTF-8) 将字符串编码为 Base64(使用 UTF-8)
    • encode

      public static String encode(String data, Charset charset)
      Encodes a string to Base64 (with specified charset) 将字符串编码为 Base64(指定字符集)
    • encodeToBytes

      public static byte[] encodeToBytes(byte[] data)
      Encodes a byte array to a Base64 byte array 将字节数组编码为 Base64 字节数组
    • decode

      public static byte[] decode(String base64)
      Decodes a Base64 string to a byte array 将 Base64 字符串解码为字节数组
    • decodeToString

      public static String decodeToString(String base64)
      Decodes a Base64 string to a string (using UTF-8) 将 Base64 字符串解码为字符串(使用 UTF-8)
    • decodeToString

      public static String decodeToString(String base64, Charset charset)
      Decodes a Base64 string to a string (with specified charset) 将 Base64 字符串解码为字符串(指定字符集)
    • decode

      public static byte[] decode(byte[] base64Bytes)
      Decodes a Base64 byte array 将 Base64 字节数组解码
    • encodeUrlSafe

      public static String encodeUrlSafe(byte[] data)
      URL 安全编码(使用 - 和 _ 替代 + 和 /,无填充)
    • encodeUrlSafe

      public static String encodeUrlSafe(String data)
      URL 安全编码字符串
    • decodeUrlSafe

      public static byte[] decodeUrlSafe(String base64)
      URL 安全解码
    • decodeUrlSafeToString

      public static String decodeUrlSafeToString(String base64)
      URL 安全解码为字符串
    • encodeMime

      public static String encodeMime(byte[] data)
      MIME 编码(每 76 字符换行)
    • decodeMime

      public static byte[] decodeMime(String base64)
      MIME 解码
    • encodeNoPadding

      public static String encodeNoPadding(byte[] data)
      Encodes 编码但不添加填充字符 =
    • encodeUrlSafeNoPadding

      public static String encodeUrlSafeNoPadding(byte[] data)
      URL 安全编码且无填充
    • isBase64

      public static boolean isBase64(String str)
      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

      public static boolean isBase64UrlSafe(String str)
      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

      public static OutputStream encodingWrap(OutputStream out)
      Creates 创建 Base64 编码输出流包装器
    • decodingWrap

      public static InputStream decodingWrap(InputStream in)
      Creates 创建 Base64 解码输入流包装器
    • encodingWrapUrlSafe

      public static OutputStream encodingWrapUrlSafe(OutputStream out)
      Creates 创建 URL 安全 Base64 编码输出流包装器
    • decodingWrapUrlSafe

      public static InputStream decodingWrapUrlSafe(InputStream in)
      Creates 创建 URL 安全 Base64 解码输入流包装器