From 5b75ff7283340291bc87f83aba0e4416479899e3 Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Thu, 13 Feb 2025 11:54:01 +0000 Subject: [PATCH] 8349907: jdk.tools.jlink.internal.plugins.ZipPlugin does not close the Deflater in exceptional cases Reviewed-by: liach, alanb, sundar, lancea --- .../jlink/internal/plugins/ZipPlugin.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ZipPlugin.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ZipPlugin.java index a68d646b72d..e16cbb445d5 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ZipPlugin.java +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/ZipPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -82,27 +82,27 @@ public final class ZipPlugin extends AbstractPlugin { } static byte[] compress(byte[] bytesIn, int compressionLevel) { - Deflater deflater = new Deflater(compressionLevel); - deflater.setInput(bytesIn); - ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length); - byte[] buffer = new byte[1024]; + try (Deflater deflater = new Deflater(compressionLevel); + ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length)) { - deflater.finish(); - while (!deflater.finished()) { - int count = deflater.deflate(buffer); - stream.write(buffer, 0, count); - } + deflater.setInput(bytesIn); - try { - stream.close(); - } catch (IOException ex) { + byte[] buffer = new byte[1024]; + + deflater.finish(); + while (!deflater.finished()) { + int count = deflater.deflate(buffer); + stream.write(buffer, 0, count); + } + return stream.toByteArray(); // the compressed output + } catch (IOException e) { + // the IOException is only declared by ByteArrayOutputStream.close() + // but the impl of ByteArrayOutputStream.close() is a no-op, so for + // all practical purposes there should never be an IOException thrown + assert false : "unexpected " + e; + // don't propagate the exception, instead return the original uncompressed input return bytesIn; } - - byte[] bytesOut = stream.toByteArray(); - deflater.end(); - - return bytesOut; } @Override