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

Brian Burkhalter bpb at openjdk.org
Fri Jul 14 22:16:28 UTC 2023


On Thu, 27 Apr 2023 06:52:16 GMT, Daniel Jeliński <djelinski at openjdk.org> wrote:

>> 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)

Commit d19659963b8a2a2cea11208459d33f1ca8c1af10 changes `implClose()` along the lines suggested by @djelinski . The `close()` is handled by a `try-with-resources` block. This assumes that any flushing of the channel or output stream would be done by the `close()` of the respective destination. The bytes held in `bb` will not, as mentioned above, be flushed.

This change passes all NIO tests.

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

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


More information about the nio-dev mailing list