1 #Region "Microsoft.VisualBasic::326a34842d49ce1fdf7ddebf7dcd8fbe, Microsoft.VisualBasic.Core\Net\Protocol\Streams\ArrayBase.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     '     Class ValueArray
35     
36     '         Constructor: (+1 OverloadsSub New
37     '         FunctionToString
38     
39     
40     ' /********************************************************************************/
41
42 #End Region
43
44 Imports Microsoft.VisualBasic.Language
45 Imports Microsoft.VisualBasic.Serialization.BinaryDumping
46 Imports Microsoft.VisualBasic.Serialization.JSON
47 Imports Buffer = System.Array
48
49 Namespace Net.Protocols.Streams.Array
50
51     ''' <summary>
52     ''' 对于<see cref="System.Int64"/>, <see cref="System.int32"/>, <see cref="System.Double"/>, <see cref="System.DateTime"/>
53     ''' 这些类型的数据来说,进行网络传输的时候使用json会被转换为字符串,数据量比较大,而转换为字节再进行传输,数据流量的消耗会比较小
54     ''' </summary>
55     ''' <typeparam name="T"></typeparam>
56     ''' <remarks>这个是定长的数组序列</remarks>
57     Public MustInherit Class ValueArray(Of T) : Inherits ArrayAbstract(Of T)
58
59         Protected ReadOnly _bufWidth As Integer
60
61         Protected Sub New(serialization As IGetBuffer(Of T),
62                           deserialization As IGetObject(Of T),
63                           bufWidth As Integer,
64                           rawStream As Byte())
65
66             Call MyBase.New(serialization, deserialization)
67
68             _bufWidth = bufWidth
69
70             If Not rawStream.IsNullOrEmpty Then
71                 Dim valueList As New List(Of T)
72                 Dim p As int = 0
73                 Dim byts As Byte() = New Byte(_bufWidth - 1) {}
74
75                 Do While p < rawStream.Length - 1
76                     Call Buffer.ConstrainedCopy(rawStream, p + bufWidth, byts, Scan0, bufWidth)
77                     Call valueList.Add(MyBase.deserialization(byts))
78                 Loop
79
80                 Values = valueList.ToArray
81             End If
82         End Sub
83
84         Public NotOverridable Overrides Function Serialize() As Byte()
85             Dim bufferArray As Byte() = New Byte(Values.Length * _bufWidth - 1) {}
86             Dim p As int = 0
87
88             For Each value As T In Values
89                 Dim byts As Byte() = serialization(value)
90                 Call Buffer.ConstrainedCopy(byts, Scan0, bufferArray, p + _bufWidth, _bufWidth)
91             Next
92
93             Return bufferArray
94         End Function
95
96         Public Overrides Function ToString() As String
97             If Values.IsNullOrEmpty Then
98                 Return GetType(T).FullName
99             Else
100                 Dim valJson$ = Values _
101                     .Select(Function(val) Scripting.ToString(val)) _
102                     .GetJson
103                 Return $"{GetType(T).FullName} {valJson}"
104             End If
105         End Function
106     End Class
107 End Namespace