00001 using System;
00002 using System.IO;
00003 using System.Runtime.CompilerServices;
00004
00005 namespace Tamir.Streams
00006 {
00007
00008
00009
00010
00011
00012
00013
00028 public class PipedOutputStream : Tamir.SharpSsh.java.io.OutputStream
00029 {
00030
00031
00032
00033
00034
00035 private PipedInputStream sink;
00036
00045 public PipedOutputStream(PipedInputStream snk)
00046 {
00047 connect(snk);
00048 }
00049
00058 public PipedOutputStream()
00059 {
00060 }
00061
00080 [MethodImpl(MethodImplOptions.Synchronized)]
00081 public virtual void connect(PipedInputStream snk)
00082 {
00083 if (snk == null)
00084 {
00085 throw new NullReferenceException();
00086 }
00087 else if (sink != null || snk.connected)
00088 {
00089 throw new IOException("Already connected");
00090 }
00091 sink = snk;
00092 snk.m_in = -1;
00093 snk.m_out = 0;
00094 snk.connected = true;
00095 int t=0;
00096 }
00097
00109 public virtual void write(int b)
00110 {
00111 if (sink == null)
00112 {
00113 throw new IOException("Pipe not connected");
00114 }
00115 sink.receive(b);
00116 }
00117
00130 public override void write(byte[] b, int off, int len)
00131 {
00132 if (sink == null)
00133 {
00134 throw new IOException("Pipe not connected");
00135 }
00136 else if (b == null)
00137 {
00138 throw new NullReferenceException();
00139 }
00140 else if ((off < 0) || (off > b.Length) || (len < 0) ||
00141 ((off + len) > b.Length) || ((off + len) < 0))
00142 {
00143 throw new IndexOutOfRangeException();
00144 }
00145 else if (len == 0)
00146 {
00147 return;
00148 }
00149 sink.receive(b, off, len);
00150 }
00151
00152 public virtual void write(byte[] b)
00153 {
00154 write(b, 0, b.Length);
00155 }
00156
00164 [MethodImpl(MethodImplOptions.Synchronized)]
00165 public override void flush()
00166 {
00167 if (sink != null)
00168 {
00169 lock (sink)
00170 {
00171
00172 System.Threading.Monitor.PulseAll(sink);
00173 }
00174 }
00175 }
00176
00184 public override void close()
00185 {
00186 if (sink != null)
00187 {
00188 sink.receivedLast();
00189 }
00190 }
00191
00193
00194
00195 public override int Read(byte[] buffer, int offset, int count)
00196 {
00197 return 0;
00198 }
00199
00200 public override int ReadByte()
00201 {
00202 return 0;
00203 }
00204
00205 public override void WriteByte(byte value)
00206 {
00207 this.write(value);
00208 }
00209
00210 public override void Write(byte[] buffer, int offset, int count)
00211 {
00212 this.write(buffer, offset, count);
00213 }
00214 public virtual void Write(byte[] buffer)
00215 {
00216 this.Write(buffer, 0, buffer.Length);
00217 }
00218 public override void Close()
00219 {
00220 base.Close ();
00221 this.close();
00222 }
00223 public override bool CanRead
00224 {
00225 get
00226 {
00227 return false;
00228 }
00229 }
00230 public override bool CanWrite
00231 {
00232 get
00233 {
00234 return true;
00235 }
00236 }
00237 public override bool CanSeek
00238 {
00239 get
00240 {
00241 return false;
00242 }
00243 }
00244 public override void Flush()
00245 {
00246 this.flush();
00247 }
00248 public override long Length
00249 {
00250 get
00251 {
00252 return sink.Length;
00253 }
00254 }
00255 public override long Position
00256 {
00257 get
00258 {
00259 return sink.m_in;
00260 }
00261 set
00262 {
00263 throw new IOException("Setting the position of this stream is not supported");
00264 }
00265 }
00266 public override void SetLength(long value)
00267 {
00268 throw new IOException("Setting the length of this stream is not supported");
00269 }
00270 public override long Seek(long offset, SeekOrigin origin)
00271 {
00272 return 0;
00273 }
00274 }
00275
00276 }