FileDocCategorySizeDatePackage
PipedWriter.javaAPI DocAndroid 1.5 API8135Wed May 06 22:41:04 BST 2009java.io

PipedWriter

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

Fields Summary
private PipedReader
dest
The destination PipedReader
private boolean
closed
Constructors Summary
public PipedWriter()
Constructs a new unconnected {@code PipedWriter}. The resulting writer must be connected to a {@code PipedReader} before data may be written to it.

see
PipedReader
since
Android 1.0

        super();
    
public PipedWriter(PipedReader dest)
Constructs a new {@code PipedWriter} connected to the {@link PipedReader} {@code dest}. Any data written to this writer can be read from {@code dest}.

param
dest the {@code PipedReader} to connect to.
throws
IOException if {@code dest} is already connected.
since
Android 1.0

        super(dest);
        connect(dest);
    
Methods Summary
public voidclose()
Closes this writer. If a {@link PipedReader} is connected to this writer, it is closed as well and the pipe is disconnected. Any data buffered in the reader can still be read.

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

        synchronized (lock) {
            /* Is the pipe connected? */
            if (dest != null) {
                dest.done();
                dest = null;
            }
            closed = true;
        }
    
public voidconnect(java.io.PipedReader stream)
Connects this {@code PipedWriter} to a {@link PipedReader}. Any data written to this writer becomes readable in the reader.

param
stream the reader to connect to.
throws
IOException if this writer is closed or already connected, or if {@code stream} is already connected.
since
Android 1.0

        synchronized (lock) {
            if (this.dest != null) {
                throw new IOException(Msg.getString("K0079")); //$NON-NLS-1$
            }
            if (closed) {
                throw new IOException(Msg.getString("K0078")); //$NON-NLS-1$
            }
            stream.establishConnection();
            this.dest = stream;
        }
    
public voidflush()
Notifies the readers of this {@code PipedReader} that characters can be read. This method does nothing if this Writer is not connected.

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

        if (dest != null) {
            dest.flush();
        }
    
public voidwrite(char[] buffer, int offset, int count)
Writes {@code count} characters from the character array {@code buffer} starting at offset {@code index} to this writer. The written data can then be read from the connected {@link PipedReader} instance.

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

param
buffer the buffer to write.
param
offset the index of the first character in {@code buffer} to write.
param
count the number of characters from {@code buffer} to write to this writer.
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 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.
throws
NullPointerException if {@code buffer} is {@code null}.
since
Android 1.0

        // BEGIN android-note
        // changed array notation to be consistent with the rest of harmony
        // END android-note
        synchronized (lock) {
            if (closed) {
                throw new IOException(Msg.getString("K0078")); //$NON-NLS-1$
            }
            if (dest == null) {
                throw new IOException(Msg.getString("K007b")); //$NON-NLS-1$
            }
            if (buffer == null) {
                throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
            }

            // avoid int overflow
            // BEGIN android-changed
            // Exception priorities (in case of multiple errors) differ from
            // RI, but are spec-compliant.
            // removed redundant check, used (offset | count) < 0
            // instead of (offset < 0) || (count < 0) to safe one operation
            if ((offset | count) < 0 || count > buffer.length - offset) {
                throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
            }
            // END android-changed
            dest.receive(buffer, offset, count);
        }
    
public voidwrite(int c)
Writes a single character {@code c} to this writer. This character can then be read from the connected {@link PipedReader} instance.

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

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

        synchronized (lock) {
            if (closed) {
                throw new IOException(Msg.getString("K0078")); //$NON-NLS-1$
            }
            if (dest == null) {
                throw new IOException(Msg.getString("K007b")); //$NON-NLS-1$
            }
            dest.receive((char) c);
        }