1 /*
2  * Copyright (c) 1996, 2019, 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       1.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}.
54      *
55      * @param      snk   The piped reader to connect to.
56      * @throws     IOException  if an I/O error occurs.
57      */
PipedWriter(PipedReader snk)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      */
PipedWriter()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} is thrown.
77      * <p>
78      * If {@code snk} is an unconnected piped reader and
79      * {@code src} 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      * @throws     IOException  if an I/O error occurs.
90      */
connect(PipedReader snk)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} 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} is thrown.
111      * <p>
112      * Implements the {@code write} method of {@code Writer}.
113      *
114      * @param   c   the {@code char} to be written.
115      * @throw   IOException  if the pipe is
116      *          <a href=PipedOutputStream.html#BROKEN> {@code broken}</a>,
117      *          {@link #connect(java.io.PipedReader) unconnected}, closed
118      *          or an I/O error occurs.
119      */
write(int c)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} characters from the specified character array
129      * starting at offset {@code off} 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} 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      *
140      * @throws  IndexOutOfBoundsException
141      *          If {@code off} is negative, or {@code len} is negative,
142      *          or {@code off + len} is negative or greater than the length
143      *          of the given array
144      *
145      * @throws  IOException  if the pipe is
146      *          <a href=PipedOutputStream.html#BROKEN>{@code broken}</a>,
147      *          {@link #connect(java.io.PipedReader) unconnected}, closed
148      *          or an I/O error occurs.
149      */
write(char cbuf[], int off, int len)150     public void write(char cbuf[], int off, int len) throws IOException {
151         if (sink == null) {
152             throw new IOException("Pipe not connected");
153         } else if ((off | len | (off + len) | (cbuf.length - (off + len))) < 0) {
154             throw new IndexOutOfBoundsException();
155         }
156         sink.receive(cbuf, off, len);
157     }
158 
159     /**
160      * Flushes this output stream and forces any buffered output characters
161      * to be written out.
162      * This will notify any readers that characters are waiting in the pipe.
163      *
164      * @throws     IOException  if the pipe is closed, or an I/O error occurs.
165      */
flush()166     public synchronized void flush() throws IOException {
167         if (sink != null) {
168             if (sink.closedByReader || closed) {
169                 throw new IOException("Pipe closed");
170             }
171             synchronized (sink) {
172                 sink.notifyAll();
173             }
174         }
175     }
176 
177     /**
178      * Closes this piped output stream and releases any system resources
179      * associated with this stream. This stream may no longer be used for
180      * writing characters.
181      *
182      * @throws     IOException  if an I/O error occurs.
183      */
close()184     public void close()  throws IOException {
185         closed = true;
186         if (sink != null) {
187             sink.receivedLast();
188         }
189     }
190 }
191