Java.io.FilterInputStream Class in Java
Last Updated :
06 Apr, 2023

Filter Streams filters data as they read and write data in the Input Stream, filters it and pass it on to the underlying Streams. Filter Streams are
FilterInputStream : Java.io.FilterInputStream class works almost like InputStream class in Java but what it does is simply overriding the InputStream class methods, passing the request to the InputStream. The read() method of FilterInputStream Class filters the data and read it and passes on the data to the underlying stream filtering which is done depending on the Streams.
Declaration:
public class FilterInputStream
extends InputStream
Constructors :
- protected FilterInputStream(InputStream in): Creates a FilterInputStream by assigning the argument in to the field this.in so as to remember it for later use.
Methods:
HelloGeeks
In the given code buffer.length = 6 , So only HelloG will b read by read(byte[] buffer) method
Output :
At position 1 : H
At position 2 : e
At position 3 : l
At position 4 : l
At position 5 : o
At position 6 : G
read(byte[] buffer, int offset, int maxlen) : java.io.FilterInputStream.read(byte[] buffer, int offset, int maxlen) reads upto maxlen of data from the FilterInputStream into a buffer.
Syntax :
public int read(byte[] buffer, int offset, int maxlen)
Parameters :
buffer : Destination buffer
offset : start position to read
maxlen : max. length of bytes to be read
Return :
total no. of bytes to be written else, -1 i.e. when end of Stream is reached.
Exception :
-> IOException : If I/O error occurs.
Implementation :
Java
// Java program illustrating the working of
// read(byte[] buffer, int offset, int maxlen) method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// LineNumberInputStream & FileInputStream initailly null
FilterInputStream geek_input = null;
InputStream geek = null;
try{
char c;
int a;
byte[] buffer = new byte[4];
// New InputStream : 'ABC' is created
geek = new FileInputStream("ABC.txt");
geek_input = new BufferedInputStream(geek);
// Offset = 1(*), Maxlen = 3 (MOH)
a = geek.read(buffer, 1, 3);
// read() method returning Bytes of Input Stream as integer
// '-1' indicating to read till end Of Input Stream
for(byte g : buffer)
{
// Since read() method returns Integer value
// So, we convert each integer value to char
c = (char)g;
if(g == 0)
c = '*';
System.out.print(c);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of Input Stream is reached
if(geek != null)
geek.close();
if(geek_input != null)
geek_input.close();
}
}
}
Note :
The following Java Code won’t run here as we can’t access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
MOHIT
Offset = 1 i.e. * and Maxlen = 3 i.e. MOH
Output :
*MOH
available() : java.io.FilterInputStream.available() returns the no. of bytes that can be read from the Input Stream.
Syntax :
public int available()
Parameters :
-------
Return :
returns the no. of bytes that can be read from the FilterInputStream.
Exception:
IOException : in case I/O error occurs
Implementation :
Java
// Java program illustrating the working of available() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
// FilterInputStream & FileInputStream initailly null
FilterInputStream geek_input = null;
InputStream geek = null;
try{
char c;
int a, b;
// New InputStream : 'ABC' is created
geek = new FileInputStream("ABC.txt");
geek_input = new BufferedInputStream(geek);
while((a = geek_input.read()) != -1)
{
// So, we convert each integer value to char
c = (char)a;
// Use of available method : return no. of bytes that can be read
a = geek_input.available();
System.out.println(c + " Bytes available : " + a);
}
}
catch(Exception e)
{
// In case of error
e.printStackTrace();
System.out.println("ERROR Occurs ");
}
finally
{
// Closing the streams, Once the End of FilterInputStream is reached
if(geek != null)
geek.close();
if(geek_input != null)
geek_input.close();
}
}
}
Note :
The following Java Code won’t run here as we can’t access any file on online IDE.
So, copy the program to your system and run it there.
The ABC.txt file used in the program contains :
MOHIT
Output :
M Bytes available : 4
O Bytes available : 3
H Bytes available : 2
I Bytes available : 1
T Bytes available : 0
read() : java.io.FilterInputStream.read() reads next byte of data from the Filter Input Stream. The value byte is returned in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
Syntax :
public int read()
Parameters :
------
Return :
Reads next data else, -1 i.e. when end of Stream is reached.
Exception :
-> IOException : If I/O error occurs.
close() : java.io.FilterInputStream.close() closes the Filter Input Stream and releases system resources associated with this stream to Garbage Collector.
Syntax :
public void close()
Parameters :
------
Return :
void
Exception :
-> IOException : If I/O error occurs.
mark(int arg) : java.io.FilterInputStream.mark(int arg) marks the current position of the FilterInputStream. It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid.
Syntax :
public void mark(int arg)
Parameters :
arg : integer specifying the read limit of the input Stream
Return :
void
skip() : java.io.FilterInputStream.skip(long arg) skips and discards 'arg' bytes from FilterInputStream data.
Syntax :
public long skip(long arg)
Parameters :
arg : no. of bytes of FilterInputStream data to skip.
Return :
no. of bytes to be skipped
Exception:
IOException : in case I/O error occurs
reset() : java.io.FilterInputStream.reset() is invoked by mark() method. It repositions the FilterInputStream to the marked position.
Syntax :
public void reset()
Parameters :
----
Return :
void
Exception :
-> IOException : If I/O error occurs.
markSupported() : java.io.FilterInputStream.markSupported() method tests if this input stream supports the mark and reset methods. The markSupported method of InputStream returns false by default.
Syntax :
public boolean markSupported()
Parameters :
-------
Return :
true if input stream supports the mark() and reset() method else,false
Java Program explaining : markSupported(), close(), reset(), mark(), read(), skip() methods
Java
// Java program illustrating the working of FilterInputStream method
// mark(), read(), skip()
// markSupported(), close(), reset()
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws Exception
{
InputStream geek = null;
// FilterInputStream initialised to null here
FilterInputStream geek_input = null;
try {
geek = new FileInputStream("GEEKS.txt");
geek_input = new BufferedInputStream(geek);
// read() method : reading and printing Characters
// one by one
System.out.println("Char : " + (char)geek_input.read());
System.out.println("Char : " + (char)geek_input.read());
System.out.println("Char : " + (char)geek_input.read());
// mark() : read limiing the 'geek' input stream
geek_input.mark(0);
// skip() : it results in redaing of 'e' in G'e'eeks
geek_input.skip(1);
System.out.println("skip() method comes to play");
System.out.println("mark() method comes to play");
System.out.println("Char : " + (char)geek_input.read());
System.out.println("Char : " + (char)geek_input.read());
System.out.println("Char : " + (char)geek_input.read());
boolean check = geek_input.markSupported();
if (geek_input.markSupported())
{
// reset() method : repositioning the stream to
// marked positions.
geek_input.reset();
System.out.println("reset() invoked");
System.out.println("Char : " + (char)geek_input.read());
System.out.println("Char : " + (char)geek_input.read());
}
else
System.out.println("reset() method not supported.");
System.out.println("geek_input.markSupported() supported"
+ " reset() : " + check);
}
catch(Exception excpt)
{
// in case of I/O error
excpt.printStackTrace();
}
finally
{
// releasing the resources back to the
// GarbageCollector when closes
if (geek != null)
{ // Use of close() : closing the file
// and releasing resources
geek.close();
}
if(geek_input != null)
geek_input.close();
}
}
}
Note :
This code won’t run on online IDE as no such file is present here.
You can run this code on your System to check the working.
GEEKS.txt file used in the code has
HelloGeeks
Output :
Char : H
Char : e
Char : l
skip() method comes to play
mark() method comes to play
Char : o
Char : G
Char : e
reset() invoked
Char : l
Char : o
geek_input.markSupported() supported reset() : true
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read