source: trunk/src/gcc/libjava/java/io/PipedInputStream.java@ 64

Last change on this file since 64 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 11.9 KB
Line 
1/* PipedInputStream.java -- Read portion of piped streams.
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38// NOTE: This implementation is very similar to that of PipedReader. If you
39// fix a bug in here, chances are you should make a similar change to the
40// PipedReader code.
41
42package java.io;
43
44/**
45 * An input stream that reads its bytes from an output stream
46 * to which it is connected.
47 * <p>
48 * Data is read and written to an internal buffer. It is highly recommended
49 * that the <code>PipedInputStream</code> and connected <code>PipedOutputStream</code>
50 * be part of different threads. If they are not, the read and write
51 * 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 ([email protected])
60 */
61public class PipedInputStream extends InputStream
62{
63 /** PipedOutputStream to which this is connected. Null only if this
64 * InputStream hasn't been connected yet. */
65 PipedOutputStream source;
66
67 /** Set to true if close() has been called on this InputStream. */
68 boolean closed;
69
70 /**
71 * The size of the internal buffer used for input/output.
72 */
73 protected static final int PIPE_SIZE = 2048;
74
75 /**
76 * This is the internal circular buffer used for storing bytes written
77 * to the pipe and from which bytes are read by this stream
78 */
79 protected byte[] buffer = new byte[PIPE_SIZE];
80
81 /**
82 * The index into buffer where the next byte from the connected
83 * <code>PipedOutputStream</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 protected int in = -1;
88
89 /**
90 * This index into the buffer where bytes will be read from.
91 */
92 protected int out = 0;
93
94 /** Buffer used to implement single-argument read/receive */
95 private byte[] read_buf = new byte[1];
96
97 /**
98 * Creates a new <code>PipedInputStream</code> that is not connected to a
99 * <code>PipedOutputStream</code>. It must be connected before bytes can
100 * be read from this stream.
101 */
102 public PipedInputStream()
103 {
104 }
105
106 /**
107 * This constructor creates a new <code>PipedInputStream</code> and connects
108 * it to the passed in <code>PipedOutputStream</code>. The stream is then
109 * ready for reading.
110 *
111 * @param source The <code>PipedOutputStream</code> to connect this stream to
112 *
113 * @exception IOException If <code>source</code> is already connected.
114 */
115 public PipedInputStream(PipedOutputStream source) throws IOException
116 {
117 connect(source);
118 }
119
120 /**
121 * This method connects this stream to the passed in <code>PipedOutputStream</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>PipedOutputStream</code> to connect this stream to
126 *
127 * @exception IOException If this PipedInputStream or <code>source</code>
128 * has been connected already.
129 */
130 public void connect(PipedOutputStream 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 receives a byte of input from the source PipedOutputStream.
144 * If the internal circular buffer is full, this method blocks.
145 *
146 * @param byte_received The byte to write to this stream
147 *
148 * @exception IOException if error occurs
149 * @specnote Weird. This method must be some sort of accident.
150 */
151 protected synchronized void receive(int b) throws IOException
152 {
153 read_buf[0] = (byte) (b & 0xff);
154 receive (read_buf, 0, 1);
155 }
156
157 /**
158 * This method is used by the connected <code>PipedOutputStream</code> to
159 * write bytes into the buffer.
160 *
161 * @param buf The array containing bytes to write to this stream
162 * @param offset The offset into the array to start writing from
163 * @param len The number of bytes to write.
164 *
165 * @exception IOException If an error occurs
166 * @specnote This code should be in PipedOutputStream.write, but we
167 * put it here in order to support that bizarre recieve(int)
168 * method.
169 */
170 synchronized void receive(byte[] buf, int offset, int len)
171 throws IOException
172 {
173 if (closed)
174 throw new IOException ("Pipe closed");
175
176 int bufpos = offset;
177 int copylen;
178
179 while (len > 0)
180 {
181 try
182 {
183 while (in == out)
184 {
185 // The pipe is full. Wake up any readers and wait for them.
186 notifyAll();
187 wait();
188 // The pipe could have been closed while we were waiting.
189 if (closed)
190 throw new IOException ("Pipe closed");
191 }
192 }
193 catch (InterruptedException ix)
194 {
195 throw new InterruptedIOException ();
196 }
197
198 if (in < 0) // The pipe is empty.
199 in = 0;
200
201 // Figure out how many bytes from buf can be copied without
202 // overrunning out or going past the length of the buffer.
203 if (in < out)
204 copylen = Math.min (len, out - in);
205 else
206 copylen = Math.min (len, buffer.length - in);
207
208 // Copy bytes until the pipe is filled, wrapping if necessary.
209 System.arraycopy(buf, bufpos, buffer, in, copylen);
210 len -= copylen;
211 bufpos += copylen;
212 in += copylen;
213 if (in == buffer.length)
214 in = 0;
215 }
216 // Notify readers that new data is in the pipe.
217 notifyAll();
218 }
219
220 /**
221 * This method reads bytes from the stream into a caller supplied buffer.
222 * It starts storing bytes at position <code>offset</code> into the buffer and
223 * reads a maximum of <code>len</code> bytes. Note that this method can actually
224 * read fewer than <code>len</code> bytes. The actual number of bytes read is
225 * returned. A -1 is returned to indicated that no bytes can be read
226 * because the end of the stream was reached. If the stream is already
227 * closed, a -1 will again be returned to indicate the end of the stream.
228 * <p>
229 * This method will block if no bytes are available to be read.
230 *
231 * @param buf The buffer into which bytes will be stored
232 * @param offset The index into the buffer at which to start writing.
233 * @param len The maximum number of bytes to read.
234 */
235 public int read() throws IOException
236 {
237 // Method operates by calling the multibyte overloaded read method
238 // Note that read_buf is an internal instance variable. I allocate it
239 // there to avoid constant reallocation overhead for applications that
240 // call this method in a loop at the cost of some unneeded overhead
241 // if this method is never called.
242
243 int r = read(read_buf, 0, 1);
244
245 if (r == -1)
246 return -1;
247 else
248 return read_buf[0];
249 }
250
251 /**
252 * This method reads bytes from the stream into a caller supplied buffer.
253 * It starts storing bytes at position <code>offset</code> into the buffer and
254 * reads a maximum of <code>len</code> bytes. Note that this method can actually
255 * read fewer than <code>len</code> bytes. The actual number of bytes read is
256 * returned. A -1 is returned to indicated that no bytes can be read
257 * because the end of the stream was reached - ie close() was called on the
258 * connected PipedOutputStream.
259 * <p>
260 * This method will block if no bytes are available to be read.
261 *
262 * @param buf The buffer into which bytes will be stored
263 * @param offset The index into the buffer at which to start writing.
264 * @param len The maximum number of bytes to read.
265 *
266 * @exception IOException If <code>close()/code> was called on this Piped
267 * InputStream.
268 */
269 public synchronized int read(byte[] buf, int offset, int len)
270 throws IOException
271 {
272 if (source == null)
273 throw new IOException ("Not connected");
274 if (closed)
275 throw new IOException ("Pipe closed");
276
277 // If the buffer is empty, wait until there is something in the pipe
278 // to read.
279 try
280 {
281 while (in < 0)
282 {
283 if (source.closed)
284 return -1;
285 wait();
286 }
287 }
288 catch (InterruptedException ix)
289 {
290 throw new InterruptedIOException();
291 }
292
293 int total = 0;
294 int copylen;
295
296 while (true)
297 {
298 // Figure out how many bytes from the pipe can be copied without
299 // overrunning in or going past the length of buf.
300 if (out < in)
301 copylen = Math.min (len, in - out);
302 else
303 copylen = Math.min (len, buffer.length - out);
304
305 System.arraycopy (buffer, out, buf, offset, copylen);
306 offset += copylen;
307 len -= copylen;
308 out += copylen;
309 total += copylen;
310
311 if (out == buffer.length)
312 out = 0;
313
314 if (out == in)
315 {
316 // Pipe is now empty.
317 in = -1;
318 out = 0;
319 }
320
321 // If output buffer is filled or the pipe is empty, we're done.
322 if (len == 0 || in == -1)
323 {
324 // Notify any waiting outputstream that there is now space
325 // to write.
326 notifyAll();
327 return total;
328 }
329 }
330 }
331
332 /**
333 * This method returns the number of bytes that can be read from this stream
334 * before blocking could occur. This is the number of bytes that are
335 * currently unread in the internal circular buffer. Note that once this
336 * many additional bytes are read, the stream may block on a subsequent
337 * read, but it not guaranteed to block.
338 *
339 * @return The number of bytes that can be read before blocking might occur
340 *
341 * @exception IOException If an error occurs
342 */
343 public synchronized int available() throws IOException
344 {
345 // The JDK 1.3 implementation does not appear to check for the closed or
346 // unconnected stream conditions here.
347
348 if (in < 0)
349 return 0;
350 else if (out < in)
351 return in - out;
352 else
353 return (buffer.length - out) + in;
354 }
355
356 /**
357 * This methods closes the stream so that no more data can be read
358 * from it.
359 *
360 * @exception IOException If an error occurs
361 */
362 public synchronized void close() throws IOException
363 {
364 closed = true;
365 // Wake any thread which may be in receive() waiting to write data.
366 notifyAll();
367 }
368}
Note: See TracBrowser for help on using the repository browser.