8227609: (fs) Files.newInputStream(...).skip(n) should allow skipping beyond file size

Reviewed-by: alanb, lancea, fweimer
This commit is contained in:
Brian Burkhalter 2019-07-30 09:46:06 -07:00
parent 918492bb58
commit 3155cd829b
3 changed files with 142 additions and 47 deletions

View file

@ -119,20 +119,21 @@ public class ChannelInputStream
public synchronized long skip(long n) throws IOException {
// special case where the channel is to a file
if (ch instanceof SeekableByteChannel && n > 0) {
if (ch instanceof SeekableByteChannel) {
SeekableByteChannel sbc = (SeekableByteChannel)ch;
try {
long pos = sbc.position();
long pos = sbc.position();
long newPos;
if (n > 0) {
newPos = pos + n;
long size = sbc.size();
if (pos >= size) {
return 0L;
if (newPos < 0 || newPos > size) {
newPos = size;
}
n = Math.min(n, size - pos);
sbc.position(pos + n);
return sbc.position() - pos;
} catch (ClosedChannelException cce) {
throw new IOException(cce);
} else {
newPos = Long.max(pos + n, 0);
}
sbc.position(newPos);
return newPos - pos;
}
return super.skip(n);
}