开发者

FileInputStream negative skip

I'm trying to find more about history of java.io.FileInputStream.skip(n) operation when n is negative. According to InputStream documentation:

If n is negative, no bytes are skipped.

It seems th开发者_开发技巧at implementation of FileInputStream from Sun used to throw IOException instead, which is now also documented in Javadoc:

If n is negative, an IOException is thrown, even though the skip method of the InputStream superclass does nothing in this case.

I just tried that, and found that FileInputStream.skip(-10) did in fact return -10! It didn't threw exception, it didn't even return 0, it returned -10. (I've tried with Java 1.5.0_22 from Sun, and Java 1.6.0_18 from Sun).

Is this a known bug? Why hasn't it been fixed, or why documentation is kept the way it is? Can someone point me to some discussion about this issue? I can't find anything.


The acutal implementation of SocketInputStream gives the answer:

  public long skip(long numbytes) throws IOException {
        if (numbytes <= 0) {
            return 0;
        }
  ...
  }

EDIT: Sorry, I examined the wrong class FileInputStreams implementation is native this is the implementation in openjdk7

if ((cur = IO_Lseek(fd, (jlong)0, (jint)SEEK_CUR)) == -1) {
        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
    } else if ((end = IO_Lseek(fd, toSkip, (jint)SEEK_CUR)) == -1) {
        JNU_ThrowIOExceptionWithLastError(env, "Seek error");
    }
    return (end - cur);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜