'End of Stream encountered before parsing was completed?

I am trying to deserialize a stream but I always get this error "End of Stream encountered before parsing was completed"?

Here is the code:

        //Some code here
        BinaryFormatter b = new BinaryFormatter();
        return (myObject)b.Deserialize(s);//s---> is a Stream object that has been fill up with data some line over here

Any one have ideas?



Solution 1:[1]

Try to set the position to 0 of your stream and do not use your object but the object type.

        BinaryFormatter b = new BinaryFormatter();
        s.Position = 0;
        return (YourObjectType)b.Deserialize(s);

Solution 2:[2]

Make sure the serialization completed, and that the serialization type matches the de-serialization type (i.e., make sure you're serializing with a BinaryFormatter if you're de-serializing with one). Also, make sure that the stream you serialized to really finished serializing, with a Stream.Flush() or something to that effect.

Solution 3:[3]

I had the same exception thrown, until I added the [Serializable] tag to the class I was Serializing :)

Then it all worked perfectly.

Solution 4:[4]

In my case I used:

stream.Seek(0, SeekOrigin.Begin);

after i serialized the stream, and before i deserialized the stream works charm. hope this helps!

Solution 5:[5]

I have spent 5 hourse and have got end of stream error and lost data (Not obvious feature in GzipStream: you should use underlying stream only after flush GzipStream).

Full example of working code:

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

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string large = LargeJsonContent.GetBigObject();
            string base64;

            using (var readStream = new MemoryStream())
            using (var writeStream = new MemoryStream())
            {
                using (GZipStream compressor = new GZipStream(writeStream, CompressionMode.Compress, true)) //pay attention to leaveOpen = true
                {
                    var formatter = new BinaryFormatter();
                    formatter.Serialize(readStream, large);

                    Console.WriteLine($"After binary serialization of JsonString: {readStream.Length} bytes");

                    readStream.Position = 0;
                    readStream.CopyTo(compressor);
                }

                Console.WriteLine($"Compressed stream size: {writeStream.Length} bytes");

                writeStream.Position = 0;
                byte[] writeBytes = writeStream.ToArray();
                base64 = Convert.ToBase64String(writeBytes);
            }


            ////

            using (var stream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, base64);
                Console.WriteLine($"Size of base64: {stream.Length} bytes");
            }

            Console.WriteLine("---------------------");
            ////

            string large2;

            var bytes = Convert.FromBase64String(base64);
            using (var readStream = new MemoryStream())
            {
                readStream.Write(bytes, 0, bytes.Length);
                readStream.Position = 0;
                Console.WriteLine($"Compressed stream size: {readStream.Length} bytes");
                using (var writeStream = new MemoryStream())
                {
                    using (GZipStream decompressor = new GZipStream(readStream, CompressionMode.Decompress, true)) //pay attention to leaveOpen = true
                    {
                        decompressor.CopyTo(writeStream);
                        writeStream.Position = 0;
                    }

                    var formatter = new BinaryFormatter();
                    large2 = (string)formatter.Deserialize(writeStream);
                }
            }

            Console.WriteLine(large == large2);
            Console.WriteLine($"large:{large.Length} | large2:{large2.Length}");
        }
    }
}

Solution 6:[6]

Check in your sender code if you are not doing the following

NetworkStream strm = client.GetStream(); // the stream
formatter.Serialize(strm, status); // the serialization process
strm.Close();// Remove this code, this was the culprit in my case

Solution 7:[7]

the class which you created must has [Serializable].

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Patrick Desjardins
Solution 2 GWLlosa
Solution 3 Ryano
Solution 4 confile
Solution 5 Yaroslav
Solution 6 Mukesh Pareek
Solution 7 Ahmet do?ru