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?
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?