FileInputStream 负跳过

发布于 2024-08-24 15:08:13 字数 917 浏览 6 评论 0原文

我试图找到有关当 n 为负时 java.io.FileInputStream.skip(n) 操作历史的更多信息。根据 InputStream 文档:

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

似乎 Sun 的 FileInputStream 实现常常抛出 IOException ,这是现在也 记录在 Javadoc< /a>:

如果 n 为负数,则会引发 IOException,即使 InputStream 超类的 Skip 方法在这种情况下不执行任何操作。

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

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

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 that 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-08-31 15:08:14

SocketInputStream 的实际实现给出了答案:

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

编辑:抱歉,我检查了错误的类 FileInputStreams 实现是本机的,这是 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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文