Deserialize problem

P

Pavel Pavel

I have 2 solution to deserialize ArrayList which contains objects
I am using for this MemoryStream, but MemoryStream is not functional - I
receive always error
#End of Stream encountered before parsing was completed.#

The second solution is functionall - I am using in this solution
FileStream - everything works fine !!
I want to use MemoryStream instead of FileStrem
Can somebody tells me where I have problem ?
I am using the same source: byte array Dim bytes() As Byte
Thanks


#################### First solution with ERROR ############

Dim bytes() As Byte = row("PANEL_DATA") 'DataRow, column PANEL_DATA

Dim bFormatter As New BinaryFormatter()
Dim ms As MemoryStream
Dim b As Byte
ms = New MemoryStream()
ms.SetLength(bytes.Length)
Dim i As Int32
For i = 0 To bytes.Length - 1
ms.WriteByte(bytes(i))
Next

Try
########### in this line is error ###############
Me.objList = bFormatter.Deserialize(ms)
Catch ex As Exception
ms.Close()
gShowError(ex, "LoadControlsSer - deserialize")
End Try
ms.Close()

#################### Second solution - working solution ############

Dim ms As New MemoryStream()
Dim bFormatter As New BinaryFormatter()
ms.Write(bytes, 0, bytes.Length)
Dim stream11 As Stream = New FileStream("C:\MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None)
stream11.Write(bytes, 0, bytes.Length)
stream11.Close()
Try
Dim formatter As BinaryFormatter = New BinaryFormatter()
Dim stream22 As Stream = New FileStream("C:\MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read)
Me.objList = formatter.Deserialize(stream22)
stream22.Close()
Catch ex As Exception
gShowError(ex, "LoadControlsSer")
Return False
End Try

Return True


Tomas Oplt

Tel: 603 171893
 
T

Tom Shelton

I have 2 solution to deserialize ArrayList which contains objects
I am using for this MemoryStream, but MemoryStream is not functional - I
receive always error
#End of Stream encountered before parsing was completed.#

The second solution is functionall - I am using in this solution
FileStream - everything works fine !!
I want to use MemoryStream instead of FileStrem
Can somebody tells me where I have problem ?
I am using the same source: byte array Dim bytes() As Byte
Thanks


#################### First solution with ERROR ############

Dim bytes() As Byte = row("PANEL_DATA") 'DataRow, column PANEL_DATA

Dim bFormatter As New BinaryFormatter()
Dim ms As MemoryStream
Dim b As Byte
ms = New MemoryStream()
ms.SetLength(bytes.Length)
Dim i As Int32
For i = 0 To bytes.Length - 1
ms.WriteByte(bytes(i))
Next

You could do away with the above... You can create a memory stream from
the byte array directly :)

Dim bytes() As Byte = row("PANEL_DATA")
Dim bFormater As New BinaryFormatter()
Dim ms As New MemoryStream(ms)

Try
Me.objList = bFormatter.Deserialize(ms)
Catch ex As Exception
gShowError(ex, "LoadControlsSer - deserialize")
Finally
ms.Close()
End Try


That should work :) In your old solution, though - you need to add this
line right after the loop.

ms.Seek(0, SeekOrigin.Begin)

ms.WriteByte was moving the current pointer down the stream, so when you
call deserialize - the pointer is at the end of the stream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top