Introduction to Filter I/O Streams
As mentioned earlier, The InputStream and OutputStream classes are used for reading from and writing to byte streams, respectively. In this section, you are going to learn of data transforming and manipulating using Filter Streams.
Like I/O streams, Filter streams are also used to manipulate data reading from an underlying stream. Apart from this, it allows the user to make a chain using multiple input stream so that, the operations that are to be applied on this chain, may create a combine effects on several filters. By using these streams, there is no need to convert the data from byte to char while writing to a file. These are the more powerful streams than the other streams of Java.
The class hierarchy of the Filter streams derived from I/O streams can be shown as:
There are two streams, that are derived from the I/O stream class:
FilterInputStream is the base class for all the input stream filters and is obtained from the InputStream class. It simply overrides all the methods of the InputStream class required to filter data. These methods performs some sort of filtering operations on the data and cannot be instantiated directly. It provides the additional and chaining functionality for the multiple input streams to be filtered.
The constructor of the FilterInputStream is written as:
|FilterInputStream fis = new FilterInputStream(Inputstream in);|
FilterOutputStream is the super class of all the output stream filters and is obtained from the OutputStream class. It simply overrides all the methods of the OutputStream class required to filter the data that is written to the output stream.
FilterIntputStream and FilterOutputStream do not perform any filtering themselves; this is done by their subclasses. To improve the performance of these I/O streams, their subclasses BufferedInputStream and BufferedOutputStream are used.
This class is a subclass of the FilterInputstream class that lets you read large amount of data from a stream and store it in an internal buffer. When data is requested, it is usually read from the buffer. This has the effect for improving the performance of some input streams, especially for those, that are bounded to slower sources like a file or network connection.
The constructor of the BufferedInputStream is written as:
BufferedInputStream (java.io.InputStream in);
This is a subclass of the BufferedOutputStream and a buffered output stream that lets you write characters to a stream. A BufferedOutputStream adds functionality to another output stream. The data is first written into a buffer. When the BufferedOutputStream is created, an internal buffer array is also created, in which the bytes are stored.
The constructor of the BufferedOutputStream is written as:
BufferedOutputStream (java.io.OutputStream out);