11

From the docs:

Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.)

When I create a Stream<String> using the lines() method on a BufferedReader as seen below, does closing the Stream also close the BufferedReader?

try (Stream<String> lines = new BufferedReader(new InputStreamReader(process.getInputStream())).lines()) {
  // Do stuff
}

// Is the BufferedReader, InputStreamReader and InputStream closed?

Some really quick tests I've tried say no (the in field of the BufferedReader is not null), but then I'm confused by the following sentence, since this example is I/O as well, right?

Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing.

If not, do I need to close both instances, or will closing the BufferedReader suffice?


Ideally, I'd like to return a Stream<String> from some method, without having the client worry about the readers. At the moment, I've created a Stream decorator which also closes the reader, but it's easier if that isn't necessary.

nhaarman
  • 90,392
  • 51
  • 233
  • 265
  • Where do you test that the `BufferedReader` is closed? Inside the `try` block? –  Jun 16 '15 at 08:40
  • I have tested it after explicitly calling `close` on the `Stream`, and checked the field using the debugger. The autoclosing try is meant as an example of how I'd like to use it. – nhaarman Jun 16 '15 at 08:42
  • See also: http://stackoverflow.com/questions/34072035/why-is-files-lines-and-similar-streams-not-automatically-closed/34073306#34073306 – Brian Goetz Oct 08 '16 at 21:05

3 Answers3

8

If you want to defer closing of the reader to the delivered Stream you need to invoke Stream.onClose():

static Stream<String> toStream(BufferedReader br){
    return br.lines().onClose(asUncheckedAutoCloseable(br));
}


static Runnable asUncheckedAutoCloseable(AutoCloseable ac) {
    return () -> {
        try {
            ac.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    };
}
Eugene
  • 102,901
  • 10
  • 149
  • 252
3

No, seems it doesn't. As the stream is created using

    return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
            iter, Spliterator.ORDERED | Spliterator.NONNULL), false);

which doesn't pass any reference to the the BufferedReader

JiriS
  • 5,307
  • 4
  • 27
  • 39
  • 1
    There is no call to `public static Stream lines(Path path, Charset cs)` in the question. [`Reader.lines()`](https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#lines--) is called. –  Jun 16 '15 at 08:53
  • This seems to be a compelling argument as to why the `BufferedReader` would not be closed from the `Stream`. Looking at the source of `lines`, I see no reason why the `Stream` must be closed, and I conclude that only the `BufferedReader` should be closed. – nhaarman Jun 16 '15 at 09:15
  • Regarding the second part of your question - try `Files.lines(Path, Charset)` – JiriS Jun 16 '15 at 09:17
  • 1
    Unfortunately, I'm not reading from a file, but from a process stream. – nhaarman Jun 16 '15 at 09:20
2

In your question you don't show how you create the Reader that is the argument of new BufferedReader(in). But from my own tests there is no reason to assume that the Stream closes this argument.

Doing the following should close everybody:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.stream.Stream;

public class SOPlayground {

    public static void main(String[] args) throws Exception {
        try (Reader in = new InputStreamReader(new FileInputStream(new File("/tmp/foo.html")));
                BufferedReader reader = new BufferedReader(in);
                Stream<String> lines = reader.lines()) {
            lines.forEach(System.out::println);
        }        
    }
}