-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathCombineStream.cs
More file actions
276 lines (240 loc) · 7.99 KB
/
CombineStream.cs
File metadata and controls
276 lines (240 loc) · 7.99 KB
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
/*
* This file is part of the CatLib package.
*
* (c) CatLib <support@catlib.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Document: https://catlib.io/
*/
using CatLib.Exception;
using CatLib.Util;
using System;
using System.IO;
namespace CatLib.IO
{
/// <summary>
/// <see cref="CombineStream"/> allow multiple different streams to be combined into one stream.
/// </summary>
public class CombineStream : Stream
{
/// <summary>
/// An array of multiple streams.
/// </summary>
private readonly Stream[] streams;
/// <summary>
/// Whether to close the sub stream when closing the combined stream.
/// </summary>
private readonly bool autoClosed;
/// <summary>
/// Indicates the position of the current cursor.
/// </summary>
private long globalPosition;
/// <summary>
/// Indicates the index of the current stream.
/// </summary>
private int index;
/// <summary>
/// The length of the <see cref="CombineStream"/>.
/// </summary>
private long length;
/// <summary>
/// Initializes a new instance of the <see cref="CombineStream"/> class.
/// </summary>
/// <param name="left">The left stream.</param>
/// <param name="right">The right stream.</param>
/// <param name="closed">Whether to close the sub stream when closing the combined stream.</param>
public CombineStream(Stream left, Stream right, bool closed = false)
: this(new[] { left, right }, closed)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="CombineStream"/> class.
/// </summary>
/// <param name="source">An array of the sub stream.</param>
/// <param name="closed">Whether to close the sub stream when closing the combined stream.</param>
public CombineStream(Stream[] source, bool closed = false)
{
index = 0;
streams = source;
length = -1;
autoClosed = closed;
}
/// <summary>
/// Gets the length of the <see cref="CombineStream"/>.
/// </summary>
public override long Length
{
get
{
if (length >= 0)
{
return length;
}
length = 0;
foreach (var stream in streams)
{
length += stream.Length;
}
return length;
}
}
/// <inheritdoc />
public override bool CanSeek
{
get
{
foreach (var stream in streams)
{
if (!stream.CanSeek)
{
return false;
}
}
return true;
}
}
/// <inheritdoc />
public override long Position
{
get => globalPosition;
set => Seek(value, SeekOrigin.Begin);
}
/// <inheritdoc />
public override bool CanRead
{
get
{
foreach (var stream in streams)
{
if (!stream.CanRead)
{
return false;
}
}
return true;
}
}
/// <inheritdoc />
public override bool CanWrite => false;
/// <inheritdoc />
public override long Seek(long offset, SeekOrigin origin)
{
if (!CanSeek)
{
throw new NotSupportedException($"{nameof(CombineStream)} not supported {nameof(Seek)}.");
}
long newGloablPosition;
switch (origin)
{
case SeekOrigin.Begin:
newGloablPosition = offset;
break;
case SeekOrigin.Current:
newGloablPosition = globalPosition + offset;
break;
case SeekOrigin.End:
newGloablPosition = Length + offset;
break;
default:
throw new NotSupportedException($"Not support {nameof(SeekOrigin)}: {origin}");
}
if (newGloablPosition < 0 || newGloablPosition > Length)
{
throw new ArgumentOutOfRangeException($"{nameof(offset)} : {offset} must large than zero or small then {nameof(Length)} : {Length}");
}
long localPosition = 0;
var newIndex = index = CalculatedIndex(newGloablPosition, ref localPosition);
streams[newIndex].Seek(localPosition, SeekOrigin.Begin);
while (++newIndex < streams.Length)
{
streams[newIndex].Seek(0, SeekOrigin.Begin);
}
return globalPosition = newGloablPosition;
}
/// <inheritdoc />
public override int Read(byte[] buffer, int offset, int count)
{
Guard.Requires<ArgumentNullException>(buffer != null);
Guard.Requires<ArgumentOutOfRangeException>(offset >= 0);
Guard.Requires<ArgumentOutOfRangeException>(count >= 0);
Guard.Requires<ArgumentOutOfRangeException>(buffer.Length - offset >= count);
var result = 0;
do
{
var read = streams[index].Read(buffer, offset, count);
if (read <= 0 && index < streams.Length - 1)
{
index++;
continue;
}
if (read <= 0)
{
break;
}
count -= read;
offset += read;
globalPosition += read;
result += read;
}
while (count > 0);
return result;
}
/// <inheritdoc />
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException($"{nameof(CombineStream)} not supported {nameof(Write)}.");
}
/// <inheritdoc />
public override void SetLength(long value)
{
throw new NotSupportedException($"{nameof(CombineStream)} not supported {nameof(SetLength)}.");
}
/// <inheritdoc />
public override void Flush()
{
throw new NotSupportedException($"{nameof(CombineStream)} not supported {nameof(Flush)}.");
}
/// <summary>
/// Calculate sub stream index and relative positions.
/// </summary>
/// <param name="globalPosition">The position of the current cursor.</param>
/// <param name="localPosition">The relative position.</param>
/// <returns>The index of the stream array.</returns>
protected int CalculatedIndex(long globalPosition, ref long localPosition)
{
long len = 0;
for (var i = 0; i < streams.Length; i++)
{
len += streams[i].Length;
if (globalPosition > len)
{
continue;
}
localPosition = streams[i].Length - (len - globalPosition);
return i;
}
throw new AssertException($"Failed to determine {nameof(localPosition)}");
}
/// <inheritdoc />
protected override void Dispose(bool disposing)
{
try
{
if (!autoClosed)
{
return;
}
foreach (var stream in streams)
{
stream?.Dispose();
}
}
finally
{
base.Dispose(disposing);
}
}
}
}