| 1 | /* PipedOutputStream.java -- Write portion of piped streams.
|
|---|
| 2 | Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.io;
|
|---|
| 40 |
|
|---|
| 41 | // NOTE: This implementation is very similar to that of PipedWriter. If you
|
|---|
| 42 | // fix a bug in here, chances are you should make a similar change to the
|
|---|
| 43 | // PipedWriter code.
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * This class writes its bytes to a <code>PipedInputStream</code> to
|
|---|
| 47 | * which it is connected.
|
|---|
| 48 | * <p>
|
|---|
| 49 | * It is highly recommended that a <code>PipedOutputStream</code> and its
|
|---|
| 50 | * connected <code>PipedInputStream</code> be in different threads. If
|
|---|
| 51 | * they are in the same thread, read and write operations could deadlock
|
|---|
| 52 | * the thread.
|
|---|
| 53 | *
|
|---|
| 54 | * @author Aaron M. Renn ([email protected])
|
|---|
| 55 | */
|
|---|
| 56 | public class PipedOutputStream extends OutputStream
|
|---|
| 57 | {
|
|---|
| 58 | /** Target PipedInputStream to which this is connected. Null only if this
|
|---|
| 59 | * OutputStream hasn't been connected yet. */
|
|---|
| 60 | PipedInputStream sink;
|
|---|
| 61 |
|
|---|
| 62 | /** Set to true if close() has been called on this OutputStream. */
|
|---|
| 63 | boolean closed;
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Create an unconnected PipedOutputStream. It must be connected
|
|---|
| 67 | * to a <code>PipedInputStream</code> using the <code>connect</code>
|
|---|
| 68 | * method prior to writing any data or an exception will be thrown.
|
|---|
| 69 | */
|
|---|
| 70 | public PipedOutputStream()
|
|---|
| 71 | {
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Create a new <code>PipedOutputStream</code> instance
|
|---|
| 76 | * to write to the specified <code>PipedInputStream</code>. This stream
|
|---|
| 77 | * is then ready for writing.
|
|---|
| 78 | *
|
|---|
| 79 | * @param sink The <code>PipedInputStream</code> to connect this stream to.
|
|---|
| 80 | *
|
|---|
| 81 | * @exception IOException If <code>sink</code> has already been connected
|
|---|
| 82 | * to a different PipedOutputStream.
|
|---|
| 83 | */
|
|---|
| 84 | public PipedOutputStream(PipedInputStream sink) throws IOException
|
|---|
| 85 | {
|
|---|
| 86 | sink.connect(this);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Connects this object to the specified <code>PipedInputStream</code>
|
|---|
| 91 | * object. This stream will then be ready for writing.
|
|---|
| 92 | *
|
|---|
| 93 | * @param sink The <code>PipedInputStream</code> to connect this stream to
|
|---|
| 94 | *
|
|---|
| 95 | * @exception IOException If the stream has not been connected or has
|
|---|
| 96 | * been closed.
|
|---|
| 97 | */
|
|---|
| 98 | public void connect(PipedInputStream sink) throws IOException
|
|---|
| 99 | {
|
|---|
| 100 | if (sink != null)
|
|---|
| 101 | throw new IOException ("Already connected");
|
|---|
| 102 | sink.connect(this);
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Write a single byte of date to the stream. Note that this method will
|
|---|
| 107 | * block if the <code>PipedInputStream</code> to which this object is
|
|---|
| 108 | * connected has a full buffer.
|
|---|
| 109 | *
|
|---|
| 110 | * @param b The byte of data to be written, passed as an <code>int</code>.
|
|---|
| 111 | *
|
|---|
| 112 | * @exception IOException If the stream has not been connected or has
|
|---|
| 113 | * been closed.
|
|---|
| 114 | */
|
|---|
| 115 | public void write(int b) throws IOException
|
|---|
| 116 | {
|
|---|
| 117 | if (sink == null)
|
|---|
| 118 | throw new IOException ("Not connected");
|
|---|
| 119 | if (closed)
|
|---|
| 120 | throw new IOException ("Pipe closed");
|
|---|
| 121 |
|
|---|
| 122 | sink.receive (b);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * This method writes <code>len</code> bytes of data from the byte array
|
|---|
| 127 | * <code>buf</code> starting at index <code>offset</code> in the array
|
|---|
| 128 | * to the stream. Note that this method will block if the
|
|---|
| 129 | * <code>PipedInputStream</code> to which this object is connected has
|
|---|
| 130 | * a buffer that cannot hold all of the bytes to be written.
|
|---|
| 131 | *
|
|---|
| 132 | * @param buf The array containing bytes to write to the stream.
|
|---|
| 133 | * @param offset The index into the array to start writing bytes from.
|
|---|
| 134 | * @param len The number of bytes to write.
|
|---|
| 135 | *
|
|---|
| 136 | * @exception IOException If the stream has not been connected or has
|
|---|
| 137 | * been closed.
|
|---|
| 138 | */
|
|---|
| 139 | public void write(byte[] b, int off, int len) throws IOException
|
|---|
| 140 | {
|
|---|
| 141 | if (sink == null)
|
|---|
| 142 | throw new IOException ("Not connected");
|
|---|
| 143 | if (closed)
|
|---|
| 144 | throw new IOException ("Pipe closed");
|
|---|
| 145 |
|
|---|
| 146 | sink.receive (b, off, len);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * This method does nothing.
|
|---|
| 151 | *
|
|---|
| 152 | * @exception IOException If the stream is closed.
|
|---|
| 153 | * @specnote You'd think that this method would block until the sink
|
|---|
| 154 | * had read all available data. Thats not the case - this method
|
|---|
| 155 | * appears to be a no-op?
|
|---|
| 156 | */
|
|---|
| 157 | public void flush()
|
|---|
| 158 | {
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /**
|
|---|
| 162 | * This method closes this stream so that no more data can be written
|
|---|
| 163 | * to it. Any further attempts to write to this stream may throw an
|
|---|
| 164 | * exception
|
|---|
| 165 | *
|
|---|
| 166 | * @exception IOException If an error occurs
|
|---|
| 167 | */
|
|---|
| 168 | public void close()
|
|---|
| 169 | {
|
|---|
| 170 | // A close call on an unconnected PipedOutputStream has no effect.
|
|---|
| 171 | if (sink != null)
|
|---|
| 172 | {
|
|---|
| 173 | closed = true;
|
|---|
| 174 | // Notify any waiting readers that the stream is now closed.
|
|---|
| 175 | synchronized (sink)
|
|---|
| 176 | {
|
|---|
| 177 | sink.notifyAll();
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|