Connecting Tech Pros Worldwide Forums | Help | Site Map

increasing the size of a byte array and reading streams

Nick
Guest
 
Posts: n/a
#1: Nov 15 '05
I have found a class that compresses and uncompresses data but need
some help with how to use part of it below is the deflate method which
compresses the string that I pass in, this works OK. 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 seperate 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.

As an extra question what is the difference between the two methods of
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??

1)inStream.Read(buf,pos,buf.Length);
2)
while (true)
{
int numRead = inStream.Read(buf, pos, 4096);
if (numRead <= 0)
{
break;
}
pos += numRead;
}

Thnaks for any help,
Nick.




public string Deflate(string strToCompress)
{
string sReturn;
try
{
MemoryStream ms = new MemoryStream();
Deflater deflater = new Deflater(3);
DeflaterOutputStream outStream = new DeflaterOutputStream(ms,
deflater);
byte[] buf = m_Encoder.GetBytes(str);
outStream.Write(buf, 0, buf.Length);
outStream.Flush();
sReturn = MemoryStreamToString(outStream);
outStream.Finish();
}
catch(Exception ex)
{
Console.WriteLine("ERROR:" + ex.Message);
}
return sReturn;
}

public string Inflate(string strToUncompress)
{
string sReturn;
try
{
InflaterInputStream inStream = new
InflaterInputStream(StringToMemoryStream(strToUnco mpress));
byte[] buf2 = new byte[/*How big?*/];
// not sure how big to make this array
inStream.Read(buf2,pos,buf2.Length);
// or
while (true)
{
int numRead = inStream.Read(buf2, pos, 4096);
if (numRead <= 0)
{
break;
}
pos += numRead;
}

}
catch(Exception ex)
{
Console.WriteLine("ERROR:" + ex.Message);
}
return sReturn;
}

private MemoryStream StringToMemoryStream(String str)
{
return new MemoryStream(StringToByteArray(str));
}

private String MemoryStreamToString(MemoryStream memStream)
{
return ByteArrayToString(memStream.GetBuffer());
}

private Byte[] StringToByteArray(string str)
{
return m_Encoder.GetBytes(str);
}

private string ByteArrayToString(byte[] byteArray)
{
int NumberOfBytes = byteArray.Length;
return m_Encoder.GetString(byteArray, 0 , NumberOfBytes);
}

David Sworder
Guest
 
Posts: n/a
#2: Nov 15 '05

re: increasing the size of a byte array and reading streams


> 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


Nick
Guest
 
Posts: n/a
#3: Nov 15 '05

re: increasing the size of a byte array and reading streams


Thanks for the answer. Could you give me a small sample of code to
show how the secondary memory stream method would roughly look like.

Thanks agian,
Nick

"David Sworder" <GilGrissom@CSILasVegas.com> wrote in message news:<#o30WRm8DHA.452@TK2MSFTNGP11.phx.gbl>...[color=blue]
> 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![/color]
David Sworder
Guest
 
Posts: n/a
#4: Nov 15 '05

re: increasing the size of a byte array and reading streams


Well, I'm not very good at writing code on the fly, but it would probably
look something like this:

http://temp.codefanatic.com/usenet/sampleThing.txt

This code snippet assumes that you have a source Stream called 'inStream'.
It'll read the data from 'inStream' and copy the bytes to a MemoryStream.
Finally, the bytes are copied to a byte array called 'myBytes'
--
Sincerely,

David Sworder
http://www.CodeFanatic.com

"Nick" <nick_tucker@hotmail.com> wrote in message
news:a061d649.0402131608.273c065@posting.google.co m...[color=blue]
> Thanks for the answer. Could you give me a small sample of code to
> show how the secondary memory stream method would roughly look like.
>
> Thanks agian,
> Nick
>
> "David Sworder" <GilGrissom@CSILasVegas.com> wrote in message[/color]
news:<#o30WRm8DHA.452@TK2MSFTNGP11.phx.gbl>...[color=blue][color=green]
> > 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[/color][/color]
that[color=blue][color=green]
> > byte[] in your ArrayList. When you're done reading, reassemble all of[/color][/color]
the[color=blue][color=green]
> > 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[/color][/color]
you're[color=blue][color=green]
> > done reading/writing bytes, call ToArray() on your secondary memory[/color][/color]
stream[color=blue][color=green]
> > to extract all of the bytes. Don't forget to Dispose() of this memory[/color][/color]
stream[color=blue][color=green]
> > when you're done with it![/color][/color]


Closed Thread