1 #Region "Microsoft.VisualBasic::910ee6437347dc3d1bb75b508f99c772, Microsoft.VisualBasic.Core\Net\Protocol\Streams\String.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 [String]
35     
36     '         Properties: Encoding, value
37     
38     '         Constructor: (+4 OverloadsSub New
39     '         Function: Serialize, ToString
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Text
47 Imports System.Xml.Serialization
48 Imports Microsoft.VisualBasic.Text
49 Imports BufferArray = System.Array
50
51 Namespace Net.Protocols.Streams
52
53     ''' <summary>
54     ''' 字符串序列流
55     ''' </summary>
56     Public Class [String] : Inherits RawStream
57
58         <XmlAttribute> Public Property value As String
59
60         <XmlAttribute> Public Property Encoding As Encodings
61             Get
62                 Return TextEncodings.GetEncodings(_encoding)
63             End Get
64             Set(value As Encodings)
65                 _encoding = value.CodePage
66             End Set
67         End Property
68
69         Dim _encoding As Encoding
70
71         Sub New()
72         End Sub
73
74         Sub New(s As StringOptional encoding As Encodings = Encodings.UTF8)
75             Call Me.New(s, encoding.CodePage)
76         End Sub
77
78         Sub New(s As StringOptional encoding As Encoding = Nothing)
79             _value = s
80             _encoding = encoding
81
82             If _encoding Is Nothing Then
83                 _encoding = Encoding.UTF8
84             End If
85         End Sub
86
87         Sub New(raw As Byte())
88             Dim encoding As Byte = raw(Scan0)
89             Dim s As Byte() = raw.Skip(1).ToArray
90
91             _encoding = CType(encoding, Encodings).CodePage
92             _value = _encoding.GetString(s)
93         End Sub
94
95         Public Overrides Function ToString() As String
96             Return value
97         End Function
98
99         Public Overrides Function Serialize() As Byte()
100             Dim s As Byte() = _encoding.GetBytes(value)
101             Dim buffer As Byte() = New Byte(s.Length) {}
102
103             buffer(Scan0) = CType(Encoding, Byte)
104             Call BufferArray.ConstrainedCopy(s, Scan0, buffer, 1, s.Length)
105
106             Return buffer
107         End Function
108     End Class
109 End Namespace