Package java.io
Class PipedOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.PipedOutputStream
-
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
public class PipedOutputStream extends OutputStream
Places information on a communications pipe. When two threads want to pass data back and forth, one creates a piped output stream and the other one creates a piped input stream.- See Also:
PipedInputStream
-
-
Constructor Summary
Constructors Constructor Description PipedOutputStream()Constructs a new unconnectedPipedOutputStream.PipedOutputStream(PipedInputStream target)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this stream.voidconnect(PipedInputStream stream)Connects this stream to aPipedInputStream.voidflush()Notifies the readers of thisPipedInputStreamthat bytes can be read.voidwrite(byte[] buffer, int offset, int count)Writescountbytes from the byte arraybufferstarting atoffsetto this stream.voidwrite(int oneByte)Writes a single byte to this stream.-
Methods inherited from class java.io.OutputStream
write
-
-
-
-
Constructor Detail
-
PipedOutputStream
public PipedOutputStream()
Constructs a new unconnectedPipedOutputStream. The resulting stream must be connected to aPipedInputStreambefore data can be written to it.
-
PipedOutputStream
public PipedOutputStream(PipedInputStream target) throws IOException
Constructs a newPipedOutputStreamconnected to thePipedInputStreamtarget. Any data written to this stream can be read from the target stream.- Parameters:
target- the piped input stream to connect to.- Throws:
IOException- if this stream ortargetare already connected.
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses this stream. If this stream is connected to an input stream, the input stream is closed and the pipe is disconnected.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- if an error occurs while closing this stream.
-
connect
public void connect(PipedInputStream stream) throws IOException
Connects this stream to aPipedInputStream. Any data written to this output stream becomes readable in the input stream.- Parameters:
stream- the piped input stream to connect to.- Throws:
IOException- if either stream is already connected.
-
flush
public void flush() throws IOExceptionNotifies the readers of thisPipedInputStreamthat bytes can be read. This method does nothing if this stream is not connected.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- if an I/O error occurs while flushing this stream.
-
write
public void write(byte[] buffer, int offset, int count) throws IOExceptionWritescountbytes from the byte arraybufferstarting atoffsetto this stream. The written data can then be read from the connected input stream.Separate threads should be used to write to a
PipedOutputStreamand to read from the connectedPipedInputStream. If the same thread is used, a deadlock may occur.- Overrides:
writein classOutputStream- Parameters:
buffer- the buffer to write.offset- the index of the first byte inbufferto write.count- the number of bytes frombufferto write to this stream.- 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 stream is not connected, if the target stream is closed or if the thread reading from the target stream is no longer alive.
-
write
public void write(int oneByte) throws IOExceptionWrites a single byte to this stream. Only the least significant byte of the integeroneByteis written. The written byte can then be read from the connected input stream.Separate threads should be used to write to a
PipedOutputStreamand to read from the connectedPipedInputStream. If the same thread is used, a deadlock may occur.- Specified by:
writein classOutputStream- Parameters:
oneByte- the byte 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 stream is not connected, if the target stream is closed or if the thread reading from the target stream is no longer alive. This case is currently not handled correctly.
-
-