Question

I am trying to send serialized,compressed data over a tcp connection using protobuf-net and GzipStream

The deserializing or reading from the zipstream just blocks and does not complete.

To test it I opted to try using a simpler FileStream to see that the data was actually being compressed and written, which it was. However the line Console.writeLine(inPerson1.name) throws a Object reference not set exception since it seems to be not reading any data from the zipstream.

Any thoughts on why, or what I am doing wrong?

Code:

public void TestZipToFile(){
  var person1 = new Person { id = 1, name = "Pete" };
  var person2 = new Person { id = 2, name = "Sarah" };
  using (var file = File.Create("people.bin"))
  {
    GZipStream zs = new GZipStream(file, CompressionMode.Compress);
    Serializer.SerializeWithLengthPrefix<Person>(zs, person1, PrefixStyle.Fixed32);//.Serialize(file, foo);
    Serializer.SerializeWithLengthPrefix<Person>(zs, person2, PrefixStyle.Fixed32);//.Serialize(file, foo);
  }
  Person inPerson1 = null;
  Person inPerson2 = null;
  using (var file = File.OpenRead("people.bin"))
  {
    GZipStream ozs = new GZipStream(file, CompressionMode.Decompress);
    inPerson1 = Serializer.DeserializeWithLengthPrefix<Person>(ozs,PrefixStyle.Fixed32);//.Deserialize<Foo>(file);
    inPerson2 = Serializer.DeserializeWithLengthPrefix<Person>(ozs, PrefixStyle.Fixed32);//.Deserialize<Foo>(file);
  }
  Console.WriteLine(inPerson1.name);
  Console.WriteLine(inPerson2.name);
}

[ProtoContract]
public class Person
{
  [ProtoMember(1)
  public int id;
  [ProtoMember(2)]
  public string name;
}
Was it helpful?

Solution

You're never closing the output GZipStream, so I suspect it ends up never writing anything to the underlying stream. You should have a using statement for each GZipStream as well as the FileStream.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top