2009-12-21 Sebastien Pouliot <sebastien@ximian.com>
authorSebastien Pouliot <sebastien@ximian.com>
Mon, 21 Dec 2009 22:40:25 +0000 (22:40 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Mon, 21 Dec 2009 22:40:25 +0000 (22:40 -0000)
* File.cs (ReadAllBytes): We cannot assume that a single call to
Read will return all the data we require.

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

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

index e8e2c797ccb053ee079142d4abb15e6b13a896cb..0cf1205cfd3977bd3d72528f8beb25c3ff19c314 100644 (file)
@@ -1,3 +1,8 @@
+2009-12-21  Sebastien Pouliot  <sebastien@ximian.com>
+
+       * File.cs (ReadAllBytes): We cannot assume that a single call to
+       Read will return all the data we require.
+
 2009-11-24  Marek Safar <marek.safar@gmail.com>
 
        * StreamReader.cs, FileStream.cs: Use recycle buffer to avoid
index 3ac53c07f104f2dd1fd39544d8ea8f14fe5bc06f..a705380f4839eba4235b513260845c73cebf510e 100644 (file)
@@ -493,19 +493,22 @@ namespace System.IO
                //
                public static byte [] ReadAllBytes (string path)
                {
-                       using (FileStream s = Open (path, FileMode.Open, FileAccess.Read, FileShare.Read)){
+                       using (FileStream s = OpenRead (path)) {
                                long size = s.Length;
-
-                               //
-                               // Is this worth supporting?
-                               // 
+                               // limited to 2GB according to http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx
                                if (size > Int32.MaxValue)
-                                       throw new ArgumentException ("Reading more than 4gigs with this call is not supported");
-                               
-                               byte [] result = new byte [s.Length];
-
-                               s.Read (result, 0, (int) size);
-
+                                       throw new IOException ("Reading more than 2GB with this call is not supported");
+
+                               int pos = 0;
+                               int count = (int) size;
+                               byte [] result = new byte [size];
+                               while (count > 0) {
+                                       int n = s.Read (result, pos, count);
+                                       if (n == 0)
+                                               throw new IOException ("Unexpected end of stream");
+                                       pos += n;
+                                       count -= n;
+                               }
                                return result;
                        }
                }