0

BaseStream provides the method onClose which takes a Runnable. Is this a method relevant for application programmers? Is it primarily intended be used when using a stream that needs to be closed, such as Files.lines(), as in this question: How to read from files with Files.lines(...).forEach(...)?? What use cases are there for this method (apart from logging).

Community
  • 1
  • 1
user140547
  • 7,750
  • 3
  • 28
  • 80
  • 1
    Closing resources is exactly the intention of the `onClose` action; in all other cases, the application is unlikely to close the Stream at all. Therefore it is crucial to document when a Stream holds resources that need closing. For an application programmer, it will be relevant, when (s)he implements a resource based stream, e.g. [this example](http://stackoverflow.com/a/32232173/2711488) implements a custom JDBC `ResultSet` based Stream… – Holger Jun 16 '16 at 18:02
  • @Holger: Thank you for the example. Actually, when inspecting the source code of `Files.lines()`, I noticed it also uses `onClose` to close the BufferedReader it opens to read from the file when the stream is closed. – user140547 Jun 16 '16 at 18:51
  • I assumed that you know the `Files.lines` example, as you referred to it. I wanted to provide an example of something not covered by the JRE, as that’s exactly the place, where an application programmer has to deal with it. – Holger Jun 17 '16 at 07:58
  • @Holger: The `Files.lines` example was originally only serving as an example of an stream which needs to be closed after use, and I guessed `onClose()` might be somehow be useful in such a context. It was a coincidence, and I realized only after your comment, that `onClose` is actually used in the implementation of `Files.lines` – user140547 Jun 17 '16 at 08:42

0 Answers0