|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace SharpCompress.Test.Mocks; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A stream wrapper that throws NotSupportedException on Flush() calls. |
| 10 | +/// This is used to test that archive iteration handles streams that don't support flushing. |
| 11 | +/// </summary> |
| 12 | +public class ThrowOnFlushStream : Stream |
| 13 | +{ |
| 14 | + private readonly Stream inner; |
| 15 | + |
| 16 | + public ThrowOnFlushStream(Stream inner) |
| 17 | + { |
| 18 | + this.inner = inner; |
| 19 | + } |
| 20 | + |
| 21 | + public override bool CanRead => inner.CanRead; |
| 22 | + |
| 23 | + public override bool CanSeek => false; |
| 24 | + |
| 25 | + public override bool CanWrite => false; |
| 26 | + |
| 27 | + public override long Length => throw new NotSupportedException(); |
| 28 | + |
| 29 | + public override long Position |
| 30 | + { |
| 31 | + get => throw new NotSupportedException(); |
| 32 | + set => throw new NotSupportedException(); |
| 33 | + } |
| 34 | + |
| 35 | + public override void Flush() => throw new NotSupportedException("Flush not supported"); |
| 36 | + |
| 37 | + public override Task FlushAsync(CancellationToken cancellationToken) => |
| 38 | + throw new NotSupportedException("FlushAsync not supported"); |
| 39 | + |
| 40 | + public override int Read(byte[] buffer, int offset, int count) => |
| 41 | + inner.Read(buffer, offset, count); |
| 42 | + |
| 43 | + public override Task<int> ReadAsync( |
| 44 | + byte[] buffer, |
| 45 | + int offset, |
| 46 | + int count, |
| 47 | + CancellationToken cancellationToken |
| 48 | + ) => inner.ReadAsync(buffer, offset, count, cancellationToken); |
| 49 | + |
| 50 | +#if !NETFRAMEWORK && !NETSTANDARD2_0 |
| 51 | + public override ValueTask<int> ReadAsync( |
| 52 | + Memory<byte> buffer, |
| 53 | + CancellationToken cancellationToken = default |
| 54 | + ) => inner.ReadAsync(buffer, cancellationToken); |
| 55 | +#endif |
| 56 | + |
| 57 | + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| 58 | + |
| 59 | + public override void SetLength(long value) => throw new NotSupportedException(); |
| 60 | + |
| 61 | + public override void Write(byte[] buffer, int offset, int count) => |
| 62 | + throw new NotSupportedException(); |
| 63 | + |
| 64 | + protected override void Dispose(bool disposing) |
| 65 | + { |
| 66 | + if (disposing) |
| 67 | + { |
| 68 | + inner.Dispose(); |
| 69 | + } |
| 70 | + |
| 71 | + base.Dispose(disposing); |
| 72 | + } |
| 73 | +} |
0 commit comments