【发布时间】:2019-08-14 06:29:58
【问题描述】:
我使用MessagePackSerializer 并尝试反序列化字节数组。 但数组可能非常大(10-20 MB)。
我正在将数据读入一个 1000 字节的中间缓冲区。 我从他们那里读取数据。
但是有一个问题:当我尝试读取一行太长时,可能会出现错误
System.ArgumentOutOfRangeException: Index and count must refer
to a location within the buffer.
我需要找出我要读取的行超出了数组的边界,我需要将缓冲区扩展为行的大小。
我该怎么做?
我使用这个代码:
var stringValue = MessagePackBinary.ReadString(bytes, off, out readSize);
简单示例:
public class Example
{
public void Serialize(Stream inputStream,string value)
{
MessagePackBinary.WriteString(inputStream, value);
}
public string Deserealize(Stream stream)
{
var off = 0;
byte[] bytes = new byte[1000];
int readSize = 0;
stream.Read(bytes, off, bytes.Length);
var stringValue = MessagePackBinary.ReadString(bytes, off, out readSize); //string can be very long
return stringValue;
}
}
【问题讨论】:
-
如果你想用更小的缓冲区来做,你需要使用循环。看看this answer。
-
@Joelies,没有。我找到了解决方案:我需要在序列化程序中写入字符串长度
标签: c# serialization deserialization msgpack