| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.IO; |
| | 8 | | using System.Text; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | | using Azure.Core.Pipeline; |
| | 12 | | using Azure.Storage.Blobs.Models; |
| | 13 | |
|
| | 14 | | namespace Azure.Storage.Blobs.ChangeFeed |
| | 15 | | { |
| | 16 | | internal class LazyLoadingBlobStream : Stream |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// BlobClient to make download calls with. |
| | 20 | | /// </summary> |
| | 21 | | private readonly BlobClient _blobClient; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// The offset within the blob of the next block we will download. |
| | 25 | | /// </summary> |
| | 26 | | private long _offset; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// The number of bytes we'll download with each download call. |
| | 30 | | /// </summary> |
| | 31 | | private readonly long _blockSize; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Underlying Stream. |
| | 35 | | /// </summary> |
| | 36 | | private Stream _stream; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// If this LazyLoadingBlobStream has been initalized. |
| | 40 | | /// </summary> |
| | 41 | | private bool _initalized; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// The number of bytes in the last download call. |
| | 45 | | /// </summary> |
| | 46 | | private long _lastDownloadBytes; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// The current length of the blob. |
| | 50 | | /// </summary> |
| | 51 | | private long _blobLength; |
| | 52 | |
|
| 272 | 53 | | public LazyLoadingBlobStream(BlobClient blobClient, long offset, long blockSize) |
| | 54 | | { |
| 272 | 55 | | _blobClient = blobClient; |
| 272 | 56 | | _offset = offset; |
| 272 | 57 | | _blockSize = blockSize; |
| 272 | 58 | | _initalized = false; |
| 272 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Constructor for mocking. |
| | 63 | | /// </summary> |
| 32 | 64 | | public LazyLoadingBlobStream() { } |
| | 65 | |
|
| | 66 | | /// <inheritdoc/> |
| | 67 | | public override int Read( |
| | 68 | | byte[] buffer, |
| | 69 | | int offset, |
| | 70 | | int count) |
| 0 | 71 | | => ReadInternal( |
| 0 | 72 | | async: false, |
| 0 | 73 | | buffer, |
| 0 | 74 | | offset, |
| 0 | 75 | | count).EnsureCompleted(); |
| | 76 | |
|
| | 77 | | /// <inheritdoc/> |
| | 78 | | public override async Task<int> ReadAsync( |
| | 79 | | byte[] buffer, |
| | 80 | | int offset, |
| | 81 | | int count, |
| | 82 | | CancellationToken cancellationToken) |
| 1549784 | 83 | | => await ReadInternal( |
| 1549784 | 84 | | async: true, |
| 1549784 | 85 | | buffer, |
| 1549784 | 86 | | offset, |
| 1549784 | 87 | | count, |
| 1549784 | 88 | | cancellationToken).ConfigureAwait(false); |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Initalizes this LazyLoadingBlobStream. |
| | 92 | | /// </summary> |
| | 93 | | private async Task Initalize(bool async, CancellationToken cancellationToken) |
| | 94 | | { |
| 268 | 95 | | await DownloadBlock(async, cancellationToken).ConfigureAwait(false); |
| 268 | 96 | | _initalized = true; |
| 268 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Downloads the next block. |
| | 101 | | /// </summary> |
| | 102 | | private async Task DownloadBlock(bool async, CancellationToken cancellationToken) |
| | 103 | | { |
| | 104 | | Response<BlobDownloadInfo> response; |
| 292 | 105 | | HttpRange range = new HttpRange(_offset, _blockSize); |
| | 106 | |
|
| 292 | 107 | | response = async |
| 292 | 108 | | ? await _blobClient.DownloadAsync(range, cancellationToken: cancellationToken).ConfigureAwait(false) |
| 292 | 109 | | : _blobClient.Download(range, cancellationToken: cancellationToken); |
| 292 | 110 | | _stream = response.Value.Content; |
| 292 | 111 | | _offset += response.Value.ContentLength; |
| 292 | 112 | | _lastDownloadBytes = response.Value.ContentLength; |
| 292 | 113 | | _blobLength = GetBlobLength(response); |
| 292 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Shared sync and async Read implementation. |
| | 118 | | /// </summary> |
| | 119 | | private async Task<int> ReadInternal( |
| | 120 | | bool async, |
| | 121 | | byte[] buffer, |
| | 122 | | int offset, |
| | 123 | | int count, |
| | 124 | | CancellationToken cancellationToken = default) |
| | 125 | | { |
| 1549784 | 126 | | ValidateReadParameters(buffer, offset, count); |
| | 127 | |
|
| 1549764 | 128 | | if (!_initalized) |
| | 129 | | { |
| 268 | 130 | | await Initalize(async, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 268 | 131 | | if (_lastDownloadBytes == 0) |
| | 132 | | { |
| 0 | 133 | | return 0; |
| | 134 | | } |
| | 135 | | } |
| | 136 | |
|
| 1549764 | 137 | | int totalCopiedBytes = 0; |
| | 138 | | do |
| | 139 | | { |
| 1549808 | 140 | | int copiedBytes = async |
| 1549808 | 141 | | ? await _stream.ReadAsync(buffer, offset, count).ConfigureAwait(false) |
| 1549808 | 142 | | : _stream.Read(buffer, offset, count); |
| 1549808 | 143 | | offset += copiedBytes; |
| 1549808 | 144 | | count -= copiedBytes; |
| 1549808 | 145 | | totalCopiedBytes += copiedBytes; |
| | 146 | |
|
| | 147 | | // We've run out of bytes in the current block. |
| 1549808 | 148 | | if (copiedBytes == 0) |
| | 149 | | { |
| | 150 | | // We hit the end of the blob with the last download call. |
| 196 | 151 | | if (_offset == _blobLength) |
| | 152 | | { |
| 172 | 153 | | return totalCopiedBytes; |
| | 154 | | } |
| | 155 | |
|
| | 156 | | // Download the next block |
| | 157 | | else |
| | 158 | | { |
| 24 | 159 | | await DownloadBlock(async, cancellationToken).ConfigureAwait(false); |
| | 160 | | } |
| | 161 | | } |
| | 162 | | } |
| 1549636 | 163 | | while (count > 0); |
| 1549592 | 164 | | return totalCopiedBytes; |
| 1549764 | 165 | | } |
| | 166 | |
|
| | 167 | | private static void ValidateReadParameters(byte[] buffer, int offset, int count) |
| | 168 | | { |
| 1549784 | 169 | | if (buffer == null) |
| | 170 | | { |
| 4 | 171 | | throw new ArgumentNullException($"{nameof(buffer)}", $"{nameof(buffer)} cannot be null."); |
| | 172 | | } |
| | 173 | |
|
| 1549780 | 174 | | if (offset < 0) |
| | 175 | | { |
| 4 | 176 | | throw new ArgumentOutOfRangeException($"{nameof(offset)} cannot be less than 0."); |
| | 177 | | } |
| | 178 | |
|
| 1549776 | 179 | | if (offset > buffer.Length) |
| | 180 | | { |
| 4 | 181 | | throw new ArgumentOutOfRangeException($"{nameof(offset)} cannot exceed {nameof(buffer)} length."); |
| | 182 | | } |
| | 183 | |
|
| 1549772 | 184 | | if (count < 0) |
| | 185 | | { |
| 4 | 186 | | throw new ArgumentOutOfRangeException($"{nameof(count)} cannot be less than 0."); |
| | 187 | | } |
| | 188 | |
|
| 1549768 | 189 | | if (offset + count > buffer.Length) |
| | 190 | | { |
| 4 | 191 | | throw new ArgumentOutOfRangeException($"{nameof(offset)} + {nameof(count)} cannot exceed {nameof(buffer) |
| | 192 | | } |
| 1549764 | 193 | | } |
| | 194 | |
|
| | 195 | | private static long GetBlobLength(Response<BlobDownloadInfo> response) |
| | 196 | | { |
| 292 | 197 | | string lengthString = response.Value.Details.ContentRange; |
| 292 | 198 | | string[] split = lengthString.Split('/'); |
| 292 | 199 | | return long.Parse(split[1], CultureInfo.InvariantCulture); |
| | 200 | | } |
| | 201 | |
|
| | 202 | | /// <inheritdoc/> |
| 0 | 203 | | public override bool CanRead => true; |
| | 204 | |
|
| | 205 | | /// <inheritdoc/> |
| 264 | 206 | | public override bool CanSeek => false; |
| | 207 | |
|
| | 208 | | /// <inheritdoc/> |
| 0 | 209 | | public override bool CanWrite => throw new NotSupportedException(); |
| | 210 | |
|
| 0 | 211 | | public override long Length => throw new NotSupportedException(); |
| | 212 | |
|
| | 213 | | /// <inheritdoc/> |
| | 214 | | public override long Position { |
| 0 | 215 | | get => _stream.Position; |
| 0 | 216 | | set => throw new NotSupportedException(); |
| | 217 | | } |
| | 218 | |
|
| | 219 | | /// <inheritdoc/> |
| | 220 | | public override void Flush() |
| | 221 | | { |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | /// <inheritdoc/> |
| | 225 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | 226 | | { |
| 0 | 227 | | return Task.CompletedTask; |
| | 228 | | } |
| | 229 | |
|
| | 230 | | /// <inheritdoc/> |
| | 231 | | public override long Seek(long offset, SeekOrigin origin) |
| | 232 | | { |
| 0 | 233 | | throw new NotSupportedException(); |
| | 234 | | } |
| | 235 | |
|
| | 236 | | /// <inheritdoc/> |
| | 237 | | public override void SetLength(long value) |
| | 238 | | { |
| 0 | 239 | | throw new NotSupportedException(); |
| | 240 | | } |
| | 241 | |
|
| | 242 | | /// <inheritdoc/> |
| | 243 | | public override void Write(byte[] buffer, int offset, int count) |
| | 244 | | { |
| 0 | 245 | | throw new NotSupportedException(); |
| | 246 | | } |
| | 247 | |
|
| 0 | 248 | | protected override void Dispose(bool disposing) => _stream.Dispose(); |
| | 249 | | } |
| | 250 | | } |