-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathByteRangeInterval.cs
More file actions
63 lines (56 loc) · 1.87 KB
/
ByteRangeInterval.cs
File metadata and controls
63 lines (56 loc) · 1.87 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
using System;
namespace Waher.Networking.HTTP
{
/// <summary>
/// Represents a range in a ranged HTTP request or response.
/// </summary>
public class ByteRangeInterval
{
private readonly long? first;
private readonly long? last;
private ByteRangeInterval next;
/// <summary>
/// Represents a range interval in a ranged HTTP request or response.
/// </summary>
/// <param name="First">First byte of interval, if provided. If not provided, the interval represents the last <paramref name="Last"/>
/// number of bytes of the resource.</param>
/// <param name="Last">Last byte of interval, inclusive, if provided. If not provided, the interval ends at the end of the resource.</param>
public ByteRangeInterval(long? First, long? Last)
{
this.first = First;
this.last = Last;
this.next = null;
}
/// <summary>
/// First byte of interval, if provided. If not provided, the interval represents the last <see cref="Last"/>
/// number of bytes of the resource.
/// </summary>
public long? First => this.first;
/// <summary>
/// Last byte of interval, inclusive, if provided. If not provided, the interval ends at the end of the resource.
/// </summary>
public long? Last => this.last;
/// <summary>
/// Next segment.
/// </summary>
public ByteRangeInterval Next
{
get => this.next;
internal set => this.next = value;
}
/// <summary>
/// Calculates the number of bytes spanned by the interval.
/// </summary>
/// <param name="TotalLength">Total number of bytes of content entity.</param>
/// <returns>Number of bytes spanned by interval.</returns>
public long GetIntervalLength(long TotalLength)
{
if (!this.first.HasValue)
return Math.Min(this.last.Value, TotalLength);
else if (!this.last.HasValue)
return TotalLength - this.first.Value;
else
return this.last.Value - this.first.Value + 1;
}
}
}