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

Brian Burkhalter brian.burkhalter at oracle.com
Mon Jul 29 19:51:54 UTC 2019


> On Jul 27, 2019, at 9:57 AM, Alan Bateman <Alan.Bateman at oracle.com> wrote:
> 
> On 26/07/2019 16:58, Brian Burkhalter wrote:
>> Note that in the version prior to the patch [1] for [2], ChannelInputStream.skip() would devolve to InputStream.skip() and would skip only to the end of the stream for the case of pos + n overflowing so the above looks like a change in behavior.
> It's the same thing with FileInputStream as it's an underlying file system issue
> 
> jshell> var in = new FileInputStream("foo.txt")
> in ==> java.io.FileInputStream at 2ef1e4fa
> 
> jshell> in.skip(Long.MAX_VALUE)
> |  Exception java.io.IOException: Invalid argument
> |        at FileInputStream.skip0 (Native Method)
> |        at FileInputStream.skip (FileInputStream.java:301)
> |        at (#2:1)

Yes, I checked FIS.skip() and FileChannel.position() for this case.

> For the re-do, I think the important thing is to fix the skip back issue as that was missed in JDK-8227080. Skipping beyond EOF is allowed with files but isn't too interesting when the file is open read-only. That is, if the solution is to only seek to 0..size then it should be okay for most needs, as in:
> 
>         if (n > 0) {
>             newPos = pos + n;
>             long size = sbc.size();
>             if (newPos < 0 || newPos > size) {
>                 newPos = size;
>             }
>         } else {
>             newPos = Long.max(pos + n, 0);
>         }
> 
> or changing the test is okay too.

I updated it to clamp to the size and changed the test accordingly:

http://cr.openjdk.java.net/~bpb/8227609/webrev.03/ <http://cr.openjdk.java.net/~bpb/8227609/webrev.03/>

Thanks,

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


More information about the nio-dev mailing list