473,401 Members | 2,068 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,401 software developers and data experts.

How to determinate the size of buffer

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
1 2602
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Sharon | last post by:
I'm using TcpClient and getting the Stream by: TcpClient tcpclnt = new TcpClient(); . . . Stream stm = tcpclnt.GetStream(); Now, when I'm trying to send a big buffer via stm.Write(...) I get an...
11
by: Stephan Steiner | last post by:
Hi Generally, FileInfo fi = new FileInfo(path); long size = fi.Length; gets you the length of a file in bytes. However, when copying files, even while the copy operation is still in...
1
by: ad | last post by:
I new a buffer for read file: How to detriminate the BolockSize when new the buffer? ...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
2
by: Macca | last post by:
Hi, My application uses an asynchronous socket server. The question I have is what i should set my socket server buffer size to. I will know the size of each data packet sent across the...
1
by: ad | last post by:
I want to determinate if a value in a Text is digital or character. How can I determinate it with Javascript
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
4
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.