471,118 Members | 1,126 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,118 software developers and data experts.

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(memoryStream, myObject);
byte[] b = memoryStream.GetBuffer();

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

MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, myObject);
b = new byte[memoryStream.Length];
Array.Copy(memoryStream.GetBuffer(), b, memoryStream.Length);

thanks,
"Andrew Robinson" <ne****@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP03.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();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, myObject);
byte[] b = memoryStream.GetBuffer();
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(memoryStream, myObject);
b = memoryStream.ToArray();
memoryStream.Close();
}

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

MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, myObject);
b = new byte[memoryStream.Length];
Array.Copy(memoryStream.GetBuffer(), b, memoryStream.Length);

thanks,
"Andrew Robinson" <ne****@nospam.nospamwrote in message
news:%2****************@TK2MSFTNGP03.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();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, myObject);
byte[] b = memoryStream.GetBuffer();
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(memoryStream, myObject);
b = memoryStream.ToArray();
memoryStream.Close();
}
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.com>
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by ad | last post: by
reply views Thread by Matthias Straka | last post: by
3 posts views Thread by Kenneth H. Young | last post: by
43 posts views Thread by Frodo Baggins | last post: by
16 posts views Thread by Matthew Geyer | last post: by

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.