KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > PipedOutputStream


1 /*
2  * @(#)PipedOutputStream.java 1.26 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 import java.io.*;
11
12 /**
13  * A piped output stream can be connected to a piped input stream
14  * to create a communications pipe. The piped output stream is the
15  * sending end of the pipe. Typically, data is written to a
16  * <code>PipedOutputStream</code> object by one thread and data is
17  * read from the connected <code>PipedInputStream</code> by some
18  * other thread. Attempting to use both objects from a single thread
19  * is not recommended as it may deadlock the thread.
20  *
21  * @author James Gosling
22  * @version 1.26, 12/19/03
23  * @see java.io.PipedInputStream
24  * @since JDK1.0
25  */

26 public
27 class PipedOutputStream extends OutputStream JavaDoc {
28
29     /* REMIND: identification of the read and write sides needs to be
30        more sophisticated. Either using thread groups (but what about
31        pipes within a thread?) or using finalization (but it may be a
32        long time until the next GC). */

33     private PipedInputStream JavaDoc sink;
34
35     /**
36      * Creates a piped output stream connected to the specified piped
37      * input stream. Data bytes written to this stream will then be
38      * available as input from <code>snk</code>.
39      *
40      * @param snk The piped input stream to connect to.
41      * @exception IOException if an I/O error occurs.
42      */

43     public PipedOutputStream(PipedInputStream JavaDoc snk) throws IOException JavaDoc {
44     connect(snk);
45     }
46     
47     /**
48      * Creates a piped output stream that is not yet connected to a
49      * piped input stream. It must be connected to a piped input stream,
50      * either by the receiver or the sender, before being used.
51      *
52      * @see java.io.PipedInputStream#connect(java.io.PipedOutputStream)
53      * @see java.io.PipedOutputStream#connect(java.io.PipedInputStream)
54      */

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

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

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

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

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

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