- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
BufferedInputStream(byte[] b, int off, int len)
有可能吗?返回 0?
READER'S DIGEST VERSION(您可以阅读下面的其余内容,了解上下文,但我认为归结为:) JDK 或常用库(即 Apache Commons、Guava)中是否存在 InputStreams(即 SocketInputStream、CipherInputStream 等) , 不能正确遵守 InputStream.read(byte[],off,len) 的契约(Contract),即使 len != 0 也可能返回“0”?
(注 1:我的兴趣是它是否真的会发生在仅使用 JDK 的代码中,或者可能发生在一些非常常见的 Java 库中,例如 Apache Commons;我正在查看 javadoc 以获取线索,但我也在查看BufferedInputStream 的源代码(Java 7,以防万一),以防某些边缘情况没有正确记录——而且我不完全相信一种或另一种方式,因此我的问题)
(注2:我不是说在一般情况下,len == 0,我的意思是在一般情况下,你传入一个非零数组,你能不能取回0字节?)
javadoc 是 here它部分说:This method implements the general contract of the *corresponding* [my emphasis added] read method of the InputStream class. As an additional convenience, it attempts to read as many bytes as possible by repeatedly invoking the read method of the underlying stream. This iterated read continues until one of the following conditions becomes true:
[省略两个不相关的条件]- The available method of the underlying stream returns zero, indicating that further input requests would block.
然后返回值的文档说:Returns: the number of bytes read, or -1 if the end of the stream has been reached.
所以:通过我的阅读,当你调用这个读取函数时,如果没有数据被缓冲并且没有数据可以从底层 InputStream
(比如说,从一个停止的 http 传输开始,)然后 read 方法应该返回 0,因为 0 字节被读取。
然而......似乎比我更了解这一点的一群人似乎相信这种读取方法将始终返回 EOF 或至少一个字节。
所以,我进一步研究了 InputStream,看看 the general contract of the corresponding read method of the InputStream class
真的意味着,我发现了这个:If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.
因此,根据 javadocs,我认为这意味着 BufferedInputStream 不应该 返回 0。如果我只看文档,我想我现在就完成了。
但是:经过检查,在我看来,BufferedInputStream 的实现并不能真正保证一个字节或多个字节;它通过依赖底层 InputStream 的正确行为来继承这个保证。抽象 InputStream 的来源似乎得到了正确的保证(我认为如果 len==0,它只能返回 0 个字节)但我不知道这是否适用于 JDK 中的所有输入流,更不用说所有输入流了任何地方。
所以。 我认为 到目前为止我的位置是: BufferedInputStream 永远不会返回 0 ,除非包装的 InputStream 不遵守 1 个或更多字节的保证——但我不知道这有多普遍。
1)我的一般分析是否正确?
2) 有人知道 InputStreams 可以返回 0 的重要情况吗? (即 InputStreams 可能返回 0 且 len 非零,因此如果将它们包装在 BufferedInputStream 中,您需要防止返回值为零?——不是某人的个人、损坏的代码,而是需要注意的重要情况,比如在 JDK 或 Apache Commons 或其他东西中。)
为长问题道歉;我在写这篇文章时做了更多的研究,所以问题越来越大。
注意:对于上下文:我发布这个问题是因为我不理解我在提到这个其他问题(Socket reading using BufferedInputStream)时的对话——这可能有助于阅读该问题的背景。
最佳答案
您没有阅读 BufferedInputStream
的规范够仔细。你引用了:
This iterated read continues until one of the following conditions becomes true:
[two irrelevant conditions omitted]
- The available method of the underlying stream returns zero, indicating that further input requests would block.
BufferedInputStream
将履行
read
的契约(Contract)通过直接委托(delegate)给第一个
read
的底层流读取至少一个字节.如果底层流在第一次读取时正确读取了至少一个字节,则契约(Contract)已完成。
available
将被跳过返回
0
告诉另一个
read
尝试会阻止(再次)。
BufferedInputStream
履行契约(Contract),就像所有其他 JDK 的
InputStream
s——据我所知。顺便说一句,如果你想完全读取一个数组,你可以将流包裹在
DataInputStream
中。它提供了
readFully
方法。
关于java - BufferedInputStream.read(byte[] b, int off, int len) 可以返回 0 吗?是否有可能导致这种情况的重要的、损坏的 InputStreams?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21166196/
给定 data Person = Person { _name :: String } makeClassy ''Person 它创建了一个 name :: Lens' Person String 我
Python内置的len()函数的成本是多少?(列表/元组/字符串/词典)
这个问题在这里已经有了答案: 9年前关闭。 Possible Duplicate: lenses, fclabels, data-accessor - which library for struct
Python 哪个性能更好: 1) for i in range(len(a[:-1])): foo() 或 2) for i in range(len(a)-1): foo() 更新
我正在学习 Python 并正在学习谷歌代码类(class)。在 list2.py 示例中,他们要求我们编写一个函数: Given two lists sorted in increasing ord
我最近开始使用 Python 进行数据分析,由于我不是从头开始学习 Python,所以我觉得我错过了一些细微差别。 我注意到的一件事是,在我的一份报告中,我从 CSV 文件中导入了一个数据集,将其作为
为什么a[len(a):] = [x]等同于a.append(x),但是a[len(a) ] = [x] 给出超出范围的错误? 最佳答案 根据 the documentation (强调我的): If
当我运行以下宏时: Sub try() Dim num As Integer num = 123 MsgBox Len(num) MsgBox VBA.Len(num)
我目前正在获取 Python 3.x 中以 0 的随机长度结尾的随机数列表。例如,我得到以下一组随机数字字符串: String 1 = 203502000000 String 2 = 30293300
我正在学习 numba 并遇到了这种我不理解的“奇怪”行为。我尝试使用以下代码(在 iPython 中,用于计时): import numpy as np import numba as nb @nb
在Go , 要检查字符串是否为空,可以使用: len(str) == 0 或 len(str) < 1 或 str == "" 基本上就是选择运营商的一米== , < , != ,但就性能而言希望选项
我正在尝试创建一个函数 hpure通过重复相同的元素直到达到所需的长度来生成 hvect。每个元素可能有不同的类型。例如:如果参数是 show 每个元素将是 show 函数的特化。 hpure sho
我正在实现一个图形操作脚本,但我对以下错误感到困惑: Traceback (most recent call last): File ".....py", line 12, in pri
通常为了节省一些时间,我希望我们在本地函数中使用 n = len(s)。我很好奇哪个调用更快或者它们相同? while i < len(s): # do something 对比 while i
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
大家好! 我从这个网站找到了这段很棒的代码: var points = [30,100]; document.getElementById("demo").innerHTML = myArrayMax
我有一个输出 17 个维度的解码器,其中不同部分是标签和数字。因此,对于标签,我使用了 one-hot 编码并使用“softmax”激活,对于数字,我使用了“sigmoid”激活函数。 这是解码器:
我在下面得到了这段代码,但即使调试它,我也不明白为什么给出 7 而不是 6。 更准确地说,当我调试每个返回时都会给我预期的结果: 第一个函数调用:ipdb> --Return-- ['a'] 第二个函
上述分配可能会出现什么样的问题?如果我们分配实际数据类型的大小而不是该类型指针的大小? 对于 sizeof (char*) > sizeof (char) 的字符来说,这会是一个问题吗?其他数据类型和
我知道 somelist[len(somelist)] 无法访问定义列表之外的索引 - 这是有道理的。 但是为什么 Python 允许你做 somelist[len(somelist):]? 我什至读
我是一名优秀的程序员,十分优秀!