|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | + |
| 4 | +namespace Test { |
| 5 | + /// <summary>Stream that can return fewer bytes than |
| 6 | + /// requested.</summary> |
| 7 | + public sealed class DelayingStream : Stream |
| 8 | + { |
| 9 | + private readonly Stream ms; |
| 10 | + |
| 11 | + /// <summary>Initializes a new instance of the |
| 12 | + /// <see cref='DelayingStream'/> class.</summary> |
| 13 | + /// <param name='ms'/> |
| 14 | + /// <exception cref='ArgumentNullException'>The parameter <paramref |
| 15 | + /// name='ms'/> is null.</exception> |
| 16 | + public DelayingStream(Stream ms) { |
| 17 | + this.ms = ms ?? throw new ArgumentNullException(nameof(ms)); |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary>Initializes a new instance of the |
| 21 | + /// <see cref='DelayingStream'/> class.</summary> |
| 22 | + /// <param name='bytes'/> |
| 23 | + public DelayingStream(byte[] bytes) : this(new MemoryStream(bytes)) { |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary>Initializes a new instance of the |
| 27 | + /// <see cref='DelayingStream'/> class.</summary> |
| 28 | + public DelayingStream() : this(new MemoryStream()) { |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary>Initializes a new instance of the |
| 32 | + /// <see cref='DelayingStream'/> class.</summary> |
| 33 | + /// <param name='size'/> |
| 34 | + public DelayingStream(int size) : this(new MemoryStream(size)) { |
| 35 | + } |
| 36 | + |
| 37 | + public new void Dispose() { |
| 38 | + this.ms.Dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + public override long Length => this.ms.Length; |
| 43 | + |
| 44 | + /// <inheritdoc/> |
| 45 | + /// <returns/> |
| 46 | + /// <param name='pos'>Not documented yet.</param> |
| 47 | + /// <param name='origin'>Not documented yet.</param> |
| 48 | + public override long Seek(long pos, SeekOrigin origin) { |
| 49 | + return this.ms.Seek(pos, origin); |
| 50 | + } |
| 51 | + |
| 52 | + /// <inheritdoc/> |
| 53 | + /// <param name='len'>Not documented yet.</param> |
| 54 | + public override void SetLength(long len) { |
| 55 | + this.ms.SetLength(len); |
| 56 | + } |
| 57 | + |
| 58 | + /// <inheritdoc/> |
| 59 | + public override long Position |
| 60 | + { |
| 61 | + get => this.ms.Position; |
| 62 | + |
| 63 | + set => this.ms.Position = value; |
| 64 | + } |
| 65 | + |
| 66 | + public byte[] ToArray() { |
| 67 | + var ms = this.ms as MemoryStream; |
| 68 | + return ms != null ? ms.ToArray() : throw new NotSupportedException(); |
| 69 | + } |
| 70 | + |
| 71 | + /// <inheritdoc/> |
| 72 | + public override bool CanRead => this.ms.CanRead; |
| 73 | + |
| 74 | + /// <inheritdoc/> |
| 75 | + public override bool CanSeek => this.ms.CanSeek; |
| 76 | + |
| 77 | + /// <inheritdoc/> |
| 78 | + public override bool CanWrite => this.ms.CanWrite; |
| 79 | + |
| 80 | + /// <inheritdoc/> |
| 81 | + /// <returns/> |
| 82 | + /// <param name='bytes'>Not documented yet.</param> |
| 83 | + /// <param name='offset'>Not documented yet.</param> |
| 84 | + /// <param name='count'>Not documented yet.</param> |
| 85 | + /// <exception cref='ArgumentNullException'>The parameter <paramref |
| 86 | + /// name='bytes'/> is null.</exception> |
| 87 | + public override int Read(byte[] bytes, int offset, int count) { |
| 88 | + if (bytes == null) { |
| 89 | + throw new ArgumentNullException(nameof(bytes)); |
| 90 | + } |
| 91 | + if (offset < 0) { |
| 92 | + throw new ArgumentException("\"offset\" (" + offset + ") is not" + |
| 93 | +"\u0020greater or equal to 0"); |
| 94 | + } |
| 95 | + if (offset > bytes.Length) { |
| 96 | + throw new ArgumentException("\"offset\" (" + offset + ") is not less" + |
| 97 | +"\u0020or equal to " + bytes.Length); |
| 98 | + } |
| 99 | + if (count < 0) { |
| 100 | + throw new ArgumentException(" (" + count + ") is not greater or" + |
| 101 | +"\u0020equal to 0"); |
| 102 | + } |
| 103 | + if (count > bytes.Length) { |
| 104 | + throw new ArgumentException(" (" + count + ") is not less or equal" + |
| 105 | +"\u0020to " + bytes.Length); |
| 106 | + } |
| 107 | + if (bytes.Length - offset < count) { |
| 108 | + throw new ArgumentException("\"bytes\" + \"'s length minus \" +" + |
| 109 | +"\u0020offset (" + (bytes.Length - offset) + ") is not greater or equal to " + |
| 110 | +count); |
| 111 | + } |
| 112 | + if (count == 0) { |
| 113 | + return 0; |
| 114 | + } |
| 115 | + int b = this.ms.ReadByte(); |
| 116 | + if (b < 0) { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + bytes[offset] = (byte)b; |
| 120 | + return 1; |
| 121 | + } |
| 122 | + |
| 123 | + /// <inheritdoc/> |
| 124 | + public override void Flush() { |
| 125 | + this.ms.Flush(); |
| 126 | + } |
| 127 | + |
| 128 | + /// <inheritdoc/> |
| 129 | + /// <param name='bytes'>Not documented yet.</param> |
| 130 | + /// <param name='offset'>Not documented yet.</param> |
| 131 | + /// <param name='count'>Not documented yet.</param> |
| 132 | + public override void Write(byte[] bytes, int offset, int count) { |
| 133 | + this.ms.Write(bytes, offset, count); |
| 134 | + } |
| 135 | + |
| 136 | + /// <inheritdoc/> |
| 137 | + /// <param name='c'>Not documented yet.</param> |
| 138 | + public override void WriteByte(byte c) { |
| 139 | + this.ms.WriteByte(c); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments