RFR: 8136895: Writer not closed with disk full error, file resource leaked [v3]

Daniel Jeliński djelinski at openjdk.org
Thu Apr 27 07:00:54 UTC 2023


On Thu, 27 Apr 2023 00:36:53 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> In`StreamEncoder::implClose`, move `flushLeftoverChar()` inside the `try` block and the closing of the underlying stream inside the `finally` block.
>
> Brian Burkhalter has updated the pull request incrementally with one additional commit since the last revision:
> 
>   8136895: Trivial formatting corrections in tests

This is what I had in mind:

--- a/src/java.base/share/classes/sun/nio/cs/StreamEncoder.java
+++ b/src/java.base/share/classes/sun/nio/cs/StreamEncoder.java
@@ -414,8 +414,7 @@ public final class StreamEncoder extends Writer {
     }

     void implClose() throws IOException {
-        IOException ioe = null;
-        try {
+        try (ch; out) {
             flushLeftoverChar(null, true);
             for (;;) {
                 CoderResult cr = encoder.flush(bb);
@@ -433,28 +432,7 @@ public final class StreamEncoder extends Writer {
                 writeBytes();
         } catch (IOException x) {
             encoder.reset();
-            ioe = x;
         }
-
-        try {
-            if (ch != null)
-                ch.close();
-            else {
-                try {
-                    out.flush();
-                } finally {
-                    out.close();
-                }
-            }
-        } catch (IOException e) {
-            if (ioe != null)
-                ioe.addSuppressed(e);
-            else
-                ioe = e;
-        }
-
-        if (ioe != null)
-            throw ioe;
     }

     String encodingName() {

(yes it still makes `StreamEncoderClose` test fail)

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13503#issuecomment-1524890086


More information about the nio-dev mailing list