Design Patterns Java™ Workbook

(Michael S) #1
Chapter 27. Decorator

This program uses the ClassLoader class to find the URL (uniform resource locator) of the
demo.doc file. If you set the class path for the ShowGzip class to include the parent
directory of a demo.doc file, the getSystemResource() method will find it. The
ShowGzip class's main() method tacks a .gz extension onto the output file and places it
into the same directory as the input. The program creates a compressed demo.doc.gz file
that any zip utility can decompress.


The OutputStream class has many more subclasses, and you can create your own. The
FilterOutputStream class is designed specifically to simplify the creation of new output
streams. This class provides stub implementations of the OutputStream class methods that
forward their operations to the underlying stream. You can subclass FilterOutputStream
and override just the methods that you need. In this regard, FilterOutputStream is
similar to its counterpart, FilterWriter.


In addition to its support for byte streams, Java includes a somewhat parallel collection of
classes that manage streams of characters. Like the byte stream hierarchies, Java provides
Reader and Writer hierarchies for managing character streams. These hierarchies include
Filter subclasses that, like their byte-oriented counterparts, are designed for subclassing.
The FilterWriter class, as shown in Figure 27.2, has three versions of write()
methods.

Free download pdf