fix: #I7VP0I 修复UNKOWN在配置文件不存在的问题
This commit is contained in:
parent
8a931725bf
commit
92d0339146
@ -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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user