Esempio n. 1
0
        private void Align(BoundedStream stream, long?alignment, bool pad = false)
        {
            if (alignment == null)
            {
                throw new ArgumentNullException(nameof(alignment));
            }

            var position = stream.RelativePosition;
            var delta    = (alignment.Value - position % alignment.Value) % alignment.Value;

            if (delta == 0)
            {
                return;
            }

            if (pad)
            {
                var padding = new byte[delta];
                stream.Write(padding, 0, padding.Length);
            }
            else
            {
                for (int i = 0; i < delta; i++)
                {
                    if (stream.ReadByte() < 0)
                    {
                        break;
                    }
                }
            }
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
0
        private static void Seek(BoundedStream BoundedStream)
        {
            while (true)
            {
                var Byte = BoundedStream.ReadByte();

                switch (Byte)
                {
                case 0b00000000:
                    return;

                case 0b11000000:
                    BoundedStream.ReadByte();
                    return;
                }
            }
        }
Esempio n. 4
0
        public void TestReadingWorks()
        {
            var ms = GetPopulatedMemoryStream(10);

            var bs = new BoundedStream(ms, 5, 3);

            var b = bs.ReadByte();

            Assert.AreEqual(5, b);
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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);
        }
Esempio n. 8
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);
                }
            }
        }