Bug #7704 GetBytes is working no more
Submitted: 6 Jan 2005 13:43 Modified: 12 Jan 2005 18:35
Reporter: [ name withheld ] Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:1.0.3-gamma OS:Microsoft Windows (Windows XP)
Assigned to: Reggie Burnett

[6 Jan 2005 13:43] [ name withheld ]
Description:
I have just tried to play with the latest connector, my previous version was 1.0.0-beta. I had a code to read a large blob from a table, using the GetBytes method. When I have changed to the newest MySQL.Data.dll I got an exception everytime I try to run the code.

How to repeat:
Here is my code for reading the blob:

    public string SaveTemplate(IDbConnection cnn, string type, string table, string targetDir) {
      string tmp = String.Empty;
      using (IDbCommand cmd = cnn.CreateCommand()) {
        cmd.CommandText = "SELECT zawartosc FROM " + table + " WHERE typ='" + type + "'";
        //DbType.
        //cmd.CreateParameter().DbType
        using (IDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess)) {
          if (r.Read() == true) {
            tmp = targetDir + type;
            using (BinaryWriter bw = new BinaryWriter(new FileStream(tmp, FileMode.Create, FileAccess.Write))) {
              int bufferSize = 8192;
              byte[] outbyte = new byte[bufferSize];
              int retval;
              long startIndex = 0;
              do {
                try {
                  retval = (int)r.GetBytes(0, startIndex, outbyte, 0, bufferSize);
                } catch (Exception ex) {
                  Log(Parent, "Błąd przy zapisywaniu szablonu", ex);
                  return tmp;
                }
                bw.Write(outbyte, 0, retval);
                bw.Flush();
                startIndex += bufferSize;
              } while (retval == bufferSize);
            }
          }
        }
      }
      return tmp;
    }    

Only the first bufferSize bytes are read, even when the blob I try to read is larger (around 100kB). The exception message and stack trace is here (the message says length must be non-negative value):

System.ArgumentOutOfRangeException
Wymagana jest liczba nieujemna.
Nazwa parametru: length
Void Copy(System.Array, Int32, System.Array, Int32, Int32)
   at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
   at MySql.Data.MySqlClient.CompressedStream.Read(Byte[] buffer, Int32 offset, Int32 count) in h:
etMySqlClientCompressedStream.cs:line 114
   at MySql.Data.MySqlClient.PacketReader.Read(Byte[]& buffer, Int64 pos, Int64 len) in h:
etMySqlClientPacketReader.cs:line 223
   at MySql.Data.Types.MySqlBinary.ReadValue(PacketReader reader, Int64 length) in h:
etMySqlClientTypesMySqlBinary.cs:line 154
   at MySql.Data.MySqlClient.NativeDriver.ReadFieldValue(Int32 index, MySqlField field, MySqlValue valObject) in h:
etMySqlClient
ativedriver.cs:line 484
   at MySql.Data.MySqlClient.CommandResult.get_Item(Int32 index) in h:
etMySqlClientCommandResult.cs:line 77
   at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(Int32 index) in h:
etMySqlClientdatareader.cs:line 703
   at MySql.Data.MySqlClient.MySqlDataReader.GetBytes(Int32 i, Int64 dataIndex, Byte[] buffer, Int32 bufferIndex, Int32 length) in h:
etMySqlClientdatareader.cs:line 217
   at ... my own code here, the line with GetBytes

Suggested fix:
I have not tried to found the error, but I guess it will not be hard as in previous versions of the connector it was ok.
[11 Jan 2005 18:48] Jorge del Conde
Thanks for the bug report.
[12 Jan 2005 18:35] Reggie Burnett
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html