PipedWriter.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* PipedWriter.java -- Write portion of piped character streams.
  2. Copyright (C) 1998, 2000, 2001, 2003 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 PipedOutputStream.
  33. // If you fix a bug in here, chances are you should make a similar change to
  34. // the PipedOutputStream code.
  35. /**
  36. * This class writes its chars to a <code>PipedReader</code> to
  37. * which it is connected.
  38. * <p>
  39. * It is highly recommended that a <code>PipedWriter</code> and its
  40. * connected <code>PipedReader</code> be in different threads. If
  41. * they are in the same thread, read and write operations could deadlock
  42. * the thread.
  43. *
  44. * @author Aaron M. Renn (arenn@urbanophile.com)
  45. */
  46. public class PipedWriter extends Writer
  47. {
  48. /** Target PipedReader to which this is connected. Null only if this
  49. * Writer hasn't been connected yet. */
  50. PipedReader sink;
  51. /** Set to true if close() has been called on this Writer. */
  52. boolean closed;
  53. /** Buffer used to implement single-argument write */
  54. char[] read_buf = new char[1];
  55. /**
  56. * Create an unconnected PipedWriter. It must be connected
  57. * to a <code>PipedReader</code> using the <code>connect</code>
  58. * method prior to writing any data or an exception will be thrown.
  59. */
  60. public PipedWriter()
  61. {
  62. }
  63. /**
  64. * Create a new <code>PipedWriter</code> instance
  65. * to write to the specified <code>PipedReader</code>. This stream
  66. * is then ready for writing.
  67. *
  68. * @param sink The <code>PipedReader</code> to connect this stream to.
  69. *
  70. * @exception IOException If <code>sink</code> has already been connected
  71. * to a different PipedWriter.
  72. */
  73. public PipedWriter(PipedReader sink) throws IOException
  74. {
  75. sink.connect(this);
  76. }
  77. /**
  78. * Connects this object to the specified <code>PipedReader</code>
  79. * object. This stream will then be ready for writing.
  80. *
  81. * @param sink The <code>PipedReader</code> to connect this stream to
  82. *
  83. * @exception IOException If the stream has not been connected or has
  84. * been closed.
  85. */
  86. public void connect(PipedReader sink) throws IOException
  87. {
  88. if (this.sink != null || sink.source != null)
  89. throw new IOException ("Already connected");
  90. sink.connect(this);
  91. }
  92. /**
  93. * Write a single char of date to the stream. Note that this method will
  94. * block if the <code>PipedReader</code> to which this object is
  95. * connected has a full buffer.
  96. *
  97. * @param b The char of data to be written, passed as an <code>int</code>.
  98. *
  99. * @exception IOException If the stream has not been connected or has
  100. * been closed.
  101. */
  102. public void write(int b) throws IOException
  103. {
  104. read_buf[0] = (char) (b & 0xffff);
  105. sink.receive (read_buf, 0, 1);
  106. }
  107. /**
  108. * This method writes <code>len</code> chars of data from the char array
  109. * <code>buf</code> starting at index <code>offset</code> in the array
  110. * to the stream. Note that this method will block if the
  111. * <code>PipedReader</code> to which this object is connected has
  112. * a buffer that cannot hold all of the chars to be written.
  113. *
  114. * @param buffer The array containing chars to write to the stream.
  115. * @param offset The index into the array to start writing chars from.
  116. * @param len The number of chars to write.
  117. *
  118. * @exception IOException If the stream has not been connected or has
  119. * been closed.
  120. */
  121. public void write(char[] buffer, int offset, int len) throws IOException
  122. {
  123. if (sink == null)
  124. throw new IOException ("Not connected");
  125. if (closed)
  126. throw new IOException ("Pipe closed");
  127. sink.receive(buffer, offset, len);
  128. }
  129. /**
  130. * This method does nothing.
  131. *
  132. * @exception IOException If the stream is closed.
  133. * @specnote You'd think that this method would block until the sink
  134. * had read all available data. Thats not the case - this method
  135. * appears to be a no-op?
  136. */
  137. public void flush() throws IOException
  138. {
  139. if (closed)
  140. throw new IOException ("Pipe closed");
  141. }
  142. /**
  143. * This method closes this stream so that no more data can be written
  144. * to it. Any further attempts to write to this stream may throw an
  145. * exception
  146. *
  147. * @exception IOException If an error occurs
  148. */
  149. public void close() throws IOException
  150. {
  151. // A close call on an unconnected PipedWriter has no effect.
  152. if (sink != null)
  153. {
  154. closed = true;
  155. // Notify any waiting readers that the stream is now closed.
  156. synchronized (sink)
  157. {
  158. sink.notifyAll();
  159. }
  160. }
  161. }
  162. }