Java Reference
In-Depth Information
•
BufferedInputStream(InputStream in)
creates a buffered input
streamthatstreamsitsinputfrom
in
.Aninternalbufferiscreatedtostorebytes
read from
in
.
•
BufferedInputStream(InputStream in, int size)
creates a
buffered input stream that streams its input from
in
. An internal buffer of
length
size
is created to store bytes read from
in
.
Thefollowingexamplechainsa
BufferedInputStream
instancetoa
FileIn-
putStream
instance. Subsequent
read()
method calls on the
BufferedIn-
putStream
instanceunbufferbytesandoccasionallyresultininternal
read()
meth-
od calls on the encapsulated
FileInputStream
instance:
FileInputStream fis = new FileInputStream("employee.dat");
BufferedInputStream bis = new BufferedInputStream(fis); //
Chain bis to fis.
int ch = bis.read(); // Read employee.dat through the buf-
fer.
// Additional read() method calls.
bis.close(); // This method call internally calls fis's
close() method.
DataOutputStream and DataInputStream
FileOutputStream
and
FileInputStream
are useful for writing and reading
bytes and arrays of bytes. However, they provide no support for writing and reading
primitive type values (such as integers) and strings.
For this reason, Java provides the concrete
DataOutputStream
and
DataIn-
putStream
filter stream classes. Each class overcomes this limitation by providing
methods to write or read primitive type values and strings in a platform-independent
way:
• Integer values are written and read in
big-endian format
(the most significant
byte comes first). Check out Wikipedia's “Endianness” entry (
ht-
tp://en.wikipedia.org/wiki/Endianness
) to learn about the
concept of
endianness
.
• Floating-point and double precision floating-point values are written and read
according to the IEEE 754 standard, which specifies four bytes per floating-
point value and eight bytes per double precision floating-point value.