I/O Filter Streams
An I/O filter stream is a high-level I/O stream that provides additional functionality to an underlying stream to which it is chained. The data from the underlying stream is manipulated in some way by the filter stream. The FilterInputStream and FilterOutputStream classes, together with their subclasses, define input and output filter streams. The subclasses BufferedInputStream and BufferedOutputStream implement filter streams that buffer input from and output to the underlying stream, respectively. The subclasses DataInputStream and DataOutputStream implement filter streams that allow binary representation of Java primitive values to be read and written, respectively, from and to an underlying stream.
Reading and Writing Binary Values
The java.io package contains the two interfaces DataInput and DataOutput, which streams can implement to allow reading and writing of binary representation of Java primitive values (boolean, char, byte, short, int, long, float, double). The methods for writing binary representations of Java primitive values are named writeX, where X is any Java primitive data type. The methods for reading binary representations of Java primitive values are similarly named readX. Table 20.3 gives an overview of the readX() and writeX() methods found in these two interfaces. A file containing binary values (i.e., binary representation of Java primitive values) is usually called a binary file.
Table 20.3 The DataInput and DataOutput Interfaces
Type | Methods in the DataInput interface | Methods in the DataOutput interface |
boolean | readBoolean() | writeBoolean(boolean b) |
char | readChar() | writeChar(int c) |
byte | readByte() | writeByte(int b) |
short | readShort() | writeShort(int s) |
int | readInt() | writeInt(int i) |
long | readLong() | writeLong(long l) |
float | readFloat() | writeFloat(float f) |
double | readDouble() | writeDouble(double d) |
String | readLine() | writeChars(String str) |
String | readUTF() | writeUTF(String str) |
Note the methods provided for reading and writing strings. However, the recommended practice for reading and writing characters is to use character streams, called readers and writers, which are discussed in ยง20.3.
The filter streams DataOutputStream and DataInputStream implement the DataOutput and DataInput interfaces, respectively, and can be used to read and write binary representation of Java primitive values from and to an underlying stream. Both the writeX() and readX() methods throw an IOException in the event of an I/O error. In particular, the readX() methods throw an EOFException (a subclass of IOException) if the input stream does not contain the correct number of bytes to read. Bytes can also be skipped from a DataInput stream, using the skipBytes(int n) method which skips n bytes.
DataInputStream(InputStream in)
DataOutputStream(OutputStream out)
These constructors can be used to set up filter streams from an underlying stream for reading and writing Java primitive values, respectively.