Java.io - SPLessons

Java.io FilterInputStream

Home > Lesson > Chapter 25
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io FilterInputStream

Java.io FilterInputStream

shape Introduction

The Java.io FilterInputStream is the class used to read the bytes which are filtered. The Java.io FilterInputStream is subclass to the input stream class and overrides all the methods from the input stream class. In filter input stream class also have its default methods and have the additional functionality called filter.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io FilterInputStream class is declared below: Public class FilterInputStream extends InputStream

shape Fields

Java.io FilterInputStream class fields are:
  • Protected InputStream in : The field represents the current stream have to filter.

Class Constructors

shape Table

Constructor Description
Protected FilterInputStream( InputStream in) The function of this constructor is to create the filer input stream instance and used to read from input stream.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
Boolean markSupported () The function of this method is to check the represented mark method is supported by this stream or not.
int read(byte[] b, int off, int n) By using this method it reads the data up to ‘n’ bytes from the input stream to an array  of the bytes.
Long skip (long m) From the input stream this method skips over and throw out ‘n’ bytes of data.
int read() The function of this method is to read  the data in bytes.

Inherited Methods

shape Description

From the following classes, methods are inherited to the filter input stream class.
  • Java.io.object
  • Java.io.InputStream

shape Examples

Usage of void mark(int readlimit) method. [c]import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; public class FilterInputStream { public static void main(String[] args) throws Exception { InputStream is = null; FilterInputStream fis = null; try{ // Creating a new input streams is = new FileInputStream("D://test.txt"); fis = new BufferedInputStream(is); // It reads and prints BufferedReader System.out.println((char)fis.read()); System.out.println((char)fis.read()); // To mark invoked at this position fis.mark(0); System.out.println("mark() method accessed"); System.out.println((char)fis.read()); System.out.println((char)fis.read()); // To the mark reset() method repositioned the stream fis.reset(); System.out.println("reset() method accessed"); System.out.println((char)fis.read()); System.out.println((char)fis.read()); }catch(IOException e){ e.printStackTrace(); }finally{ // releases all the system resources associated with this stream if(is!=null) is.close(); if(fis!=null) fis.close(); } } }[/c] Output The result will be as follows. [c] S P mark() method accessed L E reset() method accessed L E[/c] Usage of Boolean markSupported () method. [c]import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; public class FilterInputStream { public static void main(String[] args) throws Exception { InputStream is = null; FilterInputStream fis = null; boolean bl = false; try{ // creating a new input streams is = new FileInputStream("D://test.txt"); fis = new BufferedInputStream(is); // tests if the input stream supports mark() and reset() bl = fis.markSupported(); // prints System.out.print(" mark and reset methods are Supported: "+bl); }catch(IOException e){ // if any I/O error occurs e.printStackTrace(); }finally{ // releases all the system resources associated with the stream if(is!=null) is.close(); if(fis!=null) fis.close(); } } }[/c] Output The result will be as follows. [c] mark and reset methods are Supported: true[/c]

Summary

shape Key Points

  • The filter input stream is the class used to read the bytes which are filtered.
  • In filter input stream class also have its default methods and have the additional functionality called filter.