!29 fix: #I7VP0I 检测文件失败时,使用系统默认编码
Merge pull request !29 from songdragon/fix-I7VP0I
This commit is contained in:
commit
f950b42450
@ -23,6 +23,10 @@ import static org.jcnc.jnotepad.constants.TextConstants.UNKNOWN;
|
||||
public class EncodingDetector {
|
||||
|
||||
private static final Logger LOG = LogUtil.getLogger(EncodingDetector.class);
|
||||
/**
|
||||
* 编码侦测概率,阈值:50%
|
||||
*/
|
||||
public static final int THRESHOLD_CONFIDENCE = 50;
|
||||
|
||||
|
||||
private EncodingDetector() {
|
||||
@ -39,10 +43,20 @@ public class EncodingDetector {
|
||||
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file.getPath()))) {
|
||||
charsetDetector.setText(inputStream);
|
||||
CharsetMatch[] matchList = charsetDetector.detectAll();
|
||||
for (CharsetMatch match : matchList) {
|
||||
if (matchList == null || matchList.length == 0) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
CharsetMatch maxConfidence = matchList[0];
|
||||
if (maxConfidence.getConfidence() < THRESHOLD_CONFIDENCE) {
|
||||
return UNKNOWN;
|
||||
}
|
||||
for (int i = 1; i < matchList.length; i++) {
|
||||
CharsetMatch match = matchList[i];
|
||||
LOG.debug("{} : {}", match.getName(), match.getConfidence());
|
||||
if (match.getConfidence() > 50) {
|
||||
return match.getName();
|
||||
if (match.getConfidence() >= THRESHOLD_CONFIDENCE && match.getConfidence() >= maxConfidence.getConfidence()) {
|
||||
maxConfidence = match;
|
||||
} else {
|
||||
return maxConfidence.getName();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@ -60,7 +74,7 @@ public class EncodingDetector {
|
||||
public static Charset detectEncodingCharset(File file) {
|
||||
String charset = detectEncoding(file);
|
||||
if (charset.equals(UNKNOWN)) {
|
||||
return StandardCharsets.UTF_8;
|
||||
return Charset.defaultCharset();
|
||||
}
|
||||
return Charset.forName(charset);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user