| | 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.Core.TestFramework |
| | 10 | | { |
| | 11 | | internal class AsyncValidatingStream : Stream |
| | 12 | | { |
| | 13 | | private readonly bool _isAsync; |
| | 14 | |
|
| | 15 | | private readonly Stream _innerStream; |
| | 16 | |
|
| 140 | 17 | | public AsyncValidatingStream(bool isAsync, Stream innerStream) |
| | 18 | | { |
| 140 | 19 | | _isAsync = isAsync; |
| 140 | 20 | | _innerStream = innerStream; |
| 140 | 21 | | } |
| | 22 | |
|
| | 23 | | public override void Flush() |
| | 24 | | { |
| 0 | 25 | | Validate(false); |
| 0 | 26 | | _innerStream.Flush(); |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | public override Task FlushAsync(CancellationToken cancellationToken) |
| | 30 | | { |
| 0 | 31 | | Validate(true); |
| 0 | 32 | | return _innerStream.FlushAsync(cancellationToken); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | private void Validate(bool isAsync) |
| | 36 | | { |
| 264 | 37 | | if (isAsync != _isAsync) |
| | 38 | | { |
| 2 | 39 | | throw new InvalidOperationException("All stream calls were expected to be " + (_isAsync ? "async" : "syn |
| | 40 | | } |
| 262 | 41 | | } |
| | 42 | |
|
| | 43 | | public override int Read(byte[] buffer, int offset, int count) |
| | 44 | | { |
| 132 | 45 | | Validate(false); |
| 132 | 46 | | return _innerStream.Read(buffer, offset, count); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = |
| | 50 | | { |
| 122 | 51 | | Validate(true); |
| 120 | 52 | | return _innerStream.ReadAsync(buffer, offset, count, cancellationToken); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public override long Seek(long offset, SeekOrigin origin) |
| | 56 | | { |
| 20 | 57 | | return _innerStream.Seek(offset, origin); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public override void SetLength(long value) |
| | 61 | | { |
| 0 | 62 | | _innerStream.SetLength(value); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public override void Write(byte[] buffer, int offset, int count) |
| | 66 | | { |
| 0 | 67 | | Validate(false); |
| 0 | 68 | | _innerStream.Write(buffer, offset, count); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = new |
| | 72 | | { |
| 0 | 73 | | Validate(true); |
| 0 | 74 | | return _innerStream.WriteAsync(buffer, offset, count, cancellationToken); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) |
| | 78 | | { |
| 10 | 79 | | Validate(true); |
| 10 | 80 | | return _innerStream.CopyToAsync(destination, bufferSize, cancellationToken); |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public override void Close() |
| | 84 | | { |
| 46 | 85 | | _innerStream.Close(); |
| 46 | 86 | | } |
| | 87 | |
|
| 10 | 88 | | public override bool CanRead => _innerStream.CanRead; |
| 90 | 89 | | public override bool CanSeek => _innerStream.CanSeek; |
| 0 | 90 | | public override bool CanWrite => _innerStream.CanWrite; |
| 40 | 91 | | public override long Length => _innerStream.Length; |
| | 92 | | public override long Position |
| | 93 | | { |
| 10 | 94 | | get => _innerStream.Position; |
| 0 | 95 | | set => _innerStream.Position = value; |
| | 96 | | } |
| | 97 | | } |
| | 98 | | } |