RFR: 8313739: ZipOutputStream.close() should always close the wrapped stream

Eirik Bjørsnøs eirbjo at openjdk.org
Mon Jan 1 17:07:04 UTC 2024


Please consider this PR which makes `DeflaterOutputStream.close()` always close its wrapped output stream.

Currently, closing of the wrapped output stream happens outside the finally block where `finish()` is called. If `finish()` throws, this means the wrapped stream will not be closed. This can potentially lead to leaking resources such as file descriptors or sockets.

This fix is to move the closing of the wrapped stream inside the finally block.

Specification: This change brings the implementation of `DeflaterOutputStream.close()` in line with its specification:  *Writes remaining compressed data to the output stream and closes the underlying stream.*

Risk: This is a behavioural change. There is a small risk that existing code depends on the close method not following its specification.

Testing: The PR adds a new JUnit 5 test `CloseWrappedStream.java` which simulates the failure condition and verifies that the wrapped stream was closed under failing and non-failing conditions.

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

Commit messages:
 - Add a sanity check that the wrapped stream is closed for the normal case where the wrapped stream does not throw during flushing
 - Make DeflaterOutputStream.close close its wrapped output stream even if finish() fails

Changes: https://git.openjdk.org/jdk/pull/17209/files
 Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=17209&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8313739
  Stats: 119 lines in 2 files changed: 115 ins; 2 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/17209.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/17209/head:pull/17209

PR: https://git.openjdk.org/jdk/pull/17209


More information about the core-libs-dev mailing list