Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java FilterInputStream Class

In this tutorial, we will learn about FilterInputStream class in Java. This class overrides all methods of InputStream class with versions that pass all requests to the contained input stream.

Syntax

This is the syntax of FilterInputStream class and we can see it is extending the InputStream class.

public class FilterInputStream extends InputStream  

Java FilterInputStream class Methods

In the table methods of FilterInputStream class is given.

Method Description
int available() It is used to return an estimated number of bytes that can be read from the input stream.
int read() It is used to read the next byte of data from the input stream.
int read(byte[] b) It is used to read up to byte.length bytes of data from the input stream.
long skip(long n) It is used to skip over and discard n bytes of data from the input stream.
boolean markSupported() It is used to test if the input stream supports mark and reset method.
void mark(int readlimit) It is used to mark the current position in the input stream.
void reset() It is used to reset the input stream.
void close() It is used to close the input stream.

Example of FilterInputStream class Methods

In the following example, we are reading data from the output.txt file using read() method of FilterInputStream class.

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.print((char)ch);  
		}  
		file.close();  
		filter.close();   
	}  
}


Hello Studytonight

output.txt


Hello Studytonight

Example of available() in FilterInputStream class

In the example given we are using available() method to check how many bytes are available to read in the file. In the output of this program, we can see initially we have a total of 17 bytes were available to read and gradually these are decreasing as we read one by one.

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();   
	}  
}


17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0

output.txt


Hello Studytonight

Conclusion:

In this tutorial, we learned about FilterInputStream class and its various methods. All the input stream classes that pass the filtered data are the subclasses of FilterInputStream class.