Method may fail to close stream on exception

Posted by 01 on Stack Overflow See other posts from Stack Overflow or by 01
Published on 2010-04-17T10:21:43Z Indexed on 2010/04/17 10:23 UTC
Read the original article Hit count: 545

Filed under:
|
|
|

I get the critical error with finbugs

The method creates an IO stream object, does not assign it to any fields, pass it to other methods, or return it, and does not appear to close it on all possible exception paths out of the method. This may result in a file descriptor leak. It is generally a good idea to use a finally block to ensure that streams are closed.

try {
...
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
...
} catch (IOException e) {
    throw new RuntimeException(e);
} finally {
    try {
        if (stdInput != null) {
            stdInput.close();
        }
        if (stdError != null) {
            stdError.close();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
do i need to close also InputStreamReader or p.getErrorStream(it returns InputStream) ??

© Stack Overflow or respective owner

Related posts about java

Related posts about findbugs