FilterInputStream in Java

Filter streams are streams that wrap around underlying input or output streams and adds new features. In other words, filter streams are streams that filter byte input or output streams for some purpose.

For example, the basic byte input stream provides a read method that can be used only for reading bytes. If we want to read integers, doubles, or strings, we require a filter class that could wrap the byte input stream.

Using a filter class, we can read integers, doubles, and strings instead of bytes and characters.

Types of Filter Streams in Java


There are basically two types of filter streams in Java for filtering data. They are:

  • FilterInputStream
  • FilterOutputStream

FilterInputStream and FilterOutputStream are the base classes for filtering data.

If you want to process primitive numeric types, use DataInputStream and DataOutputStream to filter bytes. We will cover both topics in further tutorials.

FilterInputStream in Java


FilterInputStream in Java is a concrete subclass class of InputStream class that filters data of an underlying stream. It implements Closeable and AutoCloseable interfaces.

FilterInputStream class has four important subclasses that are as follows:

  • BufferedInputStream
  • DataInputStream
  • LineNumberInputStream
  • PushbackInputStream

The general syntax for declaration of FilterInputStream class is given below:

public class FilterInputStream
   extends InputStream
     implements Closeable, AutoCloseable

FilterInputStream was added in Java 1.0 version. It is present in java.io.FilterInputStream package.

Constructor of FilterInputStream class in Java


FilterInputStream class provides a single protected constructor that specifies the underlying stream from which filter stream reads data.

1. FilterInputStream(InputStream in): This constructor creates a FilterInputStream built on top of the underlying InputStream in.

Since the constructor provided by filter input stream is protected, we cannot use FilterInputStream class directly. We need to create an instance of subclasses of FilterInputStream class to filter data.

FilterInputStream Methods in Java


FilterInputStream class does not define any new methods. All the methods available in filter input stream are inherited from its superclass InputStream. Some important inherited methods are as follows:


1. int available(): This method returns an estimated number of bytes that can be read from the input stream.

2. int read(): This method is used to read the next byte of data from the input stream.

3. int read(byte[ ] b): This method reads up to byte.length bytes of data from the input stream.

4. long skip(long n): It skips over and discards n bytes of data from the input stream.


5. boolean markSupported(): This method evaluates if the input stream support mark and reset method.

6. void mark(int readlimit): This method marks the current position in the input stream.

7. void reset(): This method is used to reset the input stream.

8. void close(): This method is used to close the underlying input stream.

All the above methods will throw an exception named IOException when an error occurs.

Example Program based on FilterInputStream


Let’s take an example program based on the filter input stream.

Example 1:

package javaProgram;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class BufferedOutputStreamEx {
public static void main(String[ ] args) throws IOException
{
   String filepath = "D:\\myfile.txt";	
   FileInputStream  fis = new FileInputStream(filepath);  
   FilterInputStream filter = new BufferedInputStream(fis);  
   int i = 0;  
   while((i = filter.read()) != -1){  
     System.out.print((char)i);  
   }  
   filter.close();  
  }
}
Output:
       Welcome to Scientech Easy

Here, we are supposing that you have the following data in “myfile.txt” file: Welcome to Scientech Easy.


Let’s create a Java program to get the actual number of available bytes in the filter input stream.


Example 2:

package javaProgram;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class BufferedOutputStreamEx {
public static void main(String[ ] args) throws IOException
{
  String filepath = "D:\\myfile.txt";	
  FileInputStream  fis = new FileInputStream(filepath);  
  FilterInputStream filter = new BufferedInputStream(fis);  
  
  int availableBytes = filter.available();
  System.out.println("Initially, Available bytes: " +availableBytes);
 
  filter.read();
  filter.read();
  filter.read();
 
  int available = filter.available();
  System.out.println("Available bytes after reading three bytes: " +available);
  filter.close();  
 }
}
Output:
       Initially, Available bytes: 25
       Available bytes after reading three bytes: 22

Hope that this tutorial has covered almost all the important points related to FilterInputStream class in Java with example programs. I hope that you will have understood the concepts of filter input stream.

In the next tutorial, we will discuss FilterOutputStream in Java with example programs. If you find anything incorrect in this tutorial, please inform us via email.
Thanks for reading!!!