VOOZH about

URL: https://www.geeksforgeeks.org/java/java-io-filterinputstream-class-in-java/

⇱ Java.io.FilterInputStream Class in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java.io.FilterInputStream Class in Java

Last Updated : 2 May, 2026

FilterInputStream in Java is a class from the java.io package used to read data from an underlying input stream while optionally modifying or enhancing it.
It acts as a wrapper around another InputStream, delegating basic operations and allowing additional functionality.

  • It follows the Decorator Design Pattern, enabling features like buffering or data transformation by wrapping streams (e.g., BufferedInputStream).
  • Most methods simply delegate calls to the underlying stream, which can be overridden by subclasses to customize behavior.

Filter Streams

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 Class

The FilterInputStream class works similarly to InputStream, but it wraps another input stream and forwards method calls to it. It can also filter or process data before passing it to the program.

Declaration:

public class FilterInputStream extends InputStream

Constructors:

protected FilterInputStream(InputStream in)

Creates a FilterInputStream by assigning the given InputStream to an internal variable (this.in) for later use.

Important Method

1. read(byte[] buffer): Read number of bytes of buffer.length from the Filter Input Stream to the buffer array.

Syntax:

public int read(byte[] buffer) throws IOException

  • Parameters: buffer -> Array where data will be stored.
  • Return Value: Returns -1 if the end of the stream is reached.
  • Exception: IOException -> If an input/output error occurs

Implementation:

Note: This program may not run on online IDEs due to file access restrictions.

GEEKS.txt

file used in the program contains:

HelloGeeks

Output:

H e l l o G

2. 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 length) throws IOException

Parameters:

  • buffer -> Destination array
  • offset -> Starting index
  • length -> Maximum bytes to read

Return Value:

  • Number of bytes read
  • Returns -1 if end of stream is reached

Implementatio:

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

3. 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 :

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

4. 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.

5. 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.

6. 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

7. skip(): 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

8. reset() : 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.

9. markSupported(): 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

Example: markSupported(), close(), reset(), mark(), read(), skip() methods

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

Comment
Article Tags:
Article Tags: