Java Reference
In-Depth Information
InputStream
(a subclass of
InputStream
) to establish pipes between two threads in a pro-
gram. One thread sends data to another by writing to a
PipedOutputStream
. The target
thread reads information from the pipe via a
PipedInputStream
.
Filter Streams
A
FilterInputStream
filters an
InputStream
, and a
FilterOutputStream
filters an
Out-
putStream
.
Filtering
means simply that the filter stream provides additional functionality,
such as aggregating bytes into meaningful primitive-type units.
FilterInputStream
and
FilterOutputStream
are typically used as superclasses, so some of their filtering capabili-
ties are provided by their subclasses.
A
PrintStream
(a subclass of
FilterOutputStream
) performs text output to the spec-
ified stream. Actually, we've been using
PrintStream
output throughout the text to this
point—
System.out
and
System.err
are
PrintStream
objects.
Data Streams
Reading data as raw bytes is fast, but crude. Usually, programs read data as aggregates of
bytes that form
int
s,
float
s,
double
s and so on. Java programs can use several classes to
input and output data in aggregate form.
Interface
DataInput
describes methods for reading primitive types from an input
stream. Classes
DataInputStream
and
RandomAccessFile
each implement this interface
to read sets of bytes and view them as primitive-type values. Interface
DataInput
includes
methods such as
readBoolean
,
readByte
,
readChar
,
readDouble
,
readFloat
,
readFully
(for
byte
arrays),
readInt
,
readLong
,
readShort
,
readUnsignedByte
,
readUnsigned-
Short
,
readUTF
(for reading Unicode characters encoded by Java—we discuss UTF
encoding in Appendix H) and
skipBytes
.
Interface
DataOutput
describes a set of methods for writing primitive types to an
output stream. Classes
DataOutputStream
(a subclass of
FilterOutputStream
) and
Ran-
domAccessFile
each implement this interface to write primitive-type values as bytes.
Interface
DataOutput
includes overloaded versions of method
write
(for a
byte
or for a
byte
array) and methods
writeBoolean
,
writeByte
,
writeBytes
,
writeChar
,
writeChars
(for Unicode
String
s),
writeDouble
,
writeFloat
,
writeInt
,
writeLong
,
writeShort
and
writeUTF
(to output text modified for Unicode).
Buffered Streams
Buffering
is an I/O-performance-enhancement technique. With a
BufferedOutput-
Stream
(a subclass of class
FilterOutputStream
), each output statement does
not
neces-
sarily result in an actual physical transfer of data to the output device (which is a slow
operation compared to processor and main memory speeds). Rather, each output opera-
tion is directed to a region in memory called a
buffer
that's large enough to hold the data
of many output operations. Then, actual transfer to the output device is performed in one
large
physical output operation
each time the buffer fills. The output operations directed
to the output buffer in memory are often called
logical output operations
. With a
Buff-
eredOutputStream
, a partially filled buffer can be forced out to the device at any time by
invoking the stream object's
flush
method.
Using buffering can greatly increase the performance of an application. Typical I/O
operations are extremely slow compared with the speed of accessing data in computer