Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / io / PipedOutputStream.java
bloba652356761601039b5c6050e3f53aa2eeb76d0d8
1 /* PipedOutputStream.java -- Write portion of piped streams.
2 Copyright (C) 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.io;
41 // NOTE: This implementation is very similar to that of PipedWriter. If you
42 // fix a bug in here, chances are you should make a similar change to the
43 // PipedWriter code.
45 /**
46 * This class writes its bytes to a <code>PipedInputStream</code> to
47 * which it is connected.
48 * <p>
49 * It is highly recommended that a <code>PipedOutputStream</code> and its
50 * connected <code>PipedInputStream</code> be in different threads. If
51 * they are in the same thread, read and write operations could deadlock
52 * the thread.
54 * @author Aaron M. Renn (arenn@urbanophile.com)
56 public class PipedOutputStream extends OutputStream
58 /** Target PipedInputStream to which this is connected. Null only if this
59 * OutputStream hasn't been connected yet. */
60 PipedInputStream sink;
62 /** Set to true if close() has been called on this OutputStream. */
63 boolean closed;
65 /**
66 * Create an unconnected PipedOutputStream. It must be connected
67 * to a <code>PipedInputStream</code> using the <code>connect</code>
68 * method prior to writing any data or an exception will be thrown.
70 public PipedOutputStream()
74 /**
75 * Create a new <code>PipedOutputStream</code> instance
76 * to write to the specified <code>PipedInputStream</code>. This stream
77 * is then ready for writing.
79 * @param sink The <code>PipedInputStream</code> to connect this stream to.
81 * @exception IOException If <code>sink</code> has already been connected
82 * to a different PipedOutputStream.
84 public PipedOutputStream(PipedInputStream sink) throws IOException
86 sink.connect(this);
89 /**
90 * Connects this object to the specified <code>PipedInputStream</code>
91 * object. This stream will then be ready for writing.
93 * @param sink The <code>PipedInputStream</code> to connect this stream to
95 * @exception IOException If the stream has not been connected or has
96 * been closed.
98 public void connect(PipedInputStream sink) throws IOException
100 if (this.sink != null || sink.source != null)
101 throw new IOException ("Already connected");
102 sink.connect(this);
106 * Write a single byte of date to the stream. Note that this method will
107 * block if the <code>PipedInputStream</code> to which this object is
108 * connected has a full buffer.
110 * @param b The byte of data to be written, passed as an <code>int</code>.
112 * @exception IOException If the stream has not been connected or has
113 * been closed.
115 public void write(int b) throws IOException
117 if (sink == null)
118 throw new IOException ("Not connected");
119 if (closed)
120 throw new IOException ("Pipe closed");
122 sink.receive (b);
126 * This method writes <code>len</code> bytes of data from the byte array
127 * <code>buf</code> starting at index <code>offset</code> in the array
128 * to the stream. Note that this method will block if the
129 * <code>PipedInputStream</code> to which this object is connected has
130 * a buffer that cannot hold all of the bytes to be written.
132 * @param buffer The array containing bytes to write to the stream.
133 * @param offset The index into the array to start writing bytes from.
134 * @param len The number of bytes to write.
136 * @exception IOException If the stream has not been connected or has
137 * been closed.
139 public void write(byte[] buffer, int offset, int len) throws IOException
141 if (sink == null)
142 throw new IOException ("Not connected");
143 if (closed)
144 throw new IOException ("Pipe closed");
146 sink.receive(buffer, offset, len);
150 * This method does nothing.
152 * @exception IOException If the stream is closed.
153 * @specnote You'd think that this method would block until the sink
154 * had read all available data. Thats not the case - this method
155 * appears to be a no-op?
157 public void flush() throws IOException
162 * This method closes this stream so that no more data can be written
163 * to it. Any further attempts to write to this stream may throw an
164 * exception
166 * @exception IOException If an error occurs
168 public void close() throws IOException
170 // A close call on an unconnected PipedOutputStream has no effect.
171 if (sink != null)
173 closed = true;
174 // Notify any waiting readers that the stream is now closed.
175 synchronized (sink)
177 sink.notifyAll();