Java Reference
In-Depth Information
memory. Buffering reduces the number of I/O operations by first combining smaller out-
puts together in memory. The number of actual physical I/O operations is small compared
with the number of I/O requests issued by the program. Thus, the program that's using
buffering is more efficient.
Performance Tip 15.1
Buffered I/O can yield significant performance improvements over unbuffered I/O.
With a
BufferedInputStream
(a subclass of class
FilterInputStream
), many “log-
ical” chunks of data from a file are read as one large
physical input operation
into a
memory buffer. As a program requests each new chunk of data, it's taken from the buffer.
(This procedure is sometimes referred to as a
logical input operation
.) When the buffer is
empty, the next actual physical input operation from the input device is performed to read
in the next group of “logical” chunks of data. Thus, the number of actual physical input
operations is small compared with the number of read requests issued by the program.
Memory-Based
byte
Array Steams
Java stream I/O includes capabilities for inputting from
byte
arrays in memory and out-
putting to
byte
arrays in memory. A
ByteArrayInputStream
(a subclass of
InputStream
)
reads from a
byte
array in memory. A
ByteArrayOutputStream
(a subclass of
Output-
Stream
) outputs to a
byte
array in memory. One use of
byte
-array I/O is
data validation
.
A program can input an entire line at a time from the input stream into a
byte
array. Then
a validation routine can scrutinize the contents of the
byte
array and correct the data if
necessary. Finally, the program can proceed to input from the
byte
array, “knowing” that
the input data is in the proper format. Outputting to a
byte
array is a nice way to take
advantage of the powerful output-formatting capabilities of Java streams. For example,
data can be stored in a
byte
array, using the same formatting that will be displayed at a
later time, and the
byte
array can then be output to a file to preserve the formatting.
Sequencing Input from Multiple Streams
A
SequenceInputStream
(a subclass of
InputStream
) logically concatenates several
Input-
Stream
s—the program sees the group as one continuous
InputStream
. When the program
reaches the end of one input stream, that stream closes, and the next stream in the se-
quence opens.
In addition to the byte-based streams, Java provides the
Reader
and
Writer
abstract
classes, which are character-based streams like those you used for text-file processing in
Section 15.4. Most of the byte-based streams have corresponding character-based concrete
Reader
or
Writer
classes.
Character-Based Buffering
Reader
s and
Writer
s
Classes
BufferedReader
(a subclass of
abstract
class
Reader
) and
BufferedWriter
(a
subclass of
abstract
class
Writer
) enable buffering for character-based streams. Remem-
ber that character-based streams use Unicode characters—such streams can process data
in any language that the Unicode character set represents.