• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is stream in java?  RSS feed

 
Sri Dharan
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

There are somany streams like I/O Stream, InputStream, OutputStream,...
What is the meaning of Streams in java.?

 
Ulf Dittmer
Rancher
Posts: 42972
73
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

InputStream and OutputStream are the basic stream classes in Java, one letting us bring data in from somewhere, the other letting us put data out to somewhere. All the other streams just add capabilities to the basics, like the ability to read a whole chunk of data at once for performance reasons (BufferedInputStream) or convert from one kind of character set to Java's native unicode (Reader), or say where the data is coming from (FileInputStream, ServletInputStream, SocketInputStream, ByteArrayInputStream, etc.)

The various classes are meant to wrap each other, so we can build, for example, a BufferedInputStream that reads from a file:

InputStream in = new BufferedInputStream(new FileInputStream(myfile));

Then if you decide you want to read data from a network socket instead, you can just change FileInputStream() to SocketInputStream() (and add a bit of code to establish the network connection of course) and you won't have to change anything else in your program.

 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote: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.


Taking your widgets on a conveyor belt analogy a little further - What happens if I miss a widget ? How do i get it back for processing (screw, hammering etc) ?
 
Paul Clapham
Sheriff
Posts: 22695
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this imaginary scenario, you just run the conveyor belt backwards until you get the widget you missed. Note that this part of the analogy may or may not correspond to a feature of any stream implemented in Java -- analogies are not intended to be perfect implementations of the things they are describing.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the analogy breaks down a bit there, because unlike a conveyor belt, it's taking things off or putting things on that causes an I/O Stream to move. At least that's true at the level of InputStream and OutputStream. Down at lower levels of the network stack, packets are coming in from different locations and at different times, and the protocol is trying to put things back into the right order, and possibly requesting replacements for missing packets. Did you have a particular example of something you're trying to do, or are you just trying to understand the concept?
 
Andy Jack
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:Well, the analogy breaks down a bit there, because unlike a conveyor belt, it's taking things off or putting things on that causes an I/O Stream to move. At least that's true at the level of InputStream and OutputStream. Down at lower levels of the network stack, packets are coming in from different locations and at different times, and the protocol is trying to put things back into the right order, and possibly requesting replacements for missing packets. Did you have a particular example of something you're trying to do, or are you just trying to understand the concept?


Only trying to understand the concept.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!