Reading from an InputStream
with FilterInputStream
The FilterInputStream classes accomplish two significantly different things. DataInputStream allows you to read different types of primitive data as well as String objects. (All the methods start with “read,” such as readByte( ), readFloat( ), etc.) This, along with its companion DataOutputStream, allows you to move primitive data from one place to another via a stream. These “places” are determined by the classes in Table 12-1.
The remaining classes modify the way an InputStream behaves internally: whether it’s buffered or unbuffered, if it keeps track of the lines it’s reading (allowing you to ask for line numbers or set the line number), and whether you can push back a single character. The last two classes look a lot like support for building a compiler (that is, they were probably added to support the construction of the Java compiler), so you probably won’t use them in general programming.
You’ll need to buffer your input almost every time, regardless of the I/O device you’re connecting to, so it would have made more sense for the I/O library to make a special case (or simply a method call) for unbuffered input rather than buffered input.
Table 12-3. Types of FilterInputStream
Class
|
Function
|
Constructor Arguments
|
How to use it
|
Data-InputStream
|
Used in concert with DataOutputStream, so you can read primitives (int, char, long, etc.) from a stream in a portable fashion.
|
InputStream
|
Contains a full interface to allow you to read primitive types.
|
Buffered-InputStream
|
Use this to prevent a physical read every time you want more data. You’re saying “Use a buffer.”
|
InputStream, with optional buffer size.
|
This doesn’t provide an interface per se, just a requirement that a buffer be used. Attach an interface object.
|
LineNumber-InputStream
|
Keeps track of line numbers in the input stream; you can call getLineNumber( ) and setLineNumber( int).
|
InputStream
|
This just adds line numbering, so you’ll probably attach an interface object.
|
Pushback-InputStream
|
Has a one byte push-back buffer so that you can push back the last character read.
|
InputStream
|
Generally used in the scanner for a compiler and probably included because the Java compiler needed it. You probably won’t use this.
|