C# 在解析完成之前遇到流结束?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/306596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:29:45  来源:igfitidea点击:

End of Stream encountered before parsing was completed?

c#.netserialization.net-2.0c#-2.0

提问by Mister Dev

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?

有人有想法吗?

采纳答案by Patrick Desjardins

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

尝试将您的流的位置设置为 0,并且不要使用您的对象,而是使用对象类型。

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

回答by GWLlosa

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.

确保序列化完成,并且序列化类型与反序列化类型匹配(即,如果您使用 BinaryFormatter 进行反序列化,请确保您使用 BinaryFormatter 进行序列化)。另外,请确保您序列化的流确实完成了序列化,使用 Stream.Flush() 或类似的东西。

回答by Alia Ramli Ramli

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!

在我对流进行序列化之后,在反序列化流之前,该流很有魅力。希望这可以帮助!

回答by Ryano

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

我抛出了同样的异常,直到我将 [Serializable] 标签添加到我正在序列化的类中:)

Then it all worked perfectly.

然后一切都完美无缺。

回答by Yaroslav

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).

我已经花了 5 个小时,并且出现了流结束错误和数据丢失(GzipStream 中不明显的功能:您应该仅在刷新 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}");
        }
    }
}