Filter Streams Java


In the filter streams Java category, the java.io package comes with 4 classes – FilterInputStream and FilterOutputStream on byte stream side and FilterReader and FilterWriter on character streams side. All the subclasses of these 4 classes are known as filter streams.

What are filter input streams?

As you can see in the following hierarchies, there exist 4 subclasses for FilterInputStream and 3 subclasses for FilterOutputStream.

Filter Streams Java
Output Screenshot of Filter Streams Java

The hierarchy contains 12 input stream classes. All the classes can be divided into two categories – high-level streams and low-level streams. All the subclasses of FilterInputStream are known as high-level streams (there exists 4) and all the remaining are known as low-level streams (there are 8). Filter streams can be linked to another to have higher functionality, but subject to some rules.

Similar to input stream hierarchy, there exists output stream hierarchy also dealing with all output stream classes.


Filter Streams Java
Output screenshot of Filter Streams Java

All the subclasses of FilterOutputStream are known as high-level streams (there exists 3) and all the remaining are known as low-level streams (there are 6). One filter stream can be linked to another to have higher functionality (output),

Java comes with low-level byte streams and high-level byte streams and similarly low-level and high-level character streams. Sub classes of FilterInputStream and FilterOutputStream are known as high-level streams. There are four high-level streams on input streams side – LineNumberInputStream, DataInputStream, BufferedInputStream and PushbackInputStream.

What is the job of Filter Streams Java?

The job of high-level streams is to add extra functionality to the existing streams. For example, LineNumberInputStream adds line numbers in the destination file that do not exist in the source file. DataInputStream increases performance with its readInt() and readLine() methods etc.

For higher functionality, one stream can be linked or chained to another, but obeying some simple rules. Chaining is very simple. The output of one stream becomes input to the other. Or to say, we pass an object of one stream as parameter to another stream constructor; this is how chaining is affected.

1. DataInputStream and DataOutputStream

DataInputStream and DataOutputStream are high-level as they are the subclasses of FilterInputStream and FilterOutputStream. These streams' extra functionality is that they can read or write integers, doubles and lines at a time, instead of byte by byte. This increases the performance to some extent; instead of reading and writing byte by byte with FileInputStream and FileOutputStream.

2. Giving Line Numbers – LineNumberInputStream

LineNumberInputStream is a subclass of FilterInputStream stream. We know each filter stream adds some extra functionality. The LineNumberInputStream does the extra work of adding line numbers that do not exist in the source.

3. Using Buffering to Enhance the Performance

Buffering of streams increases the performance to higher extents of few thousand times. For buffering, two streams exist in java.io package in byte streams category – BufferedInputStream and BufferedOutputStream. These high-level classes are subclasses of FilterInputStream and FilterOutputStream and their functionality is to increase the performance with buffer. These streams give an implicit system-defined buffer (generally, 2048 bytes) into which data is read and written. The buffer decreases the number of transfers between source file context area and destination file context area and thereby performance increases.

4. PushbackInputStream – Pushing unwanted extra character

Java permits to unread the unwanted bytes in a stream with the class PushbackInputStream. This is an unusual filter stream and it pushes back a character into the input stream. It is used to check a sequence of characters that meets with a condition or not. It can check the delimiter of a line like \r (carriage-return) or \n (life-feed) etc. If the sequence is broken out, it uses unread() method to push a byte back to the stream. Internally, the DataInputStream uses a PushbackInputStream to check for a line-feed (\n) sequence is crossed or not. For a program, you can go through PushbackReader.

3 thoughts on “Filter Streams Java”

  1. The word filter is derived by analogy from a water filter. A water filter sits between the pipe
    and faucet, pulling out impurities. A stream filter sits between the source of the data and its
    eventual destination and applies a specific algorithm to the data. As drops of water are passed
    through the water filter and modified, so too are bytes of data passed through the stream filter.
    Of course, there are some big differences—most notably, a stream filter can add data or some
    other kind of annotation to the stream, in addition to removing things you don’t want; it may
    even produce a stream that is completely different from its original input (for example, by
    compressing the original data).

Leave a Comment

Your email address will not be published.