1 | /* PipedInputStream.java -- Read portion of piped 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 | // 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 |
|
---|
42 | package 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 (arenn@urbanophile.com)
|
---|
60 | */
|
---|
61 | public 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 | /**
|
---|
72 | * The size of the internal buffer used for input/output.
|
---|
73 | */
|
---|
74 | /* The "Constant Field Values" Javadoc of the Sun J2SE 1.4
|
---|
75 | * specifies 1024.
|
---|
76 | */
|
---|
77 | protected static final int PIPE_SIZE = 1024;
|
---|
78 |
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * This is the internal circular buffer used for storing bytes written
|
---|
82 | * to the pipe and from which bytes are read by this stream
|
---|
83 | */
|
---|
84 | protected byte[] buffer = new byte[PIPE_SIZE];
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * The index into buffer where the next byte from the connected
|
---|
88 | * <code>PipedOutputStream</code> will be written. If this variable is
|
---|
89 | * equal to <code>out</code>, then the buffer is full. If set to < 0,
|
---|
90 | * the buffer is empty.
|
---|
91 | */
|
---|
92 | protected int in = -1;
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * This index into the buffer where bytes will be read from.
|
---|
96 | */
|
---|
97 | protected int out = 0;
|
---|
98 |
|
---|
99 | /** Buffer used to implement single-argument read/receive */
|
---|
100 | private byte[] read_buf = new byte[1];
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Creates a new <code>PipedInputStream</code> that is not connected to a
|
---|
104 | * <code>PipedOutputStream</code>. It must be connected before bytes can
|
---|
105 | * be read from this stream.
|
---|
106 | */
|
---|
107 | public PipedInputStream()
|
---|
108 | {
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * This constructor creates a new <code>PipedInputStream</code> and connects
|
---|
113 | * it to the passed in <code>PipedOutputStream</code>. The stream is then
|
---|
114 | * ready for reading.
|
---|
115 | *
|
---|
116 | * @param source The <code>PipedOutputStream</code> to connect this stream to
|
---|
117 | *
|
---|
118 | * @exception IOException If <code>source</code> is already connected.
|
---|
119 | */
|
---|
120 | public PipedInputStream(PipedOutputStream source) throws IOException
|
---|
121 | {
|
---|
122 | connect(source);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * This method connects this stream to the passed in <code>PipedOutputStream</code>.
|
---|
127 | * This stream is then ready for reading. If this stream is already
|
---|
128 | * connected or has been previously closed, then an exception is thrown
|
---|
129 | *
|
---|
130 | * @param src The <code>PipedOutputStream</code> to connect this stream to
|
---|
131 | *
|
---|
132 | * @exception IOException If this PipedInputStream or <code>source</code>
|
---|
133 | * has been connected already.
|
---|
134 | */
|
---|
135 | public void connect(PipedOutputStream source) throws IOException
|
---|
136 | {
|
---|
137 | // The JDK (1.3) does not appear to check for a previously closed
|
---|
138 | // connection here.
|
---|
139 |
|
---|
140 | if (this.source != null || source.sink != null)
|
---|
141 | throw new IOException ("Already connected");
|
---|
142 |
|
---|
143 | source.sink = this;
|
---|
144 | this.source = source;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * This method receives a byte of input from the source PipedOutputStream.
|
---|
149 | * If the internal circular buffer is full, this method blocks.
|
---|
150 | *
|
---|
151 | * @param byte_received The byte to write to this stream
|
---|
152 | *
|
---|
153 | * @exception IOException if error occurs
|
---|
154 | * @specnote Weird. This method must be some sort of accident.
|
---|
155 | */
|
---|
156 | protected synchronized void receive(int b) throws IOException
|
---|
157 | {
|
---|
158 | read_buf[0] = (byte) (b & 0xff);
|
---|
159 | receive (read_buf, 0, 1);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * This method is used by the connected <code>PipedOutputStream</code> to
|
---|
164 | * write bytes into the buffer.
|
---|
165 | *
|
---|
166 | * @param buf The array containing bytes to write to this stream
|
---|
167 | * @param offset The offset into the array to start writing from
|
---|
168 | * @param len The number of bytes to write.
|
---|
169 | *
|
---|
170 | * @exception IOException If an error occurs
|
---|
171 | * @specnote This code should be in PipedOutputStream.write, but we
|
---|
172 | * put it here in order to support that bizarre recieve(int)
|
---|
173 | * method.
|
---|
174 | */
|
---|
175 | synchronized void receive(byte[] buf, int offset, int len)
|
---|
176 | throws IOException
|
---|
177 | {
|
---|
178 | if (closed)
|
---|
179 | throw new IOException ("Pipe closed");
|
---|
180 |
|
---|
181 | int bufpos = offset;
|
---|
182 | int copylen;
|
---|
183 |
|
---|
184 | while (len > 0)
|
---|
185 | {
|
---|
186 | try
|
---|
187 | {
|
---|
188 | while (in == out)
|
---|
189 | {
|
---|
190 | // The pipe is full. Wake up any readers and wait for them.
|
---|
191 | notifyAll();
|
---|
192 | wait();
|
---|
193 | // The pipe could have been closed while we were waiting.
|
---|
194 | if (closed)
|
---|
195 | throw new IOException ("Pipe closed");
|
---|
196 | }
|
---|
197 | }
|
---|
198 | catch (InterruptedException ix)
|
---|
199 | {
|
---|
200 | throw new InterruptedIOException ();
|
---|
201 | }
|
---|
202 |
|
---|
203 | if (in < 0) // The pipe is empty.
|
---|
204 | in = 0;
|
---|
205 |
|
---|
206 | // Figure out how many bytes from buf can be copied without
|
---|
207 | // overrunning out or going past the length of the buffer.
|
---|
208 | if (in < out)
|
---|
209 | copylen = Math.min (len, out - in);
|
---|
210 | else
|
---|
211 | copylen = Math.min (len, buffer.length - in);
|
---|
212 |
|
---|
213 | // Copy bytes until the pipe is filled, wrapping if necessary.
|
---|
214 | System.arraycopy(buf, bufpos, buffer, in, copylen);
|
---|
215 | len -= copylen;
|
---|
216 | bufpos += copylen;
|
---|
217 | in += copylen;
|
---|
218 | if (in == buffer.length)
|
---|
219 | in = 0;
|
---|
220 | }
|
---|
221 | // Notify readers that new data is in the pipe.
|
---|
222 | notifyAll();
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * This method reads bytes from the stream into a caller supplied buffer.
|
---|
227 | * It starts storing bytes at position <code>offset</code> into the buffer and
|
---|
228 | * reads a maximum of <code>len</code> bytes. Note that this method can actually
|
---|
229 | * read fewer than <code>len</code> bytes. The actual number of bytes read is
|
---|
230 | * returned. A -1 is returned to indicated that no bytes can be read
|
---|
231 | * because the end of the stream was reached. If the stream is already
|
---|
232 | * closed, a -1 will again be returned to indicate the end of the stream.
|
---|
233 | * <p>
|
---|
234 | * This method will block if no bytes are available to be read.
|
---|
235 | *
|
---|
236 | * @param buf The buffer into which bytes will be stored
|
---|
237 | * @param offset The index into the buffer at which to start writing.
|
---|
238 | * @param len The maximum number of bytes to read.
|
---|
239 | */
|
---|
240 | public int read() throws IOException
|
---|
241 | {
|
---|
242 | // Method operates by calling the multibyte overloaded read method
|
---|
243 | // Note that read_buf is an internal instance variable. I allocate it
|
---|
244 | // there to avoid constant reallocation overhead for applications that
|
---|
245 | // call this method in a loop at the cost of some unneeded overhead
|
---|
246 | // if this method is never called.
|
---|
247 |
|
---|
248 | int r = read(read_buf, 0, 1);
|
---|
249 |
|
---|
250 | if (r == -1)
|
---|
251 | return -1;
|
---|
252 | else
|
---|
253 | return read_buf[0];
|
---|
254 | }
|
---|
255 |
|
---|
256 | /**
|
---|
257 | * This method reads bytes from the stream into a caller supplied buffer.
|
---|
258 | * It starts storing bytes at position <code>offset</code> into the buffer and
|
---|
259 | * reads a maximum of <code>len</code> bytes. Note that this method can actually
|
---|
260 | * read fewer than <code>len</code> bytes. The actual number of bytes read is
|
---|
261 | * returned. A -1 is returned to indicated that no bytes can be read
|
---|
262 | * because the end of the stream was reached - ie close() was called on the
|
---|
263 | * connected PipedOutputStream.
|
---|
264 | * <p>
|
---|
265 | * This method will block if no bytes are available to be read.
|
---|
266 | *
|
---|
267 | * @param buf The buffer into which bytes will be stored
|
---|
268 | * @param offset The index into the buffer at which to start writing.
|
---|
269 | * @param len The maximum number of bytes to read.
|
---|
270 | *
|
---|
271 | * @exception IOException If <code>close()/code> was called on this Piped
|
---|
272 | * InputStream.
|
---|
273 | */
|
---|
274 | public synchronized int read(byte[] buf, int offset, int len)
|
---|
275 | throws IOException
|
---|
276 | {
|
---|
277 | if (source == null)
|
---|
278 | throw new IOException ("Not connected");
|
---|
279 | if (closed)
|
---|
280 | throw new IOException ("Pipe closed");
|
---|
281 |
|
---|
282 | // If the buffer is empty, wait until there is something in the pipe
|
---|
283 | // to read.
|
---|
284 | try
|
---|
285 | {
|
---|
286 | while (in < 0)
|
---|
287 | {
|
---|
288 | if (source.closed)
|
---|
289 | return -1;
|
---|
290 | wait();
|
---|
291 | }
|
---|
292 | }
|
---|
293 | catch (InterruptedException ix)
|
---|
294 | {
|
---|
295 | throw new InterruptedIOException();
|
---|
296 | }
|
---|
297 |
|
---|
298 | int total = 0;
|
---|
299 | int copylen;
|
---|
300 |
|
---|
301 | while (true)
|
---|
302 | {
|
---|
303 | // Figure out how many bytes from the pipe can be copied without
|
---|
304 | // overrunning in or going past the length of buf.
|
---|
305 | if (out < in)
|
---|
306 | copylen = Math.min (len, in - out);
|
---|
307 | else
|
---|
308 | copylen = Math.min (len, buffer.length - out);
|
---|
309 |
|
---|
310 | System.arraycopy (buffer, out, buf, offset, copylen);
|
---|
311 | offset += copylen;
|
---|
312 | len -= copylen;
|
---|
313 | out += copylen;
|
---|
314 | total += copylen;
|
---|
315 |
|
---|
316 | if (out == buffer.length)
|
---|
317 | out = 0;
|
---|
318 |
|
---|
319 | if (out == in)
|
---|
320 | {
|
---|
321 | // Pipe is now empty.
|
---|
322 | in = -1;
|
---|
323 | out = 0;
|
---|
324 | }
|
---|
325 |
|
---|
326 | // If output buffer is filled or the pipe is empty, we're done.
|
---|
327 | if (len == 0 || in == -1)
|
---|
328 | {
|
---|
329 | // Notify any waiting outputstream that there is now space
|
---|
330 | // to write.
|
---|
331 | notifyAll();
|
---|
332 | return total;
|
---|
333 | }
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * This method returns the number of bytes that can be read from this stream
|
---|
339 | * before blocking could occur. This is the number of bytes that are
|
---|
340 | * currently unread in the internal circular buffer. Note that once this
|
---|
341 | * many additional bytes are read, the stream may block on a subsequent
|
---|
342 | * read, but it not guaranteed to block.
|
---|
343 | *
|
---|
344 | * @return The number of bytes that can be read before blocking might occur
|
---|
345 | *
|
---|
346 | * @exception IOException If an error occurs
|
---|
347 | */
|
---|
348 | public synchronized int available() throws IOException
|
---|
349 | {
|
---|
350 | // The JDK 1.3 implementation does not appear to check for the closed or
|
---|
351 | // unconnected stream conditions here.
|
---|
352 |
|
---|
353 | if (in < 0)
|
---|
354 | return 0;
|
---|
355 | else if (out < in)
|
---|
356 | return in - out;
|
---|
357 | else
|
---|
358 | return (buffer.length - out) + in;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * This methods closes the stream so that no more data can be read
|
---|
363 | * from it.
|
---|
364 | *
|
---|
365 | * @exception IOException If an error occurs
|
---|
366 | */
|
---|
367 | public synchronized void close() throws IOException
|
---|
368 | {
|
---|
369 | closed = true;
|
---|
370 | // Wake any thread which may be in receive() waiting to write data.
|
---|
371 | notifyAll();
|
---|
372 | }
|
---|
373 | }
|
---|