home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PipedOutputStream.java < prev    next >
Text File  |  1997-05-20  |  5KB  |  144 lines

  1. /*
  2.  * @(#)PipedOutputStream.java    1.14 97/02/10
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.io;
  24.  
  25. import java.io.*;
  26.  
  27. /**
  28.  * A piped output stream is the sending end of a communications 
  29.  * pipe. Two threads can communicate by having one thread send data 
  30.  * through a piped output stream and having the other thread read the 
  31.  * data through a piped input stream. 
  32.  *
  33.  * @author  James Gosling
  34.  * @version 1.14, 02/10/97
  35.  * @see     java.io.PipedInputStream
  36.  * @since   JDK1.0
  37.  */
  38. public
  39. class PipedOutputStream extends OutputStream {
  40.  
  41.     /* REMIND: identification of the read and write sides needs to be
  42.        more sophisticated.  Either using thread groups (but what about
  43.        pipes within a thread?) or using finalization (but it may be a
  44.        long time until the next GC). */
  45.     private PipedInputStream sink;
  46.     boolean connected = false;
  47.  
  48.     /**
  49.      * Creates a piped output stream connected to the specified piped 
  50.      * input stream. 
  51.      *
  52.      * @param      snk   The piped input stream to connect to.
  53.      * @exception  IOException  if an I/O error occurs.
  54.      * @since      JDK1.0
  55.      */
  56.     public PipedOutputStream(PipedInputStream snk)  throws IOException {
  57.     connect(snk);
  58.     }
  59.     
  60.     /**
  61.      * Creates a piped output stream that is not yet connected to a 
  62.      * piped input stream. It must be connected to a piped input stream, 
  63.      * either by the receiver or the sender, before being used. 
  64.      *
  65.      * @see     java.io.PipedInputStream#connect(java.io.PipedOutputStream)
  66.      * @see     java.io.PipedOutputStream#connect(java.io.PipedInputStream)
  67.      * @since   JDK1.0
  68.      */
  69.     public PipedOutputStream() {
  70.     }
  71.     
  72.     /**
  73.      * Connects this piped output stream to a receiver. 
  74.      *
  75.      * @param      snk   the piped output stream to connect to.
  76.      * @exception  IOException  if an I/O error occurs.
  77.      * @since      JDK1.0
  78.      */
  79.     public void connect(PipedInputStream snk) throws IOException {
  80.     if (connected || snk.connected) {
  81.         throw new IOException("Already connected");
  82.     }
  83.     sink = snk;
  84.     snk.closed = false;
  85.     snk.in = -1;
  86.     snk.out = 0;
  87.     connected = true;
  88.     }
  89.  
  90.     /**
  91.      * Writes the specified <code>byte</code> to the piped output stream.
  92.      *
  93.      * @param      b   the <code>byte</code> to be written.
  94.      * @exception  IOException  if an I/O error occurs.
  95.      * @since      JDK1.0
  96.      */
  97.     public void write(int b)  throws IOException {
  98.     sink.receive(b);
  99.     }
  100.  
  101.     /**
  102.      * Writes <code>len</code> bytes from the specified byte array 
  103.      * starting at offset <code>off</code> to this piped output stream. 
  104.      *
  105.      * @param      b     the data.
  106.      * @param      off   the start offset in the data.
  107.      * @param      len   the number of bytes to write.
  108.      * @exception  IOException  if an I/O error occurs.
  109.      * @since      JDK1.0
  110.      */
  111.     public void write(byte b[], int off, int len) throws IOException {
  112.     sink.receive(b, off, len);
  113.     }
  114.  
  115.     /**
  116.      * Flushes this output stream and forces any buffered output bytes 
  117.      * to be written out. 
  118.      * This will notify any readers that bytes are waiting in the pipe.
  119.      *
  120.      * @exception IOException if an I/O error occurs.
  121.      * @since     JDK1.0
  122.      */
  123.     public synchronized void flush() throws IOException {
  124.     if (sink != null) {
  125.             synchronized (sink) {
  126.                 sink.notifyAll();
  127.             }
  128.     }
  129.     }
  130.  
  131.     /**
  132.      * Closes this piped output stream and releases any system resources 
  133.      * associated with this stream. 
  134.      *
  135.      * @exception  IOException  if an I/O error occurs.
  136.      * @since      JDK1.0
  137.      */
  138.     public void close()  throws IOException {
  139.     if (sink != null) {
  140.         sink.receivedLast();
  141.     }
  142.     }
  143. }
  144.