Streams and Stream Classification
Home ] Up ] Files and Directories ] I/O Exceptions ] [ Streams and Stream Classification ] Character Streams ] Byte Streams ] Data Conversion Streams ] Filter Streams ] Message Formatting ] Tokenizers ] Zip & Jar Files ] Random Access ]

 

 

What is a Stream?

A stream is a very useful abstraction that exists in a number of environments. A stream conceptually has:

  • A source
  • A "sink" (a destination)
  • A flow of data from the source to the destination

For example:

  • The UNIX operating system has the concept of streams of data which may be read or written. The various UNIX shells (such as the Bourne shell, the C shell, etc) take great advantage of the concept of a stream to 'pipe' data from one place to another
  • The C++ programming language has its I/O streams which provide similar functionality.

If you wish to perform I/O in Java (or move data around in memory), then you will use Java Streams

Classification of Streams

In Java, streams may be classified in the following ways:

  • Input streams vs Output streams
  • Character streams vs Byte streams

Input vs Output

Depending on whether the stream is an input stream or an output stream, a program can interact with the stream in the following ways:

Reading Writing
open the stream
while more data exists
  read data from stream
close the stream
open the stream
while more data
  write data to stream
close the stream

Character vs Byte

Java separates its stream classes into two types:

  • Streams dealing with characters (Readers and Writers)
  • Streams dealing with bytes (binary data)

Problems were found in JDK 1.0 when using the existing byte-oriented streams to deal with input and output of internationalized character sets. Consequently, JDK 1.1 introduced the set of classes called Readers and Writers.

Note: Don't even think about using the byte-based streams to input or output characters -- they can't handle anything but ISO Latin-1 8-bit bytes. If you did, your code would not be portable to other character sets (and eventually you will be worrying about producing products for the rest of the world!).

Other Classifications

The set of stream classes is so large that there are also other ways you could classify them:

  • Data Source/Sink streams -- streams that write/read data to/from memory buffers, files, etc.
  • Processing streams -- streams that perform some kind of operation on the data as they read or write
    • Buffering streams
    • Filtering streams
    • Conversion streams
    • etc.
 
The page was last updated February 19, 2008