M. Carlo Bramini
M. Carlo Bramini

Reputation: 2522

Java: FilterInputStream what are the advantages and use compared to other streams

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:

Upvotes: 24

Views: 16746

Answers (3)

George Aristy
George Aristy

Reputation: 1481

FilterInputStream and FilterOutputStream are there to ease the job of developers who wish to implement their own input/output streams. Implementations such as BufferedInputStream may add their own decorations around the basic InputStream API while delegating on the super class - FilteredInputStream in this case - the methods they don't need to override.

Neither FilterInputStream nor FilterOutputStream are designed for end users to use directly.

Upvotes: 2

erickson
erickson

Reputation: 269677

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.

Upvotes: 28

Lee Meador
Lee Meador

Reputation: 12985

You use them when you want to decorate the stream of data.

Remember that these stream class instances wrap themselves around another stream instance (whether another subclass of one of these or not) and add some feature, add some processing, make some changes to the data as it passes through.

For example, you might want to remove all the multiple spaces from some stream. You make your own subclass of FilterInputStream and override the read() method. I'm not going to bother all the details but here's some sorta-java for the method in the subclass:

private boolean lastWasBlank = false;
public int read() {
    int chr = super.read();
    if (chr == ' ') {
        if (lastWasBlank) {
            return read();
        } else {
            lastWasBlank = true;
        }
    } else {
        lastWasBlank = false;
    }
    return chr;
}

In real life, you would probably mess with the other two read() methods too.

Other uses:

  • Log everything flowing through the stream
  • Duplicate the 'tee' utility so the stream being read is handled two ways.
  • Convert line endings between Windows, Mac and Unix/Linux formats
  • Add delays to simulate slow transmission methods like modems or serial ports or wireless network connections.

Upvotes: 4

Related Questions