Java FilteroutputStream Class
With versions that pass all requests to the underlying output stream, the FilterOutputStream class simply overrides all methods of OutputStream. Some of these methods can be further manipulated by FilterOutputStream’s subclasses, including providing other methods and fields.
Java FilteroutputStream class Methods
| void write(int b) | The specified byte is written to the output stream. |
| void write(byte[] ary) | Used to write ary.length bytes to the output stream. |
| void write(byte[] b, int off, int len) | Only the bytes allocated from an offset to an output stream are used. |
| void flush() | It is used to flush the outlet stream. |
| void close() | It’s meant to shut down an output current. |
Java FilterOutputStream class Methods
The FilterOutputStream class is abstract in Java and doesn’t provide any methods. Instead, it serves as a base class for various concrete subclasses that extend its functionality by providing specific filtering and modification capabilities.
Here are some of the commonly used methods that can be found in FilterOutputStream and its concrete subclasses:
- write(int b): This method is inherited from the OutputStream class and is commonly overridden by subclasses. It writes a single byte to the output stream.
- write(byte[] b): Another inherited method from OutputStream, this writes an array of bytes to the output stream.
- write(byte[] b, int off, int len): This method writes a portion of the byte array to the output stream, starting from the specified offset and writing a specified number of bytes.
- flush(): This method is used to flush any buffered data to the underlying output stream. It ensures that any data waiting in the buffer is written to the destination.
- close(): The close method closes the output stream and releases any associated resources. It’s important to call this method when you’re done with the stream to ensure proper cleanup.
While the FilterOutputStream class doesn’t have additional methods, its concrete subclasses, like BufferedOutputStream and DataOutputStream, provide specialized methods to perform tasks specific to their filtering or transformation purposes.
Here are some key points about FilterOutputStream:
- Extending Functionality: FilterOutputStream is an abstract class, so it’s not used directly but serves as a base class for various concrete subclasses that provide specific filtering functionality.
- Decorator Pattern: It follows the decorator design pattern, where you can stack multiple decorators on top of an output stream to add various features and behaviors.
- Usage: To use a FilterOutputStream, you typically create an instance of one of its concrete subclasses, pass an existing output stream to its constructor, and then write data to the filtered output stream. This way, the data is processed or modified according to the specific filter provided by the subclass.
Here’s a simple example of using a FilterOutputStream to create a buffered output stream:
import java.io.*;
public class TechVidvan{
public static void main(String[] args) {
try {
FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write("Hello, World!".getBytes());
bufferedOutputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Hello, Welcome to TechVidvan!
Conclusion
In Java, understanding and utilizing FilterOutputStream and its subclasses is crucial for efficient and versatile data handling in various I/O scenarios.
Whether you need to optimize data writes, apply custom transformations, or add specific functionality to your output streams, FilterOutputStream provides the tools to do so in a modular and extensible manner.
