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

Brian Burkhalter brian.burkhalter at oracle.com
Fri Apr 12 14:16:17 UTC 2019


> On Apr 12, 2019, at 12:18 AM, Alan Bateman <Alan.Bateman at oracle.com> wrote:
> 
> On 11/04/2019 22:36, Brian Burkhalter wrote:
>> 
>> Revisiting the failure mentioned above, it turns out that this is because a StreamEncoder for an OutputStream does not flush the stream before closing it. It seems like it should. (I don’t know whether this should perhaps be the subject of a separate issue.) The webrev [1] for the current issue 8220477 is updated to do this. The difference versus the previous webrev is [2]. It might be overkill to handle the IOException from OutputStream.flush() instead of just ignoring it, I’m not sure.
>> 
> 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.

Thanks,

Brian

[1] 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/20190412/4140dfa5/attachment-0001.html>


More information about the nio-dev mailing list