DeflateStream question

B

bthetford

I am trying to roll a simple compression scheme for network
communication in an app I am developing.
On the sender side, I compress each block of a set amount of bytes
individually, then send it over the NetworkStream along with the size
of the original data, the size of the compressed data, and a byte
telling the receiving end whether to expect more.

On the receiving end, these blocks are received along with that size
data and buffers are created of exactly the required sizes.
The problem comes during decompression.
In the decompression routine, I do:

....
while(true)
{

if ( rdr.ReadByte() == 0 )
break;
int compressedSize = rdr.ReadInt32( );
byte[ ] compressedBytes = rdr.ReadBytes( compressedSize );
int decompressedSize = rdr.ReadInt32( );
byte[ ] decompressedBytes = new byte[ decompressedSize ];
MS.SetLength( 0 );
MS.Write( compressedBytes, 0, compressedSize );
MS.Position = 0;
int thisRead = DS.Read( decompressedBytes, 0, decompressedSize );
writer.Write( decompressedBytes, 0, thisRead );
MS.Position = 0;
}

All this ends up doing is making a blank file. thisRead always ends
up being 0, which means nothing gets written to the file.
It wouldn't matter, though, because the decompressedBytes array is a
bunch of zeros anyway after the call to DS.Read.

Just for reference, MS is my MemoryStream, DS is the DeflateStream,
rdr is a BinaryReader reading the received data, and writer is a
BinaryWriter writing to the destination FileStream.

I know the compressed data is making it to the receiving end because
I've dumped the buffers before decompression and they are the same on
both ends.

What am I doing wrong?
 
B

bthetford

The problem, for anyone interested, was the same that others have had
in more traditional applications of the DeflateStream class.
The DeflateStream has to be closed before it will flush.

Therefore, I create a new DeflateStream on every iteration of the loop
and close it after I've written the block to the NetworkStream.

DeflateStream badly needs a flush method to make it more network
friendly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top