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

Alan Bateman Alan.Bateman at oracle.com
Thu Jul 25 07:57:58 UTC 2019


On 25/07/2019 01:45, Brian Burkhalter wrote:
> https://bugs.openjdk.java.net/browse/JDK-8227609
> http://cr.openjdk.java.net/~bpb/8227609/webrev.00/
>
> Fix implementation previously modified for [1] to allow for skipping 
> to positions outside of the file so as to be consistent with 
> FileInputStream.skip(n) and FileChannel.position(p).
>
I think we can do better on the overflow cases. When skipping forwards 
and pos + n overflows then we should seek to Long.MAX_VALUE. When 
skipping backwards then we shouldn't attempt to seek to a file offset < 
0. I think this is closer to what we need:

long pos = sbc.position();
long newPos;
if (n > 0) {
   newPos = pos + n;
   if (newPos < 0) newPos = Long.MAX_VALUE;
} else {
   newPos = Long.max(pos + n, 0);
}
sbc.position(newPos);
return newPos - pos;

I'm also wondering if the catching of ClosedChannelException should be 
removed as it results in throwing an IOException with CCE (an 
IOException) as cause.

-Alan




More information about the nio-dev mailing list