How is an Input Stream different from an Output Stream?
Asked Answered
N

8

7

I see that both are "streams" of data. In that case, why consider them different ? What is the difference really ?

Comment - Please don't close this question. It is the basic kind of stuff that can confound people in an interview.

UPDATE 1 - Everyone seems to say the same thing - you read from IS and write into OS. So, they are basically the same. Like a pipe with water flowing through it. When you use the water from that pipe you call it InputStream and When you pump water into it, its called an output stream. Is it really that trivial ?

UPDATE2 - If the difference is not so "big", then can we have a InAndOutStream instead of having to make code for two classes (InputStream and OutputStream) ?

Nuri answered 17/12, 2012 at 7:50 Comment(5)
yes it is that trivial. its a matter of perspective - which end of the "stream" youre on. for example, look at the ProcessBuilder class in java. it allows you to start a process from your java process, and then that process' output streams are input streams to you, and vice versa.Testator
I was also thinking these answers and I also want to know if there's is some more reasons for this. My guess is though that it's a difference in native IO...Adler
@Adler - yeah, it looks trivial to me. But, I feel that we also need to look at this - If the difference is not so "big", then can we have a InAndOutStream instead of having to make code for two classes (InputStream and OutputStream) ?Nuri
the basic "stream" concept is unidirectional (as its name implies) - so 2 implementationsTestator
"Everyone seems to say the same thing - you read from IS and write into OS. So, they are basically the same". No they aren't. The concept of a stream of data is the same, but writing and reading are different operations. Having different classes for them simplifies their implementation and their API - no programmer will make the mistake of attempting to read from an output stream, because the compiler will prevent him from calling a read method on it. An implementation does not have to keep track of what is happening under the covers if read and write are called in succession.Pascha
F
16

They are conceptually different

  • from InputStream you read
  • to OutputStream you write
Faxun answered 17/12, 2012 at 7:52 Comment(5)
@davidblaine, yes, it is trivial, since InputStream and OutputStream are abstract classes. It becomes more complicated once subclasses implement those methods in different fashion.Faxun
If the difference is not so "big", then can we have a InAndOutStream instead of having to make code for two classes (InputStream and OutputStream) ?Nuri
@davidblaine, but then your InAndOutStream at runtime has to be either input or output anyway, not both at the same time, so it would be confusing.Faxun
I am not sure what that means. I guess this is what you are trying to say - We need to know which "mode" the stream is operating in, ie read or write ? For that reason we have IS and OS instead of InAndOutStream. Alternately, We could have a variable in InAndOutStream class which tells us the mode of stream - ie in or out. But that would be cumbersome, i guess.Nuri
It's best not to mix them in your program, just because the types help you keep track of what you should be doing with each stream.Disject
W
5

A stream is data that you access in sequence. You could think of it like a train that you watch from a tunnel entrance so that you can just see one car at a time. Or a stream of widgets coming across a conveyor belt requiring you to tighten a screw on each one before it passes by to the next person down the assembly line who has to pound it with a hammer, and so on. Or sticks floating down a river while you watch from a bridge.

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. A program uses an input stream to read data from a source, one item at a time: IO Stream

A program uses an output stream to write data to a destination, one item at time: IO Stream

Wain answered 17/12, 2012 at 8:8 Comment(0)
K
4

If the difference is not so "big", then can we have a InAndOutStream instead of having to make code for two classes (InputStream and OutputStream) ?

This is not possible with the exception of a the ByteArray classes. The reason is that most Streams are backed by system provided devices such as a file or a socket. In the case of a file, the file you write is the file you read from but in the case of a socket, the stream you write to is independant of the stream you read from.

Even attempting to combine them would be a bad idea IMHO as people get confused enough between text and binary streams. If they could read and write to the same stream you would get even more confusion I suspect.

In general, developers should have a clear idea as to whether they are read or writing from a device. If that is not clear to them you are bound to have problems.

FYI: I have written a library which does exactly what you suggest for in memory stream like "pipes" ;) However, I would say it is only for expert developers and even then I keep the interfaces as distinct as possible to prevent confusion.

Keelykeen answered 17/12, 2012 at 8:39 Comment(0)
R
3

The concept of the streams are not as symmetrical as you imply in your question. Since this is Java, the reader and writer of the stream occupy radically different positions in the execution environment: One implements the stream object, and the other calls methods on it.

Therefore, since the language forces asymmetry on the model, an implementer of any I/O facility in Java, then, needs to determine in advance whether to occupy the reading side or the writing side of the stream. Users of the facility don't get the choice. For instance, then; in order to implement an I/O facility that receives data from its users, one would need to implement an OutputStream (which users would call upon to write to).

To implement a symmetrical facility, the language would need to be capable of co-routine execution. In that case, streams could truly have a readable and a writable side, and a caller trying to write would yield to the reader, and vice versa. Java does not support such facilities, however. (At least outside of multi-threading, which would clearly be far too heavy-weight for most situations.)

I hope that makes it a bit more clear.

Rann answered 17/12, 2012 at 8:6 Comment(3)
Can you elaborate more on the reader and writer of the stream occupy radically different positions in the execution environment or point to an article that has more information?Adler
I thought I elaborated quite a bit on it already. I'm not sure what more I can say.Rann
@Rann - I am a newbie and not familiar with co-routines and such. I saw it on wiki, but was unable to see how it relates to my question. Can you explain that in simpler words, perhaps with an analogy ?Nuri
P
2

You read from an InputStream, and you write to an OutputStream. That's the key difference between them.

In some cases you need to be able to both read and write. One such example is a stream socket. The way the Java library handles this is by having an InputStream and an OutputStream.

Plain answered 17/12, 2012 at 7:52 Comment(3)
@davidblaine: Yes, it's that trivial.Plain
If the difference is not so "big", then can we have a InAndOutStream instead of having to make code for two classes (InputStream and OutputStream) ?Nuri
Conceptually yes. But I don't know about native low level IO so there may be a difference there..Adler
C
0

My best bet is to keep them small, when a user is only going to read a stream, why load functions that are used to write to a stream? This is based purely on my guess though...

edit: I was being silly with the word file, streams can be used for more than files.

Crate answered 17/12, 2012 at 7:53 Comment(0)
F
0

To avoid confusion between the stream, for example html registration form, the data enter into the form is known as input-data, once the registration is success or failure output is known as out-data. In the same manner an application receives data via input-stream, an application sends data via output-stream.

The following code helps us to avoid confusion between streams:

//Standard input: 
Scanner scan = new Scanner(System.in);
String s = scan.next();

//Standard output
System.out.println("Hello World!");
Fivefinger answered 19/6, 2017 at 4:19 Comment(0)
B
0

If the difference is not so "big", then can we have a InAndOutStream instead of having to make code for two classes (InputStream and OutputStream)?

Actually some languages/frameworks have the notion of universal bidirectional stream, w/o separating input and output interfaces. E.g. in .NET there is no separate InputStream and OutputStream, just Stream.

So, from theoretical perspective, it's possible. Why Java (and e.g. C++) implementers chose separate interfaces, while .NET chose single interface for both input and output operations, is a different question.

Blaisdell answered 12/12, 2023 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.