FindBugs:" may fail to close stream ", how to solve it?

Viewed 10313

The follow code is marked as erroneous by FindBugs. FindBugs says that "This method may fail to clean up (close, dispose of) a stream, database object, or other resource requiring an explicit cleanup operation." The error is marked on the line output = new FileOutputStream (localFile);

But we have already added try/finally to the block.

inputStream   input =null;
OutputStream output =null;
try {
    input = zipFile.getInputStream(entry);
    File localFile = new File(unzipFile.getAbsolutePath()
            + File.separator + entryName);
    output = new FileOutputStream (localFile);  // BUG MARKED HERE
    byte[] buffer = new byte[1024 * 8];
    int readLen = 0;
    while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {
        output.write(buffer, 0, readLen);
    }
    output.flush();
    output.close();
    input.close();

} finally {
    if(output!=null) {
        output.flush();
        output.close();
    }
    if(input!=null) {
        input.close();
    }
}
1 Answers
Related