如何将protobuf序列化内容直接写入SharpZipLib流(How to writte protobuf serialized content directly to SharpZipLib s

系统教程 行业动态 更新时间:2024-06-14 17:01:34
如何将protobuf序列化内容直接写入SharpZipLib流(How to writte protobuf serialized content directly to SharpZipLib stream)

是否有可能将protobuf序列化内容直接写入SharpZipLib流? 当我尝试这样做时,看起来所提供的流没有填充来自protobuf的数据。 后来我需要从提供的Zip流中取回反序列化的实体。 我的代码如下所示:

private byte[] ZipContent(T2 content) { const short COMPRESSION_LEVEL = 4; // 0-9 const string ENTRY_NAME = "DefaultEntryName"; byte[] result = null; if (content == null) return result; IStreamSerializerProto<T2> serializer = this.GetSerializer(content.GetType()); using (MemoryStream outputStream = new MemoryStream()) { using (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) { zipOutputStream.SetLevel(COMPRESSION_LEVEL); ZipEntry entry = new ZipEntry(ENTRY_NAME); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); serializer.Serialize(zipOutputStream, content); } result = outputStream.ToArray(); } return result; } private class ProtobufStreamSerializer<T3> : IStreamSerializerProto<T3> { public ProtobufStreamSerializer() { ProtoBuf.Serializer.PrepareSerializer<T3>(); } public void Serialize(Stream outputStream, T3 content) { Serializer.Serialize(outputStream, content); } public T3 Deserialize(Stream inputStream) { T3 deserializedObj; using (inputStream) { deserializedObj = ProtoBuf.Serializer.Deserialize<T3>(inputStream); } return deserializedObj; } }

我尝试序列化的类的示例:

[Serializable] [ProtoContract] public class Model { [XmlElement("ModelCode")] [ProtoMember(1)] public int ModelCode { get; set; } ... }

Is there possible to writte protobuf serialized content directly to SharpZipLib stream? When I try to do this, looks like the provided stream is not filled with the data from protobuf. Later I would need to get back the deserialized entity from provided Zip stream. My code looks like this:

private byte[] ZipContent(T2 content) { const short COMPRESSION_LEVEL = 4; // 0-9 const string ENTRY_NAME = "DefaultEntryName"; byte[] result = null; if (content == null) return result; IStreamSerializerProto<T2> serializer = this.GetSerializer(content.GetType()); using (MemoryStream outputStream = new MemoryStream()) { using (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) { zipOutputStream.SetLevel(COMPRESSION_LEVEL); ZipEntry entry = new ZipEntry(ENTRY_NAME); entry.DateTime = DateTime.Now; zipOutputStream.PutNextEntry(entry); serializer.Serialize(zipOutputStream, content); } result = outputStream.ToArray(); } return result; } private class ProtobufStreamSerializer<T3> : IStreamSerializerProto<T3> { public ProtobufStreamSerializer() { ProtoBuf.Serializer.PrepareSerializer<T3>(); } public void Serialize(Stream outputStream, T3 content) { Serializer.Serialize(outputStream, content); } public T3 Deserialize(Stream inputStream) { T3 deserializedObj; using (inputStream) { deserializedObj = ProtoBuf.Serializer.Deserialize<T3>(inputStream); } return deserializedObj; } }

Sample of a class which I'm trying to serialize:

[Serializable] [ProtoContract] public class Model { [XmlElement("ModelCode")] [ProtoMember(1)] public int ModelCode { get; set; } ... }

最满意答案

这是问题,我相信(在问题中的原始代码中):

public void Serialize(Stream outputStream, T3 content) { using (var stream = new MemoryStream()) { Serializer.Serialize(stream, content); } }

您完全忽略了outputStream ,而是将数据写入新的MemoryStream ,然后忽略它。

我怀疑你只是想要:

public void Serialize(Stream outputStream, T3 content) { Serializer.Serialize(outputStream, content); }

我还建议从Deserialize方法中删除using语句:我希望调用者在完成输入流时负责处理输入流。 您的方法可以简化为:

public T3 Deserialize(Stream inputStream) { return ProtoBuf.Serializer.Deserialize<T3>(inputStream); }

This is the problem, I believe (in the original code in the question):

public void Serialize(Stream outputStream, T3 content) { using (var stream = new MemoryStream()) { Serializer.Serialize(stream, content); } }

You're completely ignoring outputStream, instead writing the data just to a new MemoryStream which is then ignored.

I suspect you just want:

public void Serialize(Stream outputStream, T3 content) { Serializer.Serialize(outputStream, content); }

I'd also suggest removing the using statement from your Deserialize method: I'd expect the caller to be responsible for disposing of the input stream when they're finished with it. Your method can be simplified to:

public T3 Deserialize(Stream inputStream) { return ProtoBuf.Serializer.Deserialize<T3>(inputStream); }

更多推荐

本文发布于:2023-04-20 18:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/88c89adca2769749612e0bacaa7cbf6a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   序列化   内容   protobuf   SharpZipLib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!