Skip to content

Commit

Permalink
Override default implementation of skip(long) method
Browse files Browse the repository at this point in the history
Delegate to `stream` the implementation of `skip(long)` method.
By doing this, it is ensured that not the default implementation
from `java.io.InputStream` is being used, because it calls
`read()` causing actual reads from the underlying input stream
although `skip` is just a logical operation.

Co-authored-by: James Petty <pettja@amazon.com>
  • Loading branch information
2 people authored and electrum committed Sep 11, 2023
1 parent a30acde commit d06a3e0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ public void testInputFile()
assertThat(inputStream.getPosition()).isEqualTo(fileSize);
assertThat(inputStream.read()).isLessThan(0);
assertThat(inputStream.read(bytes)).isLessThan(0);
assertThat(inputStream.skip(10)).isEqualTo(0);
if (seekPastEndOfFileFails()) {
assertThat(inputStream.skip(10)).isEqualTo(0);
}
else {
assertThat(inputStream.skip(10)).isEqualTo(10L);
}

// seek 4 MB in and read byte at a time
inputStream.seek(4 * MEGABYTE);
Expand All @@ -270,7 +275,12 @@ public void testInputFile()
assertThat(inputStream.getPosition()).isEqualTo(fileSize);
assertThat(inputStream.read()).isLessThan(0);
assertThat(inputStream.read(bytes)).isLessThan(0);
assertThat(inputStream.skip(10)).isEqualTo(0);
if (seekPastEndOfFileFails()) {
assertThat(inputStream.skip(10)).isEqualTo(0);
}
else {
assertThat(inputStream.skip(10)).isEqualTo(10L);
}

// seek 1MB at a time
for (int i = 0; i < 16; i++) {
Expand All @@ -297,9 +307,11 @@ public void testInputFile()
assertThat(slice.getInt(0)).isEqualTo(expectedPosition / 4);
expectedPosition += size;
}
long skipSize = inputStream.skip(MEGABYTE);
assertThat(skipSize).isEqualTo(fileSize - expectedPosition);
assertThat(inputStream.getPosition()).isEqualTo(fileSize);
if (seekPastEndOfFileFails()) {
long skipSize = inputStream.skip(MEGABYTE);
assertThat(skipSize).isEqualTo(fileSize - expectedPosition);
assertThat(inputStream.getPosition()).isEqualTo(fileSize);
}

// skip N bytes
inputStream.seek(0);
Expand All @@ -318,18 +330,27 @@ public void testInputFile()
inputStream.skipNBytes(fileSize - expectedPosition);
assertThat(inputStream.getPosition()).isEqualTo(fileSize);

// skip beyond the end of the file is not allowed
inputStream.seek(expectedPosition);
assertThat(expectedPosition + MEGABYTE).isGreaterThan(fileSize);
assertThatThrownBy(() -> inputStream.skipNBytes(MEGABYTE))
.isInstanceOf(EOFException.class);
if (seekPastEndOfFileFails()) {
// skip beyond the end of the file is not allowed
inputStream.seek(expectedPosition);
assertThat(expectedPosition + MEGABYTE).isGreaterThan(fileSize);
assertThatThrownBy(() -> inputStream.skipNBytes(MEGABYTE))
.isInstanceOf(EOFException.class);
}

inputStream.seek(fileSize);
assertThatThrownBy(() -> inputStream.skipNBytes(1))
.isInstanceOf(EOFException.class);
if (seekPastEndOfFileFails()) {
assertThatThrownBy(() -> inputStream.skipNBytes(1))
.isInstanceOf(EOFException.class);
}

inputStream.seek(fileSize);
assertThat(inputStream.skip(1)).isEqualTo(0);
if (seekPastEndOfFileFails()) {
assertThat(inputStream.skip(1)).isEqualTo(0);
}
else {
assertThat(inputStream.skip(1)).isEqualTo(1L);
}

// seek beyond the end of the file, is not allowed
long currentPosition = fileSize - 500;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ public int read(byte[] b, int off, int len)
}
}

@Override
public long skip(long n)
throws IOException
{
ensureOpen();
try {
return stream.skip(n);
}
catch (IOException e) {
throw new IOException("Skipping %s bytes of file %s failed: %s".formatted(n, location, e.getMessage()), e);
}
}

@Override
public void close()
throws IOException
Expand Down

0 comments on commit d06a3e0

Please sign in to comment.