digitization, transformation, binary

Class FilterOutputStream in Java

Class FilterOutputStream in Java

FilterOutputStream is the superclass of all classes that filter output streams. These streams sit on top of an already existing output stream(the underlying output stream) but provide additional functionalities.

Each of the subclasses of FilterOutputStream works by wrapping an existing output stream called the underlying output stream and providing additional functionality. The methods of FilterOutputStream overrides the methods of outputStream with versions that call the corresponding methods of the underlying stream.

The class FilterOutputStream itself overrides all methods of OutputStream with versions that pass all requests to the underlying OutputStream.Subclasses of FilterOutputStream may further override some of these methods as well as provide additional methods and fields.

It must be subclassed to perform some sort of filtering operation and may not be instantiated directly. An instance of one of the subclasses of FilterOutputStream is constructed with another OutputStream object. The methods of a subclass of FilterOutputStream should override some methods in order to extend their behavior or provide some sort of filtering.

The structure of the class FilterOutputStream is given as:

public class java.io.FilterOutputStream extends java.io.OutputStream{
//member element
protected OutputStream out;
//The outputStream out is under consideration.
//constructor
public FilterOutputStream(OutputStream out);
//creates an output stream filter over the specified OutputStream
//methods
public void close() throws IOException;
public void flush() throws IOException;
public void write(byte b[]) throws IOException;
public void write(byte b[],int offSet,int length) throws IOException;
public void write(int b) throws IOException;
}
 

In addition, all methods may throw an IOException if an IO error occurs apart from their regular Exceptions.
The details of the class structure are given as follows:

protected OutputStream out;

protected OutputStream out is the underlying stream that this FilterOutputStream wraps or filters.

public FilterOutputStream(OutputStream out);

public FilterOutputStream(OutputStream out) constructor creates a FilterOutputStream that sends its data to out.
Parameter
out – The output stream to filter.

public void close() throws IOException;

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

public void flush() throws IOException;

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

public void write(byte b[]) throws IOException;

public void write(byte b[]) method writes the bytes contained in the given array. To accomplish this, it calls write(b,0, b.length). Subclasses need override only write(byte[], int, int) for this method to work automatically.
Parameter
b -An array of bytes to write to the stream.

public void write(byte b[],int offSet,int length) throws IOException;

public void write(byte b[],int offSet,int length) method writes len bytes contained in the given array, starting at offset off. It does this by calling write(int) for each element to be written in the array. This is inefficient, so subclasses should override write(byte[], int, int) with a method that efficiently writes a block of data.
This method returns
Parameter
b -An array of bytes to write to the stream.
off-An offset into the byte array.
len -The number of bytes to write.

public void write(int b) throws IOException;

public void write(int b) throws IOException method writes a byte containing the low-order eight bits of the given integer value. The method calls the write(int) method of the underlying stream.
Parameter
b -The value to write.
Apart from these FilterOutputStream 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)
READ  Class StringBuffer in Java

Share and Enjoy !

Leave a Comment

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