java - 如何在不使用 BOM 且以非 ASCII 字符开头的情况下识别文件的不同编码?

标签 java unicode encoding byte-order-mark non-ascii-characters

我在尝试识别没有 BOM 的文件的编码时遇到了问题,尤其是当文件以非 ASCII 字符开头时。

我发现以下两个关于如何识别文件编码的主题,

目前,我创建了一个类来识别文件的不同编码(例如 UTF-8、UTF-16、UTF-32、UTF-16 无 BOM 等),如下所示,

public class UnicodeReader extends Reader {
private static final int BOM_SIZE = 4;
private final InputStreamReader reader;

/**
 * Construct UnicodeReader
 * @param in Input stream.
 * @param defaultEncoding Default encoding to be used if BOM is not found,
 * or <code>null</code> to use system default encoding.
 * @throws IOException If an I/O error occurs.
 */
public UnicodeReader(InputStream in, String defaultEncoding) throws IOException {
    byte bom[] = new byte[BOM_SIZE];
    String encoding;
    int unread;
    PushbackInputStream pushbackStream = new PushbackInputStream(in, BOM_SIZE);
    int n = pushbackStream.read(bom, 0, bom.length);

    // Read ahead four bytes and check for BOM marks.
    if ((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF)) {
        encoding = "UTF-8";
        unread = n - 3;
    } else if ((bom[0] == (byte) 0xFE) && (bom[1] == (byte) 0xFF)) {
        encoding = "UTF-16BE";
        unread = n - 2;
    } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE)) {
        encoding = "UTF-16LE";
        unread = n - 2;
    } else if ((bom[0] == (byte) 0x00) && (bom[1] == (byte) 0x00) && (bom[2] == (byte) 0xFE) && (bom[3] == (byte) 0xFF)) {
        encoding = "UTF-32BE";
        unread = n - 4;
    } else if ((bom[0] == (byte) 0xFF) && (bom[1] == (byte) 0xFE) && (bom[2] == (byte) 0x00) && (bom[3] == (byte) 0x00)) {
        encoding = "UTF-32LE";
        unread = n - 4;
    } else {
        // No BOM detected but still could be UTF-16
        int found = 0;
        for (int i = 0; i < 4; i++) {
            if (bom[i] == (byte) 0x00)
                found++;
        }

        if(found >= 2) {
            if(bom[0] == (byte) 0x00){
                encoding = "UTF-16BE";
            }
            else {
                encoding = "UTF-16LE";
            }
            unread = n;
        }
        else {
            encoding = defaultEncoding;
            unread = n;
        }
    }

    // Unread bytes if necessary and skip BOM marks.
    if (unread > 0) {
        pushbackStream.unread(bom, (n - unread), unread);
    } else if (unread < -1) {
        pushbackStream.unread(bom, 0, 0);
    }

    // Use given encoding.
    if (encoding == null) {
        reader = new InputStreamReader(pushbackStream);
    } else {
        reader = new InputStreamReader(pushbackStream, encoding);
    }
}

public String getEncoding() {
    return reader.getEncoding();
}

public int read(char[] cbuf, int off, int len) throws IOException {
    return reader.read(cbuf, off, len);
}

public void close() throws IOException {
    reader.close();
}

上面的代码可以在所有情况下正常工作,除非文件没有 BOM 并且以非 ascii 字符开头。由于在这种情况下,检查文件是否仍然是没有BOM的UTF-16的逻辑将无法正常工作,编码将默认设置为UTF-8。

是否有一种方法可以检查没有 BOM 且以非 ascii 字符开头的文件的编码,尤其是对于 UTF-16 NO BOM 文件?

谢谢,任何想法将不胜感激。

最佳答案

一般来说,如果不提供编码,就无法确定。

您可能会根据文本中的特定模式(高位设置、设置、设置、未设置、设置、设置、设置、未设置)猜测 UTF-8,但这仍然是猜测。

UTF-16 很难;您可以在同一流上成功解析 BE 和 LE;这两种方式都会产生一些字符(尽管可能是无意义的文本)。

一些代码使用统计分析来根据符号的频率猜测编码,但这需要对文本(即“这是蒙古语文本”)和频率表(可能与文本不匹配)进行一些假设.归根结底,这仍然只是一个猜测,在 100% 的情况下都无济于事。

关于java - 如何在不使用 BOM 且以非 ASCII 字符开头的情况下识别文件的不同编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5657738/

相关文章:

java - String.compareIgnoreCase 返回错误的结果

java - 避免使用 Spring 的 RestTemplate 对 URL 查询参数进行双重编码

JavaFX 从另一个线程更新 AnchorPane

java - 使用 eclipse 和 file/filereader/bufferedreader 找不到文件...

javascript - 在 JavaScript 中对 utf-8 字符串使用 encodeURI() 与 escape()

python - 判断一个字符是否是组合变音符号

java - Selenium 将文本框视为隐藏,即使我可以在浏览器中看到它

java - Android 空对象引用

android - 音频文件的编码和解码,以通过Internet发送以减小尺寸。安卓

java - 使用 bigInteger 和其他方法重建字节数组