[corlib] Removes extra bufffer from FileStream async mode writes. Fixes #27086
authorMarek Safar <marek.safar@gmail.com>
Wed, 18 Feb 2015 11:54:49 +0000 (12:54 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 18 Feb 2015 11:56:36 +0000 (12:56 +0100)
mcs/class/corlib/System.IO/FileStream.cs
mcs/class/corlib/Test/System.IO/BinaryWriterTest.cs

index 1ca519f5d64b3948945b1522afba491c174f4029..540dd456f583b9fd1e3997351d5cb1aa975d862b 100644 (file)
@@ -692,7 +692,7 @@ namespace System.IO
                        result.BytesRead = -1;
                        result.Count = numBytes;
                        result.OriginalCount = numBytes;
-
+/*
                        if (buf_dirty) {
                                MemoryStream ms = new MemoryStream ();
                                FlushBuffer (ms);
@@ -703,7 +703,7 @@ namespace System.IO
                                array = ms.ToArray ();
                                numBytes = array.Length;
                        }
-
+*/
                        WriteDelegate w = WriteInternal;
                        return w.BeginInvoke (array, offset, numBytes, userCallback, stateObject);      
                }
@@ -1012,20 +1012,21 @@ namespace System.IO
                        return(count);
                }
 
-               void FlushBuffer (Stream st)
+               void FlushBuffer ()
                {
                        if (buf_dirty) {
-                               MonoIOError error;
+//                             if (st == null) {
+                                       MonoIOError error;
 
-                               if (CanSeek == true && !isExposed) {
-                                       MonoIO.Seek (safeHandle, buf_start, SeekOrigin.Begin, out error);
+                                       if (CanSeek == true && !isExposed) {
+                                               MonoIO.Seek (safeHandle, buf_start, SeekOrigin.Begin, out error);
 
-                                       if (error != MonoIOError.ERROR_SUCCESS) {
-                                               // don't leak the path information for isolated storage
-                                               throw MonoIO.GetException (GetSecureFileName (name), error);
+                                               if (error != MonoIOError.ERROR_SUCCESS) {
+                                                       // don't leak the path information for isolated storage
+                                                       throw MonoIO.GetException (GetSecureFileName (name), error);
+                                               }
                                        }
-                               }
-                               if (st == null) {
+
                                        int wcount = buf_length;
                                        int offset = 0;
                                        while (wcount > 0){
@@ -1037,9 +1038,9 @@ namespace System.IO
                                                wcount -= n;
                                                offset += n;
                                        }
-                               } else {
-                                       st.Write (buf, 0, buf_length);
-                               }
+//                             } else {
+//                                     st.Write (buf, 0, buf_length);
+//                             }
                        }
 
                        buf_start += buf_offset;
@@ -1047,15 +1048,10 @@ namespace System.IO
                        buf_dirty = false;
                }
 
-               private void FlushBuffer ()
-               {
-                       FlushBuffer (null);
-               }
-
                private void FlushBufferIfDirty ()
                {
                        if (buf_dirty)
-                               FlushBuffer (null);
+                               FlushBuffer ();
                }
 
                private void RefillBuffer ()
index 5d221a4d7b0b6fb01c717a04fab595c3ce043573..811f44db62c8d2d7b324dfb9243131f18ec17a0d 100644 (file)
@@ -568,6 +568,29 @@ public class BinaryWriterTest {
                Assert.AreEqual (new byte[] { 0 }, stream.ToArray (), "#7");
        }
 
+       [Test]
+       public void AsynchronousModeWrites ()
+       {
+               string filename = Path.Combine (TempFolder, "myfilename");
+
+               using (var file = new FileStream (filename, FileMode.CreateNew,
+                                         FileAccess.Write, FileShare.Read, 4096, FileOptions.Asynchronous))
+               using (var writer = new BinaryWriter (file)) {
+                       writer.Write (new byte[] { 0xCC, 0xDD } );
+                       writer.Write (new byte[] { 0xAA } );
+               }
+
+               using (var inputStream = new FileStream (filename, FileMode.Open))
+               {
+                       using (var reader = new BinaryReader (inputStream)) {
+                               var l = reader.ReadByte ();
+                               Assert.AreEqual (0xCC, l);
+
+                               Assert.AreEqual (3, inputStream.Length);
+                       }
+               }
+       }
+
        [Test]
        public void BaseStreamCallsFlush ()
        {