AdityaTS
AdityaTS

Reputation: 89

Can I wrap a FilterOutputStream with another FilterOutputStream?

I am looking to use two different filters on an output stream. Assume following are the filters.

public class FilterStream1 extends FilterOutputStream {
}

public class FilterStream2 extends FilterOutputStream {
}

I have an OutputStream out which is currently wrapped using FilterStream1 as below

return new FilterStream1(out);

Can I modify this to do the following with the addition of the second filter output stream?

OutputStream intermediateStream = new FilterStream2(out);
return new FilterStream1(intermediateStream);

Is there anything I should keep in mind in this case?

Upvotes: 0

Views: 41

Answers (1)

mtj
mtj

Reputation: 3554

Yes, putting filter streams inside other filter streams is perfectly legal. No problems on the horizon.

In fact, this is exactly the use-case for the decorator pattern which has been implemented in the stream api. (See https://en.wikipedia.org/wiki/Decorator_pattern for reference.)

Upvotes: 1

Related Questions