Java – findbugs: “the stream may not be closed” – is this valid in the case of InputStream?
In my java code, I start a new process and get its input stream to read it:
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
Findbugs reports errors here:
may fail to close stream Pattern id: OS_OPEN_STREAM,type: OS,category: BAD_PRACTICE
Must I close the InputStream of another process? Moreover, according to its Javadoc, InputStream #close () does nothing So is this a false positive, or should I really close the input flow of the process after completion?
Solution
In this case, you want to close () reader, which will close its underlying flow Yes, it's a good practice to close the flow, even if you know that the implementation you're looking at doesn't do anything (although it's actually here!) What if it changes later?
Findbugs just warn of possible errors; It can't always know for sure
Finally, your Java process has the process and process objects you generate You absolutely need to close it and the output stream No one else is using them, and it's important to do these things to avoid OS related processes and interesting business