End of Stream encountered before parsing was completed?
Asked Answered
P

8

48

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?

Pulvinate answered 20/11, 2008 at 19:48 Comment(2)
In addition to the stream position issues pointed out below, another reason this can happen is because your app exits before a large file is written to disk (if you are serializing a large amount of data to a file stream). To fix this, you need to implement a blocking wait until the file is fully written like in this post: #10982604Rarebit
For me removing saved data file from "C:\Users\<Username>\AppData\LocalLow\" fixed it because the problem was that I changed data models after saving the file which made the model of the existing data in file different and made it throw errors while deserializing it. Hope it helps.Crackerjack
C
66

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);
Cardenas answered 20/11, 2008 at 19:48 Comment(1)
Hi, I've same issue here, but after inserting s.Position =0; I get "Stream does not support seeking"Gavette
B
6

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.

Bejewel answered 20/11, 2008 at 20:0 Comment(0)
O
5

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

Then it all worked perfectly.

Ovine answered 22/5, 2017 at 0:41 Comment(2)
this was exactly my problemQuiles
That was my problem too. My serialized class was referencing another class that didn't have the serializable tag.Ideograph
D
0

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!

Dolphin answered 29/4, 2015 at 15:1 Comment(0)
I
0

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}");
        }
    }
}

Indeed answered 21/11, 2019 at 14:56 Comment(0)
D
0

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
Drescher answered 28/9, 2020 at 10:24 Comment(0)
K
0

the class which you created must has [Serializable].

Kaminsky answered 13/4, 2022 at 11:3 Comment(1)
This is a duplicate answer; it is the same guidance as @Ovine offered five years ago. Please be sure to review all existing answers before contributing a new one. Later, once you have a bit more reputation, you'll be able to validate existing answers by upvoting them.Sheldonshelduck
U
0

I had the same problem. If the class I was serializing had other custom objects in it, I had to make sure that those custom objects were [Serializable] too to make the binary formatter serialize/deserialize work without this error.

Unorganized answered 13/12, 2023 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.