Java Reference
In-Depth Information
new FileOutputStream ( "data.txt" )
)
);
Connection is permanent. Filters cannot be disconnected from a stream.
There are times when you may need to use the methods of multiple filters in a chain.
For instance, if you're reading a Unicode text file, you may want to read the byte order
mark in the first three bytes to determine whether the file is encoded as big-endian
UCS-2, little-endian UCS-2, or UTF-8, and then select the matching Reader filter for
the encoding. Or, if you're connecting to a web server, you may want to read the header
the server sends to find the Content-encoding and then use that content encoding to
pick the right Reader filter to read the body of the response. Or perhaps you want to
send floating-point numbers across a network connection using a DataOutputStream
and then retrieve a MessageDigest from the DigestOutputStream that the DataOut
putStream is chained to. In all these cases, you need to save and use references to each
of the underlying streams. However, under no circumstances should you ever read from
or write to anything other than the last filter in the chain.
Buffered Streams
The BufferedOutputStream class stores written data in a buffer (a protected byte array
field named buf ) until the buffer is full or the stream is flushed. Then it writes the data
onto the underlying output stream all at once. A single write of many bytes is almost
always much faster than many small writes that add up to the same thing. This is espe‐
cially true of network connections because each TCP segment or UDP packet carries a
finite amount of overhead, generally about 40 bytes' worth. This means that sending 1
kilobyte of data 1 byte at a time actually requires sending 40 kilobytes over the wire,
whereas sending it all at once only requires sending a little more than 1K of data. Most
network cards and TCP implementations provide some level of buffering themselves,
so the real numbers aren't quite this dramatic. Nonetheless, buffering network output
is generally a huge performance win.
The BufferedInputStream class also has a protected byte array named buf that serves
as a buffer. When one of the stream's read() methods is called, it first tries to get the
requested data from the buffer. Only when the buffer runs out of data does the stream
read from the underlying source. At this point, it reads as much data as it can from the
source into the buffer, whether it needs all the data immediately or not. Data that isn't
used immediately will be available for later invocations of read() . When reading files
from a local disk, it's almost as fast to read several hundred bytes of data from the
underlying stream as it is to read one byte of data. Therefore, buffering can substantially
improve performance. The gain is less obvious on network connections where the bot‐
tleneck is often the speed at which the network can deliver data rather than the speed
at which the network interface delivers data to the program or the speed at which the
Search WWH ::




Custom Search