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 / PipedWriter.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.0 KB  |  155 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)PipedWriter.java    1.10 98/07/07
  3.  *
  4.  * Copyright 1996-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.  
  18. /**
  19.  * Piped character-output streams.
  20.  *
  21.  * @version     1.10, 98/07/07
  22.  * @author    Mark Reinhold
  23.  * @since    JDK1.1
  24.  */
  25.  
  26. public class PipedWriter extends Writer {
  27.  
  28.     /* REMIND: identification of the read and write sides needs to be
  29.        more sophisticated.  Either using thread groups (but what about
  30.        pipes within a thread?) or using finalization (but it may be a
  31.        long time until the next GC). */
  32.     private PipedReader sink;
  33.  
  34.     /**
  35.      * Creates a piped writer connected to the specified piped 
  36.      * reader. Data characters written to this stream will then be 
  37.      * available as input from <code>snk</code>.
  38.      *
  39.      * @param      snk   The piped reader to connect to.
  40.      * @exception  IOException  if an I/O error occurs.
  41.      */
  42.     public PipedWriter(PipedReader snk)  throws IOException {
  43.     connect(snk);
  44.     }
  45.     
  46.     /**
  47.      * Creates a piped writer that is not yet connected to a 
  48.      * piped reader. It must be connected to a piped reader, 
  49.      * either by the receiver or the sender, before being used. 
  50.      *
  51.      * @see     java.io.PipedReader#connect(java.io.PipedWriter)
  52.      * @see     java.io.PipedWriter#connect(java.io.PipedReader)
  53.      */
  54.     public PipedWriter() {
  55.     }
  56.     
  57.     /**
  58.      * Connects this piped writer to a receiver. If this object
  59.      * is already connected to some other piped reader, an 
  60.      * <code>IOException</code> is thrown.
  61.      * <p>
  62.      * If <code>snk</code> is an unconnected piped reader and 
  63.      * <code>src</code> is an unconnected piped writer, they may 
  64.      * be connected by either the call:
  65.      * <blockquote><pre>
  66.      * src.connect(snk)</pre></blockquote>
  67.      * or the call:
  68.      * <blockquote><pre>
  69.      * snk.connect(src)</pre></blockquote>
  70.      * The two calls have the same effect.
  71.      *
  72.      * @param      snk   the piped reader to connect to.
  73.      * @exception  IOException  if an I/O error occurs.
  74.      */
  75.     public synchronized void connect(PipedReader snk) throws IOException {
  76.         if (snk == null) {
  77.             throw new NullPointerException();
  78.         } else if (sink != null || snk.connected) {
  79.         throw new IOException("Already connected");
  80.     }
  81.     sink = snk;
  82.     snk.in = -1;
  83.     snk.out = 0;
  84.         snk.connected = true;
  85.     }
  86.  
  87.     /**
  88.      * Writes the specified <code>char</code> to the piped output stream. 
  89.      * If a thread was reading data characters from the connected piped input 
  90.      * stream, but the thread is no longer alive, then an 
  91.      * <code>IOException</code> is thrown.
  92.      * <p>
  93.      * Implements the <code>write</code> method of <code>Writer</code>.
  94.      *
  95.      * @param      c   the <code>char</code> to be written.
  96.      * @exception  IOException  if an I/O error occurs.
  97.      */
  98.     public void write(int c)  throws IOException {
  99.         if (sink == null) {
  100.             throw new IOException("Pipe not connected");
  101.         }
  102.     sink.receive(c);
  103.     }
  104.  
  105.     /**
  106.      * Writes <code>len</code> characters from the specified character array 
  107.      * starting at offset <code>off</code> to this piped output stream. 
  108.      * If a thread was reading data characters from the connected piped input 
  109.      * stream, but the thread is no longer alive, then an 
  110.      * <code>IOException</code> is thrown.
  111.      *
  112.      * @param      cbuf  the data.
  113.      * @param      off   the start offset in the data.
  114.      * @param      len   the number of characters to write.
  115.      * @exception  IOException  if an I/O error occurs.
  116.      */
  117.     public void write(char cbuf[], int off, int len) throws IOException {
  118.         if (sink == null) {
  119.             throw new IOException("Pipe not connected");
  120.         } else if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  121.            ((off + len) > cbuf.length) || ((off + len) < 0)) {
  122.         throw new IndexOutOfBoundsException();
  123.     }
  124.     sink.receive(cbuf, off, len);
  125.     }
  126.  
  127.     /**
  128.      * Flushes this output stream and forces any buffered output characters 
  129.      * to be written out. 
  130.      * This will notify any readers that characters are waiting in the pipe.
  131.      *
  132.      * @exception IOException if an I/O error occurs.
  133.      */
  134.     public synchronized void flush() throws IOException {
  135.     if (sink != null) {
  136.             synchronized (sink) {
  137.                 sink.notifyAll();
  138.             }
  139.     }
  140.     }
  141.  
  142.     /**
  143.      * Closes this piped output stream and releases any system resources 
  144.      * associated with this stream. This stream may no longer be used for 
  145.      * writing characters.
  146.      *
  147.      * @exception  IOException  if an I/O error occurs.
  148.      */
  149.     public void close()  throws IOException {
  150.     if (sink != null) {
  151.         sink.receivedLast();
  152.     }
  153.     }
  154. }
  155.