增加注释
This commit is contained in:
parent
ffb36c5dc6
commit
fc490e8ddc
@ -5,14 +5,31 @@ import javafx.scene.control.TextArea;
|
|||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
|
||||||
|
编码检测工具类
|
||||||
|
|
||||||
|
*/
|
||||||
public class EncodingDetector {
|
public class EncodingDetector {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测 TextArea 中的文本编码
|
||||||
|
*
|
||||||
|
* @param textArea 文本区域
|
||||||
|
* @return 字符串表示的编码,如果检测失败则返回 "UNKNOWN"
|
||||||
|
*/
|
||||||
public static String detectEncoding(TextArea textArea) {
|
public static String detectEncoding(TextArea textArea) {
|
||||||
String text = textArea.getText();
|
String text = textArea.getText();
|
||||||
|
|
||||||
return detectEncoding(text);
|
return detectEncoding(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检测文本编码
|
||||||
|
*
|
||||||
|
* @param text 要检测的文本
|
||||||
|
* @return 字符串表示的编码,如果检测失败则返回 "UNKNOWN"
|
||||||
|
*/
|
||||||
public static String detectEncoding(String text) {
|
public static String detectEncoding(String text) {
|
||||||
// 尝试常见的编码
|
// 尝试常见的编码
|
||||||
for (Charset charset : commonCharsets()) {
|
for (Charset charset : commonCharsets()) {
|
||||||
@ -25,6 +42,11 @@ public class EncodingDetector {
|
|||||||
return "UNKNOWN";
|
return "UNKNOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取常见的字符集数组
|
||||||
|
*
|
||||||
|
* @return 常见字符集数组
|
||||||
|
*/
|
||||||
private static Charset[] commonCharsets() {
|
private static Charset[] commonCharsets() {
|
||||||
return new Charset[]{
|
return new Charset[]{
|
||||||
StandardCharsets.UTF_8,
|
StandardCharsets.UTF_8,
|
||||||
@ -35,6 +57,13 @@ public class EncodingDetector {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查指定编码是否能够正确解码文本
|
||||||
|
*
|
||||||
|
* @param text 要检测的文本
|
||||||
|
* @param encoding 要尝试的编码
|
||||||
|
* @return 如果指定编码能够正确解码文本,则返回 true,否则返回 false
|
||||||
|
*/
|
||||||
private static boolean isValidEncoding(String text, Charset encoding) {
|
private static boolean isValidEncoding(String text, Charset encoding) {
|
||||||
// 尝试使用指定编码解码
|
// 尝试使用指定编码解码
|
||||||
byte[] bytes = text.getBytes(encoding);
|
byte[] bytes = text.getBytes(encoding);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user