增加编码判断优化性能
This commit is contained in:
parent
63c60c99af
commit
73a6967eff
7
pom.xml
7
pom.xml
@ -16,6 +16,13 @@
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-core</artifactId>
|
||||
<version>2.5.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-fxml</artifactId>
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
module org.jcnc.jnotepad {
|
||||
requires javafx.controls;
|
||||
requires javafx.fxml;
|
||||
|
||||
exports org.jcnc.jnotepad;
|
||||
}
|
||||
@ -10,11 +10,11 @@ import javafx.stage.FileChooser;
|
||||
import org.jcnc.jnotepad.MainApp;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jcnc.jnotepad.ViewManager.*;
|
||||
import static org.jcnc.jnotepad.controller.EncodingDetector.detectEncoding;
|
||||
|
||||
/**
|
||||
* 控制器类负责处理与用户界面的交互,并实现相关事件处理逻辑。
|
||||
@ -221,14 +221,14 @@ public class Controller {
|
||||
enCodingLabel.setText("\t编码: " + encoding);
|
||||
}
|
||||
|
||||
// 判断编码是否有效
|
||||
/* // 判断编码是否有效
|
||||
public static boolean isEncodingValid(String text, Charset encoding) {
|
||||
byte[] bytes = text.getBytes(encoding);
|
||||
String decodedText = new String(bytes, encoding);
|
||||
return text.equals(decodedText);
|
||||
}
|
||||
}*/
|
||||
|
||||
// 检测文本编码
|
||||
/* // 检测文本编码
|
||||
public static String detectEncoding(String text) {
|
||||
Charset[] possibleEncodings = {StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1, StandardCharsets.UTF_16};
|
||||
for (Charset encoding : possibleEncodings) {
|
||||
@ -238,7 +238,7 @@ public class Controller {
|
||||
}
|
||||
}
|
||||
return "未知";
|
||||
}
|
||||
}*/
|
||||
|
||||
// 获取光标所在行数
|
||||
public static int getRow(int caretPosition, String text) {
|
||||
|
||||
@ -0,0 +1,47 @@
|
||||
package org.jcnc.jnotepad.controller;
|
||||
|
||||
import javafx.scene.control.TextArea;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class EncodingDetector {
|
||||
|
||||
public static String detectEncoding(TextArea textArea) {
|
||||
String text = textArea.getText();
|
||||
|
||||
return detectEncoding(text);
|
||||
}
|
||||
|
||||
public static String detectEncoding(String text) {
|
||||
// 尝试常见的编码
|
||||
for (Charset charset : commonCharsets()) {
|
||||
if (isValidEncoding(text, charset)) {
|
||||
System.out.println(isValidEncoding(text, charset));
|
||||
return charset.name();
|
||||
}
|
||||
}
|
||||
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
private static Charset[] commonCharsets() {
|
||||
return new Charset[] {
|
||||
StandardCharsets.UTF_8,
|
||||
StandardCharsets.UTF_16,
|
||||
StandardCharsets.UTF_16LE,
|
||||
StandardCharsets.UTF_16BE,
|
||||
StandardCharsets.ISO_8859_1
|
||||
};
|
||||
}
|
||||
|
||||
private static boolean isValidEncoding(String text, Charset encoding) {
|
||||
// 尝试使用指定编码解码
|
||||
byte[] bytes = text.getBytes(encoding);
|
||||
String decoded = new String(bytes, encoding);
|
||||
|
||||
|
||||
// 解码后的文本相同表示编码有效
|
||||
return text.equals(decoded);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,6 @@
|
||||
package org.jcnc.jnotepad.view;
|
||||
|
||||
import javafx.scene.control.TextArea;
|
||||
import org.jcnc.jnotepad.ViewManager;
|
||||
import org.jcnc.jnotepad.controller.Controller;
|
||||
|
||||
import static org.jcnc.jnotepad.ViewManager.*;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user