DeflateStream.Read is not working, I'm trying to read from a compressed memory stream but the byte array argument of read remains empty.
var memoryStream = new MemoryStream();
var writeStream = new DeflateStream(memoryStream, CompressionLevel.Optimal, true);
var readStream = new DeflateStream(memoryStream, CompressionMode.Decompress, true);
var serializedPayloadBytes = Serialize(new Payload { Message = "Payload" });
var serializedHeaderBytes = Serialize(new PayloadHeader { Length = serializedPayloadBytes.Length });
var headerSize = serializedHeaderBytes.Length;
var package = new byte[serializedHeaderBytes.Length + serializedPayloadBytes.Length];
Buffer.BlockCopy(serializedHeaderBytes, 0, package, 0, serializedHeaderBytes.Length);
Buffer.BlockCopy(serializedPayloadBytes, 0, package, serializedHeaderBytes.Length, serializedHeaderBytes.Length);
writeStream.Write(package, 0, package.Length);
writeStream.Flush();
writeStream.Close();
var arr = new byte[serializedHeaderBytes.Length];
readStream.Read(arr, 0, headerSize);
The arr is always empty (all bytes are zero), the memoryStream.ToArray() has data.
The
MemoryStream
s Position is at the end of the stream after writing it. You have to set it back to 0 if you want to read from the same stream again after write.