Class OpenCharset

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

public final class OpenCharset extends Object
Charset Utility Class - Charset conversion, detection and common charset constants 字符集工具类 - 字符集转换、检测和常用字符集常量

Provides charset conversion, detection, BOM handling and Reader/Writer creation.

提供字符集转换、检测、BOM 处理和 Reader/Writer 创建功能。

Features | 主要功能:

  • Charset constants (UTF_8, GBK, ISO_8859_1, etc.) - 字符集常量
  • Charset retrieval (charset, charsetOptional) - 字符集获取
  • Conversion (toBytes, toString, convert) - 转换
  • Detection (isSupported, canEncode, detect) - 检测
  • BOM handling (hasBom, removeBom, addBom) - BOM 处理
  • Reader/Writer creation (newReader, newWriter) - Reader/Writer 创建

Usage Examples | 使用示例:

// Charset constants - 字符集常量
Charset utf8 = OpenCharset.UTF_8;
Charset gbk = OpenCharset.GBK();

// Conversion - 转换
byte[] bytes = OpenCharset.toBytes("Hello", OpenCharset.UTF_8);
String str = OpenCharset.gbkToUtf8(gbkString);

// Detection - 检测
boolean valid = OpenCharset.isSupported("GBK");
Charset detected = OpenCharset.detect(bytes);

Security | 安全性:

  • Thread-safe: Yes (lazy initialization with double-checked locking) - 线程安全: 是 (双重检查锁懒加载)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Method Details

    • GBK

      public static Charset GBK()
    • GB2312

      public static Charset GB2312()
    • GB18030

      public static Charset GB18030()
    • charset

      public static Charset charset(String charsetName)
      Gets 获取字符集,null 安全(返回 UTF-8)
    • charset

      public static Charset charset(String charsetName, Charset defaultCharset)
      Gets 获取字符集,带默认值
    • charsetOptional

      public static Optional<Charset> charsetOptional(String charsetName)
      Safely gets the charset 安全获取字符集
    • defaultCharset

      public static Charset defaultCharset()
      Gets 获取系统默认字符集
    • toBytes

      public static byte[] toBytes(String str)
      Converts a string to a byte array (UTF-8) 字符串转字节数组(UTF-8)
    • toBytes

      public static byte[] toBytes(String str, Charset charset)
      Converts a string to a byte array (specified charset) 字符串转字节数组(指定字符集)
    • toBytes

      public static byte[] toBytes(String str, String charsetName)
      Converts a string to a byte array (specified charset name) 字符串转字节数组(指定字符集名称)
    • toString

      public static String toString(byte[] bytes)
      Converts a byte array to a string (UTF-8) 字节数组转字符串(UTF-8)
    • toString

      public static String toString(byte[] bytes, Charset charset)
      Converts a byte array to a string (specified charset) 字节数组转字符串(指定字符集)
    • toString

      public static String toString(byte[] bytes, String charsetName)
      Converts a byte array to a string (specified charset name) 字节数组转字符串(指定字符集名称)
    • convert

      public static String convert(String str, Charset sourceCharset, Charset targetCharset)
      Converts 转换字符串编码
    • convert

      public static String convert(String str, String sourceCharset, String targetCharset)
      Converts 转换字符串编码(字符集名称)
    • isSupported

      public static boolean isSupported(String charsetName)
      Checks 检查字符集名称是否有效
    • canEncode

      public static boolean canEncode(String str, Charset charset)
      Checks 检查字符串是否可用指定字符集编码
    • detect

      public static Charset detect(byte[] bytes)
      Detects the likely charset of a byte array (simple detection) 检测字节数组可能的字符集(简单检测)
    • hasNonAscii

      public static boolean hasNonAscii(String str)
      Checks 检查是否包含非 ASCII 字符
    • isAscii

      public static boolean isAscii(String str)
      Checks 检查是否为纯 ASCII 字符串
    • gbkToUtf8

      public static String gbkToUtf8(String gbkStr)
      GBK 转 UTF-8
    • utf8ToGbk

      public static String utf8ToGbk(String utf8Str)
      UTF-8 转 GBK
    • iso8859ToUtf8

      public static String iso8859ToUtf8(String isoStr)
      ISO-8859-1 转 UTF-8
    • removeBom

      public static byte[] removeBom(byte[] bytes)
      Removes the UTF-8 BOM header 移除 UTF-8 BOM 头
    • hasBom

      public static boolean hasBom(byte[] bytes)
      Checks 检查是否有 UTF-8 BOM
    • addBom

      public static byte[] addBom(byte[] bytes)
      Adds 添加 UTF-8 BOM
    • newReader

      public static Reader newReader(InputStream in)
      Creates 创建 UTF-8 Reader
    • newReader

      public static Reader newReader(InputStream in, Charset charset)
      Creates 创建指定字符集的 Reader
    • newWriter

      public static Writer newWriter(OutputStream out)
      Creates 创建 UTF-8 Writer
    • newWriter

      public static Writer newWriter(OutputStream out, Charset charset)
      Creates 创建指定字符集的 Writer