8220477: Channels.newWriter() does not close if underlying channel throws an IOException

Brian Burkhalter brian.burkhalter at oracle.com
Tue Apr 16 17:59:54 UTC 2019


> On Apr 12, 2019, at 7:16 AM, Brian Burkhalter <brian.burkhalter at oracle.com> wrote:
> 
>> It seems reasonable but it adds complexity to prefer the exception from flush over the close. The alternative is the much simpler:
>> 
>> try {
>>     out.flush();
>> } finally {
>>     out.close();
>> }
>> 
>> As always, if the flush fails then it's possible that the close will attempt to flush again so there may be several suppressed exceptions.
> 
> Indeed that is cleaner. Update webrev [1]. Diff versus original webrev [2].
> 
> I’ll not push this in any case until next week.

This version passed the CI testing cleanly so is ready to go, I think.

Brian

> [1] http://cr.openjdk.java.net/~bpb/8220477/webrev.02/ <http://cr.openjdk.java.net/~bpb/8220477/webrev.02/>
> [2] diff vs. webrev.00
> 
> --- a/src/java.base/share/classes/sun/nio/cs/StreamEncoder.java
> +++ b/src/java.base/share/classes/sun/nio/cs/StreamEncoder.java
> @@ -346,8 +346,13 @@
>                  writeBytes();
>              if (ch != null)
>                  ch.close();
> -            else
> -                out.close();
> +            else {
> +                try {
> +                    out.flush();
> +                } finally {
> +                    out.close();
> +                }
> +            }
>          } catch (IOException x) {
>              encoder.reset();
>              throw x;

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/nio-dev/attachments/20190416/fa2964b0/attachment.html>


More information about the nio-dev mailing list