java-streamautocloseable

Converting an AutoCloseable, Iterable class into a Stream


StreamSupport.stream() can create a Stream from an Iterable, but what if the class implements Iterable and AutoCloseable? Is it possible to convert that class to a Stream and construct it within a try-with-resources block?

public class NonWorkingExample {
    public static void main(final String[] args) {
        // this won't call MyCursor.close()
        try (Stream<String> stream = StreamSupport.stream(new MyCursor().spliterator(), false)) {
            stream.forEach(System.out::println);
        }
    }

    private static class MyCursor implements AutoCloseable, Iterable<String> {
        public void close() throws Exception {
            System.out.println("close");
        }

        public Iterator<String> iterator() {
            List<String> items = new ArrayList<>();
            items.add("foo");
            items.add("bar");
            items.add("baz");
            return items.iterator();
        }
    }
}

Solution

  • As stated in the javadoc, BaseStream.onClose() "Returns an equivalent stream with an additional close handler":

    public class WorkingExample {
        public static void main(final String[] args) {
            MyCursor cursor = new MyCursor();
            try (Stream<String> stream = StreamSupport.stream(cursor.spliterator(), false)
                                                      .onClose(cursor::close)) {
                stream.forEach(System.out::println);
            }
        }
    }
    

    will call MyCursor.close() as desired.