2

Is it safe to return an input stream from a try-with-resource statement to handle the closing of the stream once the caller has consumed it?

public static InputStream example() throws IOException {
    ...
    try (InputStream is = ...) {
        return is;
    }
}
tarka
  • 5,289
  • 10
  • 51
  • 75
  • as soon as you get out of the function execution the resource goes out of scope and hence garbage collected... All in All.. Safe but useless.... – CoderNeji Jun 04 '15 at 12:16
  • 2
    @CoderNeji Apart from the part that you usually cannot tell when an unreferenced object will be garbage collected, the `InputStream` is **closed**, not garbage collected (because there is still the 'live' reference returned from the method). What is out of scope is the `is` variable, but that is a different issue that is solved at compilation time. – SJuan76 Jun 04 '15 at 12:26
  • thanks for explaination – CoderNeji Jun 04 '15 at 12:32

1 Answers1

6

It's safe, but it will be closed, so I don't think it is particularly useful... (You can't reopen a closed stream.)

See this example:

public static void main(String[] argv) throws Exception {
    System.out.println(example());
}

public static InputStream example() throws IOException {
    try (InputStream is = Files.newInputStream(Paths.get("test.txt"))) {
        System.out.println(is);
        return is;
    }
}

Output:

sun.nio.ch.ChannelInputStream@1db9742
sun.nio.ch.ChannelInputStream@1db9742

The (same) input stream is returned (same by reference), but it will be closed. By modifying the example to this:

public static void main(String[] argv) throws Exception {
    InputStream is = example();
    System.out.println(is + " " + is.available());
}

public static InputStream example() throws IOException {
    try (InputStream is = Files.newInputStream(Paths.get("test.txt"))) {
        System.out.println(is + " " + is.available());
        return is;
    }
}

Output:

sun.nio.ch.ChannelInputStream@1db9742 1000000
Exception in thread "main" java.nio.channels.ClosedChannelException
    at sun.nio.ch.FileChannelImpl.ensureOpen(FileChannelImpl.java:109)
    at sun.nio.ch.FileChannelImpl.size(FileChannelImpl.java:299)
    at sun.nio.ch.ChannelInputStream.available(ChannelInputStream.java:116)
    at sandbox.app.Main.main(Main.java:13)
icza
  • 389,944
  • 63
  • 907
  • 827