1 | #Region "Microsoft.VisualBasic::be68831f4257c8a9199e632e3a92165e, Microsoft.VisualBasic.Core\Text\IO\EncodingHelper.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 EncodingHelper |
35 | ' |
36 | ' Properties: TextEncoding |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: GetBytes, (+2 Overloads) ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.Text |
47 | |
48 | Namespace Text |
49 | |
50 | ''' <summary> |
51 | ''' Helper for <see cref="Encodings"/> |
52 | ''' </summary> |
53 | Public Class EncodingHelper |
54 | |
55 | ''' <summary> |
56 | ''' <see cref="Encoding"/> instance |
57 | ''' </summary> |
58 | ''' <returns></returns> |
59 | Public ReadOnly Property TextEncoding As Encoding |
60 | |
61 | ''' <summary> |
62 | ''' Helper for <see cref="Encodings"/> |
63 | ''' </summary> |
64 | ''' <param name="encoding"></param> |
65 | Sub New(encoding As Encodings) |
66 | TextEncoding = encoding.CodePage |
67 | End Sub |
68 | |
69 | ' ' Summary: |
70 | ' When overridden in a derived class, encodes all the characters in the specified |
71 | ' string into a sequence of bytes. |
72 | ' ' Parameters: |
73 | ' s: |
74 | ' The string containing the characters to encode. |
75 | ' ' Returns: |
76 | ' A byte array containing the results of encoding the specified set of characters. |
77 | ' ' Exceptions: |
78 | ' T:System.ArgumentNullException: |
79 | ' s is null. |
80 | ' ' T:System.Text.EncoderFallbackException: |
81 | ' A fallback occurred (see Character Encoding in the .NET Framework for complete |
82 | ' explanation)-and-System.Text.Encoding.EncoderFallback is set to System.Text.EncoderExceptionFallback. |
83 | |
84 | ''' <summary> |
85 | ''' When overridden in a derived class, encodes all the characters in the specified |
86 | ''' string into a sequence of bytes. |
87 | ''' </summary> |
88 | ''' <param name="s">The string containing the characters to encode.</param> |
89 | ''' <returns>A byte array containing the results of encoding the specified set of characters.</returns> |
90 | Public Function GetBytes(s As String) As Byte() |
91 | Return TextEncoding.GetBytes(s) |
92 | End Function |
93 | |
94 | ' ' Summary: |
95 | ' When overridden in a derived class, decodes all the bytes in the specified byte |
96 | ' array into a string. |
97 | ' ' Parameters: |
98 | ' bytes: |
99 | ' The byte array containing the sequence of bytes to decode. |
100 | ' ' Returns: |
101 | ' A string that contains the results of decoding the specified sequence of bytes. |
102 | ' ' Exceptions: |
103 | ' T:System.ArgumentException: |
104 | ' The byte array contains invalid Unicode code points. |
105 | ' |
106 | ' T:System.ArgumentNullException: |
107 | ' bytes is null. |
108 | ' ' T:System.Text.DecoderFallbackException: |
109 | ' A fallback occurred (see Character Encoding in the .NET Framework for complete |
110 | ' explanation)-and-System.Text.Encoding.DecoderFallback is set to System.Text.DecoderExceptionFallback. |
111 | |
112 | ''' <summary> |
113 | ''' When overridden in a derived class, decodes all the bytes in the specified byte |
114 | ''' array into a string. |
115 | ''' </summary> |
116 | ''' <param name="byts">The byte array containing the sequence of bytes to decode.</param> |
117 | ''' <returns>A string that contains the results of decoding the specified sequence of bytes.</returns> |
118 | Public Overloads Function ToString(byts As Byte()) As String |
119 | Return TextEncoding.GetString(byts) |
120 | End Function |
121 | |
122 | ''' <summary> |
123 | ''' <see cref="Encoding"/> instance |
124 | ''' </summary> |
125 | ''' <returns></returns> |
126 | Public Overrides Function ToString() As String |
127 | Return TextEncoding.ToString |
128 | End Function |
129 | End Class |
130 | End Namespace |