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
165 lines (144 loc) · 5.74 KB
/
Copy pathByteRangeStream.cs
File metadata and controls
165 lines (144 loc) · 5.74 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
// 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.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 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 int ReadByte()
{
int effectiveCount = PrepareStreamForRangeRead(1);
if (effectiveCount <= 0)
{
return -1;
}
return base.ReadByte();
}
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 void WriteByte(byte value)
{
throw Error.NotSupported(Properties.Resources.ByteRangeStreamReadOnly);
}
/// <summary>
/// Gets the
/// </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)
{
long effectiveCount = Math.Min(count, _totalCount - _currentCount);
if (effectiveCount > 0)
{
// Check if we should update the stream position
long position = InnerStream.Position;
if (_lowerbounds + _currentCount != position)
{
InnerStream.Position = _lowerbounds + _currentCount;
}
// Update current number of bytes read
_currentCount += effectiveCount;
}
// Effective count can never be bigger than int
return (int)effectiveCount;
}
}
}