我正在序列化一个对象,它有几个子对象(它们也是可序列化的)。我可以在序列化过程之间检查流的大小吗? (我只想限制我正在序列化的流的大小).NET中考虑流化大小限制的序列化
1
A
回答
0
不使用任何内置类,但可以轻松地创建一个具有此类限制的流包装类。
只需将使用这样的:
Stream limitedStream = new LimitedStream(realStream, 10 * 1024 * 1024); // 10MB max
,然后序列化到这一点。
2
根据序列化程序,可能会独立地将项目放到流的末尾(并且还可以再次读取流)。不是用“纯粹的”xml(由于根元素),但我知道至少有一个支持顺序对象读/写的序列化程序(没有猜测哪个是奖品)。
如果大小是绝对键,那么也许通过MemoryStream
去 - 有点像(伪代码)
using(Stream dest = ...)
using(MemoryStream buffer = new MemoryStream())
{
foreach(SomeType obj in items) {
buffer.SetLength(0); // reset to empty, but retaining buffer to reduce allocs
someSerializer.Serialize(buffer, obj);
if(enoughSpace) {
// add our item
dest.Write(buffer.GetBuffer(), 0, buffer.Length);
// TODO: decrement space
} else {
break; // or new file, whatever
}
}
}
完成任务。这可避免通过完成每个对象到存储器流(尽可能重新使用底层内存)来完成每个对象的写入部分对象,因此只有当您知道它适合时才将数据复制到实际的流中。
下面是使用protobuf-net一个完整的示例:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using ProtoBuf;
[XmlType] // not actually serialized as xml; simply it needs
class MyData // a way of identifying the members, and it supports
{ // the Order property via XmlType/XmlElement
[XmlElement(Order = 1)] public int Id { get; set; }
[XmlElement(Order = 2)] public string Name { get; set; }
}
static class Program
{
static IEnumerable<MyData> GetItems()
{
Random rand = new Random();
int count = 0;
while (true) // an infinite sequence of data; mwahahaahah
{
yield return new MyData
{
Id = rand.Next(0, 5000),
Name = "Item " + count++
};
}
}
static void Main()
{
int space = 2048, count = 0;
long checksum = 0;
using(Stream dest = File.Create("out.bin"))
using(MemoryStream buffer = new MemoryStream())
{
foreach (MyData obj in GetItems())
{
buffer.SetLength(0); // reset to empty, but retaining buffer to reduce allocs
Serializer.SerializeWithLengthPrefix(buffer, obj, PrefixStyle.Base128, 1);
int len = (int)buffer.Length;
if(buffer.Length <= space) {
// add our item
dest.Write(buffer.GetBuffer(), 0, len);
space -= len;
checksum += obj.Id;
count++;
} else {
break; // or new file, whatever
}
}
}
Console.WriteLine("Wrote " + count + " objects; chk = " + checksum);
using (Stream source = File.OpenRead("out.bin"))
{
count = 0;
checksum = 0;
foreach (MyData item in
Serializer.DeserializeItems<MyData>(source, PrefixStyle.Base128, 1))
{
count++;
checksum += item.Id;
}
}
Console.WriteLine("Read " + count + " objects; chk = " + checksum);
}
}
您可能能这种方法与其他串行扩展(如BinaryFormatter
或DataContractSerializer
),但你可能必须写你自己的长度-prefix(写入时),使用它(读取时)并限制数据。不是火箭科学,但不是完全微不足道的。
相关问题
- 1. 使用json.net大小限制序列化数据?
- 2. 序列化对象大小?
- 3. 小胡子/霍根国际化,并采取词序考虑
- 4. .NET对象大小限制
- 5. .NET中的序列化
- 6. 如何最小化序列化数据的大小
- 7. 反序列化字符串考虑空单为空字符串
- 8. .NET中的序列化 - Android中的反序列化
- 9. 可序列化类的近似大小
- 10. 序列化/反序列化大对象单次Vs多次序列化/反序列化小对象?
- 11. WCF:序列化?流?
- 12. .NET XML序列化
- 13. .NET XML序列化
- 14. 书.Net序列化?
- 15. 在.NET中序列化JSON
- 16. protobuf-net消息序列化大小属性
- 17. CSS:不考虑最小宽度的两个50%流体列
- 18. 如何最小化序列化数据 - 二进制序列化有巨大的开销
- 19. 使用.NET序列化XML序列化到.NET类
- 20. .net二进制序列化 - 选项
- 21. 不区分大小写反序列化
- 22. sqlite中的大型数据库 - 文件大小的考虑?
- 23. C#:XML反序列化限制
- 24. 'getHighestRow'没有考虑到格式化
- 25. XCode - iOS本地化没有考虑到
- 26. .net二进制格式化程序反序列化其定义发生了很大变化的对象
- 27. Kohana ORM限制列大小
- 28. 如何在C#.Net中最小化和最大化?
- 29. Java自定义序列化和反序列化的位大小定义
- 30. 参考静态字段的.NET序列化问题
ok thanq。 因此,如果将10MB的数据写入流并且stil还有一些要被序列化的对象的其他属性会发生什么。它会引发异常吗? – sandhya 2010-03-04 12:00:03
我会写那个流类,当它达到10MB时抛出一个异常,是的。 – 2010-03-04 12:01:57