|Bruce Eckel's Thinking in Java||Contents | Prev | Next|
The Java library classes for IO are divided by input and output, as you can see by looking at the online Java class hierarchy with your Web browser. By inheritance, all classes derived from InputStream have basic methods called read( ) for reading a single byte or array of bytes. Likewise, all classes derived from OutputStream have basic methods called write( ) for writing a single byte or array of bytes. However, you won’t generally use these methods; they exist so more sophisticated classes can use them as they 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. The fact that you create more than one object to create a single resulting stream is the primary reason that Java’s stream library is confusing.
It’s helpful to categorize the classes by their functionality. 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.
Types of InputStream
InputStream’s job is to represent classes that produce input from different sources. These sources can be (and each has an associated subclass of InputStream):
- An array of bytes
- A String object
- A file
- A “pipe,” which works like a physical pipe: you put things in one end and they come out the other
- A sequence of other streams, so you can collect them together into a single stream
- Other sources, such as an Internet connection. (This will be discussed in a later chapter.)
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. This is discussed later.
Table 10-1. Types of InputStream
Types of OutputStream
This category includes the classes that decide where your output will go: an array of bytes (no String, however; 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.
Table 10-2. Types of OutputStream