1 |
#Region "Microsoft.VisualBasic::78fec5ccb84cf89ccc5daed805339cae, Microsoft.VisualBasic.Core\Serialization\BinaryDumping\Buffer.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
#End Region
|
58 |
|
59 |
Imports Microsoft.VisualBasic.Language
|
60 |
Imports Microsoft.VisualBasic.Linq
|
61 |
Imports Microsoft.VisualBasic.Net.Protocols
|
62 |
|
63 |
Namespace Serialization.BinaryDumping
|
64 |
|
65 |
Public Structure Buffer : Implements ISerializable
|
66 |
|
67 |
Dim Length As Long
|
68 |
Dim buffer As Byte()
|
69 |
|
70 |
Sub New(buf As Byte())
|
71 |
Length = buf.Length
|
72 |
buffer = buf
|
73 |
End Sub
|
74 |
|
75 |
Public ReadOnly Property TotalBytes As Long
|
76 |
Get
|
77 |
Return Length + RawStream.INT64
|
78 |
End Get
|
79 |
End Property
|
80 |
|
81 |
Public Overrides Function ToString() As String
|
82 |
Return $"{Length} bytes..."
|
83 |
End Function
|
84 |
|
85 |
Public Function Serialize() As Byte() Implements ISerializable.Serialize
|
86 |
Dim buffer As Byte() = New Byte(TotalBytes - 1) {}
|
87 |
Call Array.ConstrainedCopy(BitConverter.GetBytes(Length), Scan0, buffer, Scan0, RawStream.INT64)
|
88 |
Call Array.ConstrainedCopy(Me.buffer, Scan0, buffer, RawStream.INT64, Me.buffer.Length)
|
89 |
Return buffer
|
90 |
End Function
|
91 |
End Structure
|
92 |
|
93 |
Public Delegate Function IGetBuffer(Of T)(x As T) As Byte()
|
94 |
Public Delegate Function IGetObject(Of T)(buf As Byte()) As T
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
Public Module BufferAPI
|
100 |
|
101 |
Public Function CreateBuffer(Of T)(source As IEnumerable(Of T), getBuf As IGetBuffer(Of T)) As Byte()
|
102 |
Dim array As Buffer() = source.Select(Function(x) New Buffer(getBuf(x))).ToArray
|
103 |
Dim buffer As Byte() = New Byte(array.Sum(Function(x) x.TotalBytes) - 1L) {}
|
104 |
Dim i As Integer
|
105 |
|
106 |
For Each x As Buffer In array
|
107 |
Call System.Array.ConstrainedCopy(x.Serialize, Scan0, buffer, i, x.TotalBytes)
|
108 |
i += x.TotalBytes
|
109 |
Next
|
110 |
|
111 |
Return buffer
|
112 |
End Function
|
113 |
|
114 |
Public Iterator Function GetBuffer(Of T)(raw As Byte(), getObj As IGetObject(Of T)) As IEnumerable(Of T)
|
115 |
Dim length As Byte() = New Byte(RawStream.INT64 - 1) {}
|
116 |
Dim l As Long
|
117 |
Dim i As int = 0
|
118 |
Dim temp As Byte()
|
119 |
Dim x As T
|
120 |
|
121 |
Do While True
|
122 |
Call Array.ConstrainedCopy(raw, i + RawStream.INT64, length, Scan0, RawStream.INT64)
|
123 |
l = BitConverter.ToInt64(length, Scan0)
|
124 |
temp = New Byte(l - 1) {}
|
125 |
Call Array.ConstrainedCopy(raw, i + l, temp, Scan0, l)
|
126 |
x = getObj(temp)
|
127 |
Yield x
|
128 |
|
129 |
If i >= raw.Length - 1 Then
|
130 |
Exit Do
|
131 |
End If
|
132 |
Loop
|
133 |
End Function
|
134 |
End Module
|
135 |
End Namespace
|