8349907: jdk.tools.jlink.internal.plugins.ZipPlugin does not close the Deflater in exceptional cases

Reviewed-by: liach, alanb, sundar, lancea
This commit is contained in:
Jaikiran Pai 2025-02-13 11:54:01 +00:00
parent add3cd1ca4
commit 5b75ff7283

View File

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