Top Description Fields Constructors Methods
java.io

public Class PipedReader

extends Reader
Class Inheritance
Imports
java.util.Objects

Piped character-input streams.
Author
Mark Reinhold
Since
1.1

Field Summary

Modifier and TypeField and Description
pack-priv char[]
buffer

The circular buffer into which incoming data is placed.

pack-priv boolean
pack-priv boolean
pack-priv boolean
private static final int
DEFAULT_PIPE_SIZE

The size of the pipe's circular input buffer.

pack-priv int
in

The index of the position in the circular buffer at which the next character of data will be stored when received from the connected piped writer.

pack-priv int
out

The index of the position in the circular buffer at which the next character of data will be read by this piped reader.

pack-priv Thread
pack-priv Thread
Inherited from java.io.Reader:
lock

Constructor Summary

AccessConstructor and Description
public
PipedReader(PipedWriter
the stream to connect to.
src
)

Creates a PipedReader so that it is connected to the piped writer src.

public
PipedReader(PipedWriter
the stream to connect to.
src
,
int
the size of the pipe's buffer.
pipeSize
)

Creates a PipedReader so that it is connected to the piped writer src and uses the specified pipe size for the pipe's buffer.

public
PipedReader()

Creates a PipedReader so that it is not yet connected.

public
PipedReader(int
the size of the pipe's buffer.
pipeSize
)

Creates a PipedReader so that it is not yet connected and uses the specified pipe size for the pipe's buffer.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Closes this piped stream and releases any system resources associated with the stream.

public void
connect(PipedWriter
The piped writer to connect to.
src
)

Causes this piped reader to be connected to the piped writer src.

private void
initPipe(int pipeSize)

public synchronized int

Returns:

the next character of data, or -1 if the end of the stream is reached.
read
()

Overrides java.io.Reader.read.

Reads the next character of data from this piped stream.

public synchronized int

Returns:

The number of characters read, or -1 if the end of the stream has been reached
read
(char[]
Destination buffer
cbuf
,
int
Offset at which to start storing characters
off
,
int
Maximum number of characters to read
len
)

Implements abstract java.io.Reader.read.

Reads characters into a portion of an array.

public synchronized boolean
ready()

Overrides java.io.Reader.ready.

Tell whether this stream is ready to be read.

pack-priv synchronized void
receive(int c)

Receives a char of data.

pack-priv synchronized void
receive(char[] c, int off, int len)

Receives data into an array of characters.

pack-priv synchronized void
receivedLast()

Notifies all waiting threads that the last character of data has been received.

Inherited from java.io.Reader:
markmarkSupportednullReaderofreadreadresetskiptransferTo

Field Detail

bufferback to summary
pack-priv char[] buffer

The circular buffer into which incoming data is placed.

closedByReaderback to summary
pack-priv boolean closedByReader
closedByWriterback to summary
pack-priv boolean closedByWriter
connectedback to summary
pack-priv boolean connected
DEFAULT_PIPE_SIZEback to summary
private static final int DEFAULT_PIPE_SIZE

The size of the pipe's circular input buffer.

inback to summary
pack-priv int in

The index of the position in the circular buffer at which the next character of data will be stored when received from the connected piped writer. in < 0 implies the buffer is empty, in == out implies the buffer is full

outback to summary
pack-priv int out

The index of the position in the circular buffer at which the next character of data will be read by this piped reader.

readSideback to summary
pack-priv Thread readSide
writeSideback to summary
pack-priv Thread writeSide

Constructor Detail

PipedReaderback to summary
public PipedReader(PipedWriter src) throws IOException

Creates a PipedReader so that it is connected to the piped writer src. Data written to src will then be available as input from this stream.

Parameters
src:PipedWriter

the stream to connect to.

Exceptions
IOException:
if an I/O error occurs.
PipedReaderback to summary
public PipedReader(PipedWriter src, int pipeSize) throws IOException

Creates a PipedReader so that it is connected to the piped writer src and uses the specified pipe size for the pipe's buffer. Data written to src will then be available as input from this stream.

Parameters
src:PipedWriter

the stream to connect to.

pipeSize:int

the size of the pipe's buffer.

Annotations
@SuppressWarnings:this-escape
Exceptions
IOException:
if an I/O error occurs.
IllegalArgumentException:
if pipeSize <= 0.
Since
1.6
PipedReaderback to summary
public PipedReader()

Creates a PipedReader so that it is not yet connected. It must be connected to a PipedWriter before being used.

PipedReaderback to summary
public PipedReader(int pipeSize)

Creates a PipedReader so that it is not yet connected and uses the specified pipe size for the pipe's buffer. It must be connected to a PipedWriter before being used.

Parameters
pipeSize:int

the size of the pipe's buffer.

Exceptions
IllegalArgumentException:
if pipeSize <= 0.
Since
1.6

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Closes this piped stream and releases any system resources associated with the stream.

Exceptions
IOException:
if an I/O error occurs.
connectback to summary
public void connect(PipedWriter src) throws IOException

Causes this piped reader to be connected to the piped writer src. If this object is already connected to some other piped writer, an IOException is thrown.

If src is an unconnected piped writer and snk is an unconnected piped reader, they may be connected by either the call:

snk.connect(src)
snk.connect(src)

or the call:

src.connect(snk)
src.connect(snk)

The two calls have the same effect.

Parameters
src:PipedWriter

The piped writer to connect to.

Exceptions
IOException:
if an I/O error occurs.
initPipeback to summary
private void initPipe(int pipeSize)
readback to summary
public synchronized int read() throws IOException

Overrides java.io.Reader.read.

Reads the next character of data from this piped stream. If no character is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns:int

the next character of data, or -1 if the end of the stream is reached.

Exceptions
IOException:
if the pipe is broken, unconnected, closed, or an I/O error occurs.
readback to summary
public synchronized int read(char[] cbuf, int off, int len) throws IOException

Implements abstract java.io.Reader.read.

Doc from java.io.Reader.read.

Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.

Fewer than len characters will be read if len exceeds the pipe's buffer size.

Parameters
cbuf:char[]

Destination buffer

off:int

Offset at which to start storing characters

len:int

Maximum number of characters to read

Returns:int

The number of characters read, or -1 if the end of the stream has been reached

Exceptions
IOException:
if the pipe is broken, unconnected, closed, or an I/O error occurs.
IndexOutOfBoundsException:
If off is negative, or len is negative, or len is greater than cbuf.length - off
readyback to summary
public synchronized boolean ready() throws IOException

Overrides java.io.Reader.ready.

Tell whether this stream is ready to be read. A piped character stream is ready if the circular buffer is not empty.

Returns:boolean

Doc from java.io.Reader.ready.

True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

Exceptions
IOException:
if the pipe is broken, unconnected, or closed.
receiveback to summary
pack-priv synchronized void receive(int c) throws IOException

Receives a char of data. This method will block if no input is available.

receiveback to summary
pack-priv synchronized void receive(char[] c, int off, int len) throws IOException

Receives data into an array of characters. This method will block until some input is available.

receivedLastback to summary
pack-priv synchronized void receivedLast()

Notifies all waiting threads that the last character of data has been received.