java - 重构自动检测文件的编码

标签 java encoding refactoring

我需要检查编码文件。这段代码有效,但有点长。如何能够对这种逻辑进行任何重构。 也许可以为此目标使用其他变体?

代码:

class CharsetDetector implements Checker {

    Charset detectCharset(File currentFile, String[] charsets) {
        Charset charset = null;

        for (String charsetName : charsets) {
            charset = detectCharset(currentFile, Charset.forName(charsetName));
            if (charset != null) {
                break;
            }
        }

        return charset;
    }

    private Charset detectCharset(File currentFile, Charset charset) {
        try {
            BufferedInputStream input = new BufferedInputStream(
                    new FileInputStream(currentFile));

            CharsetDecoder decoder = charset.newDecoder();
            decoder.reset();

            byte[] buffer = new byte[512];
            boolean identified = false;
            while ((input.read(buffer) != -1) && (!identified)) {
                identified = identify(buffer, decoder);
            }

            input.close();

            if (identified) {
                return charset;
            } else {
                return null;
            }

        } catch (Exception e) {
            return null;
        }
    }

    private boolean identify(byte[] bytes, CharsetDecoder decoder) {
        try {
            decoder.decode(ByteBuffer.wrap(bytes));
        } catch (CharacterCodingException e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean check(File fileChack) {
        if (charsetDetector(fileChack)) {
            return true;
        }
        return false;
    }

    private boolean charsetDetector(File currentFile) {
        String[] charsetsToBeTested = { "UTF-8", "windows-1253", "ISO-8859-7" };

        CharsetDetector charsetDetector = new CharsetDetector();
        Charset charset = charsetDetector.detectCharset(currentFile,
                charsetsToBeTested);

        if (charset != null) {
            try {
                InputStreamReader reader = new InputStreamReader(
                        new FileInputStream(currentFile), charset);

                @SuppressWarnings("unused")
                int valueReaders = 0;
                while ((valueReaders = reader.read()) != -1) {
                    return true;
                }

                reader.close();
            } catch (FileNotFoundException exc) {
                System.out.println("File not found!");
                exc.printStackTrace();
            } catch (IOException exc) {
                exc.printStackTrace();
            }
        } else {
            System.out.println("Unrecognized charset.");
            return false;
        }

        return true;
    }
}

问题:

  • 这个程序逻辑如何重构?
  • 还有哪些检测编码的方法(如 UTF-16 序列等)?

最佳答案

重构此代码的最佳方法是引入一个为您进行字符检测的第 3 方库,因为他们可能做得更好,并且可以使您的代码更小。 见this question一些选择

关于java - 重构自动检测文件的编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15154577/

相关文章:

git - 在具有连续重构的项目上使用git/mercurial?

Python递归函数超出了递归限制。如何将其转换为迭代

ruby-on-rails - 你如何分解出 RSpec 中常见的 "before(:each)"调用,以便多个规范可以使用它们?

java - 从 jtable 中获取选定行的值

java - 我是否需要将 serialVersionUID 放在可序列化类的祖先上?

java - ffmpeg什么时候终止?

python - 如何从 unicode 文字创建 unicode 实例

java - Solr 字符串字段搜索包含特殊字符

java - 以下 ^.*$ 正则表达式匹配什么?

security - 如何确保为具有编码文本的请求返回编码文本?