**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.IO / Stream.cs
blob6623da319a197f5604f918802b458c7820b7161c
1 //
2 // System.IO/Stream.cs
3 //
4 // Authors:
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 //
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:
23 //
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 //
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;
40 namespace System.IO
42 [Serializable]
43 public abstract class Stream : MarshalByRefObject, IDisposable
45 public static readonly Stream Null = new NullStream ();
47 protected Stream ()
51 public abstract bool CanRead
53 get;
56 public abstract bool CanSeek
58 get;
61 public abstract bool CanWrite
63 get;
66 public abstract long Length
68 get;
71 public abstract long Position
73 get;
74 set;
78 public virtual void Close ()
82 void IDisposable.Dispose ()
84 Close ();
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)
101 return buffer [0];
103 return -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];
116 buffer [0] = value;
118 Write (buffer, 0, 1);
121 public virtual IAsyncResult
122 BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
124 if (!CanRead)
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);
133 // return 10;
135 StreamAsyncResult result = new StreamAsyncResult (state);
136 try {
137 int nbytes = Read (buffer, offset, count);
138 result.SetComplete (null, nbytes);
139 } catch (Exception e) {
140 result.SetComplete (e, 0);
143 if (cback != null)
144 cback (result);
146 return result;
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)
154 if (!CanWrite)
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);
166 try {
167 Write (buffer, offset, count);
168 result.SetComplete (null);
169 } catch (Exception e) {
170 result.SetComplete (e);
173 if (cback != null)
174 cback.BeginInvoke (result, null, null);
176 return result;
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");
188 if (result.Done)
189 throw new InvalidOperationException ("EndRead already called.");
191 result.Done = true;
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");
207 if (result.Done)
208 throw new InvalidOperationException ("EndWrite already called.");
210 result.Done = true;
211 if (result.Exception != null)
212 throw result.Exception;
216 class NullStream : Stream
218 public override bool CanRead
220 get {
221 return true;
225 public override bool CanSeek
227 get {
228 return true;
232 public override bool CanWrite
234 get {
235 return true;
239 public override long Length
241 get {
242 return 0;
246 public override long Position
248 get {
249 return 0;
251 set {
255 public override void Flush ()
259 public override int Read (byte[] buffer,
260 int offset,
261 int count)
263 return 0;
266 public override int ReadByte ()
268 return -1;
271 public override long Seek (long offset,
272 SeekOrigin origin)
274 return 0;
277 public override void SetLength (long value)
281 public override void Write (byte[] buffer,
282 int offset,
283 int count)
287 public override void WriteByte (byte value)