Package java.io
Class PipedWriter
java.lang.Object
java.io.Writer
java.io.PipedWriter
- All Implemented Interfaces:
Closeable,Flushable,Appendable,AutoCloseable
public class PipedWriter extends Writer
Places 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:
PipedReader
-
Field Summary
-
Constructor Summary
Constructors Constructor Description PipedWriter()Constructs a new unconnectedPipedWriter.PipedWriter(PipedReader destination)Constructs a newPipedWriterconnected todestination. -
Method Summary
Modifier and Type Method Description voidclose()Closes this writer.voidconnect(PipedReader reader)Connects thisPipedWriterto aPipedReader.voidflush()Notifies the readers of thisPipedReaderthat characters can be read.voidwrite(char[] buffer, int offset, int count)Writescountcharacters from the character arraybufferstarting at offsetindexto this writer.voidwrite(int c)Writes a single charactercto this writer.
-
Constructor Details
-
PipedWriter
public PipedWriter()Constructs a new unconnectedPipedWriter. The resulting writer must be connected to aPipedReaderbefore data may be written to it.- See Also:
PipedReader
-
PipedWriter
Constructs a newPipedWriterconnected todestination. Any data written to this writer can be read fromdestination.- Parameters:
destination- thePipedReaderto connect to.- Throws:
IOException- ifdestinationis already connected.
-
-
Method Details
-
close
Closes this writer. If aPipedReaderis connected to this writer, it is closed as well and the pipe is disconnected. Any data buffered in the reader can still be read.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- if an error occurs while closing this writer.
-
connect
Connects thisPipedWriterto aPipedReader. Any data written to this writer becomes readable in the reader.- Parameters:
reader- the reader to connect to.- Throws:
IOException- if this writer is closed or already connected, or ifreaderis already connected.
-
flush
Notifies the readers of thisPipedReaderthat characters can be read. This method does nothing if this Writer is not connected.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException- if an I/O error occurs while flushing this writer.
-
write
Writescountcharacters from the character arraybufferstarting at offsetindexto this writer. The written data can then be read from the connectedPipedReaderinstance.Separate threads should be used to write to a
PipedWriterand to read from the connectedPipedReader. If the same thread is used, a deadlock may occur.- Specified by:
writein classWriter- Parameters:
buffer- the buffer to write.offset- the index of the first character inbufferto write.count- the number of characters frombufferto write to this writer.- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis bigger than the length ofbuffer.InterruptedIOException- if the pipe is full and the current thread is interrupted waiting for space to write data. This case is not currently handled correctly.IOException- if this writer is closed or not connected, if the target reader is closed or if the thread reading from the target reader is no longer alive. This case is currently not handled correctly.NullPointerException- ifbufferisnull.
-
write
Writes a single charactercto this writer. This character can then be read from the connectedPipedReaderinstance.Separate threads should be used to write to a
PipedWriterand to read from the connectedPipedReader. If the same thread is used, a deadlock may occur.- Overrides:
writein classWriter- Parameters:
c- the character to write.- Throws:
InterruptedIOException- if the pipe is full and the current thread is interrupted waiting for space to write data. This case is not currently handled correctly.IOException- if this writer is closed or not connected, if the target reader is closed or if the thread reading from the target reader is no longer alive. This case is currently not handled correctly.
-