Jumbo patch for NET_2_0, mscorlib is now clean
[mono.git] / mcs / class / corlib / System.IO / Stream.cs
old mode 100755 (executable)
new mode 100644 (file)
index 669b947..d6e0359
@@ -1,26 +1,53 @@
 //
-// System.IO/Stream.cs
+// System.IO.Stream.cs
 //
 // Authors:
 //   Dietmar Maurer (dietmar@ximian.com)
 //   Miguel de Icaza (miguel@ximian.com)
+//   Gonzalo Paniagua Javier (gonzalo@ximian.com)
 //
 // (C) 2001, 2002 Ximian, Inc.  http://www.ximian.com
+// (c) 2004 Novell, Inc. (http://www.novell.com)
+//
+
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System.Threading;
+using System.Runtime.Remoting.Messaging;
+using System.Runtime.InteropServices;
 
 namespace System.IO
 {
        [Serializable]
+       [ComVisible (true)]
+#if NET_2_1
+       public abstract class Stream : IDisposable
+#else
        public abstract class Stream : MarshalByRefObject, IDisposable
+#endif
        {
-               public static readonly Stream Null;
-
-               static Stream ()
-               {
-                       Null = new NullStream ();
-               }
+               public static readonly Stream Null = new NullStream ();
 
                protected Stream ()
                {
@@ -41,6 +68,13 @@ namespace System.IO
                        get;
                }
 
+               [ComVisible (false)]
+               public virtual bool CanTimeout {
+                       get {
+                               return false;
+                       }
+               }
+
                public abstract long Length
                {
                        get;
@@ -53,16 +87,52 @@ namespace System.IO
                }
 
 
+               // 2.0 version of Dispose.
+               public void Dispose ()
+               {
+                       Close ();
+               }
+
+               // 2.0 version of Dispose.
+               protected virtual void Dispose (bool disposing)
+               {
+                       // nothing.
+               }
+
+               //
+               // 2.0 version of Close (): calls Dispose (true)
+               //
                public virtual void Close ()
                {
-                       Flush ();
+                       Dispose (true);
+               }
+
+               [ComVisible (false)]
+               public virtual int ReadTimeout {
+                       get {
+                               throw new InvalidOperationException ("Timeouts are not supported on this stream.");
+                       }
+                       set {
+                               throw new InvalidOperationException ("Timeouts are not supported on this stream.");
+                       }
                }
 
-               void IDisposable.Dispose ()
+               [ComVisible (false)]
+               public virtual int WriteTimeout {
+                       get {
+                               throw new InvalidOperationException ("Timeouts are not supported on this stream.");
+                       }
+                       set {
+                               throw new InvalidOperationException ("Timeouts are not supported on this stream.");
+                       }
+               }
+
+               public static Stream Synchronized (Stream stream)
                {
-                       Close ();
+                       throw new NotImplementedException ();
                }
 
+               [Obsolete ("CreateWaitHandle is due for removal.  Use \"new ManualResetEvent(false)\" instead.")]
                protected virtual WaitHandle CreateWaitHandle()
                {
                        return new ManualResetEvent (false);
@@ -70,9 +140,7 @@ namespace System.IO
                
                public abstract void Flush ();
 
-               public abstract int Read (byte[] buffer,
-                                         int offset,
-                                         int count);
+               public abstract int Read ([In,Out] byte[] buffer, int offset, int count);
 
                public virtual int ReadByte ()
                {
@@ -84,14 +152,11 @@ namespace System.IO
                        return -1;
                }
 
-               public abstract long Seek (long offset,
-                                          SeekOrigin origin);
+               public abstract long Seek (long offset, SeekOrigin origin);
 
                public abstract void SetLength (long value);
 
-               public abstract void Write (byte[] buffer,
-                                           int offset,
-                                           int count);
+               public abstract void Write (byte[] buffer, int offset, int count);
 
                public virtual void WriteByte (byte value)
                {
@@ -102,139 +167,98 @@ namespace System.IO
                        Write (buffer, 0, 1);
                }
 
-               delegate int ReadDelegate (byte [] buffer, int offset, int count);
-
                public virtual IAsyncResult
-               BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
+               BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
                {
                        if (!CanRead)
                                throw new NotSupportedException ("This stream does not support reading");
 
-                       SyncReadResult srr = new SyncReadResult (state);
-                       try
-                       {
-                               srr.Complete (Read (buffer, offset, count));
-                       }
-                       catch (IOException e)
-                       {
-                               srr._exception = e;
+                       // Creating a class derived from Stream that doesn't override BeginRead
+                       // shows that it actually calls Read and does everything synchronously.
+                       // Just put this in the Read override:
+                       //      Console.WriteLine ("Read");
+                       //      Console.WriteLine (Environment.StackTrace);
+                       //      Thread.Sleep (10000);
+                       //      return 10;
+
+                       StreamAsyncResult result = new StreamAsyncResult (state);
+                       try {
+                               int nbytes = Read (buffer, offset, count);
+                               result.SetComplete (null, nbytes);
+                       } catch (Exception e) {
+                               result.SetComplete (e, 0);
                        }
 
-                       if (cback != null)
-                               cback (srr);
+                       if (callback != null)
+                               callback (result);
 
-                       return srr;
+                       return result;
                }
 
+//             delegate void WriteDelegate (byte [] buffer, int offset, int count);
+
                public virtual IAsyncResult
-               BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
+               BeginWrite (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
                {
                        if (!CanWrite)
-                               throw new NotSupportedException ("This stream does not support reading");
-
-                       SyncWriteResult swr = new SyncWriteResult (state);
-                       try
-                       {
+                               throw new NotSupportedException ("This stream does not support writing");
+       
+                       // Creating a class derived from Stream that doesn't override BeginWrite
+                       // shows that it actually calls Write and does everything synchronously except
+                       // when invoking the callback, which is done from the ThreadPool.
+                       // Just put this in the Write override:
+                       //      Console.WriteLine ("Write");
+                       //      Console.WriteLine (Environment.StackTrace);
+                       //      Thread.Sleep (10000);
+
+                       StreamAsyncResult result = new StreamAsyncResult (state);
+                       try {
                                Write (buffer, offset, count);
-                               swr.Complete ();
-                       }
-                       catch (IOException e)
-                       {
-                               swr._exception = e;
+                               result.SetComplete (null);
+                       } catch (Exception e) {
+                               result.SetComplete (e);
                        }
 
-                       if (cback != null)
-                               cback (swr);
+                       if (callback != null)
+                               callback.BeginInvoke (result, null, null);
 
-                       return swr;
+                       return result;
                }
                
-               public virtual int EndRead (IAsyncResult async_result)
-               {
-                       if (async_result == null)
-                               throw new ArgumentNullException ("async_result");
-                       SyncReadResult srr = async_result as SyncReadResult;
-                       if (srr == null)
-                               throw new ArgumentException ("async_result is invalid");
-                       if (srr._fEndCalled)
-                               throw new InvalidOperationException ("EndRead called twice");
-                       srr._fEndCalled = true;
-                       if (srr._exception != null)
-                               throw srr._exception;
-                       return srr._cbRead;
-               }
-
-               public virtual void EndWrite (IAsyncResult async_result)
+               public virtual int EndRead (IAsyncResult asyncResult)
                {
-                       if (async_result == null)
-                               throw new ArgumentNullException ("async_result");
-                       SyncWriteResult swr = async_result as SyncWriteResult;
-                       if (swr == null)
-                               throw new ArgumentException ("async_result is invalid");
-                       if (swr._fEndCalled)
-                               throw new InvalidOperationException ("EndRead called twice");
-                       swr._fEndCalled = true;
-                       if (swr._exception != null)
-                               throw swr._exception;
-               }
-
-               // this class implements the synchronous IASyncResult for the obove methods
-               private class SyncResult : IAsyncResult
-               {
-                       object _objState;               // client-supplied state
-                       bool _fComplete;                // if the IO operation completed successfully
-                       ManualResetEvent _hWait;                // the wait event
-                       public bool _fEndCalled;                // true iff the End method was called already
-                       public Exception _exception;    // holds any exception throw during IO operation
-
-                       public SyncResult (object objState)
-                       {
-                               _objState = objState;
-                               _hWait = new ManualResetEvent (false);
-                       }
+                       if (asyncResult == null)
+                               throw new ArgumentNullException ("asyncResult");
 
-                       public void Complete ()
-                       {
-                               _fComplete = true;
-                               _hWait.Set ();
-                       }
-
-                       // IAsyncResult members
-                       object IAsyncResult.AsyncState
-                       {
-                               get { return _objState; }
-                       }
+                       StreamAsyncResult result = asyncResult as StreamAsyncResult;
+                       if (result == null || result.NBytes == -1)
+                               throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
 
-                       WaitHandle IAsyncResult.AsyncWaitHandle
-                       {
-                               get { return _hWait; }
-                       }
+                       if (result.Done)
+                               throw new InvalidOperationException ("EndRead already called.");
 
-                       bool IAsyncResult.CompletedSynchronously
-                       {
-                               get { return true; }
-                       }
+                       result.Done = true;
+                       if (result.Exception != null)
+                               throw result.Exception;
 
-                       bool IAsyncResult.IsCompleted
-                       {
-                               get { return _fComplete; }
-                       }
+                       return result.NBytes;
                }
-               private class SyncReadResult : SyncResult
+
+               public virtual void EndWrite (IAsyncResult asyncResult)
                {
-                       public int _cbRead;             // the number of bytes read
+                       if (asyncResult == null)
+                               throw new ArgumentNullException ("asyncResult");
 
-                       public SyncReadResult (object objState) : base (objState) {}
+                       StreamAsyncResult result = asyncResult as StreamAsyncResult;
+                       if (result == null || result.NBytes != -1)
+                               throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
 
-                       public void Complete (int cbRead)
-                       {
-                               _cbRead = cbRead;
-                               Complete ();
-                       }
-               }
-               private class SyncWriteResult : SyncResult
-               {
-                       public SyncWriteResult (object objState) : base (objState) {}
+                       if (result.Done)
+                               throw new InvalidOperationException ("EndWrite already called.");
+
+                       result.Done = true;
+                       if (result.Exception != null)
+                               throw result.Exception;
                }
        }
 
@@ -281,20 +305,17 @@ namespace System.IO
                {
                }
 
-               public override int Read (byte[] buffer,
-                                         int offset,
-                                         int count)
+               public override int Read (byte[] buffer, int offset, int count)
                {
                        return 0;
                }
 
                public override int ReadByte ()
                {
-                       return 0;
+                       return -1;
                }
 
-               public override long Seek (long offset,
-                                          SeekOrigin origin)
+               public override long Seek (long offset, SeekOrigin origin)
                {
                        return 0;
                }
@@ -303,9 +324,7 @@ namespace System.IO
                {
                }
 
-               public override void Write (byte[] buffer,
-                                           int offset,
-                                           int count)
+               public override void Write (byte[] buffer, int offset, int count)
                {
                }
 
@@ -314,10 +333,3 @@ namespace System.IO
                }
        }
 }
-
-
-
-
-
-
-