Java 输入/输出流:流结束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20648611/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:20:33  来源:igfitidea点击:

Input/Output Stream : End of Stream?

javaiostreamnetwork-programming

提问by salbeira

I was always wondering: What is the end of a stream?

我一直在想:流的终点是什么?

In the javadoc of most readLine methods in the java.io package, you can read that "this returns null if the end of the stream is reached" - though I never actually got a null, as most streams (in the case of a network stream that I use most often) just block the program execution until something is written into the stream on the remote end

在 java.io 包中大多数 readLine 方法的 javadoc 中,您可以读到“如果到达流的末尾,则返回 null”-尽管我实际上从未得到过 null,因为大多数流(在网络的情况下)我最常使用的流)只是阻止程序执行,直到将某些内容写入远程端的流中

Are there ways to enforce this acutal behavior happening in an actual non-exception throwing way? I am simply curious ...

有没有办法以实际的非异常抛出方式强制执行这种实际行为?我只是好奇...

采纳答案by Julián Urbano

Think of a file being read. There is an end of stream there, the end of the file. If you try to read beyond that, you simply can't. If you have a network connection though, there doesn't need to be an end of stream if you simply wait for more data to be sent.

想想正在读取的文件。那里有流的结尾,文件的结尾。如果您尝试阅读更多内容,则根本无法阅读。但是,如果您有网络连接,那么如果您只是等待发送更多数据,则不需要结束流。

In the case of the file, we know for a fact that there is no more data to be read. In the case of a network stream we (usually) don't.

对于文件,我们知道没有更多数据可供读取。在网络流的情况下,我们(通常)不这样做。

Blocking a FileReaderwhen no more data is available, awakening when there is:the simple answer is: you can't. The fundamental difference is that you read a file actively, but when you listen to a network stream you read passively. When something comes from the network your hardware sends a short of signal to the Operating System, which then gives the new data to your JVM, and the JVM then awakens your process to read the new data (so to speak). But we don't have that with a file, at least not immediately.

FileReader在没有更多数据可用时阻塞 a,在有数据时唤醒:简单的答案是:你不能。根本区别在于您主动读取文件,但是当您收听网络流时,您是被动读取的。当某些东西来自网络时,您的硬件会向操作系统发送一个短信号,然后操作系统将新数据提供给您的 JVM,然后 JVM 唤醒您的进程以读取新数据(可以这么说)。但是我们没有文件,至少不是立即。

A possible workaround would be to make a wrapper to the StreamReaderyou have, with a listener that is notified when the file is changed, which then awakens you to read further. In Java 7 you can use the WatchService.

一种可能的解决方法是对StreamReader您拥有的文件进行包装,并在文件更改时通知侦听器,然后唤醒您进一步阅读。在 Java 7 中,您可以使用WatchService.

回答by erickson

At some point, the socket will be closed, and no more data can be sent via that stream. This is when the InputStreamwill signal EOF by returning -1 from read()and its overloads. This state is irreversible. That stream is dead.

在某些时候,套接字将关闭,并且无法通过该流发送更多数据。这是当InputStream将通过从read()及其重载返回 -1 来发出 EOF 信号的时候。这种状态是不可逆的。那条流已经死了。

Simply blocking for more data on an open stream is not an EOF condition.

简单地在打开的流上阻塞更多数据不是 EOF 条件。

回答by user207421

I never actually got a null, as most streams (in the case of a network stream that I use most often) just block the program execution until something is written into the stream on the remote end

我实际上从未得到过空值,因为大多数流(在我最常使用的网络流的情况下)只是阻止程序执行,直到将某些内容写入远程端的流中

No. You never got a null because the peer never closed the connection. That's what 'end of stream' means. It doesn't mean 'no more data for the time being'.

不。你永远不会得到空值,因为对等方从未关闭连接。这就是“流结束”的意思。这并不意味着“暂时没有更多数据”。