001/* PipedOutputStream.java -- Write portion of piped streams. 002 Copyright (C) 1998, 2000, 2001, 2003 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package java.io; 040 041// NOTE: This implementation is very similar to that of PipedWriter. If you 042// fix a bug in here, chances are you should make a similar change to the 043// PipedWriter code. 044 045/** 046 * This class writes its bytes to a <code>PipedInputStream</code> to 047 * which it is connected. 048 * <p> 049 * It is highly recommended that a <code>PipedOutputStream</code> and its 050 * connected <code>PipedInputStream</code> be in different threads. If 051 * they are in the same thread, read and write operations could deadlock 052 * the thread. 053 * 054 * @author Aaron M. Renn (arenn@urbanophile.com) 055 */ 056public class PipedOutputStream extends OutputStream 057{ 058 /** Target PipedInputStream to which this is connected. Null only if this 059 * OutputStream hasn't been connected yet. */ 060 PipedInputStream sink; 061 062 /** Set to true if close() has been called on this OutputStream. */ 063 boolean closed; 064 065 /** 066 * Create an unconnected PipedOutputStream. It must be connected 067 * to a <code>PipedInputStream</code> using the <code>connect</code> 068 * method prior to writing any data or an exception will be thrown. 069 */ 070 public PipedOutputStream() 071 { 072 } 073 074 /** 075 * Create a new <code>PipedOutputStream</code> instance 076 * to write to the specified <code>PipedInputStream</code>. This stream 077 * is then ready for writing. 078 * 079 * @param sink The <code>PipedInputStream</code> to connect this stream to. 080 * 081 * @exception IOException If <code>sink</code> has already been connected 082 * to a different PipedOutputStream. 083 */ 084 public PipedOutputStream(PipedInputStream sink) throws IOException 085 { 086 sink.connect(this); 087 } 088 089 /** 090 * Connects this object to the specified <code>PipedInputStream</code> 091 * object. This stream will then be ready for writing. 092 * 093 * @param sink The <code>PipedInputStream</code> to connect this stream to 094 * 095 * @exception IOException If the stream has not been connected or has 096 * been closed. 097 */ 098 public void connect(PipedInputStream sink) throws IOException 099 { 100 if (this.sink != null || sink.source != 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 buffer 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[] buffer, int offset, 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(buffer, offset, 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() throws IOException 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() throws IOException 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}