5 // Dietmar Maurer (dietmar@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
8 // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
11 using System.Threading;
16 public abstract class Stream : MarshalByRefObject, IDisposable
18 public static readonly Stream Null;
22 Null = new NullStream ();
29 public abstract bool CanRead
34 public abstract bool CanSeek
39 public abstract bool CanWrite
44 public abstract long Length
49 public abstract long Position
56 public virtual void Close ()
61 void IDisposable.Dispose ()
66 protected virtual WaitHandle CreateWaitHandle()
68 return new ManualResetEvent (false);
71 public abstract void Flush ();
73 public abstract int Read (byte[] buffer,
77 public virtual int ReadByte ()
79 byte[] buffer = new byte [1];
81 if (Read (buffer, 0, 1) == 1)
87 public abstract long Seek (long offset,
90 public abstract void SetLength (long value);
92 public abstract void Write (byte[] buffer,
96 public virtual void WriteByte (byte value)
98 byte[] buffer = new byte [1];
102 Write (buffer, 0, 1);
105 delegate int ReadDelegate (byte [] buffer, int offset, int count);
107 public virtual IAsyncResult
108 BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
111 throw new NotSupportedException ("This stream does not support reading");
113 SyncReadResult srr = new SyncReadResult (state);
116 srr.Complete (Read (buffer, offset, count));
118 catch (IOException e)
129 public virtual IAsyncResult
130 BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
133 throw new NotSupportedException ("This stream does not support reading");
135 SyncWriteResult swr = new SyncWriteResult (state);
138 Write (buffer, offset, count);
141 catch (IOException e)
152 public virtual int EndRead (IAsyncResult async_result)
154 if (async_result == null)
155 throw new ArgumentNullException ("async_result");
156 SyncReadResult srr = async_result as SyncReadResult;
158 throw new ArgumentException ("async_result is invalid");
160 throw new InvalidOperationException ("EndRead called twice");
161 srr._fEndCalled = true;
162 if (srr._exception != null)
163 throw srr._exception;
167 public virtual void EndWrite (IAsyncResult async_result)
169 if (async_result == null)
170 throw new ArgumentNullException ("async_result");
171 SyncWriteResult swr = async_result as SyncWriteResult;
173 throw new ArgumentException ("async_result is invalid");
175 throw new InvalidOperationException ("EndRead called twice");
176 swr._fEndCalled = true;
177 if (swr._exception != null)
178 throw swr._exception;
181 // this class implements the synchronous IASyncResult for the obove methods
182 private class SyncResult : IAsyncResult
184 object _objState; // client-supplied state
185 bool _fComplete; // if the IO operation completed successfully
186 ManualResetEvent _hWait; // the wait event
187 public bool _fEndCalled; // true iff the End method was called already
188 public Exception _exception; // holds any exception throw during IO operation
190 public SyncResult (object objState)
192 _objState = objState;
193 _hWait = new ManualResetEvent (false);
196 public void Complete ()
202 // IAsyncResult members
203 object IAsyncResult.AsyncState
205 get { return _objState; }
208 WaitHandle IAsyncResult.AsyncWaitHandle
210 get { return _hWait; }
213 bool IAsyncResult.CompletedSynchronously
218 bool IAsyncResult.IsCompleted
220 get { return _fComplete; }
223 private class SyncReadResult : SyncResult
225 public int _cbRead; // the number of bytes read
227 public SyncReadResult (object objState) : base (objState) {}
229 public void Complete (int cbRead)
235 private class SyncWriteResult : SyncResult
237 public SyncWriteResult (object objState) : base (objState) {}
241 class NullStream : Stream
243 public override bool CanRead
250 public override bool CanSeek
257 public override bool CanWrite
264 public override long Length
271 public override long Position
280 public override void Flush ()
284 public override int Read (byte[] buffer,
291 public override int ReadByte ()
296 public override long Seek (long offset,
302 public override void SetLength (long value)
306 public override void Write (byte[] buffer,
312 public override void WriteByte (byte value)