java InputStream.read() 返回的 0 是什么意思?如何处理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2319395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What 0 returned by InputStream.read() means? How to handle this?
提问by Alexey Tigarev
What 0 (number of bytes read) returned by InputStream.readmeans? How to handle this situation?
通过什么方式返回 0(读取的字节数)InputStream.read?如何处理这种情况?
Update:I mean read(byte[] b)or read(byte[] b, int off, int len)methods which return number of bytes read.
更新:我的意思是read(byte[] b)或read(byte[] b, int off, int len)其中的字节数回归阅读方法。
回答by Joachim Sauer
The only situation in which a InputStreammay return 0from a call to read(byte[])is when the byte[]passed in has a length of 0:
aInputStream可能0从调用返回的唯一情况read(byte[])是当byte[]传入的长度为 0 时:
byte[] buf = new byte[0];
int read = in.read(buf); // read will contain 0
As specified by this part of the JavaDoc:
正如 JavaDoc 的这一部分所指定的:
If the length of b is zero, then no bytes are read and 0 is returned
如果 b 的长度为零,则不读取字节并返回 0
My guess: you used available()to see how big the buffer should be and it returned 0. Note that this is a misuse of available(). The JavaDoc explicitly states that:
我的猜测:你曾经available()看到缓冲区应该有多大,它返回0. 请注意,这是对available(). JavaDoc 明确指出:
It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
使用此方法的返回值来分配用于保存此流中所有数据的缓冲区永远是不正确的。
回答by Jeff Doering
Take a look at the implementation of javax.sound.AudioInputStream#read(byte[] b, int off, int len) ... yuck. They completely violated the standard java.io.InputStream semantics and return a read size of 0 if you request fewer than a whole frame of data.
看一下 javax.sound.AudioInputStream#read(byte[] b, int off, int len) 的实现……哎呀。它们完全违反了标准的 java.io.InputStream 语义,如果您请求的数据少于一整帧,则返回读取大小为 0。
So unfortunately; the common advice (and api spec) should preclude having to deal with return of zero when len > 0 but even for JDK provided classes you can't universally rely on this to be true for InputStreams of arbitrary types.
很不幸;通用建议(和 api 规范)应该排除在 len > 0 时必须处理返回零的情况,但即使对于 JDK 提供的类,您也不能普遍依赖于任意类型的 InputStreams 的这一点。
Again, yuck.
再次,呸。
回答by helios
According to Java API Doc:
根据 Java API 文档:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[])
http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[])
It only can happen if the byte[] you passed has zero items (new byte[0]).
只有当您传递的字节 [] 具有零个项目(新字节 [0])时才会发生这种情况。
In other situations it must return at least one byte. Or -1 if EOF reached. Or an exception.
在其他情况下,它必须至少返回一个字节。如果达到 EOF,则为 -1。或者例外。
Of course:it depends of the actual implementation of the InputStream you are using!!! (it could be a wrong one)
当然:这取决于您使用的 InputStream 的实际实现!!!(这可能是错误的)
回答by eremmel
I observed the same behavior (reading 0 bytes) when I build a swing console output window and made a reader-thread for stdout and stderr via the following code:
当我构建一个 Swing 控制台输出窗口并通过以下代码为 stdout 和 stderr 创建读取器线程时,我观察到了相同的行为(读取 0 字节):
this.pi = new PipedInputStream();
po = new PipedOutputStream((PipedInputStream)pi);
System.setOut(new PrintStream(po, true));
当“主”swing 应用程序退出并且我的控制台窗口仍然打开时,我从 this.pi.read() 读取了 0。读取的数据被放在控制台窗口上,导致了某种方式的竞争条件,只是忽略结果而不更新控制台窗口解决了这个问题。
