Beispiel #1
0
        public void TestPosition()
        {
            using (MemoryStream baseStream = new MemoryStream(_buff)) {
                using (BoundedStream boundedStream = new BoundedStream(baseStream, 10, 100)) {
                    Assert.AreEqual(0, boundedStream.Position);

                    boundedStream.ReadByte();
                    Assert.AreEqual(1, boundedStream.Position);

                    boundedStream.Position = 0;
                    Assert.AreEqual(0, boundedStream.Position);

                    Assert.ThrowsException <IOException>(() => boundedStream.Position = -1);
                    Assert.AreEqual(0, boundedStream.Position);

                    Assert.ThrowsException <IOException>(() => boundedStream.Position = boundedStream.UpperBound - boundedStream.LowerBound + 1);
                    Assert.AreEqual(0, boundedStream.Position);

                    Assert.ThrowsException <IOException>(() => boundedStream.Seek(-1, SeekOrigin.Begin));
                    Assert.AreEqual(0, boundedStream.Position);

                    Assert.ThrowsException <IOException>(() => boundedStream.Seek(-1, SeekOrigin.End));
                    Assert.AreEqual(0, boundedStream.Position);

                    Assert.ThrowsException <IOException>(() => boundedStream.Seek(-1, SeekOrigin.Current));
                    Assert.AreEqual(0, boundedStream.Position);

                    boundedStream.Seek(1, SeekOrigin.Begin);
                    Assert.AreEqual(1, boundedStream.Position);
                }
            }
        }
Beispiel #2
0
        internal override void DeserializeOverride(BoundedStream stream, EventShuttle eventShuttle)
        {
            /* This is weird but we need to find the base stream so we can reference it directly */
            Stream baseStream = stream;

            while (baseStream is BoundedStream)
            {
                baseStream = (baseStream as BoundedStream).Source;
            }

            var length = GetFieldLength();

            Value = length != null
                ? new Streamlet(baseStream, baseStream.Position, length.Value)
                : new Streamlet(baseStream, baseStream.Position);

            if (length != null)
            {
                stream.Seek(length.Value, SeekOrigin.Current);
            }
            else
            {
                stream.Seek(0, SeekOrigin.End);
            }
        }
Beispiel #3
0
        public void TestSeekForwardFromEnd()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 1, 6);

            bs.Seek(1, SeekOrigin.End);
        }
Beispiel #4
0
        public void TestSeekBackFromBegin()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 1, 6);

            bs.Seek(-1, SeekOrigin.Begin);
        }
Beispiel #5
0
        public void TestSeekTooFarForwardFromCurrent()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 1, 6);

            bs.Position = 3;
            bs.Seek(5, SeekOrigin.Current);
        }
Beispiel #6
0
        public void TestSeekTooFarBackFromCurrent()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 1, 7);

            bs.Position = 4;
            bs.Seek(-5, SeekOrigin.Current);
        }
Beispiel #7
0
        internal override void DeserializeOverride(BoundedStream stream, EventShuttle eventShuttle)
        {
            var rootStream = GetRootStream(stream);

            var length = GetFieldLength();

            Value = length != null
                ? new Streamlet(rootStream, rootStream.Position, length.Value)
                : new Streamlet(rootStream, rootStream.Position);

            if (length != null)
            {
                stream.Seek(length.Value, SeekOrigin.Current);
            }
            else
            {
                stream.Seek(0, SeekOrigin.End);
            }
        }
Beispiel #8
0
        public void TestSeekFromEnd()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 1, 6);

            bs.Seek(-2, SeekOrigin.End);

            var b = bs.ReadByte();

            Assert.AreEqual(5, b);
        }
Beispiel #9
0
        public void TestSeekFromBegining()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 1, 6);

            bs.Seek(2, SeekOrigin.Begin);

            var b = bs.ReadByte();

            Assert.AreEqual(3, b);
        }
Beispiel #10
0
        public void TestSeekBackFromCurrent()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 1, 6);

            bs.Position = 3;
            bs.Seek(-1, SeekOrigin.Current);

            var b = bs.ReadByte();

            Assert.AreEqual(3, b);
        }
Beispiel #11
0
    internal override void DeserializeOverride(BoundedStream stream, EventShuttle eventShuttle)
    {
        var rootStream = GetRootStream(stream);

        var length = GetFieldLength();

        Value = length != null
            ? new Streamlet(rootStream, rootStream.Position, length.ByteCount)
            : new Streamlet(rootStream, rootStream.Position);

        if (length != null)
        {
            var nullStream = new NullStream();
            stream.CopyTo(nullStream, (int)length.ByteCount, CopyToBufferSize);
        }
        else
        {
            stream.Seek(0, SeekOrigin.End);
        }
    }
Beispiel #12
0
        public void TestRead()
        {
            using (MemoryStream baseStream = new MemoryStream(_buff)) {
                using (BoundedStream boundedStream = new BoundedStream(baseStream, 10, 100)) {
                    Assert.AreEqual((boundedStream.Position + boundedStream.LowerBound) % 256, boundedStream.ReadByte() % 256);
                    Assert.AreEqual((boundedStream.Position + boundedStream.LowerBound) % 256, boundedStream.ReadByte() % 256);

                    // Partial reads to upper bound
                    boundedStream.Seek(1, SeekOrigin.End);
                    byte[] buff = new byte[2] {
                        0, 200
                    };
                    byte[] expectedBuff = new byte[2] {
                        99, 200
                    };

                    boundedStream.Read(buff, 0, buff.Length);
                    CollectionAssert.AreEqual(expectedBuff, buff);
                }
            }
        }
Beispiel #13
0
    internal override async Task DeserializeOverrideAsync(BoundedStream stream, EventShuttle eventShuttle,
                                                          CancellationToken cancellationToken)
    {
        var rootStream = GetRootStream(stream);

        var length = GetFieldLength();

        Value = length != null
            ? new Streamlet(rootStream, rootStream.Position, length.ByteCount)
            : new Streamlet(rootStream, rootStream.Position);

        if (length != null)
        {
            var nullStream = new NullStream();
            await stream.CopyToAsync(nullStream, (int)length.ByteCount, CopyToBufferSize, cancellationToken)
            .ConfigureAwait(false);
        }
        else
        {
            stream.Seek(0, SeekOrigin.End);
        }
    }