| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.IO; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | |
|
| | 9 | | namespace Azure.Storage |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Wraps a stream, and reports position updates to a progress incrementer |
| | 13 | | /// </summary> |
| | 14 | | internal class ProgressIncrementingStream : Stream |
| | 15 | | { |
| | 16 | | private readonly Stream _innerStream; |
| | 17 | | private readonly AggregatingProgressIncrementer _incrementer; |
| | 18 | |
|
| 4 | 19 | | public ProgressIncrementingStream(Stream stream, AggregatingProgressIncrementer incrementer) |
| | 20 | | { |
| 4 | 21 | | _innerStream = stream ?? throw Errors.ArgumentNull(nameof(stream)); |
| 4 | 22 | | _incrementer = incrementer ?? throw Errors.ArgumentNull(nameof(incrementer)); |
| 4 | 23 | | } |
| | 24 | |
|
| 2 | 25 | | public override bool CanRead => _innerStream.CanRead; |
| | 26 | |
|
| 12 | 27 | | public override bool CanSeek => _innerStream.CanSeek; |
| | 28 | |
|
| 0 | 29 | | public override bool CanTimeout => _innerStream.CanTimeout; |
| | 30 | |
|
| 0 | 31 | | public override bool CanWrite => _innerStream.CanWrite; |
| | 32 | |
|
| 0 | 33 | | protected override void Dispose(bool disposing) => _innerStream.Dispose(); |
| | 34 | |
|
| | 35 | | public override async Task FlushAsync(CancellationToken cancellationToken) |
| | 36 | | { |
| 0 | 37 | | var oldPosition = _innerStream.Position; |
| | 38 | |
|
| 0 | 39 | | await _innerStream.FlushAsync(cancellationToken).ConfigureAwait(false); |
| | 40 | |
|
| 0 | 41 | | var newPosition = _innerStream.Position; |
| | 42 | |
|
| 0 | 43 | | _incrementer.Report(newPosition - oldPosition); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public override void Flush() |
| | 47 | | { |
| 0 | 48 | | var oldPosition = _innerStream.Position; |
| | 49 | |
|
| 0 | 50 | | _innerStream.Flush(); |
| | 51 | |
|
| 0 | 52 | | var newPosition = _innerStream.Position; |
| | 53 | |
|
| 0 | 54 | | _incrementer.Report(newPosition - oldPosition); |
| 0 | 55 | | } |
| | 56 | |
|
| 12 | 57 | | public override long Length => _innerStream.Length; |
| | 58 | |
|
| | 59 | | public override long Position |
| | 60 | | { |
| 4 | 61 | | get => _innerStream.Position; |
| | 62 | |
|
| | 63 | | set |
| | 64 | | { |
| 0 | 65 | | var delta = value - _innerStream.Position; |
| | 66 | |
|
| 0 | 67 | | _innerStream.Position = value; |
| | 68 | |
|
| 0 | 69 | | _incrementer.Report(delta); |
| 0 | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public override int Read(byte[] buffer, int offset, int count) |
| | 74 | | { |
| 12 | 75 | | var n = _innerStream.Read(buffer, offset, count); |
| 12 | 76 | | _incrementer.Report(n); |
| 12 | 77 | | return n; |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationTo |
| | 81 | | { |
| 4 | 82 | | var n = await _innerStream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); |
| 4 | 83 | | _incrementer.Report(n); |
| 4 | 84 | | return n; |
| 4 | 85 | | } |
| | 86 | |
|
| | 87 | | public override int ReadByte() |
| | 88 | | { |
| 0 | 89 | | var b = _innerStream.ReadByte(); |
| | 90 | |
|
| 0 | 91 | | if (b != -1) // -1 = end of stream sentinel |
| | 92 | | { |
| 0 | 93 | | _incrementer.Report(1); |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | return b; |
| | 97 | | } |
| | 98 | |
|
| | 99 | | public override int ReadTimeout |
| | 100 | | { |
| 0 | 101 | | get => _innerStream.ReadTimeout; |
| | 102 | |
|
| 0 | 103 | | set => _innerStream.ReadTimeout = value; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public override long Seek(long offset, SeekOrigin origin) |
| | 107 | | { |
| 8 | 108 | | var oldPosition = _innerStream.Position; |
| | 109 | |
|
| 8 | 110 | | var newPosition = _innerStream.Seek(offset, origin); |
| | 111 | |
|
| 8 | 112 | | _incrementer.Report(newPosition - oldPosition); |
| | 113 | |
|
| 8 | 114 | | return newPosition; |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | public override void SetLength(long value) => _innerStream.SetLength(value); |
| | 118 | |
|
| | 119 | | public override void Write(byte[] buffer, int offset, int count) |
| | 120 | | { |
| 0 | 121 | | _innerStream.Write(buffer, offset, count); |
| | 122 | |
|
| 0 | 123 | | _incrementer.Report(count); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| | 127 | | { |
| 0 | 128 | | await _innerStream.WriteAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false); |
| | 129 | |
|
| 0 | 130 | | _incrementer.Report(count); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public override void WriteByte(byte value) |
| | 134 | | { |
| 0 | 135 | | _innerStream.WriteByte(value); |
| | 136 | |
|
| 0 | 137 | | _incrementer.Report(1); |
| 0 | 138 | | } |
| | 139 | |
|
| | 140 | | public override int WriteTimeout |
| | 141 | | { |
| 0 | 142 | | get => _innerStream.WriteTimeout; |
| | 143 | |
|
| 0 | 144 | | set => _innerStream.WriteTimeout = value; |
| | 145 | | } |
| | 146 | | } |
| | 147 | | } |