forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass1.vb
251 lines (227 loc) · 9.59 KB
/
class1.vb
1
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
Namespace UUCodeVB
Class Coder
Private inputFileName As String
Private outputFileName As String
Public Sub New(ByVal inFile As String, ByVal outFile As String)
inputFileName = CStr(inFile.Clone())
outputFileName = CStr(outFile.Clone())
End Sub
'<Snippet3>
Public Sub DecodeWithCharArray()
Dim inFile As System.IO.StreamReader
Dim base64CharArray() As Char
Try
inFile = New System.IO.StreamReader(inputFileName, _
System.Text.Encoding.ASCII)
ReDim base64CharArray(inFile.BaseStream.Length - 1)
inFile.Read(base64CharArray, 0, inFile.BaseStream.Length)
inFile.Close()
Catch exp As System.Exception
' Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message)
Return
End Try
' Convert the Base64 UUEncoded input into binary output.
Dim binaryData() As Byte
Try
binaryData = System.Convert.FromBase64CharArray(base64CharArray, 0, _
base64CharArray.Length)
Catch exp As System.ArgumentNullException
System.Console.WriteLine("Base 64 character array is null.")
Return
Catch exp As System.FormatException
System.Console.WriteLine("Base 64 Char Array length is not " + _
"4 or is not an even multiple of 4")
Return
End Try
' Write out the decoded data.
Dim outFile As System.IO.FileStream
Try
outFile = New System.IO.FileStream(outputFileName, _
System.IO.FileMode.Create, _
System.IO.FileAccess.Write)
outFile.Write(binaryData, 0, binaryData.Length - 1)
outFile.Close()
Catch exp As System.Exception
' Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message)
End Try
End Sub
'</Snippet3>
'<Snippet4>
Public Sub DecodeWithString()
Dim inFile As System.IO.StreamReader
Dim base64String As String
Try
Dim base64CharArray() As Char
inFile = New System.IO.StreamReader(inputFileName, _
System.Text.Encoding.ASCII)
base64CharArray = New Char(inFile.BaseStream.Length) {}
inFile.Read(base64CharArray, 0, inFile.BaseStream.Length)
base64String = New String(base64CharArray, _
0, _
base64CharArray.Length - 1)
Catch exp As System.Exception
' Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message)
Return
End Try
' Convert the Base64 UUEncoded input into binary output.
Dim binaryData() As Byte
Try
binaryData = System.Convert.FromBase64String(base64String)
Catch exp As System.ArgumentNullException
System.Console.WriteLine("Base 64 string is null.")
Return
Catch exp As System.FormatException
System.Console.WriteLine("Base 64 length is not 4 or is " + _
"not an even multiple of 4.")
Return
End Try
'Write out the decoded data.
Dim outFile As System.IO.FileStream
Try
outFile = New System.IO.FileStream(outputFileName, _
System.IO.FileMode.Create, _
System.IO.FileAccess.Write)
outFile.Write(binaryData, 0, binaryData.Length - 1)
outFile.Close()
Catch exp As System.Exception
' Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message)
End Try
End Sub
'</Snippet4>
'<Snippet2>
Public Sub EncodeWithCharArray()
Dim inFile As System.IO.FileStream
Dim binaryData() As Byte
Try
inFile = New System.IO.FileStream(inputFileName, _
System.IO.FileMode.Open, _
System.IO.FileAccess.Read)
ReDim binaryData(inFile.Length)
Dim bytesRead As Long = inFile.Read(binaryData, _
0, _
CInt(inFile.Length))
inFile.Close()
Catch exp As System.Exception
' Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message)
Return
End Try
' Convert the binary input into Base64 UUEncoded output.
' Each 3 byte sequence in the source data becomes a 4 byte
' sequence in the character array.
Dim arrayLength As Long
arrayLength = (4 / 3) * binaryData.Length
If arrayLength Mod 4 <> 0 Then
arrayLength = arrayLength + 4 - arrayLength Mod 4
End If
Dim base64CharArray(arrayLength - 1) As Char
Try
System.Convert.ToBase64CharArray(binaryData, _
0, _
binaryData.Length, _
base64CharArray, 0)
Catch exp As System.ArgumentNullException
System.Console.WriteLine("Binary data array is null.")
Return
Catch exp As System.ArgumentOutOfRangeException
System.Console.WriteLine("Char Array is not large enough.")
Return
End Try
' Write the UUEncoded version to the output file.
Dim outFile As System.IO.StreamWriter
Try
outFile = New System.IO.StreamWriter(outputFileName, _
False, _
System.Text.Encoding.ASCII)
outFile.Write(base64CharArray)
outFile.Close()
Catch exp As System.Exception
' Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message)
End Try
End Sub
'</Snippet2>
'<Snippet1>
Public Sub EncodeWithString()
Dim inFile As System.IO.FileStream
Dim binaryData() As Byte
Try
inFile = New System.IO.FileStream(inputFileName, _
System.IO.FileMode.Open, _
System.IO.FileAccess.Read)
ReDim binaryData(inFile.Length)
Dim bytesRead As Long = inFile.Read(binaryData, _
0, _
inFile.Length)
inFile.Close()
Catch exp As System.Exception
' Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message)
Return
End Try
' Convert the binary input into Base64 UUEncoded output.
Dim base64String As String
Try
base64String = System.Convert.ToBase64String(binaryData, _
0, _
binaryData.Length)
Catch exp As System.ArgumentNullException
System.Console.WriteLine("Binary data array is null.")
Return
End Try
' Write the UUEncoded version to the output file.
Dim outFile As System.IO.StreamWriter
Try
outFile = New System.IO.StreamWriter(outputFileName, _
False, _
System.Text.Encoding.ASCII)
outFile.Write(base64String)
outFile.Close()
Catch exp As System.Exception
' Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message)
End Try
End Sub
'</Snippet1>
Public Overloads Shared Sub Main()
Dim args() As String = System.Environment.GetCommandLineArgs()
If args.Length <> 5 Then
System.Console.WriteLine(("Usage: UUCodeC -d | -e " + "-s | -c inputFile outputFile"))
Return
End If
Dim inputFileName As String = args(3)
Dim outputFileName As String = args(4)
Dim coder As New Coder(inputFileName, outputFileName)
Select Case args(1)
Case "-d"
If args(2) = "-s" Then
coder.DecodeWithString()
Else
If args(2) = "-c" Then
coder.DecodeWithCharArray()
Else
System.Console.WriteLine("Second arg must be -s or -c")
Return
End If
End If
Case "-e"
If args(2) = "-s" Then
coder.EncodeWithString()
Else
If args(2) = "-c" Then
coder.EncodeWithCharArray()
Else
System.Console.WriteLine("Second arg must be -s or -c")
Return
End If
End If
Case Else
System.Console.WriteLine("First arg must be -d or -e")
End Select
End Sub
End Class
End Namespace