2

Is there a way to close all streams or handles at once in Java without having references to them? Or is there a way to close all handles (streams) associated with particular File having reference of that File in Java?

Hassan Qayyum
  • 91
  • 1
  • 9
  • 4
    My gut reaction says `no, no way, not possible`, but there is probably some implementation specific way to do it... As far as I'm aware, there isn't a registry of streams associated with files anywhere in the standard API... Why exactly do you need to do this? You should be able to maintain a registry of streams yourself... assuming you have a reference to each stream at *some* point... – Socratic Phoenix Jul 19 '17 at 12:32
  • What do you mean by "close"? Do you mean calling the `close()` method? The answer to that is no, not within Java. – President James K. Polk Jul 19 '17 at 12:32
  • 7
    Sure, kill the JVM. – Andy Turner Jul 19 '17 at 12:33
  • 2
    look at `try-with-resources` statement – Adrian Jul 19 '17 at 12:33
  • 3
    Even if there is a way, don't do it. Just close them whenever your work done with them right away. – Suresh Atta Jul 19 '17 at 12:36
  • No, not easy. Have a read here https://stackoverflow.com/questions/1947122/is-there-a-simple-way-of-obtaining-all-object-instances-of-a-specific-class-in-j – cheffe Jul 19 '17 at 12:39
  • 1
    If you have this problem you're asking the wrong question. You need to find and fix whatever situations there are in your code that can possibly give rise to such a question. – user207421 Jul 19 '17 at 12:42
  • System.exit(0); – P.J.Meisch Jul 19 '17 at 13:09
  • The reason I'm asking this question is because I'm using 3rd party code in my own code which creates some files. And from the behavior of the program it seems that the 3rd party code has forgotten to close a stream some where. – Hassan Qayyum Jul 19 '17 at 13:10

2 Answers2

2

No

Without a reference to the stream, there's no way to invoke close() on it, same with any other object - without a reference to it, you can't actually do anything with it. I'm not completely sure how streams are handled internal, and to be honest, there is probably a way to do this using internals or reflection or JNI, but all of those should generally be avoided if a better solution exists.

Instead, you should look at the design of your program/code and try to re-design it to either:

  1. close() streams immediately after use, or
  2. Maintain a list of open streams yourself

If you're using an API that doesn't give you a reference to the stream, and doesn't close() the stream, and provides you no way to close() the stream, then that is a bug/design flaw in the API your are using.

If you have a reference to a stream at one point, but lose that reference and need to close it later, then you could simply set up some sort of registry to maintain a list of streams. An extremley simple example would be something like this:

public class StreamRegistry {
    private List<Closeable> streams = new ArrayList<>();

    public void register(Closeable closeable) {
        streams.add(closeable);
    }

    public void close() throws IOException {
         for(Closeable closeable : streams) {
             closeable.close();
         }
    }
    
}

Although you'd probably want to catch the IOException from close(), record it, and continue trying to close streams.

However, I would not recommend using a registry unless you absolutely have to. As @EJP and @Suresh Atta said in the comments to your question, if you're asking this kind of question, you should probably be trying to refactor/redesign your code to avoid this issue altogether and try and close() streams as soon as you're done with them. That said, without knowing exactly why you need to do this, I can't really do anything other than propose options.

Community
  • 1
  • 1
Socratic Phoenix
  • 556
  • 1
  • 7
  • 19
  • As you mentioned in my scenario I'm using 3rd party code in my own code which creates some files. And from the behavior of the program it seems that the 3rd party code has forgotten to close a stream some where. I think I would use some code analyzer to analyze that 3rd party code for memory leaks. – Hassan Qayyum Jul 19 '17 at 13:12
1

Best way is use some static code analysis tool like sonar. It will tell where are the places you have missed to close the open stream. If you have forget to open any stream, it will tell you that there is a blocker issue which have to fix.

For more details about sonar, you can use the given link.

Sonar Qube

Sumit Das
  • 311
  • 3
  • 6
  • As you mentioned in my scenario I'm using 3rd party code in my own code which creates some files. And from the behavior of the program it seems that the 3rd party code has forgotten to close a stream some where. I think I would use some code analyzer to analyze that 3rd party code for memory leaks. – Hassan Qayyum Jul 19 '17 at 13:12