By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 1,912 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

increasing the size of a byte array and reading streams

P: n/a
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);
}
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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??
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.
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.


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
Nov 15 '05 #2

P: n/a
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" <Gi********@CSILasVegas.com> wrote in message news:<#o*************@TK2MSFTNGP11.phx.gbl>...
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!

Nov 15 '05 #3

P: n/a
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" <ni*********@hotmail.com> wrote in message
news:a0*************************@posting.google.co m...
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" <Gi********@CSILasVegas.com> wrote in message

news:<#o*************@TK2MSFTNGP11.phx.gbl>...
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!

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.