SubStream.Read C# (CSharp) Method

Read() public method

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int
    public override int Read(byte[] buffer, int offset, int count)
    {
        int size = count;
        if (Position + size > Length)
            size = (int)(Length-Position);
        stream.Position = Position + streamOffset;
        stream.Read(buffer, offset, size);
        Position = Position + size;
        return size;
    }

Usage Example

Ejemplo n.º 1
0
        public void MustReadCorrectly()
        {
            var position = 750L;
            var sut      = new SubStream(stream.Object, 100, 200);

            stream.SetupGet(s => s.Position).Returns(position);
            stream.SetupSet(s => s.Position = It.IsAny <long>()).Callback <long>(p => position = p);
            stream.Setup(s => s.Read(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())).Returns <byte[], int, int>((a, o, c) => c);
            sut.Position = 50;

            var bytesRead = sut.Read(new byte[25], 0, 25);

            stream.Verify(s => s.Read(It.IsAny <byte[]>(), 0, 25), Times.Once);

            Assert.AreEqual(25, bytesRead);
            Assert.AreEqual(75, sut.Position);
            Assert.AreEqual(750, position);

            sut.Position = 150;
            bytesRead    = sut.Read(new byte[75], 0, 75);

            stream.Verify(s => s.Read(It.IsAny <byte[]>(), 0, 50), Times.Once);

            Assert.AreEqual(50, bytesRead);
            Assert.AreEqual(200, sut.Position);
            Assert.AreEqual(750, position);
        }
All Usage Examples Of SubStream::Read