c# - 如何在从字节数组反序列化时确定字符串的长度?
问题描述
我使用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;
}
}
解决方案
20MB 现在真的不是那么多,即使在手机上也是如此。只需将整个数组读入内存并解码即可。
推荐阅读
- java - 除非我单击窗口标题,否则 JLabel 无法正常工作
- asp.net-core-identity - 自定义用户和声明商店,但在 Controller.User 上没有设置声明
- docker - 是否可以将容器安装为电子桌面应用程序的一部分?
- oracle-apex - 使用 Oracle APEX LOV 初始化多个页面项?- 顶点 21.1
- docker - Nginx 作为 Kibana over Docker 的反向代理
- python - JSON 上的 Python:如何处理凌乱的初始文件
- sql - SQL - 如何检查工资增加或减少的状态?
- arrays - C程序将数字数组向右移动N个位置购买无法正常工作
- google-cloud-platform - 用于构建和运行 Dataflow Flex 模板的 Terraform 脚本
- python - 扩展 fastapi 和问题