BufferedInputStream.read(byte [] b,int off,int len)是否可以返回0?是否有可能导致此错误的重要InputStreams? [英] Can BufferedInputStream.read(byte[] b, int off, int len) ever return 0? Are there significant, broken InputStreams that might cause this?

查看:180
本文介绍了BufferedInputStream.read(byte [] b,int off,int len)是否可以返回0?是否有可能导致此错误的重要InputStreams?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BufferedInputStream(byte [] b,int off,int len)是否有可能返回0?

读者的摘要版本(您可以在上下文中阅读下面的内容,但我认为可以归结为:) JDK或常用库(例如Apache Commons)中是否存在InputStreams(即SocketInputStream,CipherInputStream等)? ,番石榴),则无法正确履行InputStream.read(byte [],off,len)的约定,即使len!= 0也可能返回 0?

READER'S DIGEST VERSION (you can read the rest below, for context, but I think it boils down to this:) Are there InputStreams (i.e. SocketInputStream, CipherInputStream, etc. in the JDK or in commonly used libraries (i.e. Apache Commons, Guava), that don't correctly honor the contract of InputStream.read(byte[],off,len) and might return '0' even if len != 0?

(注1:我的兴趣是它是否真的可以在仅使用JDK的代码中发生,或者是否可以在某些真正常见的Java库(例如Apache Commons)中发生;在寻找线索的javadoc,但是我也正在寻找BufferedInputStream的源代码(如果有问题的话,它是Java 7),以防某些边缘情况没有得到正确记录-并且我不完全相信一种方法,因此我的问题)

(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)

(注2:在平凡的情况下,我的意思不是len == 0,我的意思是一般情况下,如果您传入一个非零数组,您是否可以返回0字节?)

(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?)

javadoc是 并在其中部分说明:

The javadoc is here and it says, in part:

此方法实现*对应*的一般合同[我的重点补充]阅读InputStream类的方法。作为一个额外的便利,它尝试通过重复调用基础流的read方法来尝试读取尽可能多的字节。重复进行此读取,直到满足以下条件之一为止:

[省略了两个不相关的条件]

[two irrelevant conditions omitted]

-基础流的可用方法返回零,指示其他输入请求将被阻止。

然后返回值的文档说:

返回:读取的字节数;如果末尾为-1,则返回-1。

因此:通过我的阅读,有可能当您调用此读取函数时,如果没有数据被缓冲底层 InputStream 中没有可用的数据(例如,从停止的http传输开始),那么read方法应该返回0,因为0字节

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.

,但是……一群似乎比我了解更多的人似乎相信这种读取方法将始终返回EOF或至少一个字节。

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.

所以,我进一步研究了InputStream,看看有什么 InputStream类的相应读取方法的常规协定的真正含义是,我发现了这一点:

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:

如果len为零,则不读取任何字节,并返回0;否则,返回0。否则,尝试读取至少一个字节。如果由于流位于文件末尾而没有字节可用,则返回值-1;否则返回值-1。否则,至少读取一个字节并将其存储到b中。

因此,根据javadocs ,认为这意味着BufferedInputStream 不应返回0。如果仅查看文档,我想现在就可以完成。

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.

但是:经过检查,在我看来BufferedInputStream的 implementation 并不能真正保证一个字节或更多字节。它通过依赖基础InputStream的正确行为来继承此保证。抽象InputStream的来源似乎获得了正确的保证(我认为,如果len == 0,它只能返回0个字节),但我不知道JDK中的所有输入流是否都正确,更不用说所有输入流了

But: upon inspection, it seems to me that the implementation of BufferedInputStream doesn't really guarantee one byte or more bytes; it inherits this guarantee by depending on correct behavior of the underlying 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.

所以。 我认为,到目前为止,我是:BufferedInputStream永远不会返回0 除非包装的InputStream不遵守1个或更多字节的保证-但我不这样做知道这是多么普遍。

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.

1)我的一般分析正确吗?

1) Is my general analysis correct?

2)有人知道吗输入流可以返回0的重大情况? (即,可能的InputStream会以非零的len返回0,因此,如果将它们包装在BufferedInputStream中,则需要避免返回零值?–不是某人的个人破损代码,

2) does anybody know of significant cases where InputStreams can return 0? (i.e. 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.)

很长的问题表示歉意;请注意。我写这篇文章时在做更多的研究,所以这个问题就增加了。

Apologies for the long question; I was doing more research as I wrote this, so the question grew.

注意:就上下文而言:我发布此问题是因为我不理解对话提到了另一个问题(使用BufferedInputStream进行套接字读取)-它可能有助于读取

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. You cited:


重复的读取持续进行,直到满足以下条件之一:

This iterated read continues until one of the following conditions becomes true:

[省略了两个不相关的条件]

[two irrelevant conditions omitted]


  • 基础流的可用方法返回零,表示

BufferedInputStream 将通过直接委派给底层流以获取 read 的合同,以读取至少一个字节。如果基础流在第一次读取时正确读取了至少一个字节,则表示合同已履行。

The BufferedInputStream will fulfill the contract of read reading at least one byte by directly delegating to the underlying stream for the first read. If the underlying stream correctly reads at least one byte for that first read, the contract has been fulfilled.

只有后续的读取尝试(迭代读取)是有条件的,即如果可用返回 0 并告知另一个已读

Only subsequent read attempts (the "iterated read") are conditional, i.e. will be skipped if available returns 0 telling that another read attempt would block (again).

所以底线是 BufferedInputStream 像其他所有JDK的 InputStream s一样履行合同。顺便说一句,如果您想完全读取一个数组,可以将流包装在 DataInputStream ,它提供了 readFully 方法。

So the bottom line is that BufferedInputStream fulfills the contract, like all other JDK’s InputStreams — as far as I know. By the way, if you want to read an array entirely you can wrap the stream in a DataInputStream which offers a readFully method.

这篇关于BufferedInputStream.read(byte [] b,int off,int len)是否可以返回0?是否有可能导致此错误的重要InputStreams?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆