我正在尝试反序列化一个流但是我总是得到这个错误“在解析完成之前遇到了流的结束”?
以下是代码:
//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
有人有想法吗?
答案 0 :(得分:50)
尝试将流的位置设置为0,不要使用对象,而是使用对象类型。
BinaryFormatter b = new BinaryFormatter();
s.Position = 0;
return (YourObjectType)b.Deserialize(s);
答案 1 :(得分:6)
确保序列化已完成,并且序列化类型与反序列化类型匹配(例如,如果要使用二进制格式化,请确保使用二进制格式序列化)。此外,请确保您序列化的流已真正完成序列化,使用Stream.Flush()或其他类似的效果。
答案 2 :(得分:3)
我抛出了同样的异常,直到我将[Serializable]标签添加到我正在序列化的类中:)
然后一切都很完美。
答案 3 :(得分:1)
s.Position = 0;
这是因为你必须先回到开始复制阵列上的数据!
答案 4 :(得分:0)
就我而言,我用过:
stream.Seek(0, SeekOrigin.Begin);
在我序列化了流后,在我反序列化之前,流工作的魅力。希望这有帮助!
答案 5 :(得分:0)
我刚刚遇到了类似的错误
这是有关在序列化和反序列化时获取不同的数据类型的信息。 错误地,当将数据存储到mariadb时,我使用了MediumText,而当使用数据时,我使用了Text,这就是为什么我只获得部分流的原因。
只需检查数据类型是否相同。
答案 6 :(得分:0)
我花了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}");
}
}
}
答案 7 :(得分:0)
如果没有执行以下操作,请检查您的发件人代码
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