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

Brian Burkhalter brian.burkhalter at oracle.com
Thu Jul 25 18:14:38 UTC 2019


> On Jul 25, 2019, at 12:57 AM, Alan Bateman <Alan.Bateman at oracle.com> wrote:
> 
> On 25/07/2019 01:45, Brian Burkhalter wrote:
>> https://bugs.openjdk.java.net/browse/JDK-8227609 <https://bugs.openjdk.java.net/browse/JDK-8227609>
>> http://cr.openjdk.java.net/~bpb/8227609/webrev.00/ <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.

Updated as suggested; test modified accordingly.

http://cr.openjdk.java.net/~bpb/8227609/webrev.01/

Thanks,

Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/nio-dev/attachments/20190725/eecae43c/attachment.html>


More information about the nio-dev mailing list