I’ve been reading on InputStream, FileInputStream, ByteArrayInputStream and how their use seems quite clear (output streams too).
What I’m struggling is to understand the use of FilterInputStream & FilterOutputStream:
A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality.
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream. DataInputStream is not necessarily safe for multithreaded access.
Most filter streams provided by the java.io package are subclasses of FilterInputStream and FilterOutputStream and are listed here: DataInputStream and DataOutputStream. BufferedInputStream and BufferedOutputStream. LineNumberInputStream.
io. FilterOutputStream class is the superclass of all those classes which filters output streams. The write() method of FilterOutputStream Class filters the data and write it to the underlying stream, filtering which is done depending on the Streams.
FilterInputStream
is an example of the the Decorator pattern.
This class must be extended, since its constructor is protected
. The derived class would add additional capabilities, but still expose the basic interface of an InputStream
.
For example, a BufferedInputStream
provides buffering of an underlying input stream to make reading data faster, and a DigestInputStream
computes a cryptographic hash of data as it's consumed.
You would use this to add functionality to existing code that depends on the InputStream
or OutputStream
API. For example, suppose that you use some library that saves data to an OutputStream
. The data are growing too large, so you want to add compression. Instead of modifying the data persistence library, you can modify your application so that it "decorates" the stream that it currently creates with a ZipOutputStream
. The library will use the stream just as it used the old version that lacked compression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With