Java Tutorial - Java Script : Filtering a Stream

Java Tutorial - Java Script :

Filtering a Stream

The simplest way to use a stream is to create it and then call its methods to send or receive data, depending on whether it’s an output stream or an input stream. Many of the classes you will work with today achieve more sophisticated results when a filter is associated with a stream before reading or writing any data. A filter is a type of stream that modifies the way an existing stream is handled. Think of a dam on a mountain stream. The dam regulates the flow of water from the points upstream to the points downstream. The dam is a type of filter—remove it, and the water would flow in a less-controlled fashion. The procedure for using a filter on a stream is as follows:
1. Create a stream associated with a data source or a data destination.
2. Associate a filter with that stream.
3. Read or write data from the filter rather than the original stream.
The methods you call on a filter are the same as the methods you would call on a stream. There are read() and write() methods, just as there would be on an unfiltered stream. You can even associate a filter with another filter, so the following path for information is possible: an input stream associated with a text file is filtered through a Spanish-to- English translation filter, which is then filtered through a no-profanity filter, and is finally sent to its destination—a human being who wants to read it. If this is confusing in the abstract, you will have opportunities to see the process in practice in the following sections.