Java Reference
In-Depth Information
program runs. Nonetheless, buffering input rarely hurts and will become more impor‐
tant over time as network speeds increase.
BufferedInputStream has two constructors, as does BufferedOutputStream :
public BufferedInputStream ( InputStream in )
public BufferedInputStream ( InputStream in , int bufferSize )
public BufferedOutputStream ( OutputStream out )
public BufferedOutputStream ( OutputStream out , int bufferSize )
The first argument is the underlying stream from which unbuffered data will be read
or to which buffered data will be written. The second argument, if present, specifies the
number of bytes in the buffer. Otherwise, the buffer size is set to 2,048 bytes for an input
stream and 512 bytes for an output stream. The ideal size for a buffer depends on what
sort of stream you're buffering. For network connections, you want something a little
larger than the typical packet size. However, this can be hard to predict and varies
depending on local network connections and protocols. Faster, higher-bandwidth net‐
works tend to use larger packets, although TCP segments are often no larger than a
kilobyte.
BufferedInputStream does not declare any new methods of its own. It only overrides
methods from InputStream . It does support marking and resetting. The two multibyte
read() methods attempt to completely fill the specified array or subarray of data by
reading from the underlying input stream as many times as necessary. They return only
when the array or subarray has been completely filled, the end of stream is reached, or
the underlying stream would block on further reads. Most input streams do not behave
like this. They read from the underlying stream or data source only once before re‐
turning.
BufferedOutputStream also does not declare any new methods of its own. You invoke
its methods exactly as you would in any output stream. The difference is that each write
places data in the buffer rather than directly on the underlying output stream. Conse‐
quently, it is essential to flush the stream when you reach a point at which the data needs
to be sent.
PrintStream
The PrintStream class is the first filter output stream most programmers encounter
because System.out is a PrintStream . However, other output streams can also be
chained to print streams, using these two constructors:
public PrintStream ( OutputStream out )
public PrintStream ( OutputStream out , boolean autoFlush )
By default, print streams should be explicitly flushed. However, if the autoFlush argu‐
ment is true , the stream will be flushed every time a byte array or linefeed is written or
a println() method is invoked.
Search WWH ::




Custom Search