Class FilterReader in Java

digitization, transformation, binary

Class FilterReader in Java

FilterReader is an abstract class that is intended to act as a superclass for character input streams that read data from some other character input stream, filter it in some way, and then return the filtered data when their own read() methods are called.

A subclass of FilterReader works by wrapping an existing reader, called the underlying reader, and providing additional functionality. The methods of FilterReader override the methods of Reader with versions that call the corresponding methods of the underlying reader.

Ads code goes here

FilterReader is declared abstract so that it cannot be instantiated. But none of its methods are themselves abstract: they all simply call the requested operation on the input stream passed to the FilterReader() constructor.

If we are allowed to instantiate a FilterReader, we will find that it is a “null filter”–i.e., it simply reads characters from the specified input stream and returns them without filtering of any kind.

Because FilterReader implements a “null filter,” it is an ideal superclass for classes that want to implement simple filters, but do not want to override all of the methods of Reader. In order to create your own filtered character input stream, you should subclass FilterReader and override both of its read() methods to perform the desired filtering operation.

Note We can implement one of the read() methods in terms of the other, and thus only implement the filtration once. Recall that the other read() methods defined by Reader are implemented in terms of these methods, so we do not need to override those.

READ  Learn How to Use Jacoco Java code Coverage Effectively in 30 Minutes

In some cases, we may also need to override other methods of FilterReader as well, and we may want to provide methods or constructors that are specific to our subclass.

The structure of the class FilterReader is given by

public abstract class java.io.FilterReader extends java.io.Reader{
// Protected Constructor
protected FilterReader(Reader in);
// Protected Instance Variables
protected Reader in;
// Public Instance Methods
public void close() throws IOException; // Defines Reader
public void mark(int readAheadLimit) throws IOException; // Overrides Reader
public boolean markSupported(); // Overrides Reader
public int read() throws IOException; // Overrides Reader
public int read(char[] cbuf, int off, int len) throws IOException; //Defines Reader
public boolean ready() throws IOException; // Overrides Reader
public void reset() throws IOException; // Overrides Reader
public long skip(long n) throws IOException; // Overrides Reader
}

The details description of the class structure is given as:

protected Reader in;

protected Reader in is the underlying reader that this FilterReader wraps or filters.

protected FilterReader(Reader in);

protected FilterReader(Reader in) constructor creates a FilterReader that gets data from in.
Parameter
in – The input reader to filter.

public void close();

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

public void mark(int readAheadLimit);

public void mark(int readAheadLimit) method calls the mark() method of the underlying reader. If the underlying reader supports mark() and reset(), this method causes the FilterReader 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
readAheadLimit – The maximum number of characters that can be read before the saved position becomes invalid.

public boolean markSupported();

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

public int read();

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

READ  Literals in Java Simplified

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

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

public int read(char[] cbuf, int off, int len);

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

The method blocks until some data is available.

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

Parameter
cbuf – An array of characters to be filled from the stream.
off – An offset into the array.
len – The number of characters to read.

public boolean ready();

public boolean ready() method calls the ready() method of the underlying reader and returns the result. If the underlying stream is ready, this method returns true.

The underlying stream is ready if the next read() is guaranteed not to block.

This method returns a boolean value that indicates whether the reader is ready to be read.

public void reset();

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

Subsequent characters read from this FilteredReader will begin from the saved position and continue normally.

public long skip(long n);

public long skip(long n) method skips n characters of input. It calls the skip() method of the underlying reader.

READ  Class Math in Java

This method returns the actual number of characters skipped.

Parameter
n – The number of characters to skip.
Apart from these FilterReader 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)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *