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

Alan Bateman Alan.Bateman at oracle.com
Sat Jul 27 16:57:06 UTC 2019


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)

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.

-Alan






More information about the nio-dev mailing list