2004-04-02 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / MemoryStream.cs
index d7aa2bf48adb338655f7953517ee1a3343f1e852..c7f2e3f12b89754c03237b0827e0cc2c31f8f739 100644 (file)
@@ -9,6 +9,8 @@
 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
 //
 
+using System.Runtime.InteropServices;
+
 namespace System.IO
 {
        [Serializable]
@@ -44,11 +46,17 @@ namespace System.IO
 
                public MemoryStream (byte [] buffer)
                {
+                       if (buffer == null)
+                               throw new ArgumentNullException ("buffer");
+                       
                        InternalConstructor (buffer, 0, buffer.Length, true, false);                        
                }
 
                public MemoryStream (byte [] buffer, bool writeable)
                {
+                       if (buffer == null)
+                               throw new ArgumentNullException ("buffer");
+                       
                        InternalConstructor (buffer, 0, buffer.Length, writeable, false);
                }
 
@@ -197,7 +205,7 @@ namespace System.IO
                        return internalBuffer;
                }
 
-               public override int Read (byte [] buffer, int offset, int count)
+               public override int Read ([In,Out] byte [] buffer, int offset, int count)
                {
                        CheckIfClosedThrowDisposed ();
 
@@ -284,10 +292,11 @@ namespace System.IO
 
                public override void SetLength (long value)
                {
-                       CheckIfClosedThrowDisposed ();
                        if (!expandable && value > capacity)
                                throw new NotSupportedException ("Expanding this MemoryStream is not supported");
 
+                       CheckIfClosedThrowDisposed ();
+
                        if (!canWrite)
                                throw new IOException ("Cannot write to this MemoryStream");
 
@@ -352,7 +361,7 @@ namespace System.IO
                        if (!canWrite)
                                throw new NotSupportedException ("Cannot write to this stream.");
 
-                       if (position + 1 >= capacity)
+                       if (position >= capacity)
                                Capacity = CalculateNewCapacity (position + 1);
 
                        if (position >= length)
@@ -371,5 +380,4 @@ namespace System.IO
                        stream.Write (internalBuffer, initialIndex, length - initialIndex);
                }
        }               
-}                      
-
+}