Results 1 to 19 of 19

Thread: [2005] Serializing into a memorystream

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    [2005] Serializing into a memorystream

    I've got a 2 dimensional string array that I'm trying to serialize into a memorystream, and then read the memorystream using a binaryreader to get a bytearray.
    The problem is that my byte array always ends up with nothing but 0's in it.
    If I however, serialize to a filestream, it works alright.

    Is there something obvious I've missed in my code?
    (TileInfo is the 2-dimensional string array)
    VB Code:
    1. Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    2.         Dim memStream As New IO.MemoryStream
    3.         formatter.Serialize(memStream, TileInfo)
    4.         Using bReader As New IO.BinaryReader(memStream)
    5.             Dim byteArray(CInt(bReader.BaseStream.Length)) As Byte
    6.             bReader.Read(byteArray, 0, CInt(bReader.BaseStream.Length))
    7.             BytesToSend.AddRange(byteArray)
    8.         End Using
    9.         memStream.Close()

    Thanks.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Serializing into a memorystream

    It seems to me that you gave me this code in a different thread (though the object being serialized was somewhat different). I did get it working, but I think I had to add one line. Unfortunately, I can't look that up here. My vague memory on the subject was that the stream.Position has to be set back to 0 before you use the reader, but I wouldn't swear to that.

    I'll look it up at lunch time.
    My usual boring signature: Nothing

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Serializing into a memorystream

    vb Code:
    1. bf.Serialize(mStream, echoMess)
    2. ReDim bOut(CInt(bReader.BaseStream.Length)            bReader.BaseStream.Position = 0
    3. bReader.Read(bOut, 2, bReader.BaseStream.Length)

    That's the code I was using, though it has a little extra bit in there where I re-szie my byte array. If the baseStream.Position is not 0 at the start of the read, you will start reading from the position indicated. After serializing, though I haven't checked this, it appears that the position is the end of the stream, not the beginning.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Serializing into a memorystream

    Hey SH. I was hoping you'd see this thread and give me some help Good stuff I have no idea it had to be reset to read from the beginning of the stream. I'll try that right away.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Serializing into a memorystream

    Let me know. To tell you the truth, that is absolutely necessary in one place, and may not be necessary in another. When I was looking that up, I couldn't remember whether this was the situation where it was necessary, or the situation where I wasn't sure. Funny how that works. I had this problem, and solved it, but I can't remember whether or not this was the key item.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Serializing into a memorystream

    It seems to work
    However, I have put a breakpoint just so i can see what my array of bytes contain. The length of the array is around 1700-ish, but all of them arent proper bytes values...most of them comes out as questionmarks (see picture), very odd, never seen that before. I will deserialize these bytes back into a 2 dimensional string array later, I hope the "questionmarks" doesnt cause a problem...

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Serializing into a memorystream

    I'd have to say that I have NEVER seen that in any tooltip for any array of any type.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Serializing into a memorystream

    Its as if it countinues to read from the stream even though there is no more to read.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Serializing into a memorystream

    Well, I guess the key question is whether or not you can get the data back out correctly, or whether it has been corrupted by this. It could just be something goofy that the IDE is doing.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Serializing into a memorystream

    And also, the bytearray seems to be of very different sizes each time. Sometimes its 30, sometimes its 130 etc etc, even though its always a 2 dimensional string array being serialized. The length of each string in the array can vary a little, but not much at all..hmmm
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Serializing into a memorystream

    Quote Originally Posted by Shaggy Hiker
    Well, I guess the key question is whether or not you can get the data back out correctly, or whether it has been corrupted by this. It could just be something goofy that the IDE is doing.
    Yeah my thoughts exactly.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  12. #12
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Serializing into a memorystream

    Hi athiest, I had a problem the other day with serializing into a mem stream and solved it but cant remember how, I have the code and will upload it later, but there are a few hitches...
    I was trying to serialize a test class so I could send it using UDP and then the client could deserialize it and use it declared as the Itest interface I made. I cant garuntee it works but I think I got it into the memory stream with its data intact, but failed to deserialize on the client and then gave up, I steped through every line and I think I got the serializeing into the mem stream working after a few hacks, but I never deserialized succsefuly so there the garuntee falls short, I will have another look and upload it later...

    Signatures suck

  13. #13

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Serializing into a memorystream

    Quote Originally Posted by cptHotkeys
    Hi athiest, I had a problem the other day with serializing into a mem stream and solved it but cant remember how, I have the code and will upload it later, but there are a few hitches...
    I was trying to serialize a test class so I could send it using UDP and then the client could deserialize it and use it declared as the Itest interface I made. I cant garuntee it works but I think I got it into the memory stream with its data intact, but failed to deserialize on the client and then gave up, I steped through every line and I think I got the serializeing into the mem stream working after a few hacks, but I never deserialized succsefuly so there the garuntee falls short, I will have another look and upload it later...
    Thank you I'd appreciate that.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Serializing into a memorystream

    Quote Originally Posted by cptHotkeys
    Hi athiest, I had a problem the other day with serializing into a mem stream and solved it but cant remember how, I have the code and will upload it later, but there are a few hitches...
    I was trying to serialize a test class so I could send it using UDP and then the client could deserialize it and use it declared as the Itest interface I made. I cant garuntee it works but I think I got it into the memory stream with its data intact, but failed to deserialize on the client and then gave up, I steped through every line and I think I got the serializeing into the mem stream working after a few hacks, but I never deserialized succsefuly so there the garuntee falls short, I will have another look and upload it later...
    What problem did you have deserializing. I got that very scenario working last weekend.
    My usual boring signature: Nothing

  15. #15
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Serializing into a memorystream

    Here it is Athiest, 2D array serialization and deserialization using a memory stream, works perfectly....
    2 subs, form load makes the 2D sample array (string), button1 click, serializes and deserializes into and from mem stream and then addes the data to listbox...
    Attached Files Attached Files

    Signatures suck

  16. #16
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Serializing into a memorystream

    Quote Originally Posted by Shaggy Hiker
    What problem did you have deserializing. I got that very scenario working last weekend.
    Well actually im not sure, I will have another look, if I put a bit of thaught into it I should be fine, I was just hacking and sending it as a string converted to bytes (it worked with code I already had in the app), and when deserializing the received bytes I got error (not valid binary format or something) next itme I wont be lazy and will create a different sub to send bytes rather than ASCII bytes, which I assume is the errors cause, thanks I will ask if thats not the problem...

    Signatures suck

  17. #17

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Serializing into a memorystream

    VB Code:
    1. bytes = MemStream.GetBuffer
    I didnt know you could get the byte array from the GetBuffer property of the memorystream. No need for the binaryreader then. Good info.
    Though I did already get the serialization working before you posted you're example cptHotkeys, I still really appreciate this piece of code
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [2005] Serializing into a memorystream

    Quote Originally Posted by cptHotkeys
    Well actually im not sure, I will have another look, if I put a bit of thaught into it I should be fine, I was just hacking and sending it as a string converted to bytes (it worked with code I already had in the app), and when deserializing the received bytes I got error (not valid binary format or something) next itme I wont be lazy and will create a different sub to send bytes rather than ASCII bytes, which I assume is the errors cause, thanks I will ask if thats not the problem...
    Ah, I thought you might be serializing a custom object, then deserializing it. If it is just a string, then it is a different issue.
    My usual boring signature: Nothing

  19. #19
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] Serializing into a memorystream

    No, I am serializing a custom object and sorry for the poor explanation. the send method was in a dll, and it only sent ascii, so I had to convert the serialized objects bytes into a string to send them, I only done that as a very quick hack, and that is what caused my problem I assume...

    Signatures suck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width