using System; using System.IO; namespace Susurrus.UnitTests { /// /// A forward-only stream that hands out at most a few bytes per read, as a network or compressed stream can. /// public class TrickleStream : Stream { private readonly byte[] data; private readonly int bytesPerRead; private int position; public TrickleStream(byte[] data, int bytesPerRead) { this.data = data; this.bytesPerRead = bytesPerRead; } public override bool CanRead => true; public override bool CanSeek => false; public override bool CanWrite => false; public override long Length => throw new NotSupportedException(); public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } public override int Read(byte[] buffer, int offset, int count) { int read = Math.Min(Math.Min(count, bytesPerRead), data.Length - position); Buffer.BlockCopy(data, position, buffer, offset, read); position += read; return read; } public override void Flush() { } public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } public override void SetLength(long value) { throw new NotSupportedException(); } public override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); } } }