-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stream.cs
330 lines (298 loc) · 19.3 KB
/
Stream.cs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
namespace LC6464.Base16384;
/// <summary>
/// Base16384 编解码器。
/// </summary>
public static partial class Base16384 {
/// <summary>
/// 强制使用长流模式(分段编码)编码二进制数据流中的数据至 Base16384 UTF-16 BE 编码数据,追加到输出数据流。<br/>
/// 特别提醒:必须保证外部提供的缓存空间长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <param name="output">输出数据流</param>
/// <param name="buffer">外部提供的缓存空间(长度必须大于等于 <see cref="Buffer0Length"/>)</param>
/// <param name="encodingBuffer">外部提供的用于编码的缓存空间(长度必须大于等于 <see cref="EncodeLength"/>(<see cref="Buffer0Length"/>))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>已写入的数据长度</returns>
public static unsafe long EncodeFromLongStreamToStream(Stream stream, Stream output, Span<byte> buffer, Span<byte> encodingBuffer) {
if (buffer.Length < Buffer0Length) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成编码。", nameof(buffer));
}
var encodeLength = (int)EncodeLength(Buffer0Length);
if (encodingBuffer.Length < encodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成编码。", nameof(encodingBuffer));
}
var readingBuffer = buffer[..Buffer0Length]; // 防止一次读入过多数据,此处使用切片
int readCount = stream.Read(readingBuffer),
writeCount = 0;
do {
var encodedLength = Encode(buffer[..readCount], encodingBuffer); // 编码结果写入缓冲区
output.Write(encodingBuffer[..encodedLength]); // 将缓冲区中指定长度的数据写入输出流
writeCount += encodedLength;
} while ((readCount = stream.Read(readingBuffer)) > 0);
output.Flush();
return writeCount;
}
/// <summary>
/// 编码二进制数据流中的数据至 Base16384 UTF-16 BE 编码数据,追加到输出数据流。<br/>
/// 特别提醒:必须保证外部提供的缓存空间长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <param name="output">输出数据流</param>
/// <param name="buffer">外部提供的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer0Length"/>,则长度必须大于等于 <see cref="Buffer0Length"/>;否则长度必须大于等于 <paramref name="stream"/>.Length - <paramref name="stream"/>.Position)</param>
/// <param name="encodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer0Length"/>,则长度必须大于等于 <see cref="EncodeLength"/>(<see cref="Buffer0Length"/>);否则长度必须大于等于 <see cref="EncodeLength"/>(<paramref name="stream"/>.Length - <paramref name="stream"/>.Position))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>已写入的数据长度</returns>
public static unsafe long EncodeToStream(Stream stream, Stream output, Span<byte> buffer, Span<byte> encodingBuffer) {
if (!stream.CanSeek || stream.Length > Buffer0Length) {
return EncodeFromLongStreamToStream(stream, output, buffer, encodingBuffer);
}
var remainingLength = stream.Length - stream.Position;
if (buffer.Length < remainingLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成编码。", nameof(buffer));
}
var encodeLength = (int)EncodeLength(remainingLength);
if (encodingBuffer.Length < encodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成编码。", nameof(encodingBuffer));
}
var encodedLength = Encode(buffer[..stream.Read(buffer)], encodingBuffer);
output.Write(encodingBuffer[..encodedLength]); // 将缓冲区中指定长度的数据写入输出流
output.Flush();
return encodedLength;
}
/// <summary>
/// 强制使用长流模式(分段编码)解码 Base16384 UTF-16 BE 编码数据流中的数据至二进制数据,追加到输出数据流。<br/>
/// 特别提醒:若使用外部提供的缓存空间,必须保证其长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <param name="output">输出数据流</param>
/// <param name="buffer">外部提供的缓存空间(长度必须大于等于 <see cref="Buffer1Length"/> + 2)</param>
/// <param name="decodingBuffer">外部提供的用于编码的缓存空间(长度必须大于等于 <see cref="DecodeLength"/>(<see cref="Buffer1Length"/>))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>已写入的数据长度</returns>
public static unsafe long DecodeFromLongStreamToStream(Stream stream, Stream output, Span<byte> buffer, Span<byte> decodingBuffer) {
if (buffer.Length < Buffer1Length + 2) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成解码。", nameof(buffer));
}
var decodeLength = (int)DecodeLength(Buffer1Length);
if (decodingBuffer.Length < decodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成解码。", nameof(decodingBuffer));
}
var readingBuffer = buffer[..Buffer1Length]; // 防止一次读入过多数据,此处使用切片
byte end; // skipcq: CS-W1022 对 end 赋值的确是不必要的
int readCount = stream.Read(readingBuffer),
writeCount = 0;
do {
if (Convert.ToBoolean(end = IsNextEnd(stream))) {
buffer[readCount++] = 61; // (byte)'=' // skipcq: CS-W1082 readCount 值已递增,61 不会被后续语句覆盖
buffer[readCount++] = end;
}
var decodedLength = Decode(buffer[..readCount], decodingBuffer); // 解码结果写入缓冲区
output.Write(decodingBuffer[..decodedLength]); // 将缓冲区中指定长度的数据写入输出流
writeCount += decodedLength;
} while ((readCount = stream.Read(readingBuffer)) > 0);
output.Flush();
return writeCount;
}
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据流中的数据至二进制数据,追加到输出数据流。<br/>
/// 特别提醒:若使用外部提供的缓存空间,必须保证其长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <param name="output">输出数据流</param>
/// <param name="buffer">外部提供的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer1Length"/>,则长度必须大于等于 <see cref="Buffer1Length"/> + 2;否则长度必须大于等于 <paramref name="stream"/>.Length - <paramref name="stream"/>.Position)</param>
/// <param name="decodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer1Length"/>,则长度必须大于等于 <see cref="DecodeLength"/>(<see cref="Buffer1Length"/>);否则长度必须大于等于 <see cref="DecodeLength"/>(<paramref name="stream"/>.Length - <paramref name="stream"/>.Position))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>已写入的数据长度</returns>
public static unsafe long DecodeToStream(Stream stream, Stream output, Span<byte> buffer, Span<byte> decodingBuffer) {
if (!stream.CanSeek || stream.Length > Buffer1Length) {
return DecodeFromLongStreamToStream(stream, output, buffer, decodingBuffer);
}
var remainingLength = stream.Length - stream.Position;
if (buffer.Length < remainingLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成解码。", nameof(buffer));
}
var decodeLength = (int)DecodeLength(remainingLength);
if (decodingBuffer.Length < decodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成解码。", nameof(decodingBuffer));
}
var decodedLength = Decode(buffer[..stream.Read(buffer)], decodingBuffer);
output.Write(decodingBuffer[..decodedLength]);
output.Flush();
return decodedLength;
}
/// <summary>
/// 编码二进制数据至 Base16384 UTF-16 BE 编码数据,追加到输出数据流。<br/>
/// 特别提醒:若使用外部提供的缓存空间,必须保证其长度足够大,否则将会引发异常。
/// </summary>
/// <param name="data">二进制数据</param>
/// <param name="output">输出数据流</param>
/// <param name="encodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="data"/>.Length > <see cref="Buffer0Length"/>,则长度必须大于等于 <see cref="EncodeLength"/>(<see cref="Buffer0Length"/>);否则长度必须大于等于 <see cref="EncodeLength"/>(<paramref name="data"/>.Length))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>已写入的数据长度</returns>
public static long EncodeToStream(ReadOnlySpan<byte> data, Stream output, Span<byte> encodingBuffer) {
if (data.Length > Buffer0Length) {
var encodeLength = (int)EncodeLength(Buffer0Length);
if (encodingBuffer.Length < encodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成编码。", nameof(encodingBuffer));
}
int remainingCount = data.Length,
encodedCount = 0,
writeCount = 0;
do {
var readCount = Math.Min(Buffer0Length, remainingCount); // 读取数据长度
remainingCount -= readCount;
var encodedLength = Encode(data.Slice(encodedCount, readCount), encodingBuffer); // 编码结果写入缓冲区
encodedCount += readCount;
output.Write(encodingBuffer[..encodedLength]); // 将缓冲区中指定长度的数据写入输出流
writeCount += encodedLength;
} while (remainingCount > 0);
output.Flush();
return writeCount;
}
{
var encodeLength = (int)EncodeLength(data.Length);
if (encodingBuffer.Length < encodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成编码。", nameof(encodingBuffer));
}
var encodedLength = Encode(data, encodingBuffer);
output.Write(encodingBuffer[..encodedLength]);
output.Flush();
return encodedLength;
}
}
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据至二进制数据,追加到输出数据流。<br/>
/// 特别提醒:若使用外部提供的缓存空间,必须保证其长度足够大,否则将会引发异常。
/// </summary>
/// <param name="data">Base16384 UTF-16 BE 编码数据</param>
/// <param name="output">输出数据流</param>
/// <param name="decodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="data"/>.Length > <see cref="Buffer1Length"/>,则长度必须大于等于 <see cref="DecodeLength"/>(<see cref="Buffer1Length"/>);否则长度必须大于等于 <see cref="DecodeLength"/>(<paramref name="data"/>.Length))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>已写入的数据长度</returns>
public static unsafe long DecodeToStream(ReadOnlySpan<byte> data, Stream output, Span<byte> decodingBuffer) {
if (data.Length > Buffer1Length) {
var decodeLength = (int)DecodeLength(Buffer1Length);
if (decodingBuffer.Length < decodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成解码。", nameof(decodingBuffer));
}
var memory = Marshal.AllocHGlobal(Buffer1Length + 2); // 临时储存待解码数据的非托管内存
var buffer = new Span<byte>((byte*)memory, Buffer1Length + 2); // 指向非托管内存的 Span
int remainingCount = data.Length,
decodedCount = 0,
writeCount = 0,
readCount = Math.Min(Buffer1Length, remainingCount);
do {
remainingCount -= readCount;
data.Slice(decodedCount, readCount).CopyTo(buffer);
decodedCount += readCount;
if (remainingCount == 2) { // 剩下两字节,可能是 '=' + (1~6),一起处理了
buffer[readCount++] = data[^2];
buffer[readCount++] = data[^1];
remainingCount = 0;
} else if (remainingCount == 1) { // 剩下一字节,一起处理了
buffer[readCount++] = data[^1];
remainingCount = 0;
}
var decodedLength = Decode(buffer[..readCount], decodingBuffer);
output.Write(decodingBuffer[..decodedLength]);
writeCount += decodedLength;
readCount = Math.Min(Buffer1Length, remainingCount);
} while (remainingCount > 0);
output.Flush();
Marshal.FreeHGlobal(memory); // 释放非托管内存
return writeCount;
}
{
var decodeLength = (int)DecodeLength(data.Length);
if (decodingBuffer.Length < decodeLength) {
throw new ArgumentException("外部提供的缓存空间不足,无法完成编码。", nameof(decodingBuffer));
}
var decodedData = Decode(data);
output.Write(decodedData);
output.Flush();
return decodedData.Length;
}
}
/// <summary>
/// 强制使用长流模式(分段编码)编码二进制数据流中的数据到新的 Base16384 UTF-16 BE 编码数据流。<br/>
/// 特别提醒:必须保证外部提供的缓存空间长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <param name="buffer">外部提供的缓存空间(长度必须大于等于 <see cref="Buffer0Length"/>)</param>
/// <param name="encodingBuffer">外部提供的用于编码的缓存空间(长度必须大于等于 <see cref="EncodeLength"/>(<see cref="Buffer0Length"/>))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>Base16384 UTF-16 BE 编码数据流</returns>
public static MemoryStream EncodeFromLongStreamToNewMemoryStream(Stream stream, Span<byte> buffer, Span<byte> encodingBuffer) {
var output = new MemoryStream();
_ = EncodeFromLongStreamToStream(stream, output, buffer, encodingBuffer);
return output;
}
/// <summary>
/// 编码二进制数据流中的数据到新的 Base16384 UTF-16 BE 编码数据流。<br/>
/// 特别提醒:必须保证外部提供的缓存空间长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <param name="buffer">外部提供的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer0Length"/>,则长度必须大于等于 <see cref="Buffer0Length"/>;否则长度必须大于等于 <paramref name="stream"/>.Length - <paramref name="stream"/>.Position)</param>
/// <param name="encodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer0Length"/>,则长度必须大于等于 <see cref="EncodeLength"/>(<see cref="Buffer0Length"/>);否则长度必须大于等于 <see cref="EncodeLength"/>(<paramref name="stream"/>.Length - <paramref name="stream"/>.Position))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>Base16384 UTF-16 BE 编码数据流</returns>
public static MemoryStream EncodeToNewMemoryStream(Stream stream, Span<byte> buffer, Span<byte> encodingBuffer) {
var output = new MemoryStream();
_ = EncodeToStream(stream, output, buffer, encodingBuffer);
return output;
}
/// <summary>
/// 强制使用长流模式(分段编码)解码 Base16384 UTF-16 BE 编码数据流中的数据到新的二进制数据流。<br/>
/// 特别提醒:若使用外部提供的缓存空间,必须保证其长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <param name="buffer">外部提供的缓存空间(长度必须大于等于 <see cref="Buffer1Length"/> + 2)</param>
/// <param name="decodingBuffer">外部提供的用于编码的缓存空间(长度必须大于等于 <see cref="DecodeLength"/>(<see cref="Buffer1Length"/>))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>二进制数据流</returns>
public static MemoryStream DecodeFromLongStreamToNewMemorySteam(Stream stream, Span<byte> buffer, Span<byte> decodingBuffer) {
var output = new MemoryStream();
_ = DecodeFromLongStreamToStream(stream, output, buffer, decodingBuffer);
return output;
}
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据流中的数据到新的二进制数据流。<br/>
/// 特别提醒:若使用外部提供的缓存空间,必须保证其长度足够大,否则将会引发异常。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <param name="buffer">外部提供的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer1Length"/>,则长度必须大于等于 <see cref="Buffer1Length"/> + 2;否则长度必须大于等于 <paramref name="stream"/>.Length - <paramref name="stream"/>.Position)</param>
/// <param name="decodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="stream"/>.Length > <see cref="Buffer1Length"/>,则长度必须大于等于 <see cref="DecodeLength"/>(<see cref="Buffer1Length"/>);否则长度必须大于等于 <see cref="DecodeLength"/>(<paramref name="stream"/>.Length - <paramref name="stream"/>.Position))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>二进制数据流</returns>
public static MemoryStream DecodeToNewMemorySteam(Stream stream, Span<byte> buffer, Span<byte> decodingBuffer) {
var output = new MemoryStream();
_ = DecodeToStream(stream, output, buffer, decodingBuffer);
return output;
}
/// <summary>
/// 编码二进制数据到新的 Base16384 UTF-16 BE 编码数据流。<br/>
/// 特别提醒:必须保证外部提供的缓存空间长度足够大,否则将会引发异常。
/// </summary>
/// <param name="data">二进制数据</param>
/// <param name="encodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="data"/>.Length > <see cref="Buffer0Length"/>,则长度必须大于等于 <see cref="EncodeLength"/>(<see cref="Buffer0Length"/>);否则长度必须大于等于 <see cref="EncodeLength"/>(<paramref name="data"/>.Length))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>Base16384 UTF-16 BE 编码数据流</returns>
public static MemoryStream EncodeToNewMemoryStream(ReadOnlySpan<byte> data, Span<byte> encodingBuffer) {
var output = new MemoryStream();
_ = EncodeToStream(data, output, encodingBuffer);
return output;
}
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据到新的二进制数据流。<br/>
/// 特别提醒:必须保证外部提供的缓存空间长度足够大,否则将会引发异常。
/// </summary>
/// <param name="data">Base16384 UTF-16 BE 编码数据</param>
/// <param name="decodingBuffer">外部提供的用于编码的缓存空间(若 <paramref name="data"/>.Length > <see cref="Buffer1Length"/>,则长度必须大于等于 <see cref="DecodeLength"/>(<see cref="Buffer1Length"/>);否则长度必须大于等于 <see cref="DecodeLength"/>(<paramref name="data"/>.Length))</param>
/// <exception cref="ArgumentException">外部提供的缓存空间不足</exception>
/// <returns>二进制数据流</returns>
public static MemoryStream DecodeToNewMemorySteam(ReadOnlySpan<byte> data, Span<byte> decodingBuffer) {
var output = new MemoryStream();
_ = DecodeToStream(data, output, decodingBuffer);
return output;
}
}