Home All Groups Group Topic Archive Search About

problem de-serializing binary data stored in db

Author
20 Jul 2006 2:24 PM
Econnolly
In .Net Framework 2.0 (VS2005) I'm having problems deserializing binary data
that was serialized and stored in a mysql 5.0 longblob field (stored via the
mysql data connector datareader object). I have modified the code to try
various suggestions I've seen in the forums, but no luck so far. Thanks in
advance for any light you can shed on this....

The error is "End of Stream encountered before parsing was completed."

The code used to serialize is:

                Stream stream = new System.IO.FileStream("BinFile.bin",
                                FileMode.Create,
                                FileAccess.Write,
                                FileShare.None);
                formatter.Serialize(stream,_DataValue.BinDataArray);
                stream.Close();

                //store binary data in approp db field.
                this._ParentObject.CurrentData.Value_Binary =
GetBinaryData("BinFile.bin");

//the GetBinaryData function is:
private static byte[] GetBinaryData(string filePath)
            {

                System.IO.FileStream bstream = new FileStream(
                    filePath,FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(bstream);

                byte[] BinaryOut = reader.ReadBytes((int)bstream.Length);

                reader.Close();
                bstream.Close();
                return BinaryOut;
            }

The code used to deserialize is:

               FileStream stream;
                BinaryWriter writer;

              .
              .
              .



                int bufSize = 100000;  //originally 100, I made it 100000
trying to see
                                                 //if that would help this
problem
                byte[] buff = new byte[bufSize];
                long retval = 0;
                long startIndex = 0;
                .
                .
                .

                    stream = new FileStream("DataBinFile.bin",
FileMode.OpenOrCreate, FileAccess.Write);
                    writer = new BinaryWriter(stream);

                    startIndex = 0;

                    //dr is an already open mysqlconnector datareader - like
any ado.net
                    //datareader
                    retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
startIndex, buff, 0, bufSize);

                    while (retval == bufSize)
                        {
                        writer.Write(buff);
                        writer.Flush();

                        startIndex += bufSize;
                        retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
startIndex, buff, 0, bufSize);
                        }


                    //let's try putting the stream back to the beginning of
the file
                    stream.Seek(0, SeekOrigin.Begin);
                    writer.Close();
                    stream.Close();
                    }
                dr.Close();


                IFormatter formatter = new BinaryFormatter();

                Stream dstream = new System.IO.FileStream("DataBinFile.bin",
                                FileMode.Open,
                                FileAccess.Read,
                                FileShare.Read);
                dstream.Seek(0, SeekOrigin.Begin);
                //the error occurs when I go to execute the next line of code
                //to deserialize
                _MyValueArray =
(DataCollBinValue)formatter.Deserialize(dstream);

                dstream.Close();

--
E. Connolly

Author
20 Jul 2006 2:36 PM
Patrice
To me the retval==bufsize test would be incorrect (IMO you don't write
anything as retval returns AFAIK the number of bytes actually read which
could be lower than the bufsize even for the first loop).

Anyway the first step to approach such a problem would be likely to keep
both files and to do as file compare. You'll see immediately if this is a
size problem...

--
Patrice

"Econnolly" <econno***@disccussions.microsoft.com> a écrit dans le message
de news: 65888BA8-CF48-4D1B-9788-4B7A11D13***@microsoft.com...
Show quote
> In .Net Framework 2.0 (VS2005) I'm having problems deserializing binary
> data
> that was serialized and stored in a mysql 5.0 longblob field (stored via
> the
> mysql data connector datareader object). I have modified the code to try
> various suggestions I've seen in the forums, but no luck so far. Thanks in
> advance for any light you can shed on this....
>
> The error is "End of Stream encountered before parsing was completed."
>
> The code used to serialize is:
>
>                Stream stream = new System.IO.FileStream("BinFile.bin",
>                                FileMode.Create,
>                                FileAccess.Write,
>                                FileShare.None);
>                formatter.Serialize(stream,_DataValue.BinDataArray);
>                stream.Close();
>
>                //store binary data in approp db field.
>                this._ParentObject.CurrentData.Value_Binary =
> GetBinaryData("BinFile.bin");
>
> //the GetBinaryData function is:
> private static byte[] GetBinaryData(string filePath)
>            {
>
>                System.IO.FileStream bstream = new FileStream(
>                    filePath,FileMode.Open, FileAccess.Read);
>                BinaryReader reader = new BinaryReader(bstream);
>
>                byte[] BinaryOut = reader.ReadBytes((int)bstream.Length);
>
>                reader.Close();
>                bstream.Close();
>                return BinaryOut;
>     }
>
> The code used to deserialize is:
>
>               FileStream stream;
>                BinaryWriter writer;
>
>              .
>              .
>              .
>
>
>
>                int bufSize = 100000;  //originally 100, I made it 100000
> trying to see
>                                                 //if that would help this
> problem
>                byte[] buff = new byte[bufSize];
>                long retval = 0;
>                long startIndex = 0;
>                .
>                .
>                .
>
>                    stream = new FileStream("DataBinFile.bin",
> FileMode.OpenOrCreate, FileAccess.Write);
>                    writer = new BinaryWriter(stream);
>
>                    startIndex = 0;
>
>                    //dr is an already open mysqlconnector datareader -
> like
> any ado.net
>                    //datareader
>                    retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
> startIndex, buff, 0, bufSize);
>
>                    while (retval == bufSize)
>                        {
>                        writer.Write(buff);
>                        writer.Flush();
>
>                        startIndex += bufSize;
>                        retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
> startIndex, buff, 0, bufSize);
>                        }
>
>
>                    //let's try putting the stream back to the beginning of
> the file
>                    stream.Seek(0, SeekOrigin.Begin);
>                    writer.Close();
>                    stream.Close();
>                    }
>                dr.Close();
>
>
>                IFormatter formatter = new BinaryFormatter();
>
>                Stream dstream = new
> System.IO.FileStream("DataBinFile.bin",
>                                FileMode.Open,
>                                FileAccess.Read,
>                                FileShare.Read);
>                dstream.Seek(0, SeekOrigin.Begin);
>                //the error occurs when I go to execute the next line of
> code
>                //to deserialize
>                _MyValueArray =
> (DataCollBinValue)formatter.Deserialize(dstream);
>
>                dstream.Close();
>
> --
> E. Connolly
Author
20 Jul 2006 3:08 PM
Econnolly
Thank you, Patrice - I'll try this (the binary file compare). I do have both
files still around - they appear to be the same size but I will do a binary
file compare to see if they are really the same. But if they are the same,
then what?

As far as the retval == bufsize test, I just took this (and almost all the
code) right from the msdn example.
--
E. Connolly


Show quote
"Patrice" wrote:

> To me the retval==bufsize test would be incorrect (IMO you don't write
> anything as retval returns AFAIK the number of bytes actually read which
> could be lower than the bufsize even for the first loop).
>
> Anyway the first step to approach such a problem would be likely to keep
> both files and to do as file compare. You'll see immediately if this is a
> size problem...
>
> --
> Patrice
>
> "Econnolly" <econno***@disccussions.microsoft.com> a écrit dans le message
> de news: 65888BA8-CF48-4D1B-9788-4B7A11D13***@microsoft.com...
> > In .Net Framework 2.0 (VS2005) I'm having problems deserializing binary
> > data
> > that was serialized and stored in a mysql 5.0 longblob field (stored via
> > the
> > mysql data connector datareader object). I have modified the code to try
> > various suggestions I've seen in the forums, but no luck so far. Thanks in
> > advance for any light you can shed on this....
> >
> > The error is "End of Stream encountered before parsing was completed."
> >
> > The code used to serialize is:
> >
> >                Stream stream = new System.IO.FileStream("BinFile.bin",
> >                                FileMode.Create,
> >                                FileAccess.Write,
> >                                FileShare.None);
> >                formatter.Serialize(stream,_DataValue.BinDataArray);
> >                stream.Close();
> >
> >                //store binary data in approp db field.
> >                this._ParentObject.CurrentData.Value_Binary =
> > GetBinaryData("BinFile.bin");
> >
> > //the GetBinaryData function is:
> > private static byte[] GetBinaryData(string filePath)
> >            {
> >
> >                System.IO.FileStream bstream = new FileStream(
> >                    filePath,FileMode.Open, FileAccess.Read);
> >                BinaryReader reader = new BinaryReader(bstream);
> >
> >                byte[] BinaryOut = reader.ReadBytes((int)bstream.Length);
> >
> >                reader.Close();
> >                bstream.Close();
> >                return BinaryOut;
> >     }
> >
> > The code used to deserialize is:
> >
> >               FileStream stream;
> >                BinaryWriter writer;
> >
> >              .
> >              .
> >              .
> >
> >
> >
> >                int bufSize = 100000;  //originally 100, I made it 100000
> > trying to see
> >                                                 //if that would help this
> > problem
> >                byte[] buff = new byte[bufSize];
> >                long retval = 0;
> >                long startIndex = 0;
> >                .
> >                .
> >                .
> >
> >                    stream = new FileStream("DataBinFile.bin",
> > FileMode.OpenOrCreate, FileAccess.Write);
> >                    writer = new BinaryWriter(stream);
> >
> >                    startIndex = 0;
> >
> >                    //dr is an already open mysqlconnector datareader -
> > like
> > any ado.net
> >                    //datareader
> >                    retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
> > startIndex, buff, 0, bufSize);
> >
> >                    while (retval == bufSize)
> >                        {
> >                        writer.Write(buff);
> >                        writer.Flush();
> >
> >                        startIndex += bufSize;
> >                        retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
> > startIndex, buff, 0, bufSize);
> >                        }
> >
> >
> >                    //let's try putting the stream back to the beginning of
> > the file
> >                    stream.Seek(0, SeekOrigin.Begin);
> >                    writer.Close();
> >                    stream.Close();
> >                    }
> >                dr.Close();
> >
> >
> >                IFormatter formatter = new BinaryFormatter();
> >
> >                Stream dstream = new
> > System.IO.FileStream("DataBinFile.bin",
> >                                FileMode.Open,
> >                                FileAccess.Read,
> >                                FileShare.Read);
> >                dstream.Seek(0, SeekOrigin.Begin);
> >                //the error occurs when I go to execute the next line of
> > code
> >                //to deserialize
> >                _MyValueArray =
> > (DataCollBinValue)formatter.Deserialize(dstream);
> >
> >                dstream.Close();
> >
> > --
> > E. Connolly
>
>
>
Author
20 Jul 2006 4:00 PM
Patrice
Ok see, if the size is 0 or a multiple of bufsize. Double check that the
sample and your code are similar (they perhaps write the remaining bytes
when the loop is ended ?)

Good luck.
--
Patrice

"Econnolly" <econno***@disccussions.microsoft.com> a écrit dans le message
de news: 2C8303E8-1D7E-425F-A28C-4C22481F2***@microsoft.com...
Show quote
> Thank you, Patrice - I'll try this (the binary file compare). I do have
> both
> files still around - they appear to be the same size but I will do a
> binary
> file compare to see if they are really the same. But if they are the same,
> then what?
>
> As far as the retval == bufsize test, I just took this (and almost all the
> code) right from the msdn example.
> --
> E. Connolly
>
>
> "Patrice" wrote:
>
>> To me the retval==bufsize test would be incorrect (IMO you don't write
>> anything as retval returns AFAIK the number of bytes actually read which
>> could be lower than the bufsize even for the first loop).
>>
>> Anyway the first step to approach such a problem would be likely to keep
>> both files and to do as file compare. You'll see immediately if this is a
>> size problem...
>>
>> --
>> Patrice
>>
>> "Econnolly" <econno***@disccussions.microsoft.com> a écrit dans le
>> message
>> de news: 65888BA8-CF48-4D1B-9788-4B7A11D13***@microsoft.com...
>> > In .Net Framework 2.0 (VS2005) I'm having problems deserializing binary
>> > data
>> > that was serialized and stored in a mysql 5.0 longblob field (stored
>> > via
>> > the
>> > mysql data connector datareader object). I have modified the code to
>> > try
>> > various suggestions I've seen in the forums, but no luck so far. Thanks
>> > in
>> > advance for any light you can shed on this....
>> >
>> > The error is "End of Stream encountered before parsing was completed."
>> >
>> > The code used to serialize is:
>> >
>> >                Stream stream = new System.IO.FileStream("BinFile.bin",
>> >                                FileMode.Create,
>> >                                FileAccess.Write,
>> >                                FileShare.None);
>> >                formatter.Serialize(stream,_DataValue.BinDataArray);
>> >                stream.Close();
>> >
>> >                //store binary data in approp db field.
>> >                this._ParentObject.CurrentData.Value_Binary =
>> > GetBinaryData("BinFile.bin");
>> >
>> > //the GetBinaryData function is:
>> > private static byte[] GetBinaryData(string filePath)
>> >            {
>> >
>> >                System.IO.FileStream bstream = new FileStream(
>> >                    filePath,FileMode.Open, FileAccess.Read);
>> >                BinaryReader reader = new BinaryReader(bstream);
>> >
>> >                byte[] BinaryOut =
>> > reader.ReadBytes((int)bstream.Length);
>> >
>> >                reader.Close();
>> >                bstream.Close();
>> >                return BinaryOut;
>> >     }
>> >
>> > The code used to deserialize is:
>> >
>> >               FileStream stream;
>> >                BinaryWriter writer;
>> >
>> >              .
>> >              .
>> >              .
>> >
>> >
>> >
>> >                int bufSize = 100000;  //originally 100, I made it
>> > 100000
>> > trying to see
>> >                                                 //if that would help
>> > this
>> > problem
>> >                byte[] buff = new byte[bufSize];
>> >                long retval = 0;
>> >                long startIndex = 0;
>> >                .
>> >                .
>> >                .
>> >
>> >                    stream = new FileStream("DataBinFile.bin",
>> > FileMode.OpenOrCreate, FileAccess.Write);
>> >                    writer = new BinaryWriter(stream);
>> >
>> >                    startIndex = 0;
>> >
>> >                    //dr is an already open mysqlconnector datareader -
>> > like
>> > any ado.net
>> >                    //datareader
>> >                    retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
>> > startIndex, buff, 0, bufSize);
>> >
>> >                    while (retval == bufSize)
>> >                        {
>> >                        writer.Write(buff);
>> >                        writer.Flush();
>> >
>> >                        startIndex += bufSize;
>> >                        retval =
>> > dr.GetBytes(dr.GetOrdinal("Value_Binary"),
>> > startIndex, buff, 0, bufSize);
>> >                        }
>> >
>> >
>> >                    //let's try putting the stream back to the beginning
>> > of
>> > the file
>> >                    stream.Seek(0, SeekOrigin.Begin);
>> >                    writer.Close();
>> >                    stream.Close();
>> >                    }
>> >                dr.Close();
>> >
>> >
>> >                IFormatter formatter = new BinaryFormatter();
>> >
>> >                Stream dstream = new
>> > System.IO.FileStream("DataBinFile.bin",
>> >                                FileMode.Open,
>> >                                FileAccess.Read,
>> >                                FileShare.Read);
>> >                dstream.Seek(0, SeekOrigin.Begin);
>> >                //the error occurs when I go to execute the next line of
>> > code
>> >                //to deserialize
>> >                _MyValueArray =
>> > (DataCollBinValue)formatter.Deserialize(dstream);
>> >
>> >                dstream.Close();
>> >
>> > --
>> > E. Connolly
>>
>>
>>
Author
20 Jul 2006 8:07 PM
Econnolly
That was indeed the problem! The files were in fact NOT the same - probably
due to the fact that I used a different technique to read the binary from the
db - writing it into a file in chunks rather than all in one, as I wrote it
in. I am able to deserialize the original file with no problem. So, I guess I
need to change either the way I write it in or the way I am reading it out.

Thanks again for the hints.
--
E. Connolly


Show quote
"Patrice" wrote:

> Ok see, if the size is 0 or a multiple of bufsize. Double check that the
> sample and your code are similar (they perhaps write the remaining bytes
> when the loop is ended ?)
>
> Good luck.
> --
> Patrice
>
> "Econnolly" <econno***@disccussions.microsoft.com> a écrit dans le message
> de news: 2C8303E8-1D7E-425F-A28C-4C22481F2***@microsoft.com...
> > Thank you, Patrice - I'll try this (the binary file compare). I do have
> > both
> > files still around - they appear to be the same size but I will do a
> > binary
> > file compare to see if they are really the same. But if they are the same,
> > then what?
> >
> > As far as the retval == bufsize test, I just took this (and almost all the
> > code) right from the msdn example.
> > --
> > E. Connolly
> >
> >
> > "Patrice" wrote:
> >
> >> To me the retval==bufsize test would be incorrect (IMO you don't write
> >> anything as retval returns AFAIK the number of bytes actually read which
> >> could be lower than the bufsize even for the first loop).
> >>
> >> Anyway the first step to approach such a problem would be likely to keep
> >> both files and to do as file compare. You'll see immediately if this is a
> >> size problem...
> >>
> >> --
> >> Patrice
> >>
> >> "Econnolly" <econno***@disccussions.microsoft.com> a écrit dans le
> >> message
> >> de news: 65888BA8-CF48-4D1B-9788-4B7A11D13***@microsoft.com...
> >> > In .Net Framework 2.0 (VS2005) I'm having problems deserializing binary
> >> > data
> >> > that was serialized and stored in a mysql 5.0 longblob field (stored
> >> > via
> >> > the
> >> > mysql data connector datareader object). I have modified the code to
> >> > try
> >> > various suggestions I've seen in the forums, but no luck so far. Thanks
> >> > in
> >> > advance for any light you can shed on this....
> >> >
> >> > The error is "End of Stream encountered before parsing was completed."
> >> >
> >> > The code used to serialize is:
> >> >
> >> >                Stream stream = new System.IO.FileStream("BinFile.bin",
> >> >                                FileMode.Create,
> >> >                                FileAccess.Write,
> >> >                                FileShare.None);
> >> >                formatter.Serialize(stream,_DataValue.BinDataArray);
> >> >                stream.Close();
> >> >
> >> >                //store binary data in approp db field.
> >> >                this._ParentObject.CurrentData.Value_Binary =
> >> > GetBinaryData("BinFile.bin");
> >> >
> >> > //the GetBinaryData function is:
> >> > private static byte[] GetBinaryData(string filePath)
> >> >            {
> >> >
> >> >                System.IO.FileStream bstream = new FileStream(
> >> >                    filePath,FileMode.Open, FileAccess.Read);
> >> >                BinaryReader reader = new BinaryReader(bstream);
> >> >
> >> >                byte[] BinaryOut =
> >> > reader.ReadBytes((int)bstream.Length);
> >> >
> >> >                reader.Close();
> >> >                bstream.Close();
> >> >                return BinaryOut;
> >> >     }
> >> >
> >> > The code used to deserialize is:
> >> >
> >> >               FileStream stream;
> >> >                BinaryWriter writer;
> >> >
> >> >              .
> >> >              .
> >> >              .
> >> >
> >> >
> >> >
> >> >                int bufSize = 100000;  //originally 100, I made it
> >> > 100000
> >> > trying to see
> >> >                                                 //if that would help
> >> > this
> >> > problem
> >> >                byte[] buff = new byte[bufSize];
> >> >                long retval = 0;
> >> >                long startIndex = 0;
> >> >                .
> >> >                .
> >> >                .
> >> >
> >> >                    stream = new FileStream("DataBinFile.bin",
> >> > FileMode.OpenOrCreate, FileAccess.Write);
> >> >                    writer = new BinaryWriter(stream);
> >> >
> >> >                    startIndex = 0;
> >> >
> >> >                    //dr is an already open mysqlconnector datareader -
> >> > like
> >> > any ado.net
> >> >                    //datareader
> >> >                    retval = dr.GetBytes(dr.GetOrdinal("Value_Binary"),
> >> > startIndex, buff, 0, bufSize);
> >> >
> >> >                    while (retval == bufSize)
> >> >                        {
> >> >                        writer.Write(buff);
> >> >                        writer.Flush();
> >> >
> >> >                        startIndex += bufSize;
> >> >                        retval =
> >> > dr.GetBytes(dr.GetOrdinal("Value_Binary"),
> >> > startIndex, buff, 0, bufSize);
> >> >                        }
> >> >
> >> >
> >> >                    //let's try putting the stream back to the beginning
> >> > of
> >> > the file
> >> >                    stream.Seek(0, SeekOrigin.Begin);
> >> >                    writer.Close();
> >> >                    stream.Close();
> >> >                    }
> >> >                dr.Close();
> >> >
> >> >
> >> >                IFormatter formatter = new BinaryFormatter();
> >> >
> >> >                Stream dstream = new
> >> > System.IO.FileStream("DataBinFile.bin",
> >> >                                FileMode.Open,
> >> >                                FileAccess.Read,
> >> >                                FileShare.Read);
> >> >                dstream.Seek(0, SeekOrigin.Begin);
> >> >                //the error occurs when I go to execute the next line of
> >> > code
> >> >                //to deserialize
> >> >                _MyValueArray =
> >> > (DataCollBinValue)formatter.Deserialize(dstream);
> >> >
> >> >                dstream.Close();
> >> >
> >> > --
> >> > E. Connolly
> >>
> >>
> >>
>
>
>

AddThis Social Bookmark Button