472,127 Members | 1,826 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

increasing the size of a byte array and reading streams

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
3 9379
> 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
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
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.

Similar topics

11 posts views Thread by Peter | last post: by
17 posts views Thread by Arnold | last post: by
4 posts views Thread by Phillip Ian | last post: by
6 posts views Thread by sd2004 | last post: by
5 posts views Thread by news.microsoft.com | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.