Findbugs:依赖默认编码,但编码存在?

时间:2013-06-12 13:06:13

标签: java character-encoding intellij-idea findbugs

FindBugs抱怨(理所当然),我没有为我的文件指定编码。我已经改变了代码,但错误仍然存​​在,我不知道为什么。

有问题的代码:

private void readGndFile() throws IOException {
    try (final BufferedReader reader = Files.newBufferedReader(gndFile.toPath(), Charset.forName("utf-8"))) {
        readGndFile(reader); // Findbugs: Found reliance on default encoding
    }
}

伴随方法:

private void readGndFile(final BufferedReader reader) throws IOException {
    while (true) {
        String line = reader.readLine();
        while (!isGndEntryHeader(line))
            line = reader.readLine();

        if (isEndOfFile(line))
            break;

        List<String> gndEntryParts = readGndEntry(reader);
        if (gndEntryParts == null)
            break;

        DataObject dataObject = tryConvertGndEntry(gndEntryParts);
        if (dataObject != null)
            dataObjects.add(dataObject);
    }
}

我正在使用IntelliJ Idea 12和FindBugs 0.9.97

如果需要更多代码:

private boolean isGndEntryHeader(final String line) {
    final String gndHeaderMask = "<http://d-nb.info/gnd/[0-9]+-([0-9]|[a-zA-Z])>";

    return (line != null && !line.matches(gndHeaderMask));
}

private boolean isEndOfFile(final String line) {
    return (line == null);
}

private List<String> readGndEntry(final BufferedReader reader) throws IOException {
    List<String> gndEntry = new ArrayList<>();

    String line = reader.readLine();
    if (line == null)
        return null;
    else
        line = line.trim();

    while (line != null && !line.isEmpty()) {
        gndEntry.add(line.trim());
        line = reader.readLine();
    }

    if (line == null && gndEntry.size() == 0)
        return null;

    return gndEntry;
}

protected abstract DataObject tryConvertGndEntry(List<String> gndEntryParts);

0 个答案:

没有答案