From e8a80c54e411c2b61577ed28744518b03793086c Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 31 Jul 2024 13:18:26 -0300 Subject: [PATCH] Attempt to wrap JPEG2000 reads to consistently throw SourceFormatException. --- .../jpeg2000/JPEG2000MetadataReader.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/main/java/edu/illinois/library/cantaloupe/processor/codec/jpeg2000/JPEG2000MetadataReader.java b/src/main/java/edu/illinois/library/cantaloupe/processor/codec/jpeg2000/JPEG2000MetadataReader.java index 24da4bef5..ecf2b947f 100644 --- a/src/main/java/edu/illinois/library/cantaloupe/processor/codec/jpeg2000/JPEG2000MetadataReader.java +++ b/src/main/java/edu/illinois/library/cantaloupe/processor/codec/jpeg2000/JPEG2000MetadataReader.java @@ -8,6 +8,7 @@ import javax.imageio.stream.ImageInputStream; import javax.xml.bind.DatatypeConverter; +import java.io.EOFException; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; @@ -268,23 +269,29 @@ private void readData() throws IOException { throw new IllegalStateException("Source not set"); } - inputStream.mark(); - byte[] bytes = read(JP2_SIGNATURE.length); - if (!Arrays.equals(JP2_SIGNATURE, bytes)) { - String hexStr = DatatypeConverter.printHexBinary(bytes); - throw new SourceFormatException("Invalid signature: " + hexStr + - " (is this a JP2?)"); - } - inputStream.reset(); + try { + inputStream.mark(); + byte[] bytes = read(JP2_SIGNATURE.length); + if (!Arrays.equals(JP2_SIGNATURE, bytes)) { + String hexStr = DatatypeConverter.printHexBinary(bytes); + throw new SourceFormatException("Invalid signature: " + hexStr + + " (is this a JP2?)"); + } + inputStream.reset(); - final Stopwatch watch = new Stopwatch(); - while (readBox() != -1) { - // Read boxes. - isReadAttempted = true; - } + final Stopwatch watch = new Stopwatch(); + + while (readBox() != -1) { + // Read boxes. + isReadAttempted = true; + } - LOGGER.debug("Read in {}: {}", watch, this); + LOGGER.debug("Read in {}: {}", watch, this); + } + catch (EOFException e) { + throw (SourceFormatException)(new SourceFormatException("JP2 appears to be corrupt; encountered EOF.").initCause(e)); + } } private int readBox() throws IOException {