1 /*
   2  * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 
  29 /**
  30  * Piped character-output streams.
  31  *
  32  * @author      Mark Reinhold
  33  * @since       JDK1.1
  34  */
  35 
  36 public class PipedWriter extends Writer {
  37 
  38     /* REMIND: identification of the read and write sides needs to be
  39        more sophisticated.  Either using thread groups (but what about
  40        pipes within a thread?) or using finalization (but it may be a
  41        long time until the next GC). */
  42     private PipedReader sink;
  43 
  44     /* This flag records the open status of this particular writer. It
  45      * is independent of the status flags defined in PipedReader. It is
  46      * used to do a sanity check on connect.
  47      */
  48     private boolean closed = false;
  49 
  50     /**
  51      * Creates a piped writer connected to the specified piped
  52      * reader. Data characters written to this stream will then be
  53      * available as input from <code>snk</code>.
  54      *
  55      * @param      snk   The piped reader to connect to.
  56      * @exception  IOException  if an I/O error occurs.
  57      */
  58     public PipedWriter(PipedReader snk)  throws IOException {
  59         connect(snk);
  60     }
  61 
  62     /**
  63      * Creates a piped writer that is not yet connected to a
  64      * piped reader. It must be connected to a piped reader,
  65      * either by the receiver or the sender, before being used.
  66      *
  67      * @see     java.io.PipedReader#connect(java.io.PipedWriter)
  68      * @see     java.io.PipedWriter#connect(java.io.PipedReader)
  69      */
  70     public PipedWriter() {
  71     }
  72 
  73     /**
  74      * Connects this piped writer to a receiver. If this object
  75      * is already connected to some other piped reader, an
  76      * <code>IOException</code> is thrown.
  77      * <p>
  78      * If <code>snk</code> is an unconnected piped reader and
  79      * <code>src</code> is an unconnected piped writer, they may
  80      * be connected by either the call:
  81      * <blockquote><pre>
  82      * src.connect(snk)</pre></blockquote>
  83      * or the call:
  84      * <blockquote><pre>
  85      * snk.connect(src)</pre></blockquote>
  86      * The two calls have the same effect.
  87      *
  88      * @param      snk   the piped reader to connect to.
  89      * @exception  IOException  if an I/O error occurs.
  90      */
  91     public synchronized void connect(PipedReader snk) throws IOException {
  92         if (snk == null) {
  93             throw new NullPointerException();
  94         } else if (sink != null || snk.connected) {
  95             throw new IOException("Already connected");
  96         } else if (snk.closedByReader || closed) {
  97             throw new IOException("Pipe closed");
  98         }
  99 
 100         sink = snk;
 101         snk.in = -1;
 102         snk.out = 0;
 103         snk.connected = true;
 104     }
 105 
 106     /**
 107      * Writes the specified <code>char</code> to the 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      * <p>
 112      * Implements the <code>write</code> method of <code>Writer</code>.
 113      *
 114      * @param      c   the <code>char</code> to be written.
 115      * @exception  IOException  if the pipe is
 116      *          <a href=PipedOutputStream.html#BROKEN> <code>broken</code></a>,
 117      *          {@link #connect(java.io.PipedReader) unconnected}, closed
 118      *          or an I/O error occurs.
 119      */
 120     public void write(int c)  throws IOException {
 121         if (sink == null) {
 122             throw new IOException("Pipe not connected");
 123         }
 124         sink.receive(c);
 125     }
 126 
 127     /**
 128      * Writes <code>len</code> characters from the specified character array
 129      * starting at offset <code>off</code> to this piped output stream.
 130      * This method blocks until all the characters are written to the output
 131      * stream.
 132      * If a thread was reading data characters from the connected piped input
 133      * stream, but the thread is no longer alive, then an
 134      * <code>IOException</code> is thrown.
 135      *
 136      * @param      cbuf  the data.
 137      * @param      off   the start offset in the data.
 138      * @param      len   the number of characters to write.
 139      * @exception  IOException  if the pipe is
 140      *          <a href=PipedOutputStream.html#BROKEN> <code>broken</code></a>,
 141      *          {@link #connect(java.io.PipedReader) unconnected}, closed
 142      *          or an I/O error occurs.
 143      */
 144     public void write(char cbuf[], int off, int len) throws IOException {
 145         if (sink == null) {
 146             throw new IOException("Pipe not connected");
 147         } else if ((off | len | (off + len) | (cbuf.length - (off + len))) < 0) {
 148             throw new IndexOutOfBoundsException();
 149         }
 150         sink.receive(cbuf, off, len);
 151     }
 152 
 153     /**
 154      * Flushes this output stream and forces any buffered output characters
 155      * to be written out.
 156      * This will notify any readers that characters are waiting in the pipe.
 157      *
 158      * @exception  IOException  if the pipe is closed, or an I/O error occurs.
 159      */
 160     public synchronized void flush() throws IOException {
 161         if (sink != null) {
 162             if (sink.closedByReader || closed) {
 163                 throw new IOException("Pipe closed");
 164             }
 165             synchronized (sink) {
 166                 sink.notifyAll();
 167             }
 168         }
 169     }
 170 
 171     /**
 172      * Closes this piped output stream and releases any system resources
 173      * associated with this stream. This stream may no longer be used for
 174      * writing characters.
 175      *
 176      * @exception  IOException  if an I/O error occurs.
 177      */
 178     public void close()  throws IOException {
 179         closed = true;
 180         if (sink != null) {
 181             sink.receivedLast();
 182         }
 183     }
 184 }