GZipStream Decompress always reads 4K minimum...!

B

Bob

I have a network stream of data I am reading, of which, a section is
compressed and can be decompressed using GZipStream's decompression. This
part works great, however, even if the part being decompressed is 1K,
GZipStream /always/ reads 4K minimum off my primary stream. Using .NET
Reflector I can see they have a 4K buffer internally and always read in
chunks of 4K.

Basically, I have to force GZipStream to only read what is compressed, and
no more. This should be possible because I am reading fixed sized
structures out of GZipStream and upon the last structure I should begin
reading uncompressed data again.

Do I have any options here?

Thanks
 
D

David Browne

Bob said:
I have a network stream of data I am reading, of which, a section is
compressed and can be decompressed using GZipStream's decompression. This
part works great, however, even if the part being decompressed is 1K,
GZipStream /always/ reads 4K minimum off my primary stream. Using .NET
Reflector I can see they have a 4K buffer internally and always read in
chunks of 4K.

Basically, I have to force GZipStream to only read what is compressed, and
no more. This should be possible because I am reading fixed sized
structures out of GZipStream and upon the last structure I should begin
reading uncompressed data again.

Do I have any options here?


Sure. Just read off your compressed data into a fixed-size buffer and feed
that to the GZipStream by wrapping it in a MemoryStream.

byte[] buf = new byte[1024];
//read fixed-length compressed data into buf
GZipStream gs = new GZipStream(new MemoryStream(buf));

David
 
B

Bob

Thanks for responding... but thats the problem, I don't know the exact size
of the compressed data. I just know "when to stop reading compressed data."
Even if I add an additional stream between the network stream and
GZipStream, I don't know where to position the uncompressed data (I don't
know how much compressed data GZipStream read, I only know how much
uncompressed data was read).


David Browne said:
Bob said:
I have a network stream of data I am reading, of which, a section is
compressed and can be decompressed using GZipStream's decompression. This
part works great, however, even if the part being decompressed is 1K,
GZipStream /always/ reads 4K minimum off my primary stream. Using .NET
Reflector I can see they have a 4K buffer internally and always read in
chunks of 4K.

Basically, I have to force GZipStream to only read what is compressed,
and no more. This should be possible because I am reading fixed sized
structures out of GZipStream and upon the last structure I should begin
reading uncompressed data again.

Do I have any options here?


Sure. Just read off your compressed data into a fixed-size buffer and
feed that to the GZipStream by wrapping it in a MemoryStream.

byte[] buf = new byte[1024];
//read fixed-length compressed data into buf
GZipStream gs = new GZipStream(new MemoryStream(buf));

David
 
L

Lloyd Dupont

You could also write the size of the compressed data first.

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
Bob said:
Thanks for responding... but thats the problem, I don't know the exact
size of the compressed data. I just know "when to stop reading compressed
data." Even if I add an additional stream between the network stream and
GZipStream, I don't know where to position the uncompressed data (I don't
know how much compressed data GZipStream read, I only know how much
uncompressed data was read).


David Browne said:
Bob said:
I have a network stream of data I am reading, of which, a section is
compressed and can be decompressed using GZipStream's decompression.
This part works great, however, even if the part being decompressed is
1K, GZipStream /always/ reads 4K minimum off my primary stream. Using
.NET Reflector I can see they have a 4K buffer internally and always read
in chunks of 4K.

Basically, I have to force GZipStream to only read what is compressed,
and no more. This should be possible because I am reading fixed sized
structures out of GZipStream and upon the last structure I should begin
reading uncompressed data again.

Do I have any options here?


Sure. Just read off your compressed data into a fixed-size buffer and
feed that to the GZipStream by wrapping it in a MemoryStream.

byte[] buf = new byte[1024];
//read fixed-length compressed data into buf
GZipStream gs = new GZipStream(new MemoryStream(buf));

David
 
J

Jon Skeet [C# MVP]

Bob said:
Thanks for responding... but thats the problem, I don't know the exact size
of the compressed data. I just know "when to stop reading compressed data."
Even if I add an additional stream between the network stream and
GZipStream, I don't know where to position the uncompressed data (I don't
know how much compressed data GZipStream read, I only know how much
uncompressed data was read).

So you've got some amount of compressed data and then some other data,
with no idea beforehand how much compressed data there was? That's a
really bad protocol design, unfortunately - you're likely to have
difficulty in decoding it.

Do you control the protocol, or does it "belong" to someone else?
 

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