473,698 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Le ngth);
2)
while (true)
{
int numRead = inStream.Read(b uf, 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);
DeflaterOutputS tream outStream = new DeflaterOutputS tream(ms,
deflater);
byte[] buf = m_Encoder.GetBy tes(str);
outStream.Write (buf, 0, buf.Length);
outStream.Flush ();
sReturn = MemoryStreamToS tring(outStream );
outStream.Finis h();
}
catch(Exception ex)
{
Console.WriteLi ne("ERROR:" + ex.Message);
}
return sReturn;
}

public string Inflate(string strToUncompress )
{
string sReturn;
try
{
InflaterInputSt ream inStream = new
InflaterInputSt ream(StringToMe moryStream(strT oUncompress));
byte[] buf2 = new byte[/*How big?*/];
// not sure how big to make this array
inStream.Read(b uf2,pos,buf2.Le ngth);
// or
while (true)
{
int numRead = inStream.Read(b uf2, pos, 4096);
if (numRead <= 0)
{
break;
}
pos += numRead;
}

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

private MemoryStream StringToMemoryS tream(String str)
{
return new MemoryStream(St ringToByteArray (str));
}

private String MemoryStreamToS tring(MemoryStr eam memStream)
{
return ByteArrayToStri ng(memStream.Ge tBuffer());
}

private Byte[] StringToByteArr ay(string str)
{
return m_Encoder.GetBy tes(str);
}

private string ByteArrayToStri ng(byte[] byteArray)
{
int NumberOfBytes = byteArray.Lengt h;
return m_Encoder.GetSt ring(byteArray, 0 , NumberOfBytes);
}
Nov 15 '05 #1
3 9508
> 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********@CSI LasVegas.com> wrote in message news:<#o******* ******@TK2MSFTN GP11.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*********@ho tmail.com> wrote in message
news:a0******** *************** **@posting.goog le.com...
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********@CSI LasVegas.com> wrote in message

news:<#o******* ******@TK2MSFTN GP11.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
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
17
15988
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 obtain it? Thanks.
2
7366
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 processing in my application: Here is a sample where I convert from a stream to a byte array: strm = mqMsg.BodyStream; BinaryReader br = new BinaryReader(strm); byteArray= br.ReadBytes(Convert.ToInt32(strm.Length));
4
2507
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 written to privide them. No other changes were made... Friend Function EncryptBytes(ByVal Data() As Byte, ByVal APassphrase As String) As Byte() Try
2
30524
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 above error. Below is the class I use and the code I use to call it. Not sure wher I'm going wrong here. The TripleDES service provider only uses a 192 bit 24 character length key. Any help would be greatly appreciated.
6
2119
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 good way of doing it, I think. could someone please sugguest/help me with better way. Thanks /////////////////////////////////////////////////////////////////////////// #include <iostream>
5
2582
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 = fsInput.Length
2
11577
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 void loadBytes(InputStream is){ DataSource ds=new DataSource(is); player = Manager.createPlayer(ds);
1
4147
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. The data is sent from the client with 2 leading bytes of data that represent the size of the packet of data sent and type of data. My question is: how do I retrieve the size of the data packet and then check that this amount of data has been...
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9029
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7732
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.