FileInputStream 负跳过
我试图找到有关当 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SocketInputStream 的实际实现给出了答案:
编辑:抱歉,我检查了错误的类
FileInputStream
s 实现是本机的,这是 openjdk7 中的实现The acutal implementation of SocketInputStream gives the answer:
EDIT: Sorry, I examined the wrong class
FileInputStream
s implementation is native this is the implementation in openjdk7