PipedInputStream.java 13 KB

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