-
Notifications
You must be signed in to change notification settings - Fork 1
/
StreamInternalBuffer.cs
133 lines (118 loc) · 5.69 KB
/
StreamInternalBuffer.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
namespace LC6464.Base16384;
/// <summary>
/// Base16384 编解码器。
/// </summary>
public static partial class Base16384 {
/// <summary>
/// 强制使用长流模式(分段编码)编码二进制数据流中的数据至 Base16384 UTF-16 BE 编码数据,追加到输出数据流。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <param name="output">输出数据流</param>
/// <returns>已写入的数据长度</returns>
public static long EncodeFromLongStreamToStream(Stream stream, Stream output) =>
EncodeFromLongStreamToStream(stream, output, new byte[Buffer0Length], new byte[EncodeLength(Buffer0Length)]);
/// <summary>
/// 编码二进制数据流中的数据至 Base16384 UTF-16 BE 编码数据,追加到输出数据流。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <param name="output">输出数据流</param>
/// <returns>已写入的数据长度</returns>
public static long EncodeToStream(Stream stream, Stream output) =>
!stream.CanSeek || stream.Length > Buffer0Length
? EncodeFromLongStreamToStream(stream, output)
: EncodeToStream(stream, output, new byte[stream.Length - stream.Position], new byte[EncodeLength(stream.Length - stream.Position)]);
/// <summary>
/// 强制使用长流模式(分段编码)解码 Base16384 UTF-16 BE 编码数据流中的数据至二进制数据,追加到输出数据流。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <param name="output">输出数据流</param>
/// <returns>已写入的数据长度</returns>
public static long DecodeFromLongStreamToStream(Stream stream, Stream output) =>
DecodeFromLongStreamToStream(stream, output, new byte[Buffer1Length + 2], new byte[DecodeLength(Buffer1Length)]);
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据流中的数据至二进制数据,追加到输出数据流。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <param name="output">输出数据流</param>
/// <returns>已写入的数据长度</returns>
public static long DecodeToStream(Stream stream, Stream output) =>
!stream.CanSeek || stream.Length > Buffer1Length
? DecodeFromLongStreamToStream(stream, output)
: DecodeToStream(stream, output, new byte[stream.Length - stream.Position], new byte[DecodeLength(stream.Length - stream.Position)]);
/// <summary>
/// 编码二进制数据至 Base16384 UTF-16 BE 编码数据,追加到输出数据流。
/// </summary>
/// <param name="data">二进制数据</param>
/// <param name="output">输出数据流</param>
/// <returns>已写入的数据长度</returns>
public static long EncodeToStream(ReadOnlySpan<byte> data, Stream output) =>
EncodeToStream(data, output, new byte[EncodeLength(Math.Min(Buffer0Length, data.Length))]);
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据至二进制数据,追加到输出数据流。
/// </summary>
/// <param name="data">Base16384 UTF-16 BE 编码数据</param>
/// <param name="output">输出数据流</param>
/// <returns>已写入的数据长度</returns>
public static long DecodeToStream(ReadOnlySpan<byte> data, Stream output) =>
DecodeToStream(data, output, new byte[DecodeLength(Math.Min(Buffer1Length, data.Length))]);
/// <summary>
/// 强制使用长流模式(分段编码)编码二进制数据流中的数据到新的 Base16384 UTF-16 BE 编码数据流。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <returns>Base16384 UTF-16 BE 编码数据流</returns>
public static MemoryStream EncodeFromLongStreamToNewMemoryStream(Stream stream) {
var output = new MemoryStream();
_ = EncodeFromLongStreamToStream(stream, output);
return output;
}
/// <summary>
/// 编码二进制数据流中的数据到新的 Base16384 UTF-16 BE 编码数据流。
/// </summary>
/// <param name="stream">二进制数据流</param>
/// <returns>Base16384 UTF-16 BE 编码数据流</returns>
public static MemoryStream EncodeToNewMemoryStream(Stream stream) {
var output = new MemoryStream();
_ = EncodeToStream(stream, output);
return output;
}
/// <summary>
/// 强制使用长流模式(分段编码)解码 Base16384 UTF-16 BE 编码数据流中的数据到新的二进制数据流。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <returns>二进制数据流</returns>
public static MemoryStream DecodeFromLongStreamToNewMemorySteam(Stream stream) {
var output = new MemoryStream();
_ = DecodeFromLongStreamToStream(stream, output);
return output;
}
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据流中的数据到新的二进制数据流。
/// </summary>
/// <param name="stream">Base16384 UTF-16 BE 编码数据流</param>
/// <returns>二进制数据流</returns>
public static MemoryStream DecodeToNewMemorySteam(Stream stream) {
var output = new MemoryStream();
_ = DecodeToStream(stream, output);
return output;
}
/// <summary>
/// 编码二进制数据到新的 Base16384 UTF-16 BE 编码数据流。
/// </summary>
/// <param name="data">二进制数据</param>
/// <returns>Base16384 UTF-16 BE 编码数据流</returns>
public static MemoryStream EncodeToNewMemoryStream(ReadOnlySpan<byte> data) {
var output = new MemoryStream();
_ = EncodeToStream(data, output);
return output;
}
/// <summary>
/// 解码 Base16384 UTF-16 BE 编码数据到新的二进制数据流。
/// </summary>
/// <param name="data">Base16384 UTF-16 BE 编码数据</param>
/// <returns>二进制数据流</returns>
public static MemoryStream DecodeToNewMemorySteam(ReadOnlySpan<byte> data) {
var output = new MemoryStream();
_ = DecodeToStream(data, output);
return output;
}
}