2003-06-11 Zoltan Varga <vargaz@freemail.hu>
authorZoltan Varga <vargaz@gmail.com>
Thu, 12 Jun 2003 08:35:22 +0000 (08:35 -0000)
committerZoltan Varga <vargaz@gmail.com>
Thu, 12 Jun 2003 08:35:22 +0000 (08:35 -0000)
* FileStream.cs: Fix errors in previous checkins:
(Write): Only take the shortcut route if the data is longer than the
buffer length.
(Write): Flush the buffer before writing out the new data
(Write): Flush the buffer after writing out a segment since otherwise
we will go into an infinite loop.
(FlushBuffer): Remove my last change since it was clearly wrong.
(Seek): Run FlushBuffer () after the in-buffer seek optimization.
(Seek): Only use the in-buffer optimization if the buffer is not
empty.
(Length): Call FlushBuffer () since buffer data might change the size
of the stream.

svn path=/trunk/mcs/; revision=15336

mcs/class/corlib/System.IO/ChangeLog
mcs/class/corlib/System.IO/FileStream.cs

index 8793418f7816e800ce5c860a0f13c67a0bf4e0cc..3a7658c9fb00855ba4f4f8747ca54348e6a05edb 100644 (file)
@@ -1,3 +1,18 @@
+2003-06-11  Zoltan Varga  <vargaz@freemail.hu>
+
+       * FileStream.cs: Fix errors in previous checkins:
+       (Write): Only take the shortcut route if the data is longer than the
+       buffer length.
+       (Write): Flush the buffer before writing out the new data
+       (Write): Flush the buffer after writing out a segment since otherwise
+       we will go into an infinite loop.
+       (FlushBuffer): Remove my last change since it was clearly wrong.
+       (Seek): Run FlushBuffer () after the in-buffer seek optimization.
+       (Seek): Only use the in-buffer optimization if the buffer is not
+       empty.
+       (Length): Call FlushBuffer () since buffer data might change the size
+       of the stream.
+
 2003-06-09  Ville Palo <vi64pa@kolumbus.fi>
 
        * FileStream.cs:
index 2e1427320c1253fcdae7e5fd62b587b8ce1b5668..e29fd3b4e13b6603e1a7f371abfbc71fbecaff3b 100644 (file)
@@ -187,6 +187,9 @@ namespace System.IO
                                if (!canseek)
                                        throw new NotSupportedException ("The stream does not support seeking");
 
+                               // Buffered data might change the length of the stream
+                               FlushBufferIfDirty ();
+
                                MonoIOError error;
                                
                                return MonoIO.GetLength (handle, out error);
@@ -344,12 +347,12 @@ namespace System.IO
                        if (!CanWrite)
                                throw new NotSupportedException ("Stream does not support writing");
 
-
-                               
                        if (count > buf_size) {
                                // shortcut for long writes
                                MonoIOError error;
-                               
+
+                               FlushBuffer ();
+
                                MonoIO.Write (handle, src, src_offset, count, out error);
                                buf_start += count;
                        } else {
@@ -360,10 +363,12 @@ namespace System.IO
                                        int n = WriteSegment (src, src_offset + copied, count);
                                        copied += n;
                                        count -= n;
-                                       
+
                                        if (count == 0) {
                                                break;
                                        }
+
+                                       FlushBuffer ();
                                }
                        }
                }
@@ -383,8 +388,6 @@ namespace System.IO
 
                        lock(this) {
 
-                               FlushBuffer ();
-
                                switch (origin) {
                                case SeekOrigin.End:
                                        pos = Length + offset;
@@ -410,13 +413,17 @@ namespace System.IO
                                        /* More undocumented crap */
                                        throw new IOException("Can't seek back over pre-existing data in append mode");
                                }
-                               
-                               if (pos >= buf_start &&
-                                   pos <= buf_start + buf_length) {
-                                       buf_offset = (int) (pos - buf_start);
-                                       return pos;
+
+                               if (buf_length > 0) {
+                                       if (pos >= buf_start &&
+                                               pos <= buf_start + buf_length) {
+                                               buf_offset = (int) (pos - buf_start);
+                                               return pos;
+                                       }
                                }
 
+                               FlushBuffer ();
+
                                MonoIOError error;
                        
                                buf_start = MonoIO.Seek (handle, pos,
@@ -568,12 +575,18 @@ namespace System.IO
                                MonoIO.Write (handle, buf, 0,
                                              buf_length, out error);
                        }
-                       
+
                        buf_start += buf_offset;
                        buf_offset = buf_length = 0;
                        buf_dirty = false;
                }
 
+               private void FlushBufferIfDirty ()
+               {
+                       if (buf_dirty)
+                               FlushBuffer ();
+               }
+
                private void RefillBuffer ()
                {
                        FlushBuffer();