1. /*
  2. * @(#)PipedOutputStream.java 1.23 00/02/02
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.io;
  11. import java.io.*;
  12. /**
  13. * A piped output stream can be connected to a piped input stream
  14. * to create a communications pipe. The piped output stream is the
  15. * sending end of the pipe. Typically, data is written to a
  16. * <code>PipedOutputStream</code> object by one thread and data is
  17. * read from the connected <code>PipedInputStream</code> by some
  18. * other thread. Attempting to use both objects from a single thread
  19. * is not recommended as it may deadlock the thread.
  20. *
  21. * @author James Gosling
  22. * @version 1.23, 02/02/00
  23. * @see java.io.PipedInputStream
  24. * @since JDK1.0
  25. */
  26. public
  27. class PipedOutputStream extends OutputStream {
  28. /* REMIND: identification of the read and write sides needs to be
  29. more sophisticated. Either using thread groups (but what about
  30. pipes within a thread?) or using finalization (but it may be a
  31. long time until the next GC). */
  32. private PipedInputStream sink;
  33. /**
  34. * Creates a piped output stream connected to the specified piped
  35. * input stream. Data bytes written to this stream will then be
  36. * available as input from <code>snk</code>.
  37. *
  38. * @param snk The piped input stream to connect to.
  39. * @exception IOException if an I/O error occurs.
  40. */
  41. public PipedOutputStream(PipedInputStream snk) throws IOException {
  42. connect(snk);
  43. }
  44. /**
  45. * Creates a piped output stream that is not yet connected to a
  46. * piped input stream. It must be connected to a piped input stream,
  47. * either by the receiver or the sender, before being used.
  48. *
  49. * @see java.io.PipedInputStream#connect(java.io.PipedOutputStream)
  50. * @see java.io.PipedOutputStream#connect(java.io.PipedInputStream)
  51. */
  52. public PipedOutputStream() {
  53. }
  54. /**
  55. * Connects this piped output stream to a receiver. If this object
  56. * is already connected to some other piped input stream, an
  57. * <code>IOException</code> is thrown.
  58. * <p>
  59. * If <code>snk</code> is an unconnected piped input stream and
  60. * <code>src</code> is an unconnected piped output stream, they may
  61. * be connected by either the call:
  62. * <blockquote><pre>
  63. * src.connect(snk)</pre></blockquote>
  64. * or the call:
  65. * <blockquote><pre>
  66. * snk.connect(src)</pre></blockquote>
  67. * The two calls have the same effect.
  68. *
  69. * @param snk the piped input stream to connect to.
  70. * @exception IOException if an I/O error occurs.
  71. */
  72. public synchronized void connect(PipedInputStream snk) throws IOException {
  73. if (snk == null) {
  74. throw new NullPointerException();
  75. } else if (sink != null || snk.connected) {
  76. throw new IOException("Already connected");
  77. }
  78. sink = snk;
  79. snk.in = -1;
  80. snk.out = 0;
  81. snk.connected = true;
  82. }
  83. /**
  84. * Writes the specified <code>byte</code> to the piped output stream.
  85. * If a thread was reading data bytes from the connected piped input
  86. * stream, but the thread is no longer alive, then an
  87. * <code>IOException</code> is thrown.
  88. * <p>
  89. * Implements the <code>write</code> method of <code>OutputStream</code>.
  90. *
  91. * @param b the <code>byte</code> to be written.
  92. * @exception IOException if an I/O error occurs.
  93. */
  94. public void write(int b) throws IOException {
  95. if (sink == null) {
  96. throw new IOException("Pipe not connected");
  97. }
  98. sink.receive(b);
  99. }
  100. /**
  101. * Writes <code>len</code> bytes from the specified byte array
  102. * starting at offset <code>off</code> to this piped output stream.
  103. * If a thread was reading data bytes from the connected piped input
  104. * stream, but the thread is no longer alive, then an
  105. * <code>IOException</code> is thrown.
  106. *
  107. * @param b the data.
  108. * @param off the start offset in the data.
  109. * @param len the number of bytes to write.
  110. * @exception IOException if an I/O error occurs.
  111. */
  112. public void write(byte b[], int off, int len) throws IOException {
  113. if (sink == null) {
  114. throw new IOException("Pipe not connected");
  115. } else if (b == null) {
  116. throw new NullPointerException();
  117. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  118. ((off + len) > b.length) || ((off + len) < 0)) {
  119. throw new IndexOutOfBoundsException();
  120. } else if (len == 0) {
  121. return;
  122. }
  123. sink.receive(b, off, len);
  124. }
  125. /**
  126. * Flushes this output stream and forces any buffered output bytes
  127. * to be written out.
  128. * This will notify any readers that bytes are waiting in the pipe.
  129. *
  130. * @exception IOException if an I/O error occurs.
  131. */
  132. public synchronized void flush() throws IOException {
  133. if (sink != null) {
  134. synchronized (sink) {
  135. sink.notifyAll();
  136. }
  137. }
  138. }
  139. /**
  140. * Closes this piped output stream and releases any system resources
  141. * associated with this stream. This stream may no longer be used for
  142. * writing bytes.
  143. *
  144. * @exception IOException if an I/O error occurs.
  145. */
  146. public void close() throws IOException {
  147. if (sink != null) {
  148. sink.receivedLast();
  149. }
  150. }
  151. }