简体   繁体   English

Can BufferedInputStream.read(byte [] b,int off,int len)是否会返回0? 是否有重要的,破坏的InputStream可能会导致这种情况?

[英]Can BufferedInputStream.read(byte[] b, int off, int len) ever return 0? Are there significant, broken InputStreams that might cause this?

Is it ever possible for BufferedInputStream(byte[] b, int off, int len) to return 0? BufferedInputStream(byte[] b, int off, int len)可能返回0?

READER'S DIGEST VERSION (you can read the rest below, for context, but I think it boils down to this:) Are there InputStreams (ie SocketInputStream, CipherInputStream, etc. in the JDK or in commonly used libraries (ie Apache Commons, Guava), that don't correctly honor the contract of InputStream.read(byte[],off,len) and might return '0' even if len != 0? READER'S DIGEST VERSION(你可以阅读下面的其余部分,关于上下文,但我认为归结为这个:)是否存在InputStreams(即JDK中的SocketInputStream,CipherInputStream等或常用库(即Apache Commons,Guava)) ,这不正确地遵守InputStream.read(byte [],off,len)的合约,即使len!= 0也可能返回'0'?


(Note 1: my interest is whether it can really happen in code that uses just the JDK, or maybe a few really common Java libraries, such as Apache Commons; I am looking at the javadoc for clue, but I am also looking at the source for BufferedInputStream (Java 7, in case it matters) in case some edge case is not documented correctly -- and I'm not fully convinced one way or another, thus my question) (注1:我感兴趣的是它是否真的可以在仅使用JDK的代码中发生,或者可能是一些非常常见的Java库,例如Apache Commons;我正在查看javadoc的线索,但我也在查看BufferedInputStream的源代码(Java 7,如果它很重要),以防某些边缘情况没有正确记录 - 而且我不完全相信这种或那种方式,因此我的问题)

(Note 2: I don't mean in the trivial case, where len==0, I mean in the general case, where you pass in a non-zero array, can you ever get back 0 bytes?) (注2:我不是指在平凡的情况下,len == 0,我的意思是在一般情况下,你传入一个非零数组,你能回到0字节吗?)

The javadoc is here and it says, in part: javadoc就在这里 ,部分说:

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:

[two irrelevant conditions omitted] [省略了两个不相关的条件]

- The available method of the underlying stream returns zero, indicating that further input requests would block.

And then the doc for the return value says: 然后返回值的文档说:

Returns: the number of bytes read, or -1 if the end of the stream has been reached.

So: by my reading of this, it's possible that when you call this read function, if no data is buffered and no data is available from the underlying InputStream (say, from a stalled http transfer,) then the read method should return 0, since 0 bytes were read. 所以:通过我的阅读,当你调用这个读取函数时,如果没有数据被缓冲并且没有数据可用于底层InputStream (例如,来自停滞的http传输),那么read方法应该返回0,因为读取了0个字节。

and yet… a bunch of folks who seem to know more about this than me seem to believe that this read method will always return EOF or at least one byte. 然而......一群似乎比我更了解这一点的人似乎相信这种读取方法总会返回EOF或至少一个字节。

So, I looked further into InputStream, to see what the general contract of the corresponding read method of the InputStream class really means, and I found this: 所以,我进一步研究了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.

So, according to the javadocs , I think this means BufferedInputStream should not return a 0. If I were looking only at the docs, I think I would be done now. 所以, 根据javadocs ,我认为这意味着BufferedInputStream 不应该返回0.如果我只查看文档,我想我现在就会完成。

But: upon inspection, it seems to me that the implementation of BufferedInputStream doesn't really guarantee one byte or more bytes; 但是:经过检查,在我看来,BufferedInputStream的实现并不能真正保证一个字节或更多字节; it inherits this guarantee by depending on correct behavior of the underlying InputStream. 它依赖于底层InputStream的正确行为来继承此保证。 The source for the abstract InputStream seems to get this guarantee correct (I think it can only return 0 bytes if len==0) but I don't know if this is true for all input streams in the JDK, let alone all input streams anywhere. 抽象InputStream的源似乎得到了这个保证正确(我认为如果len == 0它只能返回0个字节)但是我不知道JDK中的所有输入流是否都是这样,更不用说所有输入流了任何地方。

So. 所以。 I think where I am so far is: BufferedInputStream will never return 0 unless the wrapped InputStream doesn't honor this guarantee of 1 or more bytes -- but I don't know how common this is. 我认为我到目前为止的地方是:BufferedInputStream永远不会返回0, 除非包装的InputStream不支持1或更多字节的保证 - 但我不知道这有多常见。

1) Is my general analysis correct? 1)我的一般分析是否正确?

2) does anybody know of significant cases where InputStreams can return 0? 2)有没有人知道InputStreams可以返回0的重要情况? (ie InputStreams that may return 0 with a non-zero len, so that if you wrap them in a BufferedInputStream, you need to guard against a zero return value? -- not somebody's personal, broken code, but important cases to watch out for, say in the JDK or Apache Commons or something.) (即InputStreams 可能会返回0而非零len,所以如果你将它们包装在BufferedInputStream中,你需要防止零返回值? - 不是某人的个人破损代码,但需要注意的重要案例,在JDK或Apache Commons等中说。)

Apologies for the long question; 为长期问题道歉; I was doing more research as I wrote this, so the question grew. 在我写这篇文章时,我正在做更多的研究,所以这个问题越来越多。

NOTE: for context: I'm posting this question because I didn't understand a conversation I had in reference to this other question ( Socket reading using BufferedInputStream ) -- it might help to read that question for background. 注意:对于上下文:我发布这个问题是因为我不理解我在参考另一个问题( 使用BufferedInputStream读取套接字 )时的对话 - 它可能有助于阅读该问题的背景。

You didn't read the spec of BufferedInputStream carefully enough. 你没有仔细阅读BufferedInputStream的规范。 You cited: 你引用了:

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. 基础流的可用方法返回零,表示进一步的输入请求将被阻止。

The BufferedInputStream will fulfill the contract of read reading at least one byte by directly delegating to the underlying stream for the first read . BufferedInputStream将通过直接委托给第一次read的底层流来完成read读取至少一个字节的合同。 If the underlying stream correctly reads at least one byte for that first read, the contract has been fulfilled. 如果基础流正确读取第一次读取的至少一个字节,则合同已经完成。

Only subsequent read attempts (the “iterated read”) are conditional, ie will be skipped if available returns 0 telling that another read attempt would block (again). 只有后续的读取尝试(“迭代读取”)才是有条件的,即如果available返回0告诉另一次read尝试将再次阻塞。


So the bottom line is that BufferedInputStream fulfills the contract, like all other JDK's InputStream s — as far as I know. 所以底线是BufferedInputStream履行合同,就像所有其他JDK的InputStream - 据我所知。 By the way, if you want to read an array entirely you can wrap the stream in a DataInputStream which offers a readFully method. 顺便说一句,如果你想完全读取一个数组,你可以将流包装在一个提供readFully方法的DataInputStream中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BufferedInputStream.read(byte [] b,int off,int len)中的off参数 - The off parameter in BufferedInputStream.read(byte[] b, int off, int len) InputStream中的read(byte b[], int off, int len)方法和FileInputStream中的read(byte b[], int off, int len)方法一样吗? - Is the method read(byte b[], int off, int len) in InputStream same as the method read(byte b[], int off, int len) in FileInputStream? JAVA:InputStream.read(byte [] b,int off,int len)中的字节数组分配 - JAVA: Byte array allocation in InputStream.read(byte[] b, int off, int len) InputStream read(byte [] b,int off,int len)-删除文件的其余部分? - InputStream read(byte[] b, int off, int len) - Drop the rest of the file? readFully(byte [] b,int off,int len)和EOFException - readFully(byte[] b, int off, int len) and EOFException read(byte [] b,int off,int len):读取文件到最后 - read(byte[] b, int off, int len) : Reading A File To Its End DataOutputStream.write(byte b [],int off,int len)不会引发异常 - DataOutputStream.write(byte b[], int off, int len) don't throws exception 如何使用BufferedInputStream.read()获得真实的字节顺序? - How to get true byte order with BufferedInputStream.read()? 为什么我的BufferedInputStream.read()无法接收-1? - Why can't my BufferedInputStream.read() receive -1? BufferedInputStream.read(byte [])引起问题。 有人以前有这个问题吗? - BufferedInputStream.read(byte[]) Causes problems. Anyone have this problem before?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM