473,802 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialize Buffer Length

I am using the folllowing code to serialize a [serializable] object to a
byte array (byte[]). It works fine, but my resulting array is size is being
rounded up to the next largest 32768 boundary. How can I complete the
serialization without incurring the extra bytes.

I know there is a simple answer....
Thanks,
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(memo ryStream, myObject);
byte[] b = memoryStream.Ge tBuffer();

Mar 23 '07 #1
3 3336
Ok, there must be a better way than this:

MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(memo ryStream, myObject);
b = new byte[memoryStream.Le ngth];
Array.Copy(memo ryStream.GetBuf fer(), b, memoryStream.Le ngth);

thanks,
"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I am using the folllowing code to serialize a [serializable] object to a
byte array (byte[]). It works fine, but my resulting array is size is being
rounded up to the next largest 32768 boundary. How can I complete the
serializatio n without incurring the extra bytes.

I know there is a simple answer....
Thanks,
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(memo ryStream, myObject);
byte[] b = memoryStream.Ge tBuffer();
Mar 23 '07 #2
Ok, I think I have answered my own question here:
using (MemoryStream memoryStream = new MemoryStream()) {
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(memo ryStream, myObject);
b = memoryStream.To Array();
memoryStream.Cl ose();
}

"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:ua******** ******@TK2MSFTN GP02.phx.gbl...
Ok, there must be a better way than this:

MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(memo ryStream, myObject);
b = new byte[memoryStream.Le ngth];
Array.Copy(memo ryStream.GetBuf fer(), b, memoryStream.Le ngth);

thanks,
"Andrew Robinson" <ne****@nospam. nospamwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>>I am using the folllowing code to serialize a [serializable] object to a
byte array (byte[]). It works fine, but my resulting array is size is
being rounded up to the next largest 32768 boundary. How can I complete
the serialization without incurring the extra bytes.

I know there is a simple answer....
Thanks,
MemoryStream memoryStream = new MemoryStream();
BinaryFormatte r binaryFormatter = new BinaryFormatter ();
binaryFormatte r.Serialize(mem oryStream, myObject);
byte[] b = memoryStream.Ge tBuffer();
Mar 23 '07 #3
Andrew Robinson <ne****@nospam. nospamwrote:
Ok, I think I have answered my own question here:
using (MemoryStream memoryStream = new MemoryStream()) {
BinaryFormatter binaryFormatter = new BinaryFormatter ();
binaryFormatter .Serialize(memo ryStream, myObject);
b = memoryStream.To Array();
memoryStream.Cl ose();
}
Exactly. Note that you don't need to call Close() - you're using a
"using" statement which will call Dispose on your MemoryStream already.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 24 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2542
by: ad | last post by:
I new a buffer for read file: How to detriminate the BolockSize when new the buffer? ----------------------------------------------------------------------------------------------------------------------------------------- int BlockSize=???? System.IO.FileStream fsRead = new System.IO.FileStream("c:\Backup.bck", System.IO.FileMode.Open, System.IO.FileAccess.Read); byte buffer = new byte; System.Int32 size = fsRead.Read(buffer, 0,...
0
1186
by: Matthias Straka | last post by:
Hi! I'm working on a website with an IHttpHandler that listens to changes ina database and sends some text to the client. The client is always connected to the handler, this way I can use a push method. Every time a text is to be sent I flush the buffer. The problem is, that there seems to be a minimum size the buffer has to have before asp.net really flushes the buffer and sends the data. As a workaround I insert a few 100...
1
1062
by: JC | last post by:
HI I want to use a byte array to store all the content of a file. Is there anyway allow me to do so? Thx JC
3
1468
by: Kenneth H. Young | last post by:
I am getting over lapping data while sending and receiving data in a tcp listener I am working on. How does one reset or clear the buffer or bytes so they don't carry over to the next send or receive? Thanks in advance! Partial Example: ' Client sends: TempS = curAdd.ToString & "....................................................................................................." ' Padding the end of the IP address so I can...
22
4046
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to receive 5 bytes buffer , I call the BeginReceive and then wait on AsyncWaitHandle.WaitOne() but it is signald imidiatly , and the next call to EndReceive return zero bytes length , also the buffer is empty. here is the code: public static byte...
43
4949
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. That is, replace calls to strcpy with say, my_strcpy(dest,src) which will internally find the destination buffer length. For this we need to know the destination buffer size. For statically allocated strings sizeof is returning the length of the...
1
5989
by: kierenj | last post by:
Hi, I'm trying to send a zipfile to a client using Content-Disposition: attachment. Done it before and it works fine. My code is below: context.Response.Buffer = false; System.IO.MemoryStream stream = ArchiveUtil.CreateArchive(new string { "c:\\windows\
16
8713
by: Matthew Geyer | last post by:
been using asynchronous sockets with AsyncCallback and System.Net.Sockets as per the excellent MSDN example. everything has been working great, except when i want to receive more total bytes than my buffer can hold. i open a connection with an 8k buffer on the socket. if the server sends me 5k and closes connection it works fine. if the server sends me 10k, my BeginReceive callback (which does loop) gives me 8k of the data, after...
4
11242
by: danomano | last post by:
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that. So my question in general is how do I know what length to set the buffer? In specific, my question is what causes the 'completed' event in the OnSocketConnectCompleted method to fire? Does it fire at end of a packet or does happen at some end of data marker? public SocketClient(Page pg, Int32...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10536
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10304
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
10285
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
9114
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...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
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.