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

How to determinate the size of buffer

P: n/a
ad
I want to write a MemoryStream to a buffer.
I use the codes below;
The code is from the msdn help,
They always declare the size of buffer as the (length of memorystream)+1;
The code run ok, but if I declare the size of buffer as the (length of
memorystream),
it run ok too.

How to determinate the size of buffer of memorystream?


//
Code---------------------------------------------------------------------------------------------------
DataSet ds = Will.DM.ExecuteDataset("Select * from customer");
MemoryStream ms1 = new MemoryStream();
ds.WriteXml(ms1);

DataSet ds2 = new DataSet();

int iLen = (int)ms1.Length;
byte[] buffer1 = new byte[iLen+1];
ms1.Position = 0;
ms1.Read(buffer1, 0, iLen);
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
You don't need to allocate a new array. All you have to do is call the
ToArray method on the MemoryStream and it will return an array that is a
copy of the contents in the stream (properly sized, I might add).

Your code will work without the + 1. I don't know where that's from.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:eN***************@TK2MSFTNGP14.phx.gbl...
I want to write a MemoryStream to a buffer.
I use the codes below;
The code is from the msdn help,
They always declare the size of buffer as the (length of memorystream)+1;
The code run ok, but if I declare the size of buffer as the (length of
memorystream),
it run ok too.

How to determinate the size of buffer of memorystream?


//
Code---------------------------------------------------------------------------------------------------
DataSet ds = Will.DM.ExecuteDataset("Select * from customer");
MemoryStream ms1 = new MemoryStream();
ds.WriteXml(ms1);

DataSet ds2 = new DataSet();

int iLen = (int)ms1.Length;
byte[] buffer1 = new byte[iLen+1];
ms1.Position = 0;
ms1.Read(buffer1, 0, iLen);

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.