KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > PipedWriter


1 /*
2  * @(#)PipedWriter.java 1.16 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10
11 /**
12  * Piped character-output streams.
13  *
14  * @version 1.16, 03/12/19
15  * @author Mark Reinhold
16  * @since JDK1.1
17  */

18
19 public class PipedWriter extends Writer JavaDoc {
20
21     /* REMIND: identification of the read and write sides needs to be
22        more sophisticated. Either using thread groups (but what about
23        pipes within a thread?) or using finalization (but it may be a
24        long time until the next GC). */

25     private PipedReader JavaDoc sink;
26
27     /* This flag records the open status of this particular writer. It
28      * is independent of the status flags defined in PipedReader. It is
29      * used to do a sanity check on connect.
30      */

31     private boolean closed = false;
32
33     /**
34      * Creates a piped writer connected to the specified piped
35      * reader. Data characters written to this stream will then be
36      * available as input from <code>snk</code>.
37      *
38      * @param snk The piped reader to connect to.
39      * @exception IOException if an I/O error occurs.
40      */

41     public PipedWriter(PipedReader JavaDoc snk) throws IOException JavaDoc {
42     connect(snk);
43     }
44     
45     /**
46      * Creates a piped writer that is not yet connected to a
47      * piped reader. It must be connected to a piped reader,
48      * either by the receiver or the sender, before being used.
49      *
50      * @see java.io.PipedReader#connect(java.io.PipedWriter)
51      * @see java.io.PipedWriter#connect(java.io.PipedReader)
52      */

53     public PipedWriter() {
54     }
55     
56     /**
57      * Connects this piped writer to a receiver. If this object
58      * is already connected to some other piped reader, an
59      * <code>IOException</code> is thrown.
60      * <p>
61      * If <code>snk</code> is an unconnected piped reader and
62      * <code>src</code> is an unconnected piped writer, they may
63      * be connected by either the call:
64      * <blockquote><pre>
65      * src.connect(snk)</pre></blockquote>
66      * or the call:
67      * <blockquote><pre>
68      * snk.connect(src)</pre></blockquote>
69      * The two calls have the same effect.
70      *
71      * @param snk the piped reader to connect to.
72      * @exception IOException if an I/O error occurs.
73      */

74     public synchronized void connect(PipedReader JavaDoc snk) throws IOException JavaDoc {
75         if (snk == null) {
76             throw new NullPointerException JavaDoc();
77         } else if (sink != null || snk.connected) {
78         throw new IOException JavaDoc("Already connected");
79     } else if (snk.closedByReader || closed) {
80             throw new IOException JavaDoc("Pipe closed");
81         }
82         
83     sink = snk;
84     snk.in = -1;
85     snk.out = 0;
86         snk.connected = true;
87     }
88
89     /**
90      * Writes the specified <code>char</code> to the piped output stream.
91      * If a thread was reading data characters from the connected piped input
92      * stream, but the thread is no longer alive, then an
93      * <code>IOException</code> is thrown.
94      * <p>
95      * Implements the <code>write</code> method of <code>Writer</code>.
96      *
97      * @param c the <code>char</code> to be written.
98      * @exception IOException if an I/O error occurs.
99      */

100     public void write(int c) throws IOException JavaDoc {
101         if (sink == null) {
102             throw new IOException JavaDoc("Pipe not connected");
103         }
104     sink.receive(c);
105     }
106
107     /**
108      * Writes <code>len</code> characters from the specified character array
109      * starting at offset <code>off</code> to this piped output stream.
110      * If a thread was reading data characters from the connected piped input
111      * stream, but the thread is no longer alive, then an
112      * <code>IOException</code> is thrown.
113      *
114      * @param cbuf the data.
115      * @param off the start offset in the data.
116      * @param len the number of characters to write.
117      * @exception IOException if an I/O error occurs.
118      */

119     public void write(char cbuf[], int off, int len) throws IOException JavaDoc {
120         if (sink == null) {
121             throw new IOException JavaDoc("Pipe not connected");
122         } else if ((off | len | (off + len) | (cbuf.length - (off + len))) < 0) {
123         throw new IndexOutOfBoundsException JavaDoc();
124     }
125     sink.receive(cbuf, off, len);
126     }
127
128     /**
129      * Flushes this output stream and forces any buffered output characters
130      * to be written out.
131      * This will notify any readers that characters are waiting in the pipe.
132      *
133      * @exception IOException if an I/O error occurs.
134      */

135     public synchronized void flush() throws IOException JavaDoc {
136     if (sink != null) {
137             if (sink.closedByReader || closed) {
138                 throw new IOException JavaDoc("Pipe closed");
139             }
140             synchronized (sink) {
141                 sink.notifyAll();
142             }
143     }
144     }
145
146     /**
147      * Closes this piped output stream and releases any system resources
148      * associated with this stream. This stream may no longer be used for
149      * writing characters.
150      *
151      * @exception IOException if an I/O error occurs.
152      */

153     public void close() throws IOException JavaDoc {
154         closed = true;
155     if (sink != null) {
156         sink.receivedLast();
157     }
158     }
159 }
160
Popular Tags