English 中文(简体)
FindBugs: "may fail to close stream" - is this valid in case of InputStream?
原标题:

In my Java code, I start a new process, then obtain its input stream to read it:

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

FindBugs reports an error here:

may fail to close stream
Pattern id: OS_OPEN_STREAM, type: OS, category: BAD_PRACTICE

Must I close the InputStream of another process? And what s more, according to its Javadoc, InputStream#close() does nothing. So is this a false positive, or should I really close the input stream of the process when I m done?

最佳回答

In this case, you want to close() the Reader, which will close its underlying streams. Yes, it s always good practice to close streams, even if at the moment you know the implementation you re looking at doesn t do anything (though, in fact, it does here!). What if that changed later?

FindBugs is only there to warn about possible errors; it can t always know for sure.

Finally yes, your Java process owns the process and Process object you spawned. You most definitely need to close that and the output stream. Nobody else is using them, and, it s important to do such things to avoid OS-related stream funny business.

问题回答

InputStream is an abstract class - just because its implementation does nothing doesn t mean that the actual type of object returned by process.getInputStream() doesn t.

It s possible that failing to close the input stream in this particular case would do no harm - but I personally wouldn t count on it. Close it like you d close any other input stream. Aside from anything else, that makes your code more robust in case you ever decide to change it to read from something else - it would be all too easy to (say) read from a file instead, and not notice that you re not closing the FileInputStream.

I think its always a good practice to close all the streams you open. Preferably in the finally{} block. Since it does nothing as java says, why not call the close() method. Its of no harm.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签