What is meant by Streams w.r.t Java IO

640 views Asked by At

I am getting a hard time visualizing what exactly stream means in terms of IO. I imagine stream as a continuous flow of data coming from a file, socket or any other data source. Is that correct? But then I get confused on how our java programs react to stream because when we write any java code let say:

Customer getCustomer(Customer customer)

Doesn't the above java code expects the whole object to be present before it gets processed? Now lets say we are reading from a stream something like

FileInputStream in = new FileInputStream("abc.txt")
in.read();

Doesn't in.read() expects the whole file to be present in memory to be processed. If it is, then how come it is a stream? Why do we call them streams? Do they process data as it is read?

A similar confusion when reading through hadoop streams, looks like they have a different meaning altogether.

4

There are 4 answers

1
Sheetal Mohan Sharma On

From here..

A Stream is a free flowing sequence of elements. They do not hold any storage as that responsibility lies with collections such as arrays, lists and sets. Every stream starts with a source of data, sets up a pipeline, processes the elements through a pipeline and finishes with a terminal operation. They allow us to parallelize the load that comes with heavy operations without having to write any parallel code. A new package java.util.stream was introduced in Java 8 to deal with this feature.

Streams adhere to a common Pipes and Filters software pattern. A pipeline is created with data events flowing through, with various intermediate operations being applied on individual events as they move through the pipeline. The stream is said to be terminated when the pipeline is disrupted with a terminal operation. Please keep in mind that a stream is expected to be immutable — any attempts to modify the collection during the pipeline will raise a ConcurrentModifiedException exception.

0
Jesper On

The word "stream" is used for different things in different contexts. But you're specifically asking about streams in I/O, i.e. InputStream and OutputStream.

I imagine stream as a continuous flow of data coming from a file, socket or any other data source. Is that correct?

Yes, a stream is a source of a sequence of bytes, which may come from a file, socket etc.

About getCustomer: You need to have a Customer object to pass to that method. But calling methods, passing objects and getting objects returned really does not have anything to do with streams.

Doesn't in.read() expects the whole file to be present in memory to be processed.

No. FileInputStream is an object which represents the stream. It's the thing that knows how to read bytes from the file.

Streams are not a fundamentally special kind of object. It's not like that there are classes, objects and streams. Streams are just a concept that is implemented using the standard Java OO programming features (classes and objects).

1
user207421 On

Doesn't the above java code expects the whole object to be present before it gets processed?

Yes. But it's not a stream.

Doesn't in.read() expects the whole file to be present in memory to be processed.

No.

If it is

It isn't.

then how come it is a stream?

It is.

Why do we call them streams? Do they process data as it is read?

Yes.

A similar confusion

There is no confusion here, except your own confusion when comparing method calls with I/O streams, which comparing apples versus oranges.

when reading through hadoop streams, looks like they have a different meaning altogether.

Very possibly.

0
Pavan Kumar K On

An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays.

Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.

No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data.

In Java there are two kinds of streams (Byte & Character), they differ in the way how the data is transferred between source & destination.

Hope this answers your question. Please let me know if you need any further information.