Closing the Stream returned from BufferedReader.lines()

Zhong Yu zhong.j.yu at gmail.com
Mon Dec 2 10:57:46 PST 2013


On Mon, Dec 2, 2013 at 12:45 PM, Henry Jen <henry.jen at oracle.com> wrote:
> On Mon 02 Dec 2013 04:02:33 AM PST, Millies, Sebastian wrote:
>> In that context, I'd be in favor
>>
>> either of Gernot Neppert's suggestion in
>> http://mail.openjdk.java.net/pipermail/core-libs-dev/2013-November/023318.html
>> to add a java.io.Closeable#closeUnchecked() default method,
>>
>> or of making the static utility method java.nio.file.Files#asUncheckedRunnable(Closeable)
>> public somewhere.
>>
>
> We thought about this before, but decided to leave it out because,
>
> 1. If a close method is known to throw exceptions,  developer shoule
> deal with it.

The read methods of the data source also throw checked exceptions,
which Stream uniformly rethrow as unchecked. It won't be surprising to
do the same for the close method. The exception is not really
neglected - it is propagated outwards, which is a legit way of
"dealing with" it. (Though it is really dangerous that the exception
is mucked as unchecked)


> 2. It's trivial to add a static method to wrap up close if don't care
> consequences or add a utility class to cascade the exception.
>
> That said, I think a default method to simply close resource regardless
> would be convenient if name clashing is not a concern.
>
> Cheers,
> Henry
>
>> Otherwise many people will repeatedly have to write their own code in order to make
>> the example  suggested by Brian Goetz in that core-libs-dev discussion compile.
>> Is it too late for such changes?
>>
>> -- Sebastian
>>
>> -----Original Message-----
>> From: lambda-dev-bounces at openjdk.java.net [mailto:lambda-dev-bounces at openjdk.java.net] On Behalf Of Alan Bateman
>> Sent: Monday, December 02, 2013 10:21 AM
>> To: Zhong Yu
>> Cc: lambda-dev at openjdk.java.net
>> Subject: Re: Closing the Stream returned from BufferedReader.lines()
>>
>> On 02/12/2013 03:03, Zhong Yu wrote:
>>> In this code
>>>
>>>           BufferedReader br = ...;
>>>           Stream<String>  stream = br.lines();
>>>           stream.close();  // does not close `br`
>>>
>>> `stream.close()` does not trigger `br.close()`. I for one find that
>>> rather counter-intuitive. Can you guys explain the design choice? When
>>> should a Stream forward close() to its source and when should it not?
>>>
>> See discussion on core-libs-dev from about a week ago on the same topic:
>>
>> http://mail.openjdk.java.net/pipermail/core-libs-dev/2013-November/023289.html
>>
>>
>> Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com
>>
>>
>


More information about the lambda-dev mailing list