1 #Region "Microsoft.VisualBasic::bebb80e2ece0208f157f4ef527645194, Microsoft.VisualBasic.Core\Serialization\BinaryDumping\StructFormatter.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module StructFormatter
35     
36     '         Function: DeSerialize, GetSerializeBuffer, Load, Serialize
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.IO
44 Imports System.Runtime.CompilerServices
45 Imports System.Runtime.Serialization
46 Imports System.Runtime.Serialization.Formatters.Binary
47
48 Namespace Serialization.BinaryDumping
49
50     Public Module StructFormatter
51
52         ''' <summary>
53         ''' Save a structure type object into a binary file.(使用二进制序列化保存一个对象)
54         ''' </summary>
55         ''' <typeparam name="T"></typeparam>
56         ''' <param name="obj"></param>
57         ''' <param name="path"></param>
58         ''' <returns></returns>
59         ''' <remarks></remarks>
60         <Extension> Public Function Serialize(Of T)(obj As T, path As StringAs Boolean
61             Dim buffer As Byte() = obj.GetSerializeBuffer
62             Return buffer.FlushStream(path)
63         End Function
64
65         <Extension> Public Function GetSerializeBuffer(Of T)(obj As T) As Byte()
66             Dim IFormatter As IFormatter = New BinaryFormatter()
67             Dim Stream As New IO.MemoryStream()
68             Call IFormatter.Serialize(Stream, obj)
69             Dim buffer As Byte() = Stream.ToArray
70             Return buffer
71         End Function
72
73         <Extension> Public Function DeSerialize(Of T)(bytes As Byte()) As T
74             Dim obj As Object = (New BinaryFormatter).[Deserialize](New MemoryStream(bytes))
75             Return DirectCast(obj, T)
76         End Function
77
78         ''' <summary>
79         ''' Load a strucutre object from the file system by using binary serializer deserialize.
80         ''' (使用反二进制序列化从指定的文件之中加载一个对象)
81         ''' </summary>
82         ''' <typeparam name="T"></typeparam>
83         ''' <param name="path"></param>
84         ''' <returns></returns>
85         ''' <remarks></remarks>
86         <Extension> Public Function Load(Of T)(path As StringAs T
87             If Not FileIO.FileSystem.FileExists(path) Then
88                 Return Activator.CreateInstance(Of T)()
89             End If
90             Using Stream As Stream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
91                 Dim IFormatter As IFormatter = New BinaryFormatter()
92                 Dim obj As T = DirectCast(IFormatter.Deserialize(Stream), T)
93                 Return obj
94             End Using
95         End Function
96     End Module
97 End Namespace