Filter Streams in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Filter Streams in Java

    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?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Which classes exactly were you looking at and where is the chaining mentioned?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      I am talking about the both Filter Streams (Input and Output). And have a look at this link for chaining ;)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        So I don't get your question. Chaining (and its benefits) are already explained on that site.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

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

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by dmjpro
            Well.... Then how chaining works? And there is no difference between InputStream and FilterInputStre am? 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:

            Code:
            public class ConsonantStream extends FilterOutputStream {
            
               public ConsonantStream(OutputStream os) { super(os); }
            
               public void write(int  b) throws IOException {
                  if ("aeiouAEIOU".indexOf((char)b) == -1)
                     super.write(b);
               }
            }
            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

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

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

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by dmjpro
                Well, so add extra functionality i have to make my own ;)
                Now what about chaining? How does it come into these streams ?
                The FileOutputStrea m'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

                Comment

                Working...