Serialize Buffer Length

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Robinson

    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();

  • Andrew Robinson

    #2
    Re: Serialize Buffer Length

    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" <nemoby@nospam. nospamwrote in message
    news:%23cwydZZb HHA.4476@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();

    Comment

    • Andrew Robinson

      #3
      Re: Serialize Buffer Length

      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" <nemoby@nospam. nospamwrote in message
      news:uakqMeZbHH A.4716@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" <nemoby@nospam. nospamwrote in message
      news:%23cwydZZb HHA.4476@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();
      >

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Serialize Buffer Length

        Andrew Robinson <nemoby@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 - <skeet@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

        Comment

        Working...