PipedReader.java 12 KB

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