Decompressing using GZipStream

B

Big Daddy

There's an example in the .NET documentation of using GZipStream to
decompress a byte array:

http://msdn.microsoft.com/en-us/library/as1ff51s.aspx

In the example, the data is read out of the stream 100 bytes at a time
in a while loop until it's finished:

private const int buffer_size = 100;
public static int ReadAllBytesFromStream(Stream stream, byte[]
buffer)
{
// Use this method is used to read all bytes from a stream.
int offset = 0;
int totalCount = 0;
while (true)
{
int bytesRead = stream.Read(buffer, offset, buffer_size);
if (bytesRead == 0)
{
break;
}
offset += bytesRead;
totalCount += bytesRead;
}
return totalCount;
}
int totalCount = GZipTest.ReadAllBytesFromStream(zipStream,
decompressedBuffer);

Why isn't it read out in one fell swoop like this:

int bytesRead = stream.Read(buffer, offset, buffer.length);

I have tried it and it seems to work. The code would be cleaner and
run faster, but it makes me worried that there was a good reason for
reading it out 100 bytes at a time in the documentation.

Any ideas?
thanks in advance,
John
 
C

Cowboy \(Gregory A. Beamer\)

In essence, you are not using a buffer, but a dump. This is fine for small
objects, but can be nasty when you get to very large compressed bits. That
is why you normally see a buffer of n characters rather than a dump of the
entire contents.

--
Gregory A. Beamer
MVP; MCP: +I, Se, SD, DBA

Blog:
http://feeds.feedburner.com/GregoryBeamer

*************************************************
| Think outside the box! |
*************************************************
 
B

Big Daddy

In essence, you are not using a buffer, but a dump. This is fine for small
objects, but can be nasty when you get to very large compressed bits. That
is why you normally see a buffer of n characters rather than a dump of the
entire contents.
I don't understand what you mean. What's the difference between a
buffer and a dump? Do you mean that if you take it out 100 bytes at a
time (buffer), then you can process it 100 bytes at a time, and then
you don't have to have the entire contents in a byte array at once in
case it's really big? If your output might be over a gig in size, then
you might not want to put the whole thing into an array at once. But
my output won't be bigger than tens of thousands of bytes, and I have
to have the whole thing in a byte array at once. So then would it be
OK for me to just use the simpler method (all at once) rather than 100
bytes at a time?

thanks
 
M

Moe Sisko

Why isn't it read out in one fell swoop like this:

int bytesRead = stream.Read(buffer, offset, buffer.length);

I have tried it and it seems to work. The code would be cleaner and
run faster, but it makes me worried that there was a good reason for
reading it out 100 bytes at a time in the documentation.

Well, it will probably work, but there might be no guarantee according to
the documentation for Stream.Read.

More info : http://www.yoda.arachsys.com/csharp/readbinary.html

FWIW, here's a slightly cleaner (IMO) version of the looping method :

int bytesRead;
while ((bytesRead = stream.Read(buffer, offset, buffer_size))>0)
{
offset += bytesRead;
totalCount += bytesRead;
}
 

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