5 // Dietmar Maurer (dietmar@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
10 // (c) 2004 Novell, Inc. (http://www.novell.com)
13 using System.Threading;
14 using System.Runtime.Remoting.Messaging;
15 using System.Runtime.InteropServices;
20 public abstract class Stream : MarshalByRefObject, IDisposable
22 public static readonly Stream Null;
26 Null = new NullStream ();
33 public abstract bool CanRead
38 public abstract bool CanSeek
43 public abstract bool CanWrite
48 public abstract long Length
53 public abstract long Position
60 public virtual void Close ()
65 void IDisposable.Dispose ()
70 protected virtual WaitHandle CreateWaitHandle()
72 return new ManualResetEvent (false);
75 public abstract void Flush ();
77 public abstract int Read ([In,Out] byte[] buffer, int offset, int count);
79 public virtual int ReadByte ()
81 byte[] buffer = new byte [1];
83 if (Read (buffer, 0, 1) == 1)
89 public abstract long Seek (long offset, SeekOrigin origin);
91 public abstract void SetLength (long value);
93 public abstract void Write (byte[] buffer, int offset, int count);
95 public virtual void WriteByte (byte value)
97 byte[] buffer = new byte [1];
101 Write (buffer, 0, 1);
104 public virtual IAsyncResult
105 BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
108 throw new NotSupportedException ("This stream does not support reading");
110 // Creating a class derived from Stream that doesn't override BeginRead
111 // shows that it actually calls Read and does everything synchronously.
112 // Just put this in the Read override:
113 // Console.WriteLine ("Read");
114 // Console.WriteLine (Environment.StackTrace);
115 // Thread.Sleep (10000);
118 StreamAsyncResult result = new StreamAsyncResult (state);
120 int nbytes = Read (buffer, offset, count);
121 result.SetComplete (null, nbytes);
122 } catch (Exception e) {
123 result.SetComplete (e, 0);
132 delegate void WriteDelegate (byte [] buffer, int offset, int count);
134 public virtual IAsyncResult
135 BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
138 throw new NotSupportedException ("This stream does not support writing");
140 // Creating a class derived from Stream that doesn't override BeginWrite
141 // shows that it actually calls Write and does everything synchronously except
142 // when invoking the callback, which is done from the ThreadPool.
143 // Just put this in the Write override:
144 // Console.WriteLine ("Write");
145 // Console.WriteLine (Environment.StackTrace);
146 // Thread.Sleep (10000);
148 StreamAsyncResult result = new StreamAsyncResult (state);
150 Write (buffer, offset, count);
151 result.SetComplete (null);
152 } catch (Exception e) {
153 result.SetComplete (e);
157 cback.BeginInvoke (result, null, null);
162 public virtual int EndRead (IAsyncResult async_result)
164 if (async_result == null)
165 throw new ArgumentNullException ("async_result");
167 StreamAsyncResult result = async_result as StreamAsyncResult;
168 if (result == null || result.NBytes == -1)
169 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
172 throw new InvalidOperationException ("EndRead already called.");
175 if (result.Exception != null)
176 throw result.Exception;
178 return result.NBytes;
181 public virtual void EndWrite (IAsyncResult async_result)
183 if (async_result == null)
184 throw new ArgumentNullException ("async_result");
186 StreamAsyncResult result = async_result as StreamAsyncResult;
187 if (result == null || result.NBytes != -1)
188 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
191 throw new InvalidOperationException ("EndWrite already called.");
194 if (result.Exception != null)
195 throw result.Exception;
199 class NullStream : Stream
201 public override bool CanRead
208 public override bool CanSeek
215 public override bool CanWrite
222 public override long Length
229 public override long Position
238 public override void Flush ()
242 public override int Read (byte[] buffer,
249 public override int ReadByte ()
254 public override long Seek (long offset,
260 public override void SetLength (long value)
264 public override void Write (byte[] buffer,
270 public override void WriteByte (byte value)