Class OpenCodec

java.lang.Object
cloud.opencode.base.core.codec.OpenCodec

public final class OpenCodec extends Object
Codec Facade - Central entry point for encoding and decoding operations 编解码门面类 - 编解码操作的统一入口

Provides static factory methods to obtain codec instances for various encoding schemes. All returned codecs are thread-safe singletons.

提供静态工厂方法获取各种编码方案的编解码器实例。所有返回的编解码器均为线程安全的单例。

Supported Codecs | 支持的编解码器:

  • Base64 (standard, URL-safe, no-padding) - Base64(标准、URL 安全、无填充)
  • Hex (lowercase, uppercase) - 十六进制(小写、大写)
  • URL (RFC 3986 percent-encoding) - URL(RFC 3986 百分号编码)
  • HTML (OWASP entity escaping) - HTML(OWASP 实体转义)
  • Base32 (RFC 4648, padding/no-padding) - Base32(RFC 4648,有/无填充)
  • ASCII85 (Base85) - ASCII85(Base85)

Usage Examples | 使用示例:

// Base64 roundtrip
Codec<byte[], String> b64 = OpenCodec.base64();
String encoded = b64.encode("Hello".getBytes());
byte[] decoded = b64.decode(encoded);

// URL encoding
Codec<String, String> url = OpenCodec.url();
String safe = url.encode("hello world&foo=bar");
// "hello%20world%26foo%3Dbar"

// HTML escaping
Codec<String, String> html = OpenCodec.html();
String escaped = html.encode("<script>alert('xss')</script>");
// "&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;"

// Codec composition
Codec<byte[], String> hexThenUrl = OpenCodec.hex().andThen(OpenCodec.url());

Security | 安全性:

  • Thread-safe: Yes (all codecs are immutable singletons) - 线程安全: 是(所有编解码器为不可变单例)
  • Null-safe: null input throws NullPointerException - 空值: null 输入抛出 NPE
Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static Codec<byte[],String>
    Returns an ASCII85 (Base85) codec 返回 ASCII85(Base85)编解码器
    static Codec<byte[],String>
    Returns a Base32 codec (RFC 4648, with padding) 返回 Base32 编解码器(RFC 4648,带填充)
    static Codec<byte[],String>
    Returns a Base32 codec without padding 返回无填充 Base32 编解码器
    static Codec<byte[],String>
    Returns a standard Base64 codec (RFC 4648, with padding) 返回标准 Base64 编解码器(RFC 4648,带填充)
    static Codec<byte[],String>
    Returns a Base64 codec without padding 返回无填充 Base64 编解码器
    static Codec<byte[],String>
    Returns a URL-safe Base64 codec (RFC 4648, without padding) 返回 URL 安全 Base64 编解码器(RFC 4648,无填充)
    static Codec<byte[],String>
    hex()
    Returns a lowercase hex codec 返回小写十六进制编解码器
    static Codec<byte[],String>
    Returns an uppercase hex codec 返回大写十六进制编解码器
    Returns an HTML entity codec (OWASP compliant) 返回 HTML 实体编解码器(符合 OWASP 规范)
    url()
    Returns a URL codec (RFC 3986 percent-encoding) 返回 URL 编解码器(RFC 3986 百分号编码)

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • base64

      public static Codec<byte[],String> base64()
      Returns a standard Base64 codec (RFC 4648, with padding) 返回标准 Base64 编解码器(RFC 4648,带填充)
      Returns:
      a Base64 codec | Base64 编解码器
    • base64UrlSafe

      public static Codec<byte[],String> base64UrlSafe()
      Returns a URL-safe Base64 codec (RFC 4648, without padding) 返回 URL 安全 Base64 编解码器(RFC 4648,无填充)
      Returns:
      a URL-safe Base64 codec | URL 安全 Base64 编解码器
    • base64NoPadding

      public static Codec<byte[],String> base64NoPadding()
      Returns a Base64 codec without padding 返回无填充 Base64 编解码器
      Returns:
      a no-padding Base64 codec | 无填充 Base64 编解码器
    • hex

      public static Codec<byte[],String> hex()
      Returns a lowercase hex codec 返回小写十六进制编解码器
      Returns:
      a hex codec | 十六进制编解码器
    • hexUpper

      public static Codec<byte[],String> hexUpper()
      Returns an uppercase hex codec 返回大写十六进制编解码器
      Returns:
      an uppercase hex codec | 大写十六进制编解码器
    • url

      public static Codec<String,String> url()
      Returns a URL codec (RFC 3986 percent-encoding) 返回 URL 编解码器(RFC 3986 百分号编码)

      Encodes all characters except unreserved characters (A-Z, a-z, 0-9, '-', '_', '.', '~'). Spaces are encoded as %20 (not +).

      Returns:
      a URL codec | URL 编解码器
    • html

      public static Codec<String,String> html()
      Returns an HTML entity codec (OWASP compliant) 返回 HTML 实体编解码器(符合 OWASP 规范)

      Escapes: < > & " '. Unescapes named entities and numeric character references.

      Returns:
      an HTML codec | HTML 编解码器
    • base32

      public static Codec<byte[],String> base32()
      Returns a Base32 codec (RFC 4648, with padding) 返回 Base32 编解码器(RFC 4648,带填充)
      Returns:
      a Base32 codec | Base32 编解码器
    • base32NoPadding

      public static Codec<byte[],String> base32NoPadding()
      Returns a Base32 codec without padding 返回无填充 Base32 编解码器
      Returns:
      a no-padding Base32 codec | 无填充 Base32 编解码器
    • ascii85

      public static Codec<byte[],String> ascii85()
      Returns an ASCII85 (Base85) codec 返回 ASCII85(Base85)编解码器
      Returns:
      an ASCII85 codec | ASCII85 编解码器