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)
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Threading;
37 using System.Runtime.Remoting.Messaging;
38 using System.Runtime.InteropServices;
46 public abstract class Stream : MarshalByRefObject, IDisposable
48 public static readonly Stream Null = new NullStream ();
54 public abstract bool CanRead
59 public abstract bool CanSeek
64 public abstract bool CanWrite
71 public virtual bool CanTimeout {
78 public abstract long Length
83 public abstract long Position
91 // 2.0 version of Dispose.
92 public void Dispose ()
97 // 2.0 version of Dispose.
98 protected virtual void Dispose (bool disposing)
104 // 2.0 version of Close (): calls Dispose (true)
106 public virtual void Close ()
112 public virtual int ReadTimeout {
114 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
117 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
122 public virtual int WriteTimeout {
124 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
127 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
131 public static Stream Synchronized (Stream stream)
133 throw new NotImplementedException ();
136 // 1.1 version of Close
137 public virtual void Close ()
142 void IDisposable.Dispose ()
149 [Obsolete ("CreateWaitHandle is due for removal. Use \"new ManualResetEvent(false)\" instead.")]
151 protected virtual WaitHandle CreateWaitHandle()
153 return new ManualResetEvent (false);
156 public abstract void Flush ();
158 public abstract int Read ([In,Out] byte[] buffer, int offset, int count);
160 public virtual int ReadByte ()
162 byte[] buffer = new byte [1];
164 if (Read (buffer, 0, 1) == 1)
170 public abstract long Seek (long offset, SeekOrigin origin);
172 public abstract void SetLength (long value);
174 public abstract void Write (byte[] buffer, int offset, int count);
176 public virtual void WriteByte (byte value)
178 byte[] buffer = new byte [1];
182 Write (buffer, 0, 1);
185 public virtual IAsyncResult
186 BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
189 throw new NotSupportedException ("This stream does not support reading");
191 // Creating a class derived from Stream that doesn't override BeginRead
192 // shows that it actually calls Read and does everything synchronously.
193 // Just put this in the Read override:
194 // Console.WriteLine ("Read");
195 // Console.WriteLine (Environment.StackTrace);
196 // Thread.Sleep (10000);
199 StreamAsyncResult result = new StreamAsyncResult (state);
201 int nbytes = Read (buffer, offset, count);
202 result.SetComplete (null, nbytes);
203 } catch (Exception e) {
204 result.SetComplete (e, 0);
213 delegate void WriteDelegate (byte [] buffer, int offset, int count);
215 public virtual IAsyncResult
216 BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
219 throw new NotSupportedException ("This stream does not support writing");
221 // Creating a class derived from Stream that doesn't override BeginWrite
222 // shows that it actually calls Write and does everything synchronously except
223 // when invoking the callback, which is done from the ThreadPool.
224 // Just put this in the Write override:
225 // Console.WriteLine ("Write");
226 // Console.WriteLine (Environment.StackTrace);
227 // Thread.Sleep (10000);
229 StreamAsyncResult result = new StreamAsyncResult (state);
231 Write (buffer, offset, count);
232 result.SetComplete (null);
233 } catch (Exception e) {
234 result.SetComplete (e);
238 cback.BeginInvoke (result, null, null);
243 public virtual int EndRead (IAsyncResult async_result)
245 if (async_result == null)
246 throw new ArgumentNullException ("async_result");
248 StreamAsyncResult result = async_result as StreamAsyncResult;
249 if (result == null || result.NBytes == -1)
250 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
253 throw new InvalidOperationException ("EndRead already called.");
256 if (result.Exception != null)
257 throw result.Exception;
259 return result.NBytes;
262 public virtual void EndWrite (IAsyncResult async_result)
264 if (async_result == null)
265 throw new ArgumentNullException ("async_result");
267 StreamAsyncResult result = async_result as StreamAsyncResult;
268 if (result == null || result.NBytes != -1)
269 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
272 throw new InvalidOperationException ("EndWrite already called.");
275 if (result.Exception != null)
276 throw result.Exception;
280 class NullStream : Stream
282 public override bool CanRead
289 public override bool CanSeek
296 public override bool CanWrite
303 public override long Length
310 public override long Position
319 public override void Flush ()
323 public override int Read (byte[] buffer,
330 public override int ReadByte ()
335 public override long Seek (long offset,
341 public override void SetLength (long value)
345 public override void Write (byte[] buffer,
351 public override void WriteByte (byte value)