FileDocCategorySizeDatePackage
PipedOutputStream.javaAPI DocAndroid 1.5 API7087Wed May 06 22:41:04 BST 2009java.io

PipedOutputStream

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
PipedInputStream
since
Android 1.0

Fields Summary
private PipedInputStream
dest
The destination PipedInputStream
Constructors Summary
public PipedOutputStream()
Constructs a new unconnected {@code PipedOutputStream}. The resulting stream must be connected to a {@link PipedInputStream} before data can be written to it.

since
Android 1.0

        super();
    
public PipedOutputStream(PipedInputStream dest)
Constructs a new {@code PipedOutputStream} connected to the {@link PipedInputStream} {@code dest}. Any data written to this stream can be read from the target stream.

param
dest the piped input stream to connect to.
throws
IOException if this stream or {@code dest} are already connected.
since
Android 1.0

        super();
        connect(dest);
    
Methods Summary
public voidclose()
Closes this stream. If this stream is connected to an input stream, the input stream is closed and the pipe is disconnected.

throws
IOException if an error occurs while closing this stream.
since
Android 1.0

        // Is the pipe connected?
        if (dest != null) {
            dest.done();
            dest = null;
        }
    
public voidconnect(java.io.PipedInputStream stream)
Connects this stream to a {@link PipedInputStream}. Any data written to this output stream becomes readable in the input stream.

param
stream the destination input stream.
throws
IOException if either stream is already connected.
since
Android 1.0

        if (null == stream) {
            throw new NullPointerException();
        }
        if (this.dest != null) {
            throw new IOException(Msg.getString("K0079")); //$NON-NLS-1$
        }
        synchronized (stream) {
            if (stream.isConnected) {
                throw new IOException(Msg.getString("K007a")); //$NON-NLS-1$
            }
            stream.buffer = new byte[PipedInputStream.PIPE_SIZE];
            stream.isConnected = true;
            this.dest = stream;
        }
    
public voidflush()
Notifies the readers of this {@link PipedInputStream} that bytes can be read. This method does nothing if this stream is not connected.

throws
IOException if an I/O error occurs while flushing this stream.
since
Android 1.0

        if (dest != null) {
            synchronized (dest) {
                dest.notifyAll();
            }
        }
    
public voidwrite(byte[] buffer, int offset, int count)
Writes {@code count} bytes from the byte array {@code buffer} starting at {@code offset} to this stream. The written data can then be read from the connected input stream.

Separate threads should be used to write to a {@code PipedOutputStream} and to read from the connected {@link PipedInputStream}. If the same thread is used, a deadlock may occur.

param
buffer the buffer to write.
param
offset the index of the first byte in {@code buffer} to write.
param
count the number of bytes from {@code buffer} to write to this stream.
throws
IndexOutOfBoundsException if {@code offset < 0} or {@code count < 0}, or if {@code offset + count} is bigger than the length of {@code buffer}.
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.
throws
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.
since
Android 1.0

        // BEGIN android-note
        // changed array notation to be consistent with the rest of harmony
        // END android-note
        if (dest == null) {
            // K007b=Pipe Not Connected
            throw new IOException(Msg.getString("K007b")); //$NON-NLS-1$
        }
        super.write(buffer, offset, count);
    
public voidwrite(int oneByte)
Writes a single byte to this stream. Only the least significant byte of the integer {@code oneByte} is written. The written byte can then be read from the connected input stream.

Separate threads should be used to write to a {@code PipedOutputStream} and to read from the connected {@link PipedInputStream}. If the same thread is used, a deadlock may occur.

param
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.
throws
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.
since
Android 1.0

        if (dest == null) {
            throw new IOException(Msg.getString("K007b")); //$NON-NLS-1$
        }
        dest.receive(oneByte);