1 | /* PipedReader.java -- Read portion of piped character streams.
|
---|
2 | Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
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.
|
---|
10 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
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.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 | package java.io;
|
---|
39 |
|
---|
40 | // NOTE: This implementation is very similar to that of PipedInputStream.
|
---|
41 | // If you fix a bug in here, chances are you should make a similar change to
|
---|
42 | // the PipedInputStream code.
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * An input stream that reads characters from a piped writer to which it is
|
---|
46 | * connected.
|
---|
47 | * <p>
|
---|
48 | * Data is read and written to an internal buffer. It is highly recommended
|
---|
49 | * that the <code>PipedReader</code> and connected <code>PipedWriter</code>
|
---|
50 | * be part of different threads. If they are not, there is a possibility
|
---|
51 | * that the read and write operations could deadlock their thread.
|
---|
52 | *
|
---|
53 | * @specnote The JDK implementation appears to have some undocumented
|
---|
54 | * functionality where it keeps track of what thread is writing
|
---|
55 | * to pipe and throws an IOException if that thread susequently
|
---|
56 | * dies. This behaviour seems dubious and unreliable - we don't
|
---|
57 | * implement it.
|
---|
58 | *
|
---|
59 | * @author Aaron M. Renn (arenn@urbanophile.com)
|
---|
60 | */
|
---|
61 | public class PipedReader extends Reader
|
---|
62 | {
|
---|
63 | /** PipedWriter to which this is connected. Null only if this
|
---|
64 | * Reader hasn't been connected yet. */
|
---|
65 | PipedWriter source;
|
---|
66 |
|
---|
67 | /** Set to true if close() has been called on this Reader. */
|
---|
68 | boolean closed;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * The size of the internal buffer used for input/output.
|
---|
72 | */
|
---|
73 | static final int PIPE_SIZE = 2048;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * This is the internal circular buffer used for storing chars written
|
---|
77 | * to the pipe and from which chars are read by this stream
|
---|
78 | */
|
---|
79 | char[] buffer = new char[PIPE_SIZE];
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * The index into buffer where the next char from the connected
|
---|
83 | * <code>PipedWriter</code> will be written. If this variable is
|
---|
84 | * equal to <code>out</code>, then the buffer is full. If set to < 0,
|
---|
85 | * the buffer is empty.
|
---|
86 | */
|
---|
87 | int in = -1;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * This index into the buffer where chars will be read from.
|
---|
91 | */
|
---|
92 | int out = 0;
|
---|
93 |
|
---|
94 | /** Buffer used to implement single-argument read/receive */
|
---|
95 | char[] read_buf = new char[1];
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Creates a new <code>PipedReader</code> that is not connected to a
|
---|
99 | * <code>PipedWriter</code>. It must be connected before chars can
|
---|
100 | * be read from this stream.
|
---|
101 | */
|
---|
102 | public PipedReader()
|
---|
103 | {
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * This constructor creates a new <code>PipedReader</code> and connects
|
---|
108 | * it to the passed in <code>PipedWriter</code>. The stream is then
|
---|
109 | * ready for reading.
|
---|
110 | *
|
---|
111 | * @param source The <code>PipedWriter</code> to connect this stream to
|
---|
112 | *
|
---|
113 | * @exception IOException If <code>source</code> is already connected.
|
---|
114 | */
|
---|
115 | public PipedReader(PipedWriter source) throws IOException
|
---|
116 | {
|
---|
117 | connect(source);
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * This method connects this stream to the passed in <code>PipedWriter</code>.
|
---|
122 | * This stream is then ready for reading. If this stream is already
|
---|
123 | * connected or has been previously closed, then an exception is thrown
|
---|
124 | *
|
---|
125 | * @param src The <code>PipedWriter</code> to connect this stream to
|
---|
126 | *
|
---|
127 | * @exception IOException If this PipedReader or <code>source</code>
|
---|
128 | * has been connected already.
|
---|
129 | */
|
---|
130 | public void connect(PipedWriter source) throws IOException
|
---|
131 | {
|
---|
132 | // The JDK (1.3) does not appear to check for a previously closed
|
---|
133 | // connection here.
|
---|
134 |
|
---|
135 | if (this.source != null || source.sink != null)
|
---|
136 | throw new IOException ("Already connected");
|
---|
137 |
|
---|
138 | source.sink = this;
|
---|
139 | this.source = source;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * This method is used by the connected <code>PipedWriter</code> to
|
---|
144 | * write chars into the buffer.
|
---|
145 | *
|
---|
146 | * @param buf The array containing chars to write to this stream
|
---|
147 | * @param offset The offset into the array to start writing from
|
---|
148 | * @param len The number of chars to write.
|
---|
149 | *
|
---|
150 | * @exception IOException If an error occurs
|
---|
151 | * @specnote This code should be in PipedWriter.write, but we
|
---|
152 | * put it here in order to support that bizarre recieve(int)
|
---|
153 | * method.
|
---|
154 | */
|
---|
155 | void receive(char[] buf, int offset, int len)
|
---|
156 | throws IOException
|
---|
157 | {
|
---|
158 | synchronized (lock)
|
---|
159 | {
|
---|
160 | if (closed)
|
---|
161 | throw new IOException ("Pipe closed");
|
---|
162 |
|
---|
163 | int bufpos = offset;
|
---|
164 | int copylen;
|
---|
165 |
|
---|
166 | while (len > 0)
|
---|
167 | {
|
---|
168 | try
|
---|
169 | {
|
---|
170 | while (in == out)
|
---|
171 | {
|
---|
172 | // The pipe is full. Wake up any readers and wait for them.
|
---|
173 | lock.notifyAll();
|
---|
174 | lock.wait();
|
---|
175 | // The pipe could have been closed while we were waiting.
|
---|
176 | if (closed)
|
---|
177 | throw new IOException ("Pipe closed");
|
---|
178 | }
|
---|
179 | }
|
---|
180 | catch (InterruptedException ix)
|
---|
181 | {
|
---|
182 | throw new InterruptedIOException ();
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (in < 0) // The pipe is empty.
|
---|
186 | in = 0;
|
---|
187 |
|
---|
188 | // Figure out how many chars from buf can be copied without
|
---|
189 | // overrunning out or going past the length of the buffer.
|
---|
190 | if (in < out)
|
---|
191 | copylen = Math.min (len, out - in);
|
---|
192 | else
|
---|
193 | copylen = Math.min (len, buffer.length - in);
|
---|
194 |
|
---|
195 | // Copy chars until the pipe is filled, wrapping if necessary.
|
---|
196 | System.arraycopy(buf, bufpos, buffer, in, copylen);
|
---|
197 | len -= copylen;
|
---|
198 | bufpos += copylen;
|
---|
199 | in += copylen;
|
---|
200 | if (in == buffer.length)
|
---|
201 | in = 0;
|
---|
202 | }
|
---|
203 | // Notify readers that new data is in the pipe.
|
---|
204 | lock.notifyAll();
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * This method reads chars from the stream into a caller supplied buffer.
|
---|
210 | * It starts storing chars at position <code>offset</code> into the buffer and
|
---|
211 | * reads a maximum of <code>len</code> chars. Note that this method can actually
|
---|
212 | * read fewer than <code>len</code> chars. The actual number of chars read is
|
---|
213 | * returned. A -1 is returned to indicated that no chars can be read
|
---|
214 | * because the end of the stream was reached. If the stream is already
|
---|
215 | * closed, a -1 will again be returned to indicate the end of the stream.
|
---|
216 | * <p>
|
---|
217 | * This method will block if no chars are available to be read.
|
---|
218 | *
|
---|
219 | * @param buf The buffer into which chars will be stored
|
---|
220 | * @param offset The index into the buffer at which to start writing.
|
---|
221 | * @param len The maximum number of chars to read.
|
---|
222 | */
|
---|
223 | public int read() throws IOException
|
---|
224 | {
|
---|
225 | // Method operates by calling the multichar overloaded read method
|
---|
226 | // Note that read_buf is an internal instance variable. I allocate it
|
---|
227 | // there to avoid constant reallocation overhead for applications that
|
---|
228 | // call this method in a loop at the cost of some unneeded overhead
|
---|
229 | // if this method is never called.
|
---|
230 |
|
---|
231 | int r = read(read_buf, 0, 1);
|
---|
232 |
|
---|
233 | if (r == -1)
|
---|
234 | return -1;
|
---|
235 | else
|
---|
236 | return read_buf[0];
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * This method reads characters from the stream into a caller supplied buffer.
|
---|
241 | * It starts storing chars at position <code>offset</code> into the buffer and
|
---|
242 | * reads a maximum of <code>len</code> chars. Note that this method can actually
|
---|
243 | * read fewer than <code>len</code> chars. The actual number of chars read is
|
---|
244 | * returned. A -1 is returned to indicated that no chars can be read
|
---|
245 | * because the end of the stream was reached - ie close() was called on the
|
---|
246 | * connected PipedWriter.
|
---|
247 | * <p>
|
---|
248 | * This method will block if no chars are available to be read.
|
---|
249 | *
|
---|
250 | * @param buf The buffer into which chars will be stored
|
---|
251 | * @param offset The index into the buffer at which to start writing.
|
---|
252 | * @param len The maximum number of chars to read.
|
---|
253 | *
|
---|
254 | * @exception IOException If <code>close()/code> was called on this Piped
|
---|
255 | * Reader.
|
---|
256 | */
|
---|
257 | public int read(char[] buf, int offset, int len)
|
---|
258 | throws IOException
|
---|
259 | {
|
---|
260 | synchronized (lock)
|
---|
261 | {
|
---|
262 | if (source == null)
|
---|
263 | throw new IOException ("Not connected");
|
---|
264 | if (closed)
|
---|
265 | throw new IOException ("Pipe closed");
|
---|
266 |
|
---|
267 | // If the buffer is empty, wait until there is something in the pipe
|
---|
268 | // to read.
|
---|
269 | try
|
---|
270 | {
|
---|
271 | while (in < 0)
|
---|
272 | {
|
---|
273 | if (source.closed)
|
---|
274 | return -1;
|
---|
275 | lock.wait();
|
---|
276 | }
|
---|
277 | }
|
---|
278 | catch (InterruptedException ix)
|
---|
279 | {
|
---|
280 | throw new InterruptedIOException();
|
---|
281 | }
|
---|
282 |
|
---|
283 | int total = 0;
|
---|
284 | int copylen;
|
---|
285 |
|
---|
286 | while (true)
|
---|
287 | {
|
---|
288 | // Figure out how many chars from the pipe can be copied without
|
---|
289 | // overrunning in or going past the length of buf.
|
---|
290 | if (out < in)
|
---|
291 | copylen = Math.min (len, in - out);
|
---|
292 | else
|
---|
293 | copylen = Math.min (len, buffer.length - out);
|
---|
294 |
|
---|
295 | System.arraycopy (buffer, out, buf, offset, copylen);
|
---|
296 | offset += copylen;
|
---|
297 | len -= copylen;
|
---|
298 | out += copylen;
|
---|
299 | total += copylen;
|
---|
300 |
|
---|
301 | if (out == buffer.length)
|
---|
302 | out = 0;
|
---|
303 |
|
---|
304 | if (out == in)
|
---|
305 | {
|
---|
306 | // Pipe is now empty.
|
---|
307 | in = -1;
|
---|
308 | out = 0;
|
---|
309 | }
|
---|
310 |
|
---|
311 | // If output buffer is filled or the pipe is empty, we're done.
|
---|
312 | if (len == 0 || in == -1)
|
---|
313 | {
|
---|
314 | // Notify any waiting Writer that there is now space
|
---|
315 | // to write.
|
---|
316 | lock.notifyAll();
|
---|
317 | return total;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | public boolean ready() throws IOException
|
---|
324 | {
|
---|
325 | // The JDK 1.3 implementation does not appear to check for the closed or
|
---|
326 | // unconnected stream conditions here. However, checking for a
|
---|
327 | // closed stream is explicitly required by the JDK 1.2 and 1.3
|
---|
328 | // documentation (for Reader.close()), so we do it.
|
---|
329 |
|
---|
330 | synchronized (lock)
|
---|
331 | {
|
---|
332 | if (closed)
|
---|
333 | throw new IOException("Pipe closed");
|
---|
334 |
|
---|
335 | if (in < 0)
|
---|
336 | return false;
|
---|
337 |
|
---|
338 | int count;
|
---|
339 | if (out < in)
|
---|
340 | count = in - out;
|
---|
341 | else
|
---|
342 | count = (buffer.length - out) - in;
|
---|
343 |
|
---|
344 | return (count > 0);
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * This methods closes the stream so that no more data can be read
|
---|
350 | * from it.
|
---|
351 | *
|
---|
352 | * @exception IOException If an error occurs
|
---|
353 | */
|
---|
354 | public void close() throws IOException
|
---|
355 | {
|
---|
356 | synchronized (lock)
|
---|
357 | {
|
---|
358 | closed = true;
|
---|
359 | // Wake any thread which may be in receive() waiting to write data.
|
---|
360 | lock.notifyAll();
|
---|
361 | }
|
---|
362 | }
|
---|
363 | }
|
---|