Java Reference
In-Depth Information
could be a parameter to the output pipe. What is important is that an in-
put/output pair be attached to each other. We create the new
TextGener-
ator
object, with the
PipedWriter
as the output stream for the generated
characters. Then we loop, reading characters from the text generator
and writing them to the system output stream. At the end, we make
sure that the last line of output is terminated.
Piped
streams need not be connected when they are constructedthere is
a no-arg constructorbut can be connected at a later stage via the
con-
nect
method.
PipedReader.connect
takes a
PipedWriter
parameter and vice
versa. As with the constructor, it does not matter whether you connect
x
to
y
, or
y
to
x
, the result is the same. Trying to use a
Piped
stream be-
fore it is connected or trying to connect it when it is already connected
results in an
IOException
.
20.5.5.
ByteArray
Byte Streams
You can use arrays of bytes as the source or destination of byte streams
by using
ByteArray
streams. The
ByteArrayInputStream
class uses a
byte
array as its input source, and reading on it can never block. It has two
constructors:
public
ByteArrayInputStream(byte[] buf, int offset, int count)
Creates a
ByteArrayInputStream
from the specified array of
bytes using only the part of
buf
from
buf[offset]
to
buf[offset+count-1]
or the end of the array, whichever is smal-
ler. The input array is used directly, not copied, so you should
take care not to modify it while it is being used as an input
source.
public
ByteArrayInputStream(byte[] buf)
Equivalent to
ByteArrayInputStream(buf,0, buf.length)
.