!30 fix: #I7VP0I 修复UNKOWN在配置文件不存在的问题

Merge pull request !30 from songdragon/fix-I7VP0I
This commit is contained in:
Luke 2023-08-25 09:57:57 +00:00 committed by Gitee
commit a95a65e6d5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -3,16 +3,12 @@ package org.jcnc.jnotepad.tool;
import com.ibm.icu.text.CharsetDetector; import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch; import com.ibm.icu.text.CharsetMatch;
import org.jcnc.jnotepad.constants.TextConstants;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import static org.jcnc.jnotepad.constants.TextConstants.UNKNOWN;
/** /**
@ -28,7 +24,6 @@ public class EncodingDetector {
*/ */
public static final int THRESHOLD_CONFIDENCE = 50; public static final int THRESHOLD_CONFIDENCE = 50;
private EncodingDetector() { private EncodingDetector() {
} }
@ -44,11 +39,11 @@ public class EncodingDetector {
charsetDetector.setText(inputStream); charsetDetector.setText(inputStream);
CharsetMatch[] matchList = charsetDetector.detectAll(); CharsetMatch[] matchList = charsetDetector.detectAll();
if (matchList == null || matchList.length == 0) { if (matchList == null || matchList.length == 0) {
return UNKNOWN; return null;
} }
CharsetMatch maxConfidence = matchList[0]; CharsetMatch maxConfidence = matchList[0];
if (maxConfidence.getConfidence() < THRESHOLD_CONFIDENCE) { if (maxConfidence.getConfidence() < THRESHOLD_CONFIDENCE) {
return UNKNOWN; return null;
} }
for (int i = 1; i < matchList.length; i++) { for (int i = 1; i < matchList.length; i++) {
CharsetMatch match = matchList[i]; CharsetMatch match = matchList[i];
@ -62,7 +57,7 @@ public class EncodingDetector {
} catch (Exception e) { } catch (Exception e) {
LOG.error("", e); LOG.error("", e);
} }
return UNKNOWN; return null;
} }
/** /**
@ -73,9 +68,11 @@ public class EncodingDetector {
*/ */
public static Charset detectEncodingCharset(File file) { public static Charset detectEncodingCharset(File file) {
String charset = detectEncoding(file); String charset = detectEncoding(file);
if (charset.equals(UNKNOWN)) { try {
assert charset != null;
return Charset.forName(charset);
} catch (Exception e) {
return Charset.defaultCharset(); return Charset.defaultCharset();
} }
return Charset.forName(charset);
} }
} }