Java Reference
In-Depth Information
Buffered byte streams use the
BufferedInputStream
and
BufferedOutputStream
classes.
A buffered input stream is created using one of the following two constructors:
BufferedInputStream(
InputStream
)
—Creates a buffered input stream for the
specified
InputStream
object
n
BufferedInputStream(
InputStream
,
int
)—
Creates the specified
InputStream
buffered stream with a buffer of
int
size
n
The simplest way to read data from a buffered input stream is to call its
read()
method
with no arguments, which normally returns an integer from
0
to
255
representing the next
byte in the stream. If the end of the stream has been reached and no byte is available,
-1
is returned.
You also can use the
read(
byte[]
,
int
,
int
)
method available for other input streams,
which loads stream data into a byte array.
A buffered output stream is created using one of these two constructors:
BufferedOutputStream(
OutputStream
)
—Creates a buffered output stream for the
specified
OutputStream
object
n
BufferedOutputStream(
OutputStream
,
int
)
—Creates the specified
OutputStream
buffered stream with a buffer of
int
size
n
The output stream's
write(
int
)
method can be used to send a single byte to the stream,
and the
write(
byte[]
,
int
,
int
)
method writes multiple bytes from the specified byte
array. The arguments to this method are the byte array, array starting point, and number
of bytes to write.
Although the
write()
method takes an integer as input, the value
should be from
0
to
255
. If you specify a number higher than 255,
it will be stored as the remainder of the number divided by 256.
You can test this when running the project you will create later
today.
NOTE
When data is directed to a buffered stream, it is not output to its destination until the
stream fills or the buffered stream's
flush()
method is called.