> As an extra question what is the difference between the two methods of[color=blue]
> reading the stream below (where buf is a byte array). Why do people
> tend to prefer the second method?? Is it purly to do with being able
> to read as much as possible if something goes wrong??[/color]
There is nothing wrong with the first method. I use it all the time.
When you're reading a file, for example, and you know that the file is
exactly 'x' bytes long, there's no problem just creating a big byte array of
length 'x' and doing one massive read.
There are times, however, when it makes sense to read a stream in small
chunks. For example, maybe you're writing a file sharing program and you
want your application to be able to deliver a 12gig file containing "The
Matrix Revolutions" to other users that might request it. You wouldn't want
to create a 12gig byte array and read in all of the bytes [this would be
impossible on a 32bit O/S anyway]. So you'd just read in a little bit at a
time, distributing various pieces of the file to users as needed.
Another more common scenario where you want to might want to read in
small chunks is when you're reading from a network stream. Even if you know
that the stream will be exactly 'x' bytes, the bandwidth over the network is
pretty thin -- so instead of waiting for all 'x' bytes in one call, you
might want to read a little, go do something else while waiting for the data
to arrive, read a little more, etc.
[color=blue]
>At the end of
> this message is the inflate method this is where I get stuck I know
> that I need a byte array but because I am decompressing a string I
> have no idea of how big the byte array will need to be in the end (the
> inflate and deflate methods will be in two separate exe's so I cann't
> just path the length of the original string). Us there a way to keep
> increasing the size of the byte array as I read more of it.[/color]
There's a couple of ways to handle this. One solution is to use an
ArrayList. Each time you read a chunk of data into a byte array, store that
byte[] in your ArrayList. When you're done reading, reassemble all of the
byte[]s in your ArrayList into one long byte[].
Another more practical approach would be to set up a secondary
MemoryStream. Whenever you read some bytes from your compressed stream,
write those uncompressed bytes to your secondary MemoryStream. When you're
done reading/writing bytes, call ToArray() on your secondary memory stream
to extract all of the bytes. Don't forget to Dispose() of this memory stream
when you're done with it!
--
Sincerely,
David Sworder
http://www.CodeFanatic.com