Example #1
0
        public void Should_ReadBytes()
        {
            var bytes     = new byte[] { 0xFF, 0x01, 0xCC, 0xAA };
            var stream    = new BitStream(bytes);
            var reader    = new BitStreamReader(stream);
            var readBytes = new byte[4];

            reader.Read(readBytes, 0, readBytes.Length);
            CollectionAssert.AreEqual(bytes, readBytes);
        }
Example #2
0
        public void ShouldNot_ReadBitsAndBytes()
        {
            var bytes  = new byte[] { 0xFF, 0x01, 0xCC, 0xAA };
            var stream = new BitStream(bytes);

            stream.AllowUnalignedOperations = false;
            var reader    = new BitStreamReader(stream);
            var bit1      = reader.ReadBit();
            var bit2      = reader.ReadBit();
            var readBytes = new byte[4];

            Assert.Throws <StreamUnalignedException>(() => reader.Read(readBytes, 0, readBytes.Length));
        }
Example #3
0
        public void Should_ReadBitsAndBytes()
        {
            var bytes  = new byte[] { 0xFF, 0x01, 0xCC, 0xAA };
            var stream = new BitStream(bytes);

            stream.AllowUnalignedOperations = true;
            var reader = new BitStreamReader(stream);

            reader.ReadBit();
            reader.ReadBit();
            var readBytes = new byte[4];

            reader.Read(readBytes, 0, readBytes.Length);
            // all bits will be shifted by 2
            CollectionAssert.AreEqual(new byte[] { 127, 0, 179, 234 }, readBytes);
        }
        public void CompletelyRead()
        {
            var data   = new byte[] { 0x3c, 0x4b, 0x1a, 0x54, 0x22, 0x10, 0xaf, 0x89, 0x97, 0x65 };
            var stream = new MemoryStream(data);
            var reader = new BitStreamReader(stream);

            Assert.AreEqual(true, reader.CanRead);
            Assert.AreEqual(true, reader.CanSeek);
            Assert.AreEqual(false, reader.CanWrite);

            Assert.AreEqual(80, reader.BitLength);
            Assert.AreEqual(0, reader.BitPosition);
            Assert.AreEqual(10, reader.Length);
            Assert.AreEqual(0, reader.Position);

            Assert.AreEqual(false, reader.ReadBoolean());
            Assert.AreEqual(false, reader.ReadBoolean());
            Assert.AreEqual(true, reader.ReadBoolean());
            Assert.AreEqual(true, reader.ReadBoolean());
            Assert.AreEqual(true, reader.ReadBoolean());
            Assert.AreEqual(true, reader.ReadBoolean());
            Assert.AreEqual(false, reader.ReadBoolean());
            Assert.AreEqual(false, reader.ReadBoolean());

            Assert.AreEqual(0x4b, reader.ReadByte());

            var expected = new byte[8];

            Array.Copy(data, 2, expected, 0, 8);
            var result = new byte[8];
            int toRead = 8;

            while (toRead > 0)
            {
                int read = reader.Read(result, 8 - toRead, toRead);
                toRead -= read;
            }

            CollectionAssert.AreEqual(expected, result);

            Assert.AreEqual(80, reader.BitLength);
            Assert.AreEqual(80, reader.BitPosition);
            Assert.AreEqual(10, reader.Length);
            Assert.AreEqual(10, reader.Position);
        }
        public void SetPosition()
        {
            var data   = new byte[] { 0x3c, 0x4b, 0x1a, 0x54, 0x22, 0x10, 0xaf, 0x89, 0x97, 0x65 };
            var stream = new MemoryStream(data);
            var reader = new BitStreamReader(stream);

            Assert.AreEqual(true, reader.CanRead);
            Assert.AreEqual(true, reader.CanSeek);
            Assert.AreEqual(false, reader.CanWrite);

            Assert.AreEqual(80, reader.BitLength);
            Assert.AreEqual(0, reader.BitPosition);
            Assert.AreEqual(10, reader.Length);
            Assert.AreEqual(0, reader.Position);

            reader.Position = 7;

            Assert.AreEqual(56, reader.BitPosition);
            Assert.AreEqual(7, reader.Position);

            Assert.AreEqual(0x89, reader.ReadByte());
            Assert.AreEqual(true, reader.ReadBoolean());

            Assert.AreEqual(7, reader.Position = 7);

            Assert.AreEqual(true, reader.ReadBoolean());
            Assert.AreEqual(0x13, reader.ReadByte());

            var buffer = new byte[10];

            Assert.AreEqual(1, reader.Read(buffer, 0, 10));
            Assert.AreEqual(0x2e, buffer[0]);
            Assert.AreEqual(-1, reader.ReadByte());

            Assert.AreEqual(73, reader.BitPosition);
            Assert.AreEqual(9, reader.Position);
        }
        public void LeaveOpen_False_ObjectDisposed()
        {
            var data   = new byte[] { 0x3c, 0x4b, 0x1a, 0x54, 0x22, 0x10, 0xaf, 0x89, 0x97, 0x65 };
            var stream = new MemoryStream(data);

            using (var reader = new BitStreamReader(stream, false))
            {
                Assert.AreEqual(true, reader.CanRead);
                Assert.AreEqual(true, reader.CanSeek);
                Assert.AreEqual(false, reader.CanWrite);

                Assert.AreEqual(80, reader.BitLength);
                Assert.AreEqual(0, reader.BitPosition);
                Assert.AreEqual(10, reader.Length);
                Assert.AreEqual(0, reader.Position);

                reader.Seek(7, SeekOrigin.Begin);

                Assert.AreEqual(56, reader.BitPosition);
                Assert.AreEqual(7, reader.Position);

                Assert.AreEqual(0x89, reader.ReadByte());
                Assert.AreEqual(true, reader.ReadBoolean());

                Assert.AreEqual(7, reader.Position = 7);

                Assert.AreEqual(true, reader.ReadBoolean());
                Assert.AreEqual(0x13, reader.ReadByte());

                var buffer = new byte[10];
                Assert.AreEqual(1, reader.Read(buffer, 0, 10));
                Assert.AreEqual(0x2e, buffer[0]);
                Assert.AreEqual(-1, reader.ReadByte());

                Assert.AreEqual(73, reader.BitPosition);
                Assert.AreEqual(9, reader.Position);
            }
            stream.Position = 0;
            using (var reader = new BitStreamReader(stream, false))
            {
                Assert.AreEqual(true, reader.CanRead);
                Assert.AreEqual(true, reader.CanSeek);
                Assert.AreEqual(false, reader.CanWrite);

                Assert.AreEqual(80, reader.BitLength);
                Assert.AreEqual(0, reader.BitPosition);
                Assert.AreEqual(10, reader.Length);
                Assert.AreEqual(0, reader.Position);

                reader.Seek(7, SeekOrigin.Begin);

                Assert.AreEqual(56, reader.BitPosition);
                Assert.AreEqual(7, reader.Position);

                Assert.AreEqual(0x89, reader.ReadByte());
                Assert.AreEqual(true, reader.ReadBoolean());

                Assert.AreEqual(7, reader.Position = 7);

                Assert.AreEqual(true, reader.ReadBoolean());
                Assert.AreEqual(0x13, reader.ReadByte());

                var buffer = new byte[10];
                Assert.AreEqual(1, reader.Read(buffer, 0, 10));
                Assert.AreEqual(0x2e, buffer[0]);
                Assert.AreEqual(-1, reader.ReadByte());

                Assert.AreEqual(73, reader.BitPosition);
                Assert.AreEqual(9, reader.Position);
            }
        }