4

我试图找到更多关于负面的java.io.FileInputStream.skip(n)操作历史。n根据InputStream 文档

如果 n 为负数,则不跳过任何字节。

似乎 Sun 的 FileInputStream 实现过去常常抛出 IOException,现在Javadoc 中也记录了这一点:

如果 n 为负数,则抛出 IOException,即使 InputStream 超类的 skip 方法在这种情况下什么也不做。

我刚刚尝试过,发现FileInputStream.skip(-10)确实返回了-10!它没有抛出异常,它甚至没有返回 0,它返回了 -10。(我尝试过使用 Sun 的 Java 1.5.0_22 和 Sun 的 Java 1.6.0_18)。

这是一个已知的错误?为什么它没有被修复,或者为什么文档保持原样?有人可以指出我对这个问题的一些讨论吗?我什么也找不到。

4

1 回答 1

1

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);
于 2010-03-11T21:32:49.717 回答