Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input/Output Stream : End of Stream?

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

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

like image 826
salbeira Avatar asked Dec 18 '13 02:12

salbeira


People also ask

What is the end of stream?

The top end of a stream, where its flow begins, is its source. The bottom end is its mouth. In between, the stream flows through its main course or trunk.

What is input stream and output stream?

An input stream handles data flowing into a program. An output stream handles data flowing out of a program. In the picture, each "O" is a piece of data. The data are streaming from the source into the program. Computed results are streaming from the program to the destination.

Do you need to close input stream?

You do need to close the input Stream, because the stream returned by the method you mention is actually FileInputStream or some other subclass of InputStream that holds a handle for a file. If you do not close this stream you have resource leakage.

How do I close an output stream?

close() method closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.


2 Answers

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 FileReader when 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.

A possible workaround would be to make a wrapper to the StreamReader you 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.

like image 55
Julián Urbano Avatar answered Oct 13 '22 19:10

Julián Urbano


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

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

like image 38
erickson Avatar answered Oct 13 '22 17:10

erickson