J2EEOnlineJ2EE 





Java Streams  «Prev  Next»
Lesson 2What is an input stream?
ObjectiveExamine an analogy for thinking about input streams.

Java Stream Input Definition

An input stream is a data sequence of undetermined length from which your program can read bytes, one by one and in order. It is called a stream because it's like a stream of water that continues to flow and there is no definite end to it.
An input stream is like a siphon that sucks up water and an output stream is like a hose that sprays out water. Siphons can be connected to hoses to move water from one place to another. Sometimes a siphon may run out of water if it is drawing from a finite source like a bucket. On the other hand, if the siphon is drawing water from a river, it may well provide water indefinitely. Similarly an input stream may read from a finite source of bytes like a file or an unlimited source of bytes like
System.in.

Likewise, an output stream may have a definite number of bytes to output or an indefinite number of bytes.

A better analogy might be a queue of people waiting to get on a ride at an amusement park. It is the same type of line that you wait in every day to buy coffee at StarBucks or to get lunch in the cafeteria. However, a queue for a roller coaster is much longer than the lines at StarBucks, because it needs to hold more people.
A queue at an amusement park is more formal, in order to move people ahead in an orderly fashion. Because these lines must hold a dozen or more people, the queue is long and usually in several sections.
The length of the roller coaster queue may be adjusted for fewer people. If this were a busy day at the park, all the rows would be filled with people (elements) who would enter the line at the front of the queue, then move forward as more and more people ride the roller coaster.
Roller Coaster Java Stream Analogy



It is important to understand the following topics in File I/O as a prerequisite for streams.
  1. Buffer handling
  2. Kernel versus user space
  3. Virtual memory
  4. Paging
  5. File-oriented versus stream I/O
  6. Multiplexed I/O (readiness selection)

New I/O versus Traditional File I/O

New I/O, usually called NIO, is a collection of Java programming language APIs that offer features for intensive I/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I/O. NIO was developed under the Java Community Process as JSR 51.
As of 2006, an extension to NIO, called NIO2 was developed under JSR 203; JSR 203 was included in Java SE 7 ("Dolphin").