Abstract Class FilterWriter in Java

Abstract Class FilterWriter in Java

This abstract class is intended to act as a superclass for character output streams that filter the data written to them before writing it to some other character output stream.

FilterWriter is declared abstract so that it cannot be instantiated. But none of its methods are themselves abstract: they all simply invoke the corresponding method on the output stream that was passed to the FilterWriter constructor.

Ads code goes here

A subclass of FilterWriter works by wrapping an existing writer, called the underlying writer, and providing additional functionality. The methods of FilterWriter simply override the methods of Writer with versions that call the corresponding methods of the underlying writer.

If we were allowed to instantiate a FilterWriter object, we would find that it acts as a “null filter”–that it simply passes the characters written to it along, without any filtration.

Because FilterWriter implements a “null filter,” it is an ideal superclass for classes that want to implement simple filters without having to override all of the methods of Writer. In order to create our own filtered character output stream, we should subclass FilterWriter and override all of its write() methods to perform the desired filtering operation.
Note We can implement two of the write() methods in terms of the third, and thus only implement our filtering algorithm once. In some cases, we may want to override other Writer methods as well, and you may often want to add other methods or constructors that are specific to our subclass.

READ  Class OutputStreamWriter in Java

The structure of the Abstract Class FilterWriter is given by

public abstract class java.io.FilterWriter extends java.io.Writer{
// Protected Constructor
protected FilterWriter(Writer out);
// Protected Instance Variables
protected Writer out;
// Public Instance Methods
public void close() throws IOException; // Defines Writer
public void flush() throws IOException; // Defines Writer
public void write(int c) throws IOException; // Overrides Writer
public void write(char[] cbuf, int off, int len) throws IOException; //Defines Writer
public void write(String str, int off, int len) throws IOException; //Overrides Writer
}

The details of the class structure are given as:

protected Writer out;

protected Writer out is the underlying writer that this FilterWriter wraps or filters.

protected FilterWriter(Writer out);

protected FilterWriter(Writer out) constructor creates a FilterWriter that sends data to out.
Parameter
out – The output writer to filter.

public void close();

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

public void flush();

public void flush() method calls the flush() method of the underlying writer, which forces any characters that may be buffered by this FilterWriter to be written to the underlying device.

public void write(int c);

public void write(int c) method writes a character containing the low-order 16 bits of the given integer value. It calls the write(int) method of the underlying writer.
Parameter
c – The value to write.

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

public void write(char[] cbuf, int off, int len) method writes len characters contained in the given array, starting at offset off. It does this by calling the write(char[], int, int) method of the underlying writer.

READ  Class UnsupportedEncodingException in Java

Parameter
cbuf– An array of characters to write to the stream.
off -An offset into the array.
len -The number of characters to write.

public void write(String str, int off, int len);

public void write(String str, int off, int len) method writes len characters contained in the given string, starting at offset off. It does this by calling the write(String, int, int) method of the underlying writer.

Parameter
str -A string to write to the stream.
off -An offset into the string.
len -The number of characters to write.
Apart from these FilterWriter 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 *