Browse Source

fix charset bug

Looly 5 years ago
parent
commit
804a15a105
2 changed files with 13 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 12 1
      hutool-core/src/main/java/cn/hutool/core/util/CharsetUtil.java

+ 1 - 0
CHANGELOG.md

@@ -8,6 +8,7 @@
 ### 新特性
 * 【core 】     废弃isMactchRegex,改为isMatchRegex(方法错别字)
 ### Bug修复
+* 【core 】     CharsetUtil在不支持GBK的系统中运行报错问题(issue#731@Github)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 12 - 1
hutool-core/src/main/java/cn/hutool/core/util/CharsetUtil.java

@@ -26,7 +26,18 @@ public class CharsetUtil {
 	/** UTF-8 */
 	public static final Charset CHARSET_UTF_8 = StandardCharsets.UTF_8;
 	/** GBK */
-	public static final Charset CHARSET_GBK = Charset.forName(GBK);
+	public static final Charset CHARSET_GBK;
+
+	static{
+		//避免不支持GBK的系统中运行报错 issue#731
+		Charset _CHARSET_GBK = null;
+		try{
+			_CHARSET_GBK = Charset.forName(GBK);
+		} catch (UnsupportedCharsetException e){
+			//ignore
+		}
+		CHARSET_GBK = _CHARSET_GBK;
+	}
 	
 	/**
 	 * 转换为Charset对象