Skip to content

Commit

Permalink
Merge pull request #681 from adam-vessey/fix/wrapped-reads
Browse files Browse the repository at this point in the history
Fix/wrapped reads
  • Loading branch information
DiegoPino authored Aug 14, 2024
2 parents dc7947a + 73119cd commit b7acf8b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ private Directory read(TagSet tagSet) throws IOException {

private byte[] readBytes(int length) throws IOException {
byte[] data = new byte[length];
int n, offset = 0;
while ((n = inputStream.read(data, offset, data.length - offset)) < offset) {
offset += n;
}
inputStream.readFully(data);
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,7 @@ private void readPlainTextExtension() throws IOException {

private byte[] read(int length) throws IOException {
byte[] data = new byte[length];
int n, offset = 0;
while ((n = inputStream.read(
data, offset, data.length - offset)) < offset) {
offset += n;
}
inputStream.readFully(data);
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,7 @@ private void readAPP14Segment() throws IOException {

private byte[] read(int length) throws IOException {
byte[] data = new byte[length];
int n, offset = 0;
while ((n = inputStream.read(
data, offset, data.length - offset)) < offset) {
offset += n;
}
inputStream.readFully(data);
return data;
}

Expand Down
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 Expand Up @@ -434,11 +441,7 @@ private void readCOCSegment() throws IOException {

private byte[] read(int length) throws IOException {
byte[] data = new byte[length];
int n, offset = 0;
while ((n = inputStream.read(
data, offset, data.length - offset)) < offset) {
offset += n;
}
inputStream.readFully(data);
return data;
}

Expand Down

0 comments on commit b7acf8b

Please sign in to comment.