001: /*
002: * @(#)PipedOutputStream.java 1.31 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.io;
029:
030: import java.io.*;
031:
032: /**
033: * A piped output stream can be connected to a piped input stream
034: * to create a communications pipe. The piped output stream is the
035: * sending end of the pipe. Typically, data is written to a
036: * <code>PipedOutputStream</code> object by one thread and data is
037: * read from the connected <code>PipedInputStream</code> by some
038: * other thread. Attempting to use both objects from a single thread
039: * is not recommended as it may deadlock the thread.
040: *
041: * @author James Gosling
042: * @version 1.23, 02/02/00
043: * @see java.io.PipedInputStream
044: * @since JDK1.0
045: */
046: public class PipedOutputStream extends OutputStream {
047:
048: /* NOTE: identification of the read and write sides needs to be
049: more sophisticated. Either using thread groups (but what about
050: pipes within a thread?) or using finalization (but it may be a
051: long time until the next GC). */
052: private PipedInputStream sink;
053:
054: /**
055: * Creates a piped output stream connected to the specified piped
056: * input stream. Data bytes written to this stream will then be
057: * available as input from <code>snk</code>.
058: *
059: * @param snk The piped input stream to connect to.
060: * @exception IOException if an I/O error occurs.
061: */
062: public PipedOutputStream(PipedInputStream snk) throws IOException {
063: connect(snk);
064: }
065:
066: /**
067: * Creates a piped output stream that is not yet connected to a
068: * piped input stream. It must be connected to a piped input stream,
069: * either by the receiver or the sender, before being used.
070: *
071: * @see java.io.PipedInputStream#connect(java.io.PipedOutputStream)
072: * @see java.io.PipedOutputStream#connect(java.io.PipedInputStream)
073: */
074: public PipedOutputStream() {
075: }
076:
077: /**
078: * Connects this piped output stream to a receiver. If this object
079: * is already connected to some other piped input stream, an
080: * <code>IOException</code> is thrown.
081: * <p>
082: * If <code>snk</code> is an unconnected piped input stream and
083: * <code>src</code> is an unconnected piped output stream, they may
084: * be connected by either the call:
085: * <blockquote><pre>
086: * src.connect(snk)</pre></blockquote>
087: * or the call:
088: * <blockquote><pre>
089: * snk.connect(src)</pre></blockquote>
090: * The two calls have the same effect.
091: *
092: * @param snk the piped input stream to connect to.
093: * @exception IOException if an I/O error occurs.
094: */
095: public synchronized void connect(PipedInputStream snk)
096: throws IOException {
097: if (snk == null) {
098: throw new NullPointerException();
099: } else if (sink != null || snk.connected) {
100: throw new IOException("Already connected");
101: }
102: sink = snk;
103: snk.in = -1;
104: snk.out = 0;
105: snk.connected = true;
106: }
107:
108: /**
109: * Writes the specified <code>byte</code> to the piped output stream.
110: * If a thread was reading data bytes from the connected piped input
111: * stream, but the thread is no longer alive, then an
112: * <code>IOException</code> is thrown.
113: * <p>
114: * Implements the <code>write</code> method of <code>OutputStream</code>.
115: *
116: * @param b the <code>byte</code> to be written.
117: * @exception IOException if an I/O error occurs.
118: */
119: public void write(int b) throws IOException {
120: if (sink == null) {
121: throw new IOException("Pipe not connected");
122: }
123: sink.receive(b);
124: }
125:
126: /**
127: * Writes <code>len</code> bytes from the specified byte array
128: * starting at offset <code>off</code> to this piped output stream.
129: * If a thread was reading data bytes from the connected piped input
130: * stream, but the thread is no longer alive, then an
131: * <code>IOException</code> is thrown.
132: *
133: * @param b the data.
134: * @param off the start offset in the data.
135: * @param len the number of bytes to write.
136: * @exception IOException if an I/O error occurs.
137: */
138: public void write(byte b[], int off, int len) throws IOException {
139: if (sink == null) {
140: throw new IOException("Pipe not connected");
141: } else if (b == null) {
142: throw new NullPointerException();
143: } else if ((off < 0) || (off > b.length) || (len < 0)
144: || ((off + len) > b.length) || ((off + len) < 0)) {
145: throw new IndexOutOfBoundsException();
146: } else if (len == 0) {
147: return;
148: }
149: sink.receive(b, off, len);
150: }
151:
152: /**
153: * Flushes this output stream and forces any buffered output bytes
154: * to be written out.
155: * This will notify any readers that bytes are waiting in the pipe.
156: *
157: * @exception IOException if an I/O error occurs.
158: */
159: public synchronized void flush() throws IOException {
160: if (sink != null) {
161: synchronized (sink) {
162: sink.notifyAll();
163: }
164: }
165: }
166:
167: /**
168: * Closes this piped output stream and releases any system resources
169: * associated with this stream. This stream may no longer be used for
170: * writing bytes.
171: *
172: * @exception IOException if an I/O error occurs.
173: */
174: public void close() throws IOException {
175: if (sink != null) {
176: sink.receivedLast();
177: }
178: }
179: }
|