microsoft-visualbasic-runtime/Net/Protocol/Streams/ArrayBase.vb

108 lines
3.8 KiB
VB.net
Raw Normal View History

2019-07-29 02:05:05 +08:00
#Region "Microsoft.VisualBasic::b7ceedf73869affc6ffd0357bff5e945, Microsoft.VisualBasic.Core\Net\Protocol\Streams\ArrayBase.vb"
2018-10-06 01:51:08 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
' Class ValueArray
'
' Constructor: (+1 Overloads) Sub New
' Function: ToString
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
Imports Microsoft.VisualBasic.Language
Imports Microsoft.VisualBasic.Serialization.BinaryDumping
Imports Microsoft.VisualBasic.Serialization.JSON
Imports Buffer = System.Array
2018-08-02 20:14:48 +08:00
Namespace Net.Protocols.Streams.Array
''' <summary>
''' 对于<see cref="System.Int64"/>, <see cref="System.int32"/>, <see cref="System.Double"/>, <see cref="System.DateTime"/>
''' 这些类型的数据来说进行网络传输的时候使用json会被转换为字符串数据量比较大而转换为字节再进行传输数据流量的消耗会比较小
''' </summary>
''' <typeparam name="T"></typeparam>
''' <remarks>这个是定长的数组序列</remarks>
Public MustInherit Class ValueArray(Of T) : Inherits ArrayAbstract(Of T)
Protected ReadOnly _bufWidth As Integer
Protected Sub New(serialization As IGetBuffer(Of T),
deserialization As IGetObject(Of T),
2018-08-02 20:14:48 +08:00
bufWidth As Integer,
rawStream As Byte())
Call MyBase.New(serialization, deserialization)
_bufWidth = bufWidth
If Not rawStream.IsNullOrEmpty Then
Dim valueList As New List(Of T)
2019-03-06 20:20:17 +08:00
Dim p As VBInteger = 0
2018-08-02 20:14:48 +08:00
Dim byts As Byte() = New Byte(_bufWidth - 1) {}
Do While p < rawStream.Length - 1
Call Buffer.ConstrainedCopy(rawStream, p + bufWidth, byts, Scan0, bufWidth)
Call valueList.Add(MyBase.deserialization(byts))
2018-08-02 20:14:48 +08:00
Loop
Values = valueList.ToArray
End If
End Sub
Public NotOverridable Overrides Function Serialize() As Byte()
Dim bufferArray As Byte() = New Byte(Values.Length * _bufWidth - 1) {}
2019-03-06 20:20:17 +08:00
Dim p As VBInteger = 0
2018-08-02 20:14:48 +08:00
For Each value As T In Values
Dim byts As Byte() = serialization(value)
Call Buffer.ConstrainedCopy(byts, Scan0, bufferArray, p + _bufWidth, _bufWidth)
2018-08-02 20:14:48 +08:00
Next
Return bufferArray
2018-08-02 20:14:48 +08:00
End Function
Public Overrides Function ToString() As String
If Values.IsNullOrEmpty Then
Return GetType(T).FullName
Else
Dim valJson$ = Values _
.Select(Function(val) Scripting.ToString(val)) _
.GetJson
Return $"{GetType(T).FullName} {valJson}"
2018-08-02 20:14:48 +08:00
End If
End Function
End Class
End Namespace