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