Skip to content

Commit

Permalink
Now ensures all opened streams are closed
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Smith committed Feb 29, 2024
1 parent bd083d3 commit 183b0cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
10 changes: 5 additions & 5 deletions Source/com/drew/imaging/png/PngMetadataReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,9 @@ private static void processChunk(@NotNull Metadata metadata, @NotNull PngChunk c
int bytesLeft = bytes.length - (profileNameBytes.length + 1 + 1);
byte[] compressedProfile = reader.getBytes(bytesLeft);

try {
InflaterInputStream inflateStream = new InflaterInputStream(new ByteArrayInputStream(compressedProfile));
try (ByteArrayInputStream bais = new ByteArrayInputStream(compressedProfile);
InflaterInputStream inflateStream = new InflaterInputStream(bais)) {
new IccReader().extract(new RandomAccessStreamReader(inflateStream), metadata, directory);
inflateStream.close();
} catch(java.util.zip.ZipException zex) {
directory.addError(String.format("Exception decompressing PNG iCCP chunk : %s", zex.getMessage()));
metadata.addDirectory(directory);
Expand Down Expand Up @@ -222,8 +221,9 @@ private static void processChunk(@NotNull Metadata metadata, @NotNull PngChunk c
int bytesLeft = bytes.length - (keywordsv.getBytes().length + 1 + 1);
byte[] textBytes = null;
if (compressionMethod == 0) {
try {
textBytes = StreamUtil.readAllBytes(new InflaterInputStream(new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft)));
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes, bytes.length - bytesLeft, bytesLeft);
InflaterInputStream inflateStream = new InflaterInputStream(bais)) {
textBytes = StreamUtil.readAllBytes(inflateStream);
} catch(java.util.zip.ZipException zex) {
PngDirectory directory = new PngDirectory(PngChunkType.zTXt);
directory.addError(String.format("Exception decompressing PNG zTXt chunk with keyword \"%s\": %s", keyword, zex.getMessage()));
Expand Down
17 changes: 9 additions & 8 deletions Source/com/drew/lang/StreamUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ public final class StreamUtil
{
public static byte[] readAllBytes(InputStream stream) throws IOException
{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = stream.read(buffer);
if (bytesRead == -1)
break;
outputStream.write(buffer, 0, bytesRead);
}

byte[] buffer = new byte[1024];
while (true) {
int bytesRead = stream.read(buffer);
if (bytesRead == -1)
break;
outputStream.write(buffer, 0, bytesRead);
return outputStream.toByteArray();
}

return outputStream.toByteArray();
}
}

0 comments on commit 183b0cd

Please sign in to comment.