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();
}
}
}
As stated in the javadoc,
BaseStream.onClose()"Returns an equivalent stream with an additional close handler":will call
MyCursor.close()as desired.