Spread the love

Class FilterInputStream in Java

Spread the love

FilterInputStream in Java:

FilterInputStream is the superclass of all classes that filter input streams. These streams sit on top of an already existing input stream(the underlying input stream) but provide additional functionalities. It is an abstract class provides an interface for useful functionalities to other InputStream classes.

Each of the subclasses of FilterInputStream works by wrapping an existing input stream called the underlying input stream and providing additional functionality. The methods of
FilterInputStream simply override the methods of InputStream with versions that call the
corresponding methods of the underlying stream.

It must be subclassed to perform some sort of filtering operation, and may not be instantiated directly.

It has the following classifications:

  1. DataInputStream
  2. BufferedInputStream
  3. LineNumberInputStream
  4. PushbackInputStream

FilterInputStream cannot be instantiated directly; it must be subclassed. An instance of one of the subclasses of FilterInputStream is constructed with another InputStream object. The methods of a subclass of FilterInputStream should override some methods in order to extend their behavior or provide some sort of filtering.

The class structure of the class FilterInputStream is given below:

public class java.io.FilterInputStream extends java.io.InputStream{
//member element
protected InputStream in;//This input stream is under consideration
//constructor
protected FilterInputStream(InputStream in);//creates an object of the FilterInputStream on top of the input stream specified.
//Methods
public int available() throws IOException;
public int read() throws IOException;
public int read(byte b[]) throws IOException;
public int read(byte b[],int offSet,int length) throws IOException;
public void close() throws IOException;
public synchronized void mark(int readLimit);
public boolean markSupported();
public synchronized void reset() throws IOException;
public long skip(long l) throws IOException;
}
 

In addition,all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.

READ  Class PropertyResourceBundle in Java

The details of the class structure are given as follows:

protected InputStream in;

protected InputStream in is the underlying stream that this FilterInputStream wraps or filters.

protected FilterInputStream(InputStream in);

protected FilterInputStream(InputStream in) constructor creates a FilterInputStream that gets its data from in.
The parameter
in – The input stream to filter.

public int available();

public int available() method calls the available() method of the underlying stream and returns the result.

READ  Class PropertyResourceBundle in Java

This method returns the number of bytes that can be read without blocking.

public void close();

public void close() method calls the close() method of the underlying stream, which releases any system resources associated with this object.

public synchronized void mark(int readlimit);

public synchronized void mark(int readlimit) method calls the mark() method of the underlying stream. If the underlying stream supports mark() and reset(), this method causes the FilterInputStream to remember its current position.

A subsequent call to reset() causes the object to return to that saved position, and thus re-read a portion of the input.
Parameter
readlimit -The maximum number of bytes that can be read before the saved position becomes invalid.

public boolean markSupported();

public boolean markSupported() method calls the markSupported() method of the underlying stream and returns the result.
This method returns true if this stream supports mark() and reset(); false otherwise.

public int read();

public int read() method calls the read() method of the underlying stream and returns the result.

This method blocks until some data are available, the end of the stream is detected, or an exception is thrown.

READ  Literals in Java Simplified

This method returns the next byte of data or -1 if the end of the stream is encountered.

public int read(byte[] b);

public int read(byte[] b) method reads bytes of input to fill the given array. It does this by calling read(b, 0,b.length), which allows subclasses to only override read(byte[], int, int) and have read(byte[]) work automatically.

READ  How to Create A Basic Calculator In Java Applet In Advanced Way

The method blocks until some data is available.

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.
Parameter
b – An array of bytes to be filled from the stream.

public int read(byte[] b, int off, int len);

public int read(byte[] b, int off, int len) method reads up to len bytes of input into the given array starting at index off. It does this by calling the read(byte[], int, int) method of the underlying stream and returning the result.

The method blocks until some data is available.

This method returns the actual number of bytes read or -1 if the end of the stream is encountered immediately.

Parameter
b -An array of bytes to be filled from the stream.
off -An offset into the byte array.
len -The number of bytes to read.

public synchronized void reset();

public synchronized void reset() method calls the reset() method of the underlying stream. If the underlying stream supports mark() and reset(), this method sets the position of the FilterInputStream to a position that was saved by a previous call to mark().

Subsequent bytes read from this FilterInputStream will begin from the saved position and continue normally.

public long skip(long n);

public long skip(long n) method skips n bytes of input. It calls the skip() method of the underlying stream.
This method returns the actual number of bytes skipped.
Parameter
n -The number of bytes to skip.

READ  Polymorphism Concept Simplified in Java

Apart from these FilterInputStream class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)




Spread the love
READ  Class DeflaterOutputStream in Java
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special keyword called...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love