Stream is AutoCloseable, Stream.onClose

Joe Bowbeer joe.bowbeer at gmail.com
Mon Jun 24 16:22:36 PDT 2013


I like the benefits, and I still think there is an intermediate API (closer
to File) that will provide these benefits while at the same time avoiding
the misfit at the Stream API. But I got nothing to back up intuition so
far...

In case there's any confusion, I reiterate the method name problem one more
time below.  I have a strong opinion about this because I spend most of my
time writing code for 3rd party APIs, and this seems like an easy problem
to avoid.

Given the prevalent naming style in many popular frameworks, a name like
onClose would be interpreted as a callback method on a Stream object.
 Programmers would not expect that it should be called directly. Countless
javadoc admonishments would have discouraged them from even thinking about
it.  ("This is a method for subclasses, etc.")  Similarly, a reader of
Stream code would do a double-take if they saw the snippet
"stream.onClose(stream)...".  The expected expression would look something
like "stream.setOnCloseListener(callback)...".

To avoid these problems, we need some method name that refers to closing
but doesn't start with the "on" prefix.

Btw, the onClose(resource::close) is an improvement, but is still too
surprising for my taste, and I think it sets a bad precedent.

Of the alternatives suggested, I like Remi's:

finallyClosing(stream)

That seems very clear to me.

--Joe


On Fri, Jun 21, 2013 at 10:39 AM, Brian Goetz <brian.goetz at oracle.com>wrote:

> Let me step back and describe why these use cases are important.
>
> Crawling directories is something that's always been a pain in Java and
> easy in scripting languages.  The work Henry did in java.nio.Files was
> designed to alleviate that pain.
>
> For example, to walk a tree:
>
>   Files.list(Path)
>
> produces a Stream<Path>.  But it also encapsulates a DirectoryStream
> through its Iterator, which should be closed when we're finished
> enumerating the stream.  The route of "CloseableStream extends Stream,
> Closeable" was intended to make that easy through TWR:
>
>   try (Stream<Path> s = Files.list(path)) {
>     s.filter(e -> e.getName().endsWith(".java"))
>      .forEach(...);
>   }
>
> But as it turns out, this isn't even enough.  There's also Files.lines(f),
> which gives you the lines in a given file.  If we want all the lines in a
> set of files:
>
>   Files.list(path)
>        .flatMap(Files::lines)
>        ...
>
> This is really nice!  But, even with TWR at the top level, we don't have a
> way to close the BufferedReader that is implicit in each iteration of
> flatMap, only the top-level stream, which is not enough.
>
> If we step back and say "All streams support closing", then we can
> implement flatMap in a close-friendly way:
>
>       // In ReferencePipeline.flatMap
>       try (Stream<? extends R> result = mapper.apply(u)) {
>           if (result != null)
>               result.sequential().forEach(**downstream);
>       }
>
> For most streams, the implicit close would be a no-op, but for streams
> with a close handler, it would get run when the nested stream goes out of
> scope.
>
> Now, if Files.lines() produces a stream with a close hook, the above plays
> nicely with TWR:
>
>   try(Stream<Path> paths = Files.list(path)) {
>        paths.flatMap(Files::lines)
>             ...
>   }
>
> and even if the user does not use TWR, only the top-level stream is at
> risk for leaking, rather than the thousands of BufferedReader-backed
> streams.
>
> This is why the "have a different, statically-identified kind of stream"
> approach fails; if we need to release resources, we need it to be turtles
> all the way down.
>
> Here's how the Files.lines implementation looks now:
>
>         BufferedReader br = Files.newBufferedReader(path, cs);
>         return new DelegatingCloseableStream<>(**br, br.lines());
>
> under the new approach, it would look like:
>
>         BufferedReader br = Files.newBufferedReader(path, cs);
>         return br.lines().onClose(br::close);
>
>
>
>
>
> On 6/20/2013 11:17 AM, Paul Sandoz wrote:
>
>> Hi,
>>
>> Having another go at this...
>>
>> The current solution of resource management for Stream is poor (as
>> previously mentioned):
>>
>> 1) CloseableStream/**DelegatingStream add a lot of API surface area.
>>
>> 2) Operations on CloseableStream do not return a CloseableStream.
>>
>> 3) try-with-resources always needs to be used with the CloseableStream
>> since the Stream itself has no closeable semantics.
>>
>> 4) Stream.concat fails to propagate the closing
>>
>>
>> If Stream extends from AutoCloseable we can address the first 3 issues.
>> In fact it can be addressed with just a close method but it is awkward to
>> transform that into an AutoCloseable for use with try-with-resources: try
>> (AutoCloseable ac = () -> s.close()) { … }
>>
>> A negative point is it is no longer clear whether a stream should be
>> closed or not, which is anyway the case for issues 2/3/4. However, i don't
>> think that should stop us trying to improve the general situation, it's not
>> gonna be perfect but i think we can do better than what we currently have
>> [*].
>>
>>
>> Issue 4 can be addressed by adding a Stream.onClose(AutoCloseable ac)
>> method.
>>
>> Stream s = ...
>> s = s.onClose(a).filter(...).**onClose(b).
>> s.close(); // b is called, then a is called
>> s.toArray(); // throws ISE
>>
>> Stream s = ...
>> s = s.onClose(a).onClose(b).
>> s.close(); // b is called, then a is called
>> s.toArray(); // throws ISE
>>
>> The Stream.concat implementation becomes:
>>
>>          Stream<T> cs = (a.isParallel() || b.isParallel())
>>                 ? StreamSupport.parallelStream(**split)
>>                 : StreamSupport.stream(split);
>>
>>          return cs.onClose(() -> { a.close(); b.close(); } ) // ignoring
>> exception handling to be brief
>>
>>
>> The Stream.close/onClose methods enable us to specify more precisely the
>> behaviour of Stream.close, the order in which calls to close on
>> AutoClosable instances passed to onClose occur, and what happens if
>> AutoCloseable.close throws an exception.
>>
>>
>> Of course it is possible to do silly things like this:
>>
>>    s = s.conClose(a).filter(...).**onClose(s).
>>
>> but we could detect if s is stage in the pipeline and throw an IAE.
>>
>>
>> FWIW Stream.close/onClose is easy to implement efficiently.
>>
>> Paul.
>>
>> [*] The JDK world is murky, see ByteArrayInputStream.close:
>>
>>      /**
>>       * Closing a <tt>ByteArrayInputStream</tt> has no effect. The
>> methods in
>>       * this class can be called after the stream has been closed without
>>       * generating an <tt>IOException</tt>.
>>       * <p>
>>       */
>>      public void close() throws IOException {
>>      }
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/lambda-libs-spec-experts/attachments/20130624/b1b00683/attachment.html 


More information about the lambda-libs-spec-experts mailing list