Package java.io
Class PipedReader
java.lang.Object
java.io.Reader
java.io.PipedReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
public class PipedReader extends Reader
Receives information on a communications pipe. When two threads want to pass
data back and forth, one creates a piped writer and the other creates a piped
reader.
- See Also:
PipedWriter
-
Field Summary
-
Constructor Summary
Constructors Constructor Description PipedReader()Constructs a new unconnectedPipedReader.PipedReader(int pipeSize)Constructs a new unconnectedPipedReaderwith the given buffer size.PipedReader(PipedWriter out)PipedReader(PipedWriter out, int pipeSize)Constructs a newPipedReaderconnected to the givenPipedWriter, with the given buffer size. -
Method Summary
Modifier and Type Method Description voidclose()Closes this reader.voidconnect(PipedWriter src)Connects thisPipedReaderto aPipedWriter.intread()Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0.intread(char[] buffer, int offset, int count)Reads up tocountcharacters from this reader and stores them in the character arraybufferstarting atoffset.booleanready()Indicates whether this reader is ready to be read without blocking.
-
Constructor Details
-
PipedReader
public PipedReader()Constructs a new unconnectedPipedReader. The resulting reader must be connected to aPipedWriterbefore data may be read from it. -
PipedReader
Constructs a newPipedReaderconnected to thePipedWriterout. Any data written to the writer can be read from the this reader.- Parameters:
out- thePipedWriterto connect to.- Throws:
IOException- ifoutis already connected.
-
PipedReader
public PipedReader(int pipeSize)Constructs a new unconnectedPipedReaderwith the given buffer size. The resulting reader must be connected to aPipedWriterbefore data may be read from it.- Parameters:
pipeSize- the size of the buffer in chars.- Throws:
IllegalArgumentException- if pipeSize is less than or equal to zero.- Since:
- 1.6
-
PipedReader
Constructs a newPipedReaderconnected to the givenPipedWriter, with the given buffer size. Any data written to the writer can be read from this reader.- Parameters:
out- thePipedWriterto connect to.pipeSize- the size of the buffer in chars.- Throws:
IOException- if an I/O error occursIllegalArgumentException- if pipeSize is less than or equal to zero.- Since:
- 1.6
-
-
Method Details
-
close
Closes this reader. This implementation releases the buffer used for the pipe and notifies all threads waiting to read or write.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classReader- Throws:
IOException- if an error occurs while closing this reader.
-
connect
Connects thisPipedReaderto aPipedWriter. Any data written to the writer becomes readable in this reader.- Parameters:
src- the writer to connect to.- Throws:
IOException- if this reader is closed or already connected, or ifsrcis already connected.
-
read
Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0. Returns -1 if the end of the reader has been reached. If there is no data in the pipe, this method blocks until data is available, the end of the reader is detected or an exception is thrown.Separate threads should be used to read from a
PipedReaderand to write to the connectedPipedWriter. If the same thread is used, a deadlock may occur.- Overrides:
readin classReader- Returns:
- the character read or -1 if the end of the reader has been reached.
- Throws:
IOException- if this reader is closed or some other I/O error occurs.
-
read
Reads up tocountcharacters from this reader and stores them in the character arraybufferstarting atoffset. If there is no data in the pipe, this method blocks until at least one byte has been read, the end of the reader is detected or an exception is thrown.Separate threads should be used to read from a
PipedReaderand to write to the connectedPipedWriter. If the same thread is used, a deadlock may occur.Returns the number of characters read or -1 if the end of the reader has been reached.
- Specified by:
readin classReader- Throws:
IndexOutOfBoundsException- ifoffset < 0 || count < 0 || offset + count > buffer.length.InterruptedIOException- if the thread reading from this reader is interrupted.IOException- if this reader is closed or not connected to a writer, or if the thread writing to the connected writer is no longer alive.
-
ready
Indicates whether this reader is ready to be read without blocking. Returnstrueif this reader will not block whenreadis called,falseif unknown or blocking will occur. This implementation returnstrueif the internal buffer contains characters that can be read.- Overrides:
readyin classReader- Returns:
- always
false. - Throws:
IOException- if this reader is closed or not connected, or if some other I/O error occurs.- See Also:
read(),read(char[], int, int)
-