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

- FilterInputStream
- FilterOutput Stream
public class FilterInputStream extends InputStreamConstructors :
- 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.
- read(byte[] buffer) : java.io.FilterInputStream.read(byte[] buffer) reads number of bytes of buffer.length from the Filter Input Stream to the buffer array.
Syntax :
public int read(byte[] buffer) Parameters : buffer : buffer to be read Return : reads number of bytes of buffer.length to the buffer else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs.
Implementation :
// Java program illustrating the working of read(byte[] buffer) 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[6];
// New InputStream : 'GEEKS' is created
geek = new FileInputStream("GEEKS.txt");
geek_input = new BufferedInputStream(geek);
a = geek.read(buffer);
// read() method returning Bytes of Input Stream as integer
// '-1' indicating to read till end Of Input Stream
int length = 1 ;
for(byte g : buffer)
{
// Since read() method returns Integer value
// So, we convert each integer value to char
c = (char)g;
System.out.println("At position " + length + " : " + c);
length++;
}
}
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();
}
}
}
HelloGeeksIn 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
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.
// 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();
}
}
}
MOHITOffset = 1 i.e. * and Maxlen = 3 i.e. MOH Output :
*MOH
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
// 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();
}
}
}
MOHITOutput :
M Bytes available : 4 O Bytes available : 3 H Bytes available : 2 I Bytes available : 1 T Bytes available : 0
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.
public void close() Parameters : ------ Return : void Exception : -> IOException : If I/O error occurs.
public void mark(int arg) Parameters : arg : integer specifying the read limit of the input Stream Return : void
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
public void reset() Parameters : ---- Return : void Exception : -> IOException : If I/O error occurs.
public boolean markSupported() Parameters : ------- Return : true if input stream supports the mark() and reset() method else,false
// 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();
}
}
}
HelloGeeksOutput :
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