Browse Source

Merge pull request 'fix: 增大小文本文件检测字符编码的正确率;处理并发隐患' (#3) from asiawu/kkFileView:master into master

pull/241/head
gitchenjh 1 year ago
parent
commit
3cfc51a4d7
  1. 7
      server/src/main/java/cn/keking/utils/EncodingDetects.java
  2. 4604
      server/src/main/java/cn/keking/utils/SimpleEncodingDetects.java
  3. 2
      server/src/test/java/cn/keking/EncodingTests.java
  4. 3
      server/src/test/resources/testData/28/Small GB2312

7
server/src/main/java/cn/keking/utils/EncodingDetects.java

@ -16,8 +16,8 @@ import java.nio.file.Files;
* @description: 自动获取文件的编码 * @description: 自动获取文件的编码
*/ */
public class EncodingDetects { public class EncodingDetects {
private static UniversalDetector detector = new UniversalDetector(null);
private static final int DEFAULT_LENGTH = 4096; private static final int DEFAULT_LENGTH = 4096;
private static final int LIMIT = 50;
private static final Logger logger = LoggerFactory.getLogger(EncodingDetects.class); private static final Logger logger = LoggerFactory.getLogger(EncodingDetects.class);
public static String getJavaEncode(String filePath) { public static String getJavaEncode(String filePath) {
@ -36,7 +36,10 @@ public class EncodingDetects {
} }
public static String getJavaEncode(byte[] content) { public static String getJavaEncode(byte[] content) {
detector.reset(); if (content != null && content.length <= LIMIT) {
return SimpleEncodingDetects.getJavaEncode(content);
}
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(content, 0, content.length); detector.handleData(content, 0, content.length);
detector.dataEnd(); detector.dataEnd();
String charsetName = detector.getDetectedCharset(); String charsetName = detector.getDetectedCharset();

4604
server/src/main/java/cn/keking/utils/SimpleEncodingDetects.java

File diff suppressed because it is too large Load Diff

2
server/src/test/java/cn/keking/EncodingTests.java

@ -17,7 +17,7 @@ import java.net.URISyntaxException;
public class EncodingTests { public class EncodingTests {
@Test @Test
void testCharDet() throws URISyntaxException { void testCharDet() throws URISyntaxException {
for (int i = 0; i < 28; i++) { for (int i = 0; i < 29; i++) {
File dir = new File(getClass().getClassLoader().getResource("testData\\" + i).toURI()); File dir = new File(getClass().getClassLoader().getResource("testData\\" + i).toURI());
String dirPath = dir.getPath(); String dirPath = dir.getPath();
String textFileName = dir.list()[0]; String textFileName = dir.list()[0];

3
server/src/test/resources/testData/28/Small GB2312

@ -0,0 +1,3 @@
发斯蒂芬斯蒂芬
顶顶顶顶~
Loading…
Cancel
Save