PC Review


Reply
Thread Tools Rate Thread

DeflateStream question

 
 
bthetford
Guest
Posts: n/a
 
      21st Feb 2007
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?

 
Reply With Quote
 
 
 
 
bthetford
Guest
Posts: n/a
 
      23rd Feb 2007
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.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deflatestream compression Tony Johansson Microsoft C# .NET 1 30th Jul 2010 02:48 PM
Is DeflateStream = .zip format? Grok Microsoft VB .NET 4 31st Jan 2008 04:14 PM
Alternatives to DeflateStream Smithers Microsoft C# .NET 1 26th Sep 2007 08:39 AM
DeflateStream & MemoryStream? =?Utf-8?B?QXNhZg==?= Microsoft C# .NET 10 24th Feb 2006 10:32 PM
DeflateStream Poor Compression? Neo Microsoft Dot NET 0 26th Jan 2006 04:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:55 PM.