home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / PipedOutputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.6 KB  |  167 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)PipedOutputStream.java    1.21 98/04/09
  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.21, 04/09/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.  
  42.     /**
  43.      * Creates a piped output stream connected to the specified piped 
  44.      * input stream. Data bytes written to this stream will then be 
  45.      * available as input from <code>snk</code>.
  46.      *
  47.      * @param      snk   The piped input stream to connect to.
  48.      * @exception  IOException  if an I/O error occurs.
  49.      */
  50.     public PipedOutputStream(PipedInputStream snk)  throws IOException {
  51.     connect(snk);
  52.     }
  53.     
  54.     /**
  55.      * Creates a piped output stream that is not yet connected to a 
  56.      * piped input stream. It must be connected to a piped input stream, 
  57.      * either by the receiver or the sender, before being used. 
  58.      *
  59.      * @see     java.io.PipedInputStream#connect(java.io.PipedOutputStream)
  60.      * @see     java.io.PipedOutputStream#connect(java.io.PipedInputStream)
  61.      */
  62.     public PipedOutputStream() {
  63.     }
  64.     
  65.     /**
  66.      * Connects this piped output stream to a receiver. If this object
  67.      * is already connected to some other piped input stream, an 
  68.      * <code>IOException</code> is thrown.
  69.      * <p>
  70.      * If <code>snk</code> is an unconnected piped input stream and 
  71.      * <code>src</code> is an unconnected piped output stream, they may 
  72.      * be connected by either the call:
  73.      * <blockquote><pre>
  74.      * src.connect(snk)</pre></blockquote>
  75.      * or the call:
  76.      * <blockquote><pre>
  77.      * snk.connect(src)</pre></blockquote>
  78.      * The two calls have the same effect.
  79.      *
  80.      * @param      snk   the piped input stream to connect to.
  81.      * @exception  IOException  if an I/O error occurs.
  82.      */
  83.     public synchronized void connect(PipedInputStream snk) throws IOException {
  84.         if (snk == null) {
  85.             throw new NullPointerException();
  86.         } else if (sink != null || snk.connected) {
  87.         throw new IOException("Already connected");
  88.     }
  89.     sink = snk;
  90.     snk.in = -1;
  91.     snk.out = 0;
  92.         snk.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.         if (sink == null) {
  108.             throw new IOException("Pipe not connected");
  109.         }
  110.     sink.receive(b);
  111.     }
  112.  
  113.     /**
  114.      * Writes <code>len</code> bytes from the specified byte array 
  115.      * starting at offset <code>off</code> to this piped output stream. 
  116.      * If a thread was reading data bytes from the connected piped input 
  117.      * stream, but the thread is no longer alive, then an 
  118.      * <code>IOException</code> is thrown.
  119.      *
  120.      * @param      b     the data.
  121.      * @param      off   the start offset in the data.
  122.      * @param      len   the number of bytes to write.
  123.      * @exception  IOException  if an I/O error occurs.
  124.      */
  125.     public void write(byte b[], int off, int len) throws IOException {
  126.         if (sink == null) {
  127.             throw new IOException("Pipe not connected");
  128.         } else if (b == null) {
  129.         throw new NullPointerException();
  130.     } else if ((off < 0) || (off > b.length) || (len < 0) ||
  131.            ((off + len) > b.length) || ((off + len) < 0)) {
  132.         throw new IndexOutOfBoundsException();
  133.     } else if (len == 0) {
  134.         return;
  135.     } 
  136.     sink.receive(b, off, len);
  137.     }
  138.  
  139.     /**
  140.      * Flushes this output stream and forces any buffered output bytes 
  141.      * to be written out. 
  142.      * This will notify any readers that bytes are waiting in the pipe.
  143.      *
  144.      * @exception IOException if an I/O error occurs.
  145.      */
  146.     public synchronized void flush() throws IOException {
  147.     if (sink != null) {
  148.             synchronized (sink) {
  149.                 sink.notifyAll();
  150.             }
  151.     }
  152.     }
  153.  
  154.     /**
  155.      * Closes this piped output stream and releases any system resources 
  156.      * associated with this stream. This stream may no longer be used for 
  157.      * writing bytes.
  158.      *
  159.      * @exception  IOException  if an I/O error occurs.
  160.      */
  161.     public void close()  throws IOException {
  162.     if (sink != null) {
  163.         sink.receivedLast();
  164.     }
  165.     }
  166. }
  167.