Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I serialize a huge list(binary serialization) all works properly but when i try to deserialize, an exception is raised saying that: "the end of the stream encountered before the end of the analysis" How to overcomer this issue?

Please try to be clear, thanks in advance to all contributors
Posted
Updated 3-Sep-14 23:32pm
v2
Comments
kbrandwijk 4-Sep-14 5:35am    
Need help? Post code...
orélle 4-Sep-14 5:39am    
public void save(object toSave, string path)
{
//On utilise la classe BinaryFormatter dans le namespace System.Runtime.Serialization.Formatters.Binary.
BinaryFormatter formatter = new BinaryFormatter();
//La classe BinaryFormatter ne fonctionne qu'avec un flux, et non pas un TextWriter.
//Nous allons donc utiliser un FileStream. Remarquez que n'importe quel flux est
//compatible.
FileStream flux = null;
try
{
//On ouvre le flux en mode création / écrasement de fichier et on
//donne au flux le droit en écriture seulement.
flux = new FileStream(path, FileMode.Create, FileAccess.Write);
//Et hop ! On sérialise !
formatter.Serialize(flux, toSave);
//On s'assure que le tout soit écrit dans le fichier.
flux.Flush();
}
catch { }
finally
{
//Et on ferme le flux.
if (flux != null)
flux.Close();
}


}

/***************************************************************************************************************************/
public T load<t>(string path)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream flux = null;
try
{
//On ouvre le fichier en mode lecture seule. De plus, puisqu'on a sélectionné le mode Open,
//si le fichier n'existe pas, une exception sera levée.
flux = new FileStream(path, FileMode.Open);
return (T)formatter.Deserialize(flux);

}
catch (SerializationException e)
{
//On retourne la valeur par défaut du type T.
System.Windows.Forms.MessageBox.Show(" Failed! Raison: "+e.Message);
return default(T);
}
finally
{
if (flux != null)
flux.Close();
}

}


This is how i call them:
designer.save(designer.ListEquips, Path + @"\equips.dat");
designer.save(designer.ListLinks, Path + @"\links.dat");
designer.save(designer.ListConnector, Path + @"\connectors.dat");

List<equipment> equipments = designer.load<List<equipment>>(Path + @"\equips.dat");
List<Link> links = designer.load<List>(Path + @"\links.dat");
List<shape> connectors = designer.load<List<shape>>(Path + @"\connectors.dat");

It means that whatever you are deserializing is either corrupted or of a different type / version than the serialized data.

Check the way you call the generic load method: are you serializing a derived class and deserializing a base class, or vice versa?
 
Share this answer
 
Comments
orélle 4-Sep-14 8:31am    
The objects that i serialize are not derived.
My differents classes: Equipment, Link and Shape are base classes.

Sometimes the deserialization works pretty well sometimes not. I am confused!!
- Are you sure that serialization completes successfully? You have an empty catch, so all exceptions are swallowed.
- Try using a different formatter so you can at least read the output file to check if it's okay

If you're absolutely sure that serialization works well, focus on deserialization:

- Try setting the position of the FileStream to 0 when loading.
- Are you sure the type specifications are correct, equipments and connectors being NON-generic lists?
 
Share this answer
 
Comments
orélle 4-Sep-14 9:15am    
I throw serializationException in my catch i discover that serialization was not completed successfully. An exception was thrown telling that ShapeClass attribute of my Equipment class "is not marked Serializable". This was the problem!!!
Then i solved the issue by modifying my business object Equipment class(Shapeclass as member it was useless).
It works properly now :)

Thanks for your problem solving skill, i retained that: "Prior to solve a problem try to understand from where its come from ortherwise you may be tried to solve the wrong problem".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900