* corlib_test.dll.sources: added Consts.cs.
[mono.git] / mcs / class / corlib / System.IO / Stream.cs
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)
11 //
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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.
34 //
35
36 using System.Threading;
37 using System.Runtime.Remoting.Messaging;
38 using System.Runtime.InteropServices;
39
40 namespace System.IO
41 {
42         [Serializable]
43 #if NET_2_0
44         [ComVisible (true)]
45 #endif
46         public abstract class Stream : MarshalByRefObject, IDisposable
47         {
48                 public static readonly Stream Null = new NullStream ();
49
50                 protected Stream ()
51                 {
52                 }
53
54                 public abstract bool CanRead
55                 {
56                         get;
57                 }
58
59                 public abstract bool CanSeek
60                 {
61                         get;
62                 }
63
64                 public abstract bool CanWrite
65                 {
66                         get;
67                 }
68
69 #if NET_2_0
70                 [ComVisible (false)]
71                 public virtual bool CanTimeout {
72                         get {
73                                 return false;
74                         }
75                 }
76 #endif
77
78                 public abstract long Length
79                 {
80                         get;
81                 }
82
83                 public abstract long Position
84                 {
85                         get;
86                         set;
87                 }
88
89
90 #if NET_2_0
91                 // 2.0 version of Dispose.
92                 public void Dispose ()
93                 {
94                         Close ();
95                 }
96
97                 // 2.0 version of Dispose.
98                 protected virtual void Dispose (bool disposing)
99                 {
100                         // nothing.
101                 }
102
103                 //
104                 // 2.0 version of Close (): calls Dispose (true)
105                 //
106                 public virtual void Close ()
107                 {
108                         Dispose (true);
109                 }
110
111                 [ComVisible (false)]
112                 public virtual int ReadTimeout {
113                         get {
114                                 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
115                         }
116                         set {
117                                 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
118                         }
119                 }
120
121                 [ComVisible (false)]
122                 public virtual int WriteTimeout {
123                         get {
124                                 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
125                         }
126                         set {
127                                 throw new InvalidOperationException ("Timeouts are not supported on this stream.");
128                         }
129                 }
130
131                 public static Stream Synchronized (Stream stream)
132                 {
133                         throw new NotImplementedException ();
134                 }
135 #else
136                 // 1.1 version of Close
137                 public virtual void Close ()
138                 {
139                         // nothing
140                 }
141
142                 void IDisposable.Dispose ()
143                 {
144                         Close ();
145                 }
146 #endif
147
148 #if NET_2_0
149                 [Obsolete ("CreateWaitHandle is due for removal.  Use \"new ManualResetEvent(false)\" instead.")]
150 #endif
151                 protected virtual WaitHandle CreateWaitHandle()
152                 {
153                         return new ManualResetEvent (false);
154                 }
155                 
156                 public abstract void Flush ();
157
158                 public abstract int Read ([In,Out] byte[] buffer, int offset, int count);
159
160                 public virtual int ReadByte ()
161                 {
162                         byte[] buffer = new byte [1];
163
164                         if (Read (buffer, 0, 1) == 1)
165                                 return buffer [0];
166                         
167                         return -1;
168                 }
169
170                 public abstract long Seek (long offset, SeekOrigin origin);
171
172                 public abstract void SetLength (long value);
173
174                 public abstract void Write (byte[] buffer, int offset, int count);
175
176                 public virtual void WriteByte (byte value)
177                 {
178                         byte[] buffer = new byte [1];
179
180                         buffer [0] = value;
181
182                         Write (buffer, 0, 1);
183                 }
184
185                 public virtual IAsyncResult
186                 BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
187                 {
188                         if (!CanRead)
189                                 throw new NotSupportedException ("This stream does not support reading");
190
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);
197                         //      return 10;
198
199                         StreamAsyncResult result = new StreamAsyncResult (state);
200                         try {
201                                 int nbytes = Read (buffer, offset, count);
202                                 result.SetComplete (null, nbytes);
203                         } catch (Exception e) {
204                                 result.SetComplete (e, 0);
205                         }
206
207                         if (cback != null)
208                                 cback (result);
209
210                         return result;
211                 }
212
213                 delegate void WriteDelegate (byte [] buffer, int offset, int count);
214
215                 public virtual IAsyncResult
216                 BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
217                 {
218                         if (!CanWrite)
219                                 throw new NotSupportedException ("This stream does not support writing");
220         
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);
228
229                         StreamAsyncResult result = new StreamAsyncResult (state);
230                         try {
231                                 Write (buffer, offset, count);
232                                 result.SetComplete (null);
233                         } catch (Exception e) {
234                                 result.SetComplete (e);
235                         }
236
237                         if (cback != null)
238                                 cback.BeginInvoke (result, null, null);
239
240                         return result;
241                 }
242                 
243                 public virtual int EndRead (IAsyncResult async_result)
244                 {
245                         if (async_result == null)
246                                 throw new ArgumentNullException ("async_result");
247
248                         StreamAsyncResult result = async_result as StreamAsyncResult;
249                         if (result == null || result.NBytes == -1)
250                                 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
251
252                         if (result.Done)
253                                 throw new InvalidOperationException ("EndRead already called.");
254
255                         result.Done = true;
256                         if (result.Exception != null)
257                                 throw result.Exception;
258
259                         return result.NBytes;
260                 }
261
262                 public virtual void EndWrite (IAsyncResult async_result)
263                 {
264                         if (async_result == null)
265                                 throw new ArgumentNullException ("async_result");
266
267                         StreamAsyncResult result = async_result as StreamAsyncResult;
268                         if (result == null || result.NBytes != -1)
269                                 throw new ArgumentException ("Invalid IAsyncResult", "async_result");
270
271                         if (result.Done)
272                                 throw new InvalidOperationException ("EndWrite already called.");
273
274                         result.Done = true;
275                         if (result.Exception != null)
276                                 throw result.Exception;
277                 }
278         }
279
280         class NullStream : Stream
281         {
282                 public override bool CanRead
283                 {
284                         get {
285                                 return true;
286                         }
287                 }
288
289                 public override bool CanSeek
290                 {
291                         get {
292                                 return true;
293                         }
294                 }
295
296                 public override bool CanWrite
297                 {
298                         get {
299                                 return true;
300                         }
301                 }
302
303                 public override long Length
304                 {
305                         get {
306                                 return 0;
307                         }
308                 }
309
310                 public override long Position
311                 {
312                         get {
313                                 return 0;
314                         }
315                         set {
316                         }
317                 }
318
319                 public override void Flush ()
320                 {
321                 }
322
323                 public override int Read (byte[] buffer,
324                                           int offset,
325                                           int count)
326                 {
327                         return 0;
328                 }
329
330                 public override int ReadByte ()
331                 {
332                         return -1;
333                 }
334
335                 public override long Seek (long offset,
336                                            SeekOrigin origin)
337                 {
338                         return 0;
339                 }
340
341                 public override void SetLength (long value)
342                 {
343                 }
344
345                 public override void Write (byte[] buffer,
346                                             int offset,
347                                             int count)
348                 {
349                 }
350
351                 public override void WriteByte (byte value)
352                 {
353                 }
354         }
355 }