1 /* PipedReader.java -- Read portion of piped character streams.
2 Copyright (C) 1998, 1999, 2000, 2001 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)
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
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
29 // NOTE: This implementation is very similar to that of PipedInputStream.
30 // If you fix a bug in here, chances are you should make a similar change to
31 // the PipedInputStream code.
34 * An input stream that reads characters from a piped writer to which it is
37 * Data is read and written to an internal buffer. It is highly recommended
38 * that the <code>PipedReader</code> and connected <code>PipedWriter</code>
39 * be part of different threads. If they are not, there is a possibility
40 * that the read and write operations could deadlock their thread.
42 * @specnote The JDK implementation appears to have some undocumented
43 * functionality where it keeps track of what thread is writing
44 * to pipe and throws an IOException if that thread susequently
45 * dies. This behaviour seems dubious and unreliable - we don't
48 * @author Aaron M. Renn (arenn@urbanophile.com)
50 public class PipedReader extends Reader
52 /** PipedWriter to which this is connected. Null only if this
53 * Reader hasn't been connected yet. */
56 /** Set to true if close() has been called on this Reader. */
60 * The size of the internal buffer used for input/output.
62 static final int PIPE_SIZE = 2048;
65 * This is the internal circular buffer used for storing chars written
66 * to the pipe and from which chars are read by this stream
68 char[] buffer = new char[PIPE_SIZE];
71 * The index into buffer where the next char from the connected
72 * <code>PipedWriter</code> will be written. If this variable is
73 * equal to <code>out</code>, then the buffer is full. If set to < 0,
74 * the buffer is empty.
79 * This index into the buffer where chars will be read from.
83 /** Buffer used to implement single-argument read/receive */
84 char[] read_buf = new char[1];
87 * Creates a new <code>PipedReader</code> that is not connected to a
88 * <code>PipedWriter</code>. It must be connected before chars can
89 * be read from this stream.
96 * This constructor creates a new <code>PipedReader</code> and connects
97 * it to the passed in <code>PipedWriter</code>. The stream is then
100 * @param source The <code>PipedWriter</code> to connect this stream to
102 * @exception IOException If <code>source</code> is already connected.
104 public PipedReader(PipedWriter source) throws IOException
110 * This method connects this stream to the passed in <code>PipedWriter</code>.
111 * This stream is then ready for reading. If this stream is already
112 * connected or has been previously closed, then an exception is thrown
114 * @param src The <code>PipedWriter</code> to connect this stream to
116 * @exception IOException If this PipedReader or <code>source</code>
117 * has been connected already.
119 public void connect(PipedWriter source) throws IOException
121 // The JDK (1.3) does not appear to check for a previously closed
124 if (this.source != null || source.sink != null)
125 throw new IOException ("Already connected");
128 this.source = source;
132 * This method is used by the connected <code>PipedWriter</code> to
133 * write chars into the buffer.
135 * @param buf The array containing chars to write to this stream
136 * @param offset The offset into the array to start writing from
137 * @param len The number of chars to write.
139 * @exception IOException If an error occurs
140 * @specnote This code should be in PipedWriter.write, but we
141 * put it here in order to support that bizarre recieve(int)
144 void receive(char[] buf, int offset, int len)
150 throw new IOException ("Pipe closed");
161 // The pipe is full. Wake up any readers and wait for them.
164 // The pipe could have been closed while we were waiting.
166 throw new IOException ("Pipe closed");
169 catch (InterruptedException ix)
171 throw new InterruptedIOException ();
174 if (in < 0) // The pipe is empty.
177 // Figure out how many chars from buf can be copied without
178 // overrunning out or going past the length of the buffer.
180 copylen = Math.min (len, out - in);
182 copylen = Math.min (len, buffer.length - in);
184 // Copy chars until the pipe is filled, wrapping if neccessary.
185 System.arraycopy(buf, bufpos, buffer, in, copylen);
189 if (in == buffer.length)
192 // Notify readers that new data is in the pipe.
198 * This method reads chars from the stream into a caller supplied buffer.
199 * It starts storing chars at position <code>offset</code> into the buffer and
200 * reads a maximum of <cod>>len</code> chars. Note that this method can actually
201 * read fewer than <code>len</code> chars. The actual number of chars read is
202 * returned. A -1 is returned to indicated that no chars can be read
203 * because the end of the stream was reached. If the stream is already
204 * closed, a -1 will again be returned to indicate the end of the stream.
206 * This method will block if no chars are available to be read.
208 * @param buf The buffer into which chars will be stored
209 * @param offset The index into the buffer at which to start writing.
210 * @param len The maximum number of chars to read.
212 public int read() throws IOException
214 // Method operates by calling the multichar overloaded read method
215 // Note that read_buf is an internal instance variable. I allocate it
216 // there to avoid constant reallocation overhead for applications that
217 // call this method in a loop at the cost of some unneeded overhead
218 // if this method is never called.
220 int r = read(read_buf, 0, 1);
229 * This method reads characters from the stream into a caller supplied buffer.
230 * It starts storing chars at position <code>offset</code> into the buffer and
231 * reads a maximum of <cod>>len</code> chars. Note that this method can actually
232 * read fewer than <code>len</code> chars. The actual number of chars read is
233 * returned. A -1 is returned to indicated that no chars can be read
234 * because the end of the stream was reached - ie close() was called on the
235 * connected PipedWriter.
237 * This method will block if no chars are available to be read.
239 * @param buf The buffer into which chars will be stored
240 * @param offset The index into the buffer at which to start writing.
241 * @param len The maximum number of chars to read.
243 * @exception IOException If <code>close()/code> was called on this Piped
246 public int read(char[] buf, int offset, int len)
252 throw new IOException ("Not connected");
254 throw new IOException ("Pipe closed");
256 // If the buffer is empty, wait until there is something in the pipe
267 catch (InterruptedException ix)
269 throw new InterruptedIOException();
277 // Figure out how many chars from the pipe can be copied without
278 // overrunning in or going past the length of buf.
280 copylen = Math.min (len, in - out);
282 copylen = Math.min (len, buffer.length - out);
284 System.arraycopy (buffer, out, buf, offset, copylen);
290 if (out == buffer.length)
295 // Pipe is now empty.
300 // If output buffer is filled or the pipe is empty, we're done.
301 if (len == 0 || in == -1)
303 // Notify any waiting Writer that there is now space
312 public boolean ready() throws IOException
314 // The JDK 1.3 implementation does not appear to check for the closed or
315 // unconnected stream conditions here.
326 count = (buffer.length - out) - in;
333 * This methods closes the stream so that no more data can be read
336 * @exception IOException If an error occurs
338 public void close() throws IOException
343 // Wake any thread which may be in receive() waiting to write data.