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;
43 public abstract class Stream : MarshalByRefObject, IDisposable
45 public static readonly Stream Null = new NullStream ();
51 public abstract bool CanRead
56 public abstract bool CanSeek
61 public abstract bool CanWrite
66 public abstract long Length
71 public abstract long Position
78 public virtual void Close ()
82 void IDisposable.Dispose ()
87 protected virtual WaitHandle CreateWaitHandle()
89 return new ManualResetEvent (false);
92 public abstract void Flush ();
94 public abstract int Read ([In,Out] byte[] buffer, int offset, int count);
96 public virtual int ReadByte ()
98 byte[] buffer = new byte [1];
100 if (Read (buffer, 0, 1) == 1)
106 public abstract long Seek (long offset, SeekOrigin origin);
108 public abstract void SetLength (long value);
110 public abstract void Write (byte[] buffer, int offset, int count);
112 public virtual void WriteByte (byte value)
114 byte[] buffer = new byte [1];
118 Write (buffer, 0, 1);
121 public virtual IAsyncResult
122 BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
125 throw new NotSupportedException ("This stream does not support reading");
127 // Creating a class derived from Stream that doesn't override BeginRead
128 // shows that it actually calls Read and does everything synchronously.
129 // Just put this in the Read override:
130 // Console.WriteLine ("Read");
131 // Console.WriteLine (Environment.StackTrace);
132 // Thread.Sleep (10000);
135 StreamAsyncResult result = new StreamAsyncResult (state);
137 int nbytes = Read (buffer, offset, count);
138 result.SetComplete (null, nbytes);
139 } catch (Exception e) {
140 result.SetComplete (e, 0);
149 delegate void WriteDelegate (byte [] buffer, int offset, int count);
151 public virtual IAsyncResult
152 BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
155 throw new NotSupportedException ("This stream does not support writing");
157 // Creating a class derived from Stream that doesn't override BeginWrite
158 // shows that it actually calls Write and does everything synchronously except
159 // when invoking the callback, which is done from the ThreadPool.
160 // Just put this in the Write override:
161 // Console.WriteLine ("Write");
162 // Console.WriteLine (Environment.StackTrace);
163 // Thread.Sleep (10000);
165 StreamAsyncResult result = new StreamAsyncResult (state);
167 Write (buffer, offset, count);
168 result.SetComplete (null);
169 } catch (Exception e) {
170 result.SetComplete (e);
174 cback.BeginInvoke (result, null, null);
179 public virtual int EndRead (IAsyncResult async_result)
181 if (async_result == null)
182 throw new ArgumentNullException ("async_result");
184 StreamAsyncResult result = async_result as StreamAsyncResult;
185 if (result == null || result.NBytes == -1)
186 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
189 throw new InvalidOperationException ("EndRead already called.");
192 if (result.Exception != null)
193 throw result.Exception;
195 return result.NBytes;
198 public virtual void EndWrite (IAsyncResult async_result)
200 if (async_result == null)
201 throw new ArgumentNullException ("async_result");
203 StreamAsyncResult result = async_result as StreamAsyncResult;
204 if (result == null || result.NBytes != -1)
205 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
208 throw new InvalidOperationException ("EndWrite already called.");
211 if (result.Exception != null)
212 throw result.Exception;
216 class NullStream : Stream
218 public override bool CanRead
225 public override bool CanSeek
232 public override bool CanWrite
239 public override long Length
246 public override long Position
255 public override void Flush ()
259 public override int Read (byte[] buffer,
266 public override int ReadByte ()
271 public override long Seek (long offset,
277 public override void SetLength (long value)
281 public override void Write (byte[] buffer,
287 public override void WriteByte (byte value)