Connecting Tech Pros Worldwide Help | Site Map

Filter Streams in Java

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#1: May 18 '09
I checked out the Source code of Filter Streams in Java.
There i saw there is a corresponding underlying Streams and the methods simply being used of underlying Streams'.
One thing is mentioned chaining is there ... can you explain how it's significant?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: May 18 '09

re: Filter Streams in Java


Which classes exactly were you looking at and where is the chaining mentioned?
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: May 18 '09

re: Filter Streams in Java


I am talking about the both Filter Streams (Input and Output). And have a look at this link for chaining ;)
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: May 18 '09

re: Filter Streams in Java


So I don't get your question. Chaining (and its benefits) are already explained on that site.
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#5: May 18 '09

re: Filter Streams in Java


Well.... Then how chaining works? And there is no difference between InputStream and FilterInputStream? Then why the filtering comes in?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: May 18 '09

re: Filter Streams in Java


Quote:

Originally Posted by dmjpro View Post

Well.... Then how chaining works? And there is no difference between InputStream and FilterInputStream? Then why the filtering comes in?

As per the API documentation: the FilterXXXStream classes wrap another XXXStream and do nothing by themselves, i.e. you have to sub-class those classes to make them do anything useful. e.g. the following class doesn't print any vowels:

Expand|Select|Wrap|Line Numbers
  1. public class ConsonantStream extends FilterOutputStream {
  2.  
  3.    public ConsonantStream(OutputStream os) { super(os); }
  4.  
  5.    public void write(int  b) throws IOException {
  6.       if ("aeiouAEIOU".indexOf((char)b) == -1)
  7.          super.write(b);
  8.    }
  9. }
  10.  
According to the documentation all the other write methods use this single argument method so everything ends up at that method for the actual writing.

kind regards,

Jos
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#7: May 18 '09

re: Filter Streams in Java


Well, so add extra functionality i have to make my own ;)
Now what about chaining? How does it come into these streams ?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#8: May 18 '09

re: Filter Streams in Java


Quote:

Originally Posted by dmjpro View Post

Well, so add extra functionality i have to make my own ;)
Now what about chaining? How does it come into these streams ?

The FileOutputStream's methods all call its write(int b) method which, in turn calls the same method from the encapsulate OutputStream. That is what chaining is all about. You wrote you have read the source code of those filter streams, so you should've read how those classes implement it. Also re-read my little example: it is chained between a target OutputStream and the caller of my stream. That entire process can be repeated at will but chaining doesn't "come into streams"; it is just the decorator/wrapper pattern that does it all.

kind regards,

Jos
Reply