* Tds.cs (LoadData): Improve exception message when attempting to
authorGert Driesen <drieseng@users.sourceforge.net>
Tue, 30 Dec 2008 20:10:38 +0000 (20:10 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Tue, 30 Dec 2008 20:10:38 +0000 (20:10 -0000)
read before the current position. Update the remaining length of the
stream when skipping bytes.

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

mcs/class/Mono.Data.Tds/Mono.Data.Tds.Protocol/ChangeLog
mcs/class/Mono.Data.Tds/Mono.Data.Tds.Protocol/Tds.cs

index 99fdd8a125da1251a6176acbb833bad6a571f393..f8577e0dc2e1c3135930082390e8a5c30e52f2a3 100644 (file)
@@ -1,3 +1,8 @@
+2008-12-30  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * Tds.cs (LoadData): Improve exception message when attempting to
+       read before the current position. Update the remaining length of the
+       stream when skipping bytes.
 
 2008-12-05 Gonzalo Paniagua Javier <gonzalo@novell.com>
 
index 230ae122608232eb3e0abfd8faa57775897704bf..b5f7e9eac2907581498a53142277f8d14fd38f8a 100644 (file)
@@ -349,16 +349,25 @@ namespace Mono.Data.Tds.Protocol
                                return StreamLength;
 
                        if (fieldIndex < StreamIndex)
-                               throw new InvalidOperationException ("field index less than stream pos");
+                               throw new InvalidOperationException (string.Format (
+                                       "Attempting to read at index '{0}' is not " +
+                                       "allowed as this is less than the current " +
+                                       "position. You must read from index '{1}' " +
+                                       "or greater.", fieldIndex, StreamIndex));
 
                        if (fieldIndex >= (StreamLength + StreamIndex))
                                return 0;
 
-                       // Skip to the index
-                       Comm.Skip ((int) (fieldIndex - StreamIndex));
+                       // determine number of bytes to skip
+                       int skip = (int) (fieldIndex - StreamIndex);
+                       // skip bytes
+                       Comm.Skip (skip);
+                       // update the current position
                        StreamIndex += (fieldIndex - StreamIndex);
+                       // update the remaining length
+                       StreamLength -= skip;
 
-                       // Load the reqd amt of bytes   
+                       // Load the reqd amt of bytes
                        int loadlen = (int) ((size > StreamLength) ? StreamLength : size);
                        byte[] arr = Comm.GetBytes (loadlen, true);