简体   繁体   English

输入 stream.read 返回 0 或 -1?

[英]Input stream.read return 0 or -1?

What the difference between有什么区别

byte[] buffer = new byte[1024];
// this:
if (inputStream.read(buffer) > 0) { /*...*/ }
// and:
if (inputStream.read(buffer) != -1) { /*...*/ }

Can both determine the.network stream terminate?两者都可以确定.network stream终止吗?

The Javadocs for InputStream.read() say: InputStream.read()的Javadocs说:

If the length of b is zero, then no bytes are read and 0 is returned 如果b的长度为零,则不读取任何字节,返回0

In normal use, this should never happen, so there's not much point to testing for this condition explicitly. 在正常使用中,这种情况永远不会发生,因此明确地测试这种情况没有太大意义。 (If you want to avoid looping forever because the buffer is zero-length and fail-fast in this situation, just test the length of the buffer.) (如果你想避免永远循环,因为在这种情况下缓冲区是零长度和快速失败,只需测试缓冲区的长度。)

Further on, there's: 还有,有:

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. 返回:读入缓冲区的总字节数,如果由于已到达流末尾而没有更多数据,则返回 -1

If you want to test for end-of-file (or network stream, or whatever), use the test: 如果要测试文件结束 (或网络流,或其他),请使用测试:

if ( inputStream.read(buffer) != -1 ) ...

A non-buggy Java implementation will never return anything else to indicate there's no more data available. 非bug的Java实现永远不会返回任何其他内容,以表明没有更多的数据可用。

According to the doc , inputstream.read(buffer) !=-1 will tell you that the stream is ended. 根据docinputstream.read(buffer) !=-1会告诉你流已经结束了。 inputstream.read(buffer) == 0 just says that there are no bytes available to read but the stream is still active (ie the peer just hasn't sent anything since you last read everything you could). inputstream.read(buffer) == 0只是说没有可用的字节可供读取,但是流仍处于活动状态(即,自上次读取所有内容后,对等体才发送了任何内容)。

If you already know that the buffer length is not zero, there is no effective difference between these two expressions . 如果您已经知道缓冲区长度不为零, 则这两个表达式之间没有有效差异 Given this basic stipulation about a valid buffer, it can be inferred from the docs that read will never return 0 . 鉴于有关有效缓冲区的这一基本规定,可以从read文档中推断read永远不会返回0

This method blocks until input data is available, end of file is detected, or an exception is thrown. 此方法将阻塞,直到输入数据可用,检测到文件结尾或引发异常。

There is indeed a difference between inputstream.read(buffer) !=-1 and inputstream.read(buffer) > 0 . inputstream.read(buffer) !=-1inputstream.read(buffer) > 0之间确实存在差异。 I recently experienced this while using jSerialComm library.我最近在使用jSerialComm库时遇到了这个问题。 When reading from an RS-232 serial port and a BREAK signal is received the InputStream (which I retrieved using the SerialPort.getInputStream() method) actually returns 0 .当从 RS-232 串行端口读取并收到BREAK 信号时,InputStream(我使用 SerialPort.getInputStream() 方法检索)实际上返回 0 This is quite useful when doing blocking I/O operations on an RS-232 serial port, for example using the BREAK signal (a 0-length read from the input stream) one can determine the end of a request/response message.这在 RS-232 串行端口上执行阻塞 I/O 操作时非常有用,例如使用 BREAK 信号(从输入流读取 0 长度)可以确定请求/响应消息的结束。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM