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

This commit is contained in:
songdragon 2023-08-24 23:24:38 +08:00
parent 8a931725bf
commit 92d0339146

View File

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