mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 23:34:52 +02:00
8227609: (fs) Files.newInputStream(...).skip(n) should allow skipping beyond file size
Reviewed-by: alanb, lancea, fweimer
This commit is contained in:
parent
918492bb58
commit
3155cd829b
3 changed files with 142 additions and 47 deletions
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue