home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / PipedOutputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  5.3 KB  |  158 lines

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