SerializationException: End of Stream encountered before parsing was completed

Hello everybody,

hours of research and debugging couldn’t help me, which is why you are my last hope. The error “SerializationException: End of Stream encountered before parsing was completed” occurs when I try to read a file from a local folder.

I already tried:

  • using “using” instead of just stream.Close();
  • setting stream.position = 0
  • searching for errors like serialized class not serializable or other syntax errors

The main class to save:

using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveHandler
{
    private static string filePath = Application.persistentDataPath + "/2401202.sav";
    public static void save()
    {
        BinaryFormatter bf = new BinaryFormatter();
        using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
        {
            //fs.Position = 0;
            SaveDataCollector saveData = new SaveDataCollector();
            bf.Serialize(fs, saveData);
        }
    }

    public static void load()
    {
        if (File.Exists(filePath))
        {
            BinaryFormatter bif = new BinaryFormatter();
            using (FileStream fes = new FileStream(filePath, FileMode.Open))
            {
                //fes.Position = 0;
                SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
                savoDato.distributeData();
            }
        }
        else
        {
            Debug.Log("Errorcode 401: Savefile not Found");
        }
    }
}

    public static void load()
    {
        if (File.Exists(filePath))
        {
            BinaryFormatter bif = new BinaryFormatter();
            using (FileStream fes = new FileStream(filePath, FileMode.Open))
            {
                //fes.Position = 0;
                SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
                savoDato.distributeData();
            }
        }
        else
        {
            Debug.Log("Errorcode 401: Savefile not Found");
        }
    }

The serialized class:

using UnityEngine;

[System.Serializable]
public class SaveDataCollector
{
    public float highScore;
    public bool legacyControl;

    public SaveDataCollector()
    {
        legacyControl = InterData.legacyControl;
        highScore = InterData.highScore;
    }

    public void distributeData()
    {
        InterData.legacyControl = this.legacyControl;
        InterData.highScore = this.highScore;
    }
}

Please help me, I’m really getting desperate.
Thanks in advance!

You most likely try to load invalid or outdated data. Do you have modified your “SaveDataCollector” after you saved the file? This would completely break the file you saved. That’s the main reason why the BinaryFormatter is a bad choice for a savegame solution apart from the large amount of overhead.

Have you tried actually deleting your save file and saving a new one? Have you checked if the file was actually created? Note that your save class can only store a single float and a single bool value at the moment.