Java Reference
In-Depth Information
We can use our uppercase convertor as follows:
public static void main(String[] args)
throws IOException
{
StringReader src = new StringReader(args[0]);
FilterReader f = new UppercaseConvertor(src);
int c;
while ((c = f.read()) != -1)
System.out.print((char)c);
System.out.println();
}
We use a string as our data source by using a
StringReader
(see
Section
20.5.7
on page
523
). The
StringReader
is then wrapped by our
Uppercase-
Convertor
. Reading from the filtered stream converts all the characters
from the string stream into uppercase. For the input
"nolowercase"
we
get the output:
NO LOWERCASE
You can chain any number of
Filter
byte or character streams. The ori-
ginal source of input can be a stream that is not a
Filter
stream. You
can use an
InputStreamReader
to convert a byte input stream to a charac-
ter input stream.
Filter
output streams can be chained similarly so that data written to
one stream will filter and write data to the next output stream. All the
streams, from the first to the next-to-last, must be
Filter
output stream
objects, but the last stream can be any kind of output stream. You can
use an
OutputStreamWriter
to convert a character output stream to a byte
output stream.
Not all classes that are
Filter
streams actually alter the data. Some
classes are behavioral filters, such as the buffered streams you'll learn