PipedOutputStream.java 6.3 KB

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