Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/wrapped reads #681

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading