Exemplo n.º 1
0
        public void BufferListStreamTest()
        {
            byte[] buffer = new byte[256];
            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] = (byte)i;
            }

            ArraySegment <byte>[] segments = new ArraySegment <byte>[]
            {
                new ArraySegment <byte>(buffer, 0, 7),
                new ArraySegment <byte>(buffer, 7, 14),
                new ArraySegment <byte>(buffer, 21, 28),
                new ArraySegment <byte>(buffer, 49, 62),
                new ArraySegment <byte>(buffer, 111, 88),
                new ArraySegment <byte>(buffer, 199, 55),
                new ArraySegment <byte>(buffer, 254, 2),
            };

            BufferListStream stream = new BufferListStream(segments);

            Assert.True(stream.CanRead);
            Assert.True(stream.CanSeek);
            Assert.True(!stream.CanWrite);
            Assert.Equal(buffer.Length, stream.Length);

            stream.Seek(119, SeekOrigin.Begin);
            Assert.Equal(119, stream.Position);
            Assert.Equal(119, stream.ReadByte());

            stream.Seek(256, SeekOrigin.Begin);
            Assert.Equal(-1, stream.ReadByte());

            stream.Seek(-1, SeekOrigin.Current);
            Assert.Equal(255, stream.ReadByte());

            stream.Seek(-256, SeekOrigin.End);
            Assert.Equal(0, stream.ReadByte());

            try
            {
                stream.Seek(-198, SeekOrigin.Current);
                Assert.True(false, "Seek should fail with argument out of range exception");
            }
            catch (ArgumentOutOfRangeException)
            {
            }

            stream.Position = 120;

            // The position is 120 now
            stream.Seek(99, SeekOrigin.Current);
            Assert.Equal(219, stream.Position);
            Assert.Equal(219, stream.ReadByte());

            // The position is 220 now
            stream.Seek(-177, SeekOrigin.Current);
            Assert.Equal(43, stream.Position);
            Assert.Equal(43, stream.ReadByte());

            stream.Seek(0, SeekOrigin.Begin);
            for (int i = 0; i < buffer.Length; i++)
            {
                Assert.Equal(i, stream.Position);
                Assert.Equal(i, stream.ReadByte());
            }
            Assert.Equal(-1, stream.ReadByte());

            stream.Seek(25, SeekOrigin.Begin);
            byte[] tempBuffer = new byte[86];
            int    count      = stream.Read(tempBuffer, 0, tempBuffer.Length);

            Assert.Equal(tempBuffer.Length, count);
            Assert.Equal(111, stream.Position);
            Assert.Equal(111, stream.ReadByte());
            for (int i = 0; i < tempBuffer.Length; i++)
            {
                Assert.Equal(i + 25, tempBuffer[i]);
            }

            stream.Seek(25, SeekOrigin.Begin);
            tempBuffer = new byte[255];
            count      = stream.Read(tempBuffer, 0, tempBuffer.Length);
            Assert.Equal(231, count);
            Assert.Equal(-1, stream.ReadByte());

            stream.Seek(25, SeekOrigin.Begin);
            bool more = false;

            ArraySegment <byte>[] buffers = stream.ReadBuffers(229, true, out more);
            Assert.True(more);
            Assert.Equal(4, buffers.Length);
            Assert.Equal(24, buffers[0].Count);
            Assert.Equal(62, buffers[1].Count);
            Assert.Equal(88, buffers[2].Count);
            Assert.Equal(55, buffers[3].Count);
            Assert.Equal(254, stream.Position);

            stream.Seek(25, SeekOrigin.Begin);
            more    = false;
            buffers = stream.ReadBuffers(int.MaxValue, true, out more);
            Assert.False(more);
            Assert.Equal(5, buffers.Length);
            Assert.Equal(24, buffers[0].Count);
            Assert.Equal(62, buffers[1].Count);
            Assert.Equal(88, buffers[2].Count);
            Assert.Equal(55, buffers[3].Count);
            Assert.Equal(2, buffers[4].Count);
            Assert.Equal(256, stream.Position);

            stream.Seek(25, SeekOrigin.Begin);
            more    = false;
            buffers = stream.ReadBuffers(231, false, out more);
            Assert.False(more);
            Assert.Equal(25, stream.Position);

            stream.Dispose();
            try
            {
                stream.Position = 100;
                Assert.True(false, "Stream is disposed!!");
            }
            catch (ObjectDisposedException)
            {
            }
        }