473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 9482
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
17
by: Arnold | last post by:
Is using fseek and ftell a reliable method of getting the file size on a binary file? I thought I remember reading somewhere it wasn't... If not what would be the "right" and portable method to...
2
by: Matt | last post by:
I just wanted to know if I am converting to/from streams the easiest and correct way. I am performing the following statements to convert byte arrays to and from streams during different...
4
by: Phillip Ian | last post by:
Version: VS 2005 I took the sample code from help about encrypting and decrypting strings, and changed it to work directly with byte arrays and get the key and IV values from functions I've...
2
by: GRB | last post by:
A client wants me to decrypt a cookie. They encrypted the data in java using TripleDES. The key provided is a 168 bit 44 character key, DESede alogorithm. Ive tried to decrypt in vb.net and get the...
6
by: sd2004 | last post by:
Hi, I am new to C++. I am reading data from file into array. The problem is , I do not know ahead how many lines the file will be. I am arbitrary assign array size with "int MAX" which is not...
5
by: news.microsoft.com | last post by:
Hello, what is the most performant size for the byte array for reading/writing using 2 filestreams? example code: Dim bytearrayinput(4095) As Byte Dim rdlen As Long = 0 Dim totlen As Long...
2
by: shahiz | last post by:
basically im having null pointer exception //read an inputstream is = new DataInputStream(new FileInputStream("test.mpg")); loadBytes(is); //pass it as a datasource for the player public...
1
by: quddusaliquddus | last post by:
Hi :D, I am sending data to server via TCP IP Connection. I am using a continuous loop at the server end - that accepts new clients and while streams can be read, it reads data stream. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.