468,771 Members | 1,810 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

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 3170
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
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.