forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteRangeStream.cs
More file actions
231 lines (199 loc) · 7.73 KB
/
ByteRangeStream.cs
File metadata and controls
231 lines (199 loc) · 7.73 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;
namespace System.Net.Http.Internal
{
/// <summary>
/// Stream which only exposes a read-only only range view of an
/// inner stream.
/// </summary>
internal class ByteRangeStream : DelegatingStream
{
// The offset stream position at which the range starts.
private readonly long _lowerbounds;
// The total number of bytes within the range.
private readonly long _totalCount;
// The current number of bytes read into the range
private long _currentCount;
public ByteRangeStream(Stream innerStream, RangeItemHeaderValue range)
: base(innerStream)
{
if (range == null)
{
throw Error.ArgumentNull("range");
}
if (!innerStream.CanSeek)
{
throw Error.Argument("innerStream", Properties.Resources.ByteRangeStreamNotSeekable, typeof(ByteRangeStream).Name);
}
if (innerStream.Length < 1)
{
throw Error.ArgumentOutOfRange("innerStream", innerStream.Length, Properties.Resources.ByteRangeStreamEmpty, typeof(ByteRangeStream).Name);
}
if (range.From.HasValue && range.From.Value > innerStream.Length)
{
throw Error.ArgumentOutOfRange("range", range.From, Properties.Resources.ByteRangeStreamInvalidFrom, innerStream.Length);
}
// Ranges are inclusive so 0-9 means the first 10 bytes
long maxLength = innerStream.Length - 1;
long upperbounds;
if (range.To.HasValue)
{
if (range.From.HasValue)
{
// e.g bytes=0-499 (the first 500 bytes offsets 0-499)
upperbounds = Math.Min(range.To.Value, maxLength);
_lowerbounds = range.From.Value;
}
else
{
// e.g bytes=-500 (the final 500 bytes)
upperbounds = maxLength;
_lowerbounds = Math.Max(innerStream.Length - range.To.Value, 0);
}
}
else
{
if (range.From.HasValue)
{
// e.g bytes=500- (from byte offset 500 and up)
upperbounds = maxLength;
_lowerbounds = range.From.Value;
}
else
{
// e.g. bytes=- (invalid so will never get here)
upperbounds = maxLength;
_lowerbounds = 0;
}
}
_totalCount = upperbounds - _lowerbounds + 1;
ContentRange = new ContentRangeHeaderValue(_lowerbounds, upperbounds, innerStream.Length);
}
public ContentRangeHeaderValue ContentRange { get; private set; }
public override long Length
{
get { return _totalCount; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Position
{
get
{
return _currentCount;
}
set
{
if (value < 0)
{
throw Error.ArgumentMustBeGreaterThanOrEqualTo("value", value, 0L);
}
_currentCount = value;
}
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return base.BeginRead(buffer, offset, PrepareStreamForRangeRead(count), callback, state);
}
public override int Read(byte[] buffer, int offset, int count)
{
return base.Read(buffer, offset, PrepareStreamForRangeRead(count));
}
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return base.ReadAsync(buffer, offset, PrepareStreamForRangeRead(count), cancellationToken);
}
public override int ReadByte()
{
int effectiveCount = PrepareStreamForRangeRead(1);
if (effectiveCount <= 0)
{
return -1;
}
return base.ReadByte();
}
public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
_currentCount = offset;
break;
case SeekOrigin.Current:
_currentCount = _currentCount + offset;
break;
case SeekOrigin.End:
_currentCount = _totalCount + offset;
break;
default:
throw Error.InvalidEnumArgument("origin", (int)origin, typeof(SeekOrigin));
}
if (_currentCount < 0L)
{
throw new IOException(Properties.Resources.ByteRangeStreamInvalidOffset);
}
return _currentCount;
}
public override void SetLength(long value)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
public override void Write(byte[] buffer, int offset, int count)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
public override void EndWrite(IAsyncResult asyncResult)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
public override void WriteByte(byte value)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
/// <summary>
/// Gets the correct count for the next read operation.
/// </summary>
/// <param name="count">The count requested to be read by the caller.</param>
/// <returns>The remaining bytes to read within the range defined for this stream.</returns>
private int PrepareStreamForRangeRead(int count)
{
// A negative count causes base.Raad* methods to throw an ArgumentOutOfRangeException.
if (count <= 0)
{
return count;
}
// Reading past the end simply does nothing.
if (_currentCount >= _totalCount)
{
return 0;
}
long effectiveCount = Math.Min(count, _totalCount - _currentCount);
// Check if we should update the inner stream's position.
var newPosition = _lowerbounds + _currentCount;
var position = InnerStream.Position;
if (newPosition != position)
{
InnerStream.Position = newPosition;
}
// Update current number of bytes read.
_currentCount += effectiveCount;
// Effective count can never be bigger than int.
return (int)effectiveCount;
}
}
}