Skip to content

Commit

Permalink
Attempt to wrap JPEG2000 reads to consistently throw SourceFormatExce…
Browse files Browse the repository at this point in the history
…ption.
  • Loading branch information
adam-vessey committed Aug 13, 2024
1 parent 40ff2e5 commit e8a80c5
Showing 1 changed file with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e8a80c5

Please sign in to comment.