Imports System.Xml.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Xml.Formatting
Imports System.IO

Module modSerialization

    'serializes profiles to disk in binary format
    Public Sub serializeClass(ByVal arrayListToSerialize As ArrayList, ByVal fileName As String)
        Dim serializer As New BinaryFormatter()
        Dim filestr As FileStream

        If IO.File.Exists(fileName) Then
            Dim arrayToSave As ArrayList = deSerializeClass(fileName)

            For a = 0 To arrayListToSerialize.Count - 1
                arrayToSave.Add(arrayListToSerialize(a))
            Next
            filestr = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None)
            serializer.Serialize(filestr, arrayToSave)
            filestr.Close()
        Else
            filestr = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None)
            serializer.Serialize(filestr, arrayListToSerialize)
            filestr.Close()
        End If


    End Sub

    'deserializes data from binary format to an arraylist
    Public Function deSerializeClass(ByVal fileName As String) As ArrayList
        Dim deSerializer As New BinaryFormatter()

        Dim filestr As New FileStream(fileName, FileMode.Open, FileAccess.Read)
        Dim arrayList1 As ArrayList

        arrayList1 = CType(deSerializer.Deserialize(filestr), ArrayList)

        filestr.Close()

        Return arrayList1
    End Function


End Module
