Make MemoryStream more .net compatible
authorMarek Safar <marek.safar@gmail.com>
Thu, 1 Sep 2011 15:56:36 +0000 (16:56 +0100)
committerMarek Safar <marek.safar@gmail.com>
Fri, 2 Sep 2011 15:36:52 +0000 (16:36 +0100)
mcs/class/corlib/System.IO/MemoryStream.cs
mcs/class/corlib/Test/System.IO/MemoryStreamTest.cs

index ca74ff4087e18ce281eadd656373230545962ea6..cb9ac86ff71fe322c248272ea3d5a1a53caff9a7 100644 (file)
@@ -153,8 +153,6 @@ namespace System.IO
 
                        set {
                                CheckIfClosedThrowDisposed ();
-                               if (value == capacity)
-                                       return; // LAMENESS: see MemoryStreamTest.ConstructorFive
 
                                if (!expandable)
                                        throw new NotSupportedException ("Cannot expand this MemoryStream");
@@ -234,8 +232,6 @@ namespace System.IO
 
                public override int Read ([In,Out] byte [] buffer, int offset, int count)
                {
-                       CheckIfClosedThrowDisposed ();
-
                        if (buffer == null)
                                throw new ArgumentNullException ("buffer");
 
@@ -246,6 +242,8 @@ namespace System.IO
                                throw new ArgumentException ("offset+count",
                                                              "The size of the buffer is less than offset + count.");
 
+                       CheckIfClosedThrowDisposed ();
+
                        if (position >= length || count == 0)
                                return 0;
 
@@ -372,8 +370,6 @@ namespace System.IO
 
                public override void Write (byte [] buffer, int offset, int count)
                {
-                       CheckIfClosedThrowDisposed ();
-
                        if (!canWrite)
                                throw new NotSupportedException ("Cannot write to this stream.");
 
@@ -387,6 +383,8 @@ namespace System.IO
                                throw new ArgumentException ("offset+count",
                                                             "The size of the buffer is less than offset + count.");
 
+                       CheckIfClosedThrowDisposed ();
+
                        // reordered to avoid possible integer overflow
                        if (position > length - count)
                                Expand (position + count);
index f0ed197b31346070a6dd73b3f24333087d7669eb..80ba4eecc50633b7ac241dc8789ec01f25e84293 100644 (file)
@@ -155,29 +155,25 @@ namespace MonoTests.System.IO
                                testStreamData [51] = saved;
                        }
                        ms.Position = 100;
-                       bool gotException = false;
+
                        try {
                                ms.WriteByte (23);
+                               Assert.Fail ("#05");
                        } catch (NotSupportedException) {
-                               gotException = true;
                        }
 
-                       if (!gotException)
-                               Assert.Fail ("#05");
-
-                       ms.Capacity = 100; // Allowed. It's the same as the one in the ms.
-                                          // This is lame, as the length is 50!!!
+                       try {
+                               ms.Capacity = 100;
+                               Assert.Fail ("#06");
+                       } catch (NotSupportedException) {
+                       }
                                           
-                       gotException = false;
                        try {
                                ms.Capacity = 51;
+                               Assert.Fail ("#07");
                        } catch (NotSupportedException) {
-                               gotException = true;
                        }
 
-                       if (!gotException)
-                               Assert.Fail ("#07");
-
                        AssertEquals ("#08", 50, ms.ToArray ().Length);
                }
 
@@ -316,26 +312,18 @@ namespace MonoTests.System.IO
                        if (!thrown)
                                Assert.Fail ("#02");
 
-                       // The first exception thrown is ObjectDisposed, not ArgumentNull
-                       thrown = false;
                        try {
                                ms.Read (null, 0, 1);
-                       } catch (ObjectDisposedException) {
-                               thrown = true;
-                       }
-
-                       if (!thrown)
                                Assert.Fail ("#03");
+                       } catch (ArgumentNullException) {
+                       }
 
-                       thrown = false;
                        try {
                                ms.Write (null, 0, 1);
-                       } catch (ObjectDisposedException) {
+                               Assert.Fail ("#04");
+                       } catch (ArgumentNullException) {
                                thrown = true;
                        }
-
-                       if (!thrown)
-                               Assert.Fail ("#03");
                }
 
                [Test]