簡體   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?

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

READER'S DIGEST VERSION(你可以閱讀下面的其余部分,關於上下文,但我認為歸結為這個:)是否存在InputStreams(即JDK中的SocketInputStream,CipherInputStream等或常用庫(即Apache Commons,Guava)) ,這不正確地遵守InputStream.read(byte [],off,len)的合約,即使len!= 0也可能返回'0'?


(注1:我感興趣的是它是否真的可以在僅使用JDK的代碼中發生,或者可能是一些非常常見的Java庫,例如Apache Commons;我正在查看javadoc的線索,但我也在查看BufferedInputStream的源代碼(Java 7,如果它很重要),以防某些邊緣情況沒有正確記錄 - 而且我不完全相信這種或那種方式,因此我的問題)

(注2:我不是指在平凡的情況下,len == 0,我的意思是在一般情況下,你傳入一個非零數組,你能回到0字節嗎?)

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:

[省略了兩個不相關的條件]

- 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等中說。)

為長期問題道歉; 在我寫這篇文章時,我正在做更多的研究,所以這個問題越來越多。

注意:對於上下文:我發布這個問題是因為我不理解我在參考另一個問題( 使用BufferedInputStream讀取套接字 )時的對話 - 它可能有助於閱讀該問題的背景。

你沒有仔細閱讀BufferedInputStream的規范。 你引用了:

迭代讀取將繼續,直到滿足以下條件之一:

[省略了兩個不相關的條件]

  • 基礎流的可用方法返回零,表示進一步的輸入請求將被阻止。

BufferedInputStream將通過直接委托給第一次read的底層流來完成read讀取至少一個字節的合同。 如果基礎流正確讀取第一次讀取的至少一個字節,則合同已經完成。

只有后續的讀取嘗試(“迭代讀取”)才是有條件的,即如果available返回0告訴另一次read嘗試將再次阻塞。


所以底線是BufferedInputStream履行合同,就像所有其他JDK的InputStream - 據我所知。 順便說一句,如果你想完全讀取一個數組,你可以將流包裝在一個提供readFully方法的DataInputStream中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM