【问题标题】:End of Stream encountered before parsing was completed?在解析完成之前遇到流结束?
【发布时间】:2023-03-05 12:27:01
【问题描述】:

我正在尝试反序列化流,但总是收到此错误“在解析完成之前遇到流结束”?

代码如下:

        //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

有人有想法吗?

【问题讨论】:

  • 除了下面指出的流位置问题之外,发生这种情况的另一个原因是您的应用在 large 文件写入磁盘之前退出(如果您正在序列化一个大量数据到文件流)。要解决此问题,您需要执行阻塞等待,直到文件完全写入,如这篇文章中所示:stackoverflow.com/questions/10982104/…
  • 对我来说,从“C:\Users\\AppData\LocalLow\”中删除保存的数据文件修复了它,因为问题是我在保存文件后更改了数据模型,这使得模型文件中的现有数据不同,并使其在反序列化时引发错误。希望对您有所帮助。

标签: c# .net serialization .net-2.0 c#-2.0


【解决方案1】:

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

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

【讨论】:

  • 嗨,我这里也有同样的问题,但是在插入 s.Position =0; 之后我得到“流不支持搜索”
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    就我而言,我使用的是:

    stream.Seek(0, SeekOrigin.Begin);
    

    在我序列化流之后,在我反序列化流之前工作魅力。希望这会有所帮助!

    【讨论】:

      【解决方案4】:

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

      然后一切都很顺利。

      【讨论】:

      • 这正是我的问题
      • 这也是我的问题。我的序列化类引用了另一个没有可序列化标记的类。
      【解决方案5】:

      我已经花了 5 个小时,并且遇到了流错误和丢失数据的结束(GzipStream 中不明显的功能:您应该仅在刷新 GzipStream 后使用底层流)。

      工作代码的完整示例:

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

      【讨论】:

        【解决方案6】:

        如果您没有执行以下操作,请检查您的发件人代码

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多