Java FilterInputStream Class

The InputStream is implemented by the Java FilterInputStream class. To provide extra functionality, it has various subclasses like DataInputStream and BufferedInputStream. So, its individual use is declining.

Explanation

The Java FilterInputStream class functions similarly to the Java InputStream class; however, it merely overrides the InputStream class methods and passes the request to the InputStream.

The FilterInputStream Class’s read() method filters, reads and forwards the data to the underlying stream filtering, which the Streams carry out.

Syntax

public class FilterInputStream extends Inputstream

Constructor

protected FilterInputStream(InputStream in): assigns the argument to the field this.in to create a FilterInputStream and stores it for later use.

Methods

Method  Description 
int available() It provides a ballpark estimate of the data that can be read from the input stream.
int read() It reads the subsequent data byte from the input stream.
int read(byte[] b) It reads data from the input stream up to byte.length bytes in length.
long skip(long n) It aims to remove and skip n bytes of data from the input stream.
boolean markSupported() It checks whether the input stream supports the mark and reset methods.
void mark(int readlimit) It serves as a marker for the input stream’s current position.
void reset() Resetting the input stream is done with it.
Void close() The input stream is closed using it.

Example

import java.io.*;  
public class TechVidvan{  
    public static void main(String[] args) throws IOException {  
        File data = new File("D:\\testout.txt");  
        FileInputStream  file = new FileInputStream(data);  
        FilterInputStream filter = new BufferedInputStream(file);  
        int k =0;  
        while((k=filter.read())!=-1){  
            System.out.print((char)k);  
        }  
        file.close();  
        filter.close();  
    }  
}

Output
Java programming in TechVidvan

Example

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class TechVidvan {
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("Initial bytes: " +available);
 
 filter.read();
 filter.read();
 filter.read();
 
 int available = filter.available();
 System.out.println("after reading 3bytes: " +available);
 filter.close();  
 }
}

Output
Initial bytes: 25
after reading 3bytes: 22

Example

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class StudyTonight 
{
  public static void main(String[] args) throws IOException 
  { 
    File data = new File("E:\\studytonight\\output.txt");  
    FileInputStream  file = new FileInputStream(data);  
    FilterInputStream filter = new BufferedInputStream(file);  
    int ch =0;  
    while((ch=filter.read())!=-1){  
      System.out.println(filter.available());  
    }  
    file.close();  
    filter.close();   
  }  
}

Output

5
4
3
2
1
0
Java

Conclusion

The superclass of the input operation, the input stream, is used to read user input from a file, keyboard, etc. It is employed to depict a byte-stream input. It skips n bytes of data from the input stream and discards it.