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();

Signature
E. Connolly
Patrice - 20 Jul 2006 15:36 GMT
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...

Signature
Patrice
> In .Net Framework 2.0 (VS2005) I'm having problems deserializing binary
> data
[quoted text clipped - 100 lines]
>
> dstream.Close();
Econnolly - 20 Jul 2006 16:08 GMT
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.

Signature
E. Connolly
> 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
[quoted text clipped - 108 lines]
> >
> > dstream.Close();
Patrice - 20 Jul 2006 17:00 GMT
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.

Signature
Patrice
> Thank you, Patrice - I'll try this (the binary file compare). I do have
> both
[quoted text clipped - 126 lines]
>> >
>> > dstream.Close();
Econnolly - 20 Jul 2006 21:07 GMT
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.

Signature
E. Connolly
> 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
[quoted text clipped - 131 lines]
> >> >
> >> > dstream.Close();