home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / java.z / PipedOutputStream.java < prev    next >
Text File  |  1996-05-03  |  3KB  |  113 lines

  1. /*
  2.  * @(#)PipedOutputStream.java    1.10 96/02/26 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. import java.io.*;
  23.  
  24. /**
  25.  * Piped output stream, must be connected to a PipedInputStream.
  26.  * A thread reading from a PipedInputStream receives data from
  27.  * a thread writing to the PipedOutputStream it is connected to.
  28.  * @see    PipedInputStream
  29.  * @version     96/02/26
  30.  * @author    James Gosling
  31.  */
  32. public
  33. class PipedOutputStream extends OutputStream {
  34.  
  35.     /* REMIND: identification of the read and write sides needs to be
  36.        more sophisticated.  Either using thread groups (but what about
  37.        pipes within a thread?) or using finalization (but it may be a
  38.        long time until the next GC). */
  39.     private PipedInputStream sink;
  40.  
  41.     /**
  42.      * Creates an output file connected to the specified 
  43.      * PipedInputStream.
  44.      * @param snk The InputStream to connect to.
  45.      */
  46.     public PipedOutputStream(PipedInputStream snk)  throws IOException {
  47.     connect(snk);
  48.     }
  49.     
  50.     /**
  51.      * Creates an output file that isn't connected to anything (yet).
  52.      * It must be connected before being used.
  53.      */
  54.     public PipedOutputStream() {
  55.     }
  56.     
  57.     /**
  58.      * Connect this output stream to a receiver.
  59.      * @param snk    The InputStream to connect to.
  60.      */
  61.     public void connect(PipedInputStream snk) throws IOException {
  62.     sink = snk;
  63.     snk.closed = false;
  64.     snk.in = -1;
  65.     snk.out = 0;
  66.     }
  67.  
  68.     /**
  69.      * Write a byte. This method will block until the byte is actually
  70.      * written.
  71.      * @param b the byte to be written
  72.      * @exception IOException If an I/O error has occurred.
  73.      */
  74.     public void write(int b)  throws IOException {
  75.     sink.receive(b);
  76.     }
  77.  
  78.     /**
  79.      * Writes a sub array of bytes.
  80.      * @param b    the data to be written
  81.      * @param off    the start offset in the data
  82.      * @param len    the number of bytes that are written
  83.      * @exception IOException If an I/O error has occurred.
  84.      */
  85.     public void write(byte b[], int off, int len) throws IOException {
  86.     sink.receive(b, off, len);
  87.     }
  88.  
  89.     /**
  90.      * Flushes the stream. This will notify any readers that bytes are
  91.      * waiting in the pipe.
  92.      * @exception IOException If an I/O error has occurred.
  93.      */
  94.     public synchronized void flush() throws IOException {
  95.     if (sink != null) {
  96.             synchronized (sink) {
  97.                 sink.notifyAll();
  98.             }
  99.     }
  100.     }
  101.     /**
  102.      * Closes the stream. This method must be called
  103.      * to release any resources associated with the
  104.      * stream.
  105.      * @exception IOException If an I/O error has occurred.
  106.      */
  107.     public void close()  throws IOException {
  108.     if (sink != null) {
  109.         sink.receivedLast();
  110.     }
  111.     }
  112. }
  113.