【正文】
the channel is the mine containing the seam of coal (the data), and the buffer is the cart that you send into the mine. The cart es back full of coal, and you get the coal from the cart. That is, you don’t interact directly with the channel。 they exist so that other classes can use them— these other classes provide a more useful interface. Thus, you’ll rarely create your stream object by using a single class, but instead will layer multiple objects together to provide your desired functionality (this is the Decorator design pattern, as you shall see in this section). The fact that you create more than one object to produce a single stream is the primary reason that Java’s I/O library is confusing. It’s helpful to categorize the classes by their functionality. In Java , the library designers started by deciding that all classes that had anything to do with input would be inherited from InputStream, and all classes that were associated with output would be inherited from OutputStream. Type of InputStream InputStream’s job is to represent classes that produce input from different sources. These sources can be: 1. An array of bytes. 2. A String obj ect. 3. A file. 4. A pipe, which works like a physical pipe: You put things in at one end and they e out the other. 5. A sequence of other streams, so you can collect them together into a single stream. 中原工學院信息商務學院畢業(yè)設(shè)計(論文)譯文專用紙 10 6. Other sources, such as an Inter connection. Each of these has an associated subclass of InputStream. In addition, the FilterInputStream is also a type of InputStream, to provide a base class for decorator classes that attach attributes or useful interfaces to input streams. Types of OutputStream This category includes the classes that decide where your output will go: an array of bytes (but not a String— presumably, you can create one using the array of bytes), a file, or a pipe. In addition, the FilterOutputStream provides a base class for decorator classes that attach attributes or useful interfaces to output streams. This is discussed later. Adding attributes and useful interfaces Decorators were introduced in the Generics chapter, on page 717. The Java I/O library requires many different binations of features, and this is the justification for using the Decorator design The reason for the existence of the filter classes in the Java I/O library is that the abstract filter class is the base class for all the decorators. A decorator must have the same interface as the object it decorates, but the decorator can also extend the interface, which occurs in several of the filter classes. There is a drawback to Decorator, however. Decorators give you much more flexibility while you’re writing a program (since you can easily mix and match attributes), but they add plexity to your code. The reason that the Java I/O library is awkward to use is that you must create many classes— the core I/O type plus all the decorators— in order to get the single I/O object that you want. The classes that provide the decorator interface to control a particular InputStream or OutputStream are the FilterlnputStream and FilterOutputStre