Skip to content

Commit

Permalink
fix file range impl
Browse files Browse the repository at this point in the history
Need to override the multi-byte read for performance
  • Loading branch information
pwinckles committed Jul 23, 2024
1 parent 3fc669c commit ae88052
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public FixityCheckInputStream retrieveFile() {
@Override
public InputStream retrieveRange(Long startPosition, Long endPosition) {
try {
var length = endPosition - startPosition;
var length = endPosition - startPosition + 1;
var file = new RandomAccessFile(filePath.toFile(), "r");
if (startPosition > 0) {
file.seek(startPosition);
Expand All @@ -87,13 +87,24 @@ public InputStream retrieveRange(Long startPosition, Long endPosition) {

@Override
public int read() throws IOException {
if (bytesRead > length) {
if (bytesRead >= length) {
return -1;
}
bytesRead++;
return file.read();
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
if (bytesRead >= length) {
return -1;
}
var maxLen = length - bytesRead;
var modifiedLen = Math.min(len, maxLen);
bytesRead += modifiedLen;
return file.read(b, off, (int) modifiedLen);
}

@Override
public void close() throws IOException {
file.close();
Expand Down

0 comments on commit ae88052

Please sign in to comment.