6516099: InputStream.skipFully(int k) to skip exactly k bytes
Brian Burkhalter
brian.burkhalter at oracle.com
Fri Oct 26 20:42:56 UTC 2018
Hi Roger,
> On Oct 25, 2018, at 7:15 AM, Roger Riggs <Roger.Riggs at oracle.com> wrote:
>
> The FIS skipping past of end of file is puzzling.
Quite so.
> If the 'were beyond EOF' was considering the possibility that the file was being
> extended concurrently with the skip operation then it would not be random,
> just a normal writer/reader race. The return value from skip would be accurate
> and still usable for skipNBytes.
>
> If the spec for skipNBytes describes its behavior in terms of the normal behaviors
> of skip(n) and read(n) then it will not making promises it can't keep regardless of the subclass behavior.
That sounds like a good idea.
> FIS also says it can do negative seeks which seems in conflict with InputStream.
> The FIS.skip(-n) behavior raises the question about whether InputStream.skipNBytes should
> allow -n?
Actually I think the specification [1] is inconsistent. On the one hand it states "If n is negative, the method will try to skip backwards.” and on the other “Throws: IOException <https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/IOException.html> - if n is negative ….”. It’s the throws clause that appears inaccurate, at least on macOS. Output of program below [2] is
Position 0
Skipped 1024 / 1024
Position 1024
Skipped -512 / -512
Position 512
This is not actually in conflict with InputStream [3] which states "If n is negative, the skip method for class InputStream always returns 0, and no bytes are skipped. Subclasses may handle the negative value differently.”
Ugly stuff.
Thanks,
Brian
[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/FileInputStream.html#skip(long) <https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/FileInputStream.html#skip(long)>
[2] FISTest
import java.io.FileInputStream;
import java.nio.channels.FileChannel;
public class FISTest {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(args[0]);
FileChannel fc = fis.getChannel();
System.out.format("Position %d%n", fc.position());
System.out.format("Skipped %d / %d%n", fis.skip(1024), 1024);
System.out.format("Position %d%n", fc.position());
System.out.format("Skipped %d / %d%n", fis.skip(-512), -512);
System.out.format("Position %d%n", fc.position());
fis.close();
}
}
[3] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/InputStream.html#skip(long) <https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/InputStream.html#skip(long)>
More information about the core-libs-dev
mailing list