Java Stream Classes

The Java Input/Output (I/O) is a part of java.io package. This package contains a relatively large number of classes that support input and output operations. These classes may be categorized into groups based on the data type on which they operate.

  1. Byte Stream Classes that provide support for handling I/O operations on bytes.
  2. Character Stream Classes that provide support for managing I/O operations on characters.

These two groups may further be classified based on their purpose. Figure 3 below shows how stream classes are grouped based on their functions. Byte stream and Character stream classes contain specialized input and output stream classes to deal with input and output operations independently on various types of streams.

Figure 3: Classification of Java Stream Classes

 

1. Byte Stream Classes

Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary (8-bit) data. Byte streams are defined by using two class hierarchies. At the top there are two abstract classes: java.io.InputStream and java.io.OutputStream. Each of these abstract classes has several concrete subclasses of each of these. The abstract classes InputStream and OutputStream define several key methods that the other stream classes implement. Two of the most important are read() and write(), which, respectively, read and write bytes of data. Both methods are declared as abstract inside InputStream and OutputStream. They are overridden by derived stream classes. The java.io package can be categorized along with its stream classes in a hierarchy structure shown below:

Figure 4: Hierarchy of Byte Stream Classes

1.1 InputStream Classes

The InputStream class is used for reading the data such as a byte and array of bytes from an input source. An input source can be a file, a string, or memory that may contain the data. It is an abstract class that defines the programming interface for all input streams that are inherited from it. An input stream is automatically opened when we create it. We cans explicitly close a stream with the close() method, or let it be closed implicitly when the object is found as a garbage. InputStream is inherited from the java.lang.Object class. Each subclass of the InputStream provided by the java.io package is intended for a different purpose. The subclasses inherited from the InputStream class can be seen in a hierarchy manner shown below:

Figure 5: Hierarchy of InputStream Classes

 

Summary of InputStream Methods

int available()

Returns the number of bytes that can be read from the input stream

void close()

Closes this input stream and releases any system resources associated with the stream.

void mark(int readlimit)

Marks the current position in this input stream.

boolean markSupported()

Tests if this input stream supports the mark and reset methods.

abstract int read()

Reads the next byte of data from the input stream.

int read(byte[ ] array)

Reads some number of bytes from the input stream and stores them into the buffer array named array.

int read(byte[ ] array, int off, int len)

Reads up to len bytes of data from the input stream into an array of bytes.

void reset()

Repositions this stream to the position at the time the mark method was last called on this input stream.

long skip(long n)

Skips over and discards n bytes of data from the input stream.

 

1.2 OutputStream Classes

The OutputStream class is a sibling to InputStream that is used for writing byte and array of bytes to an output source. Similar to input sources, an output source can be anything such as a file, a string, or memory containing the data. Like an input stream, an output stream is automatically opened when we create it. We can explicitly close an output stream with the close() method, or let it be closed implicitly when the object is garbage collected. OutputStream is also inherited from the java.lang.Object class. Each subclass of the OutputStream provided by the java.io package is intended for a different purpose. The classes inherited from the OutputStream class can be seen in a hierarchy structure shown below:

Figure 6: Hierarchy of OutputStream Classes

 

Summary of OutputStream Methods

void close()

Closes this output stream and releases any system resources associated with this stream.

void flush()

Flushes this output stream and forces any buffered output bytes to be written out.

void write(byte[ ] array)

Writes array.length bytes from the specified byte array to this output stream.

void write(byte[ ] array, int off, int len)

Writes len bytes from the specified byte array starting at offset off to this output stream.

abstract void write(int array)

Writes the specified byte to this output stream.

 

 

2. Character Stream Classes

Java offers another type of streams called Character Streams, which are used to read from the input device and write to the output device in units of 16-bit (Unicode) characters. In some cases, character streams are more efficient than byte streams. The oldest version of Java (Java 1.0) did not include character streams and, thus, all I/O was byte-oriented. Character streams were added by Java 1.1, and certain byte-oriented classes and methods were deprecated.

Character streams are defined by using two class hierarchies. At the top there are two abstract classes, Reader and Writer. These abstract classes handle Unicode character (16-bit) streams. Java has several concrete subclasses of each of these. The abstract classes Reader and Writer define several key methods that the other stream classes implement. Two of the most important methods are read() and write(), which read and write characters of data, respectively. These methods are overridden by derived stream classes.

2.1  Reader Stream Classes

The Reader class contains methods that are identical to those available in the InputStream class, except Reader is designed to handle characters. Therefore, Reader classes can perform all the functions implemented by the InputStream classes. The hierarchy of Reader character stream classes is shown below:

Figure 7: Hierarchy of Reader Classes

 

2.2  Writer Stream Classes

The Writer class contains methods that are identical to those available in the OutputStream class, except Writer is designed to handle characters. Therefore, Writer classes can perform all the functions implemented by the OutputStream classes. The hierarchy of Writer character stream classes is shown below:

Figure 8: Hierarchy of Writer Classes

 

In the next section, we will talk about the High-level and Low-level Stream Classes.