Java Streams  «Prev  Next»

Lesson 3Streaming data
ObjectiveExplore how input streams work.

Input Streaming Data using Java Streams

Read the byte 32

To bring data into a program, a Java program opens a stream to a data source, such as a file or remote socket, and reads the information serially. On the flip side, a program can open a stream to a data source and write to it in a serial fashion. Whether you are reading from a file or from a socket, the concept of serially reading from, and writing to different data sources is the same. For that very reason, once you understand the top level classes (,, the remaining classes are straightforward to work with.

InputStream Visualization

Here is a visualization of an input stream. In the animation below, the program is first going to read the byte 32. Then it will read the byte value 45, then byte 78, then byte 21. As each byte is read, it is removed from the queue. The order in which the bytes are read is determined by the stream.
Furthermore, a raw input stream does not provide any facilities for rereading a byte once it is read or for reading the bytes out of order.
You can skip over some bytes if you want, but once you have skipped them, they are gone forever.

Stream animation

In telecommunications, a data stream is a sequence of digitally encoded coherent signals (packets of data or datapackets) used to transmit or receive information that is in transmission. In electronics and computer architecture, a data stream determines for which time which data item is scheduled to enter or leave which port of a
  1. systolic array,
  2. a Reconfigurable Data Path Array or
  3. similar pipe network,
  4. or other processing unit or block.
Often the data stream is seen as the counterpart of an instruction stream, since the von Neumann machine is instruction-stream-driven.

Input to a Java program can come from many sources and output can go to many different kinds of destinations. The power of the stream metaphor and in turn the stream classes is that the differences between these sources and destinations are abstracted away. All input and output are simply treated as streams.
Class InputStream
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
AudioInputStream, ByteArrayInputStream, FileInputStream, 
FilterInputStream, InputStream, ObjectInputStream, 
PipedInputStream, SequenceInputStream, StringBufferInputStream

Java Streaming Data
In Java, a stream is composed of discrete bytes. The bytes may represent data of type char or other kinds of data. The data of type char may come faster than what your program has been written to handle, or your thread may block while waiting for the next one to arrive.

Processing Streams Exercise

How do you deal with processing streams in a Java program when you cannot control how fast the bytes are coming or you do not know how long the stream is?
Click the Exercise link below to enter your recommendation and submit your solution to this problem.
Processing Streams - Exercise