Class OpenCharset
java.lang.Object
cloud.opencode.base.core.OpenCharset
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]addBom(byte[] bytes) Adds 添加 UTF-8 BOMstatic booleanChecks 检查字符串是否可用指定字符集编码static CharsetGets 获取字符集,null 安全(返回 UTF-8)static CharsetGets 获取字符集,带默认值charsetOptional(String charsetName) Safely gets the charset 安全获取字符集static StringConverts 转换字符串编码(字符集名称)static StringConverts 转换字符串编码static CharsetGets 获取系统默认字符集static Charsetdetect(byte[] bytes) Detects the likely charset of a byte array (simple detection) 检测字节数组可能的字符集(简单检测)static CharsetGB18030()static CharsetGB2312()static CharsetGBK()static StringGBK 转 UTF-8static booleanhasBom(byte[] bytes) Checks 检查是否有 UTF-8 BOMstatic booleanhasNonAscii(String str) Checks 检查是否包含非 ASCII 字符static booleanChecks 检查是否为纯 ASCII 字符串static Stringiso8859ToUtf8(String isoStr) ISO-8859-1 转 UTF-8static booleanisSupported(String charsetName) Checks 检查字符集名称是否有效static ReadernewReader(InputStream in) Creates 创建 UTF-8 Readerstatic ReadernewReader(InputStream in, Charset charset) Creates 创建指定字符集的 Readerstatic WriternewWriter(OutputStream out) Creates 创建 UTF-8 Writerstatic WriternewWriter(OutputStream out, Charset charset) Creates 创建指定字符集的 Writerstatic byte[]removeBom(byte[] bytes) Removes the UTF-8 BOM header 移除 UTF-8 BOM 头static byte[]Converts a string to a byte array (UTF-8) 字符串转字节数组(UTF-8)static byte[]Converts a string to a byte array (specified charset name) 字符串转字节数组(指定字符集名称)static byte[]Converts a string to a byte array (specified charset) 字符串转字节数组(指定字符集)static StringtoString(byte[] bytes) Converts a byte array to a string (UTF-8) 字节数组转字符串(UTF-8)static StringConverts a byte array to a string (specified charset name) 字节数组转字符串(指定字符集名称)static StringConverts a byte array to a string (specified charset) 字节数组转字符串(指定字符集)static StringUTF-8 转 GBK
-
Field Details
-
UTF_8
-
UTF_16
-
UTF_16BE
-
UTF_16LE
-
ISO_8859_1
-
US_ASCII
-
UTF_8_NAME
- See Also:
-
GBK_NAME
- See Also:
-
ISO_8859_1_NAME
- See Also:
-
-
Method Details
-
GBK
-
GB2312
-
GB18030
-
charset
-
charset
-
charsetOptional
-
defaultCharset
Gets 获取系统默认字符集 -
toBytes
Converts a string to a byte array (UTF-8) 字符串转字节数组(UTF-8) -
toBytes
-
toBytes
-
toString
Converts a byte array to a string (UTF-8) 字节数组转字符串(UTF-8) -
toString
-
toString
-
convert
-
convert
-
isSupported
Checks 检查字符集名称是否有效 -
canEncode
-
detect
Detects the likely charset of a byte array (simple detection) 检测字节数组可能的字符集(简单检测) -
hasNonAscii
Checks 检查是否包含非 ASCII 字符 -
isAscii
Checks 检查是否为纯 ASCII 字符串 -
gbkToUtf8
-
utf8ToGbk
-
iso8859ToUtf8
-
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
Creates 创建 UTF-8 Reader -
newReader
Creates 创建指定字符集的 Reader -
newWriter
Creates 创建 UTF-8 Writer -
newWriter
Creates 创建指定字符集的 Writer
-