| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Globalization; |
| | 7 | | using Azure.Core; |
| | 8 | |
|
| | 9 | | namespace Azure |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Defines a range of bytes within an HTTP resource, starting at an offset and |
| | 13 | | /// ending at offset+count-1 inclusively. |
| | 14 | | /// </summary> |
| | 15 | | public readonly struct HttpRange : IEquatable<HttpRange> |
| | 16 | | { |
| | 17 | | // We only support using the "bytes" unit. |
| | 18 | | private const string Unit = "bytes"; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Gets the starting offset of the <see cref="HttpRange"/>. |
| | 22 | | /// </summary> |
| 52 | 23 | | public long Offset { get; } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Gets the size of the <see cref="HttpRange"/>. null means the range |
| | 27 | | /// extends all the way to the end. |
| | 28 | | /// </summary> |
| 42 | 29 | | public long? Length { get; } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Creates an instance of HttpRange. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="offset">The starting offset of the <see cref="HttpRange"/>. Defaults to 0.</param> |
| | 35 | | /// <param name="length">The length of the range. null means to the end.</param> |
| | 36 | | public HttpRange(long offset = 0, long? length = default) |
| | 37 | | { |
| 26 | 38 | | if (offset < 0) |
| 2 | 39 | | throw new ArgumentOutOfRangeException(nameof(offset)); |
| 24 | 40 | | if (length.HasValue && length <= 0) |
| 4 | 41 | | throw new ArgumentOutOfRangeException(nameof(length)); |
| | 42 | |
|
| 20 | 43 | | Offset = offset; |
| 20 | 44 | | Length = length; |
| 20 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Converts the specified range to a string. |
| | 49 | | /// </summary> |
| | 50 | | /// <returns>String representation of the range.</returns> |
| | 51 | | /// <remarks>For more information, see https://docs.microsoft.com/en-us/rest/api/storageservices/specifying-the- |
| | 52 | | public override string ToString() |
| | 53 | | { |
| | 54 | | // No additional validation by design. API can validate parameter by case, and use this method. |
| 10 | 55 | | var endRange = ""; |
| 10 | 56 | | if (Length.HasValue && Length != 0) |
| | 57 | | { |
| 6 | 58 | | endRange = (Offset + Length.Value - 1).ToString(CultureInfo.InvariantCulture); |
| | 59 | | } |
| | 60 | |
|
| 10 | 61 | | return FormattableString.Invariant($"{Unit}={Offset}-{endRange}"); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Check if two <see cref="HttpRange"/> instances are equal. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="left">The first instance to compare.</param> |
| | 68 | | /// <param name="right">The second instance to compare.</param> |
| | 69 | | /// <returns>True if they're equal, false otherwise.</returns> |
| 16 | 70 | | public static bool operator ==(HttpRange left, HttpRange right) => left.Equals(right); |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Check if two <see cref="HttpRange"/> instances are not equal. |
| | 74 | | /// </summary> |
| | 75 | | /// <param name="left">The first instance to compare.</param> |
| | 76 | | /// <param name="right">The second instance to compare.</param> |
| | 77 | | /// <returns>True if they're not equal, false otherwise.</returns> |
| 8 | 78 | | public static bool operator !=(HttpRange left, HttpRange right) => !(left == right); |
| | 79 | |
|
| | 80 | | /// <summary> |
| | 81 | | /// Check if two <see cref="HttpRange"/> instances are equal. |
| | 82 | | /// </summary> |
| | 83 | | /// <param name="other">The instance to compare to.</param> |
| | 84 | | /// <returns>True if they're equal, false otherwise.</returns> |
| 18 | 85 | | public bool Equals(HttpRange other) => Offset == other.Offset && Length == other.Length; |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Check if two <see cref="HttpRange"/> instances are equal. |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="obj">The instance to compare to.</param> |
| | 91 | | /// <returns>True if they're equal, false otherwise.</returns> |
| | 92 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 93 | | public override bool Equals(object? obj) => obj is HttpRange other && Equals(other); |
| | 94 | |
|
| | 95 | | /// <summary> |
| | 96 | | /// Get a hash code for the <see cref="HttpRange"/>. |
| | 97 | | /// </summary> |
| | 98 | | /// <returns>Hash code for the <see cref="HttpRange"/>.</returns> |
| | 99 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 100 | | public override int GetHashCode() => HashCodeBuilder.Combine(Offset, Length); |
| | 101 | | } |
| | 102 | | } |