Exemplo n.º 1
0
 T IFileFormat <T> .Read(Stream stream)
 {
     using (var br = new BitsReader(stream))
     {
         return(_readFunc(br));
     }
 }
        public void ReadDoubleSuccess()
        {
            var bytes  = BitConverter.GetBytes(Math.PI);
            var result = new BitsReader(bytes).ReadDouble();

            Assert.AreEqual(Math.PI, result);
        }
        public void ReadFloatSuccess()
        {
            const float Pi     = 3.1415926f;
            var         bytes  = BitConverter.GetBytes(Pi);
            var         result = new BitsReader(bytes).ReadFloat();

            Assert.AreEqual(Pi, result);
        }
Exemplo n.º 4
0
        public void ReadShouldThrow(byte[] input)
        {
            var reader = new BitsReader(input);

            for (int i = 0; i < 2; ++i)
            {
                reader.ReadUInt64(64);
            }
        }
Exemplo n.º 5
0
 private static void ReadMeExample1()
 {
     byte[] bytes  = { 25, 26, 27, 28, 29, 30, 31 };
     var    reader = new BitsReader(bytes);
     byte   A      = reader.ReadUInt8(4);
     short  B      = reader.ReadInt16(12);
     int    C      = reader.ReadInt32(24);
     ushort D      = reader.ReadUInt16(13);
     sbyte  E      = reader.ReadInt8(3);
 }
Exemplo n.º 6
0
        public void ReadBytesSuccessfully()
        {
            var result = new List <ulong>(mResult.Length);
            var reader = new BitsReader(mInput);

            for (int i = 1; i < 65; i++)
            {
                result.Add(reader.ReadUInt64(i));
                result.Add(reader.ReadUInt64(i));
            }

            Assert.AreEqual(mResult, result.ToArray());
        }
Exemplo n.º 7
0
        public void ResetSuccess()
        {
            var reader = new BitsReader(new byte[] { 86, 8 });

            reader.ReadInt16(12);

            Assert.AreNotEqual(0, reader.BitsRead);

            reader.Reset(new byte[] { 240 });

            Assert.AreEqual(0, reader.BitsRead);

            reader.ReadInt8(5);
        }
Exemplo n.º 8
0
        private static void BitsReaderSample()
        {
            byte[] buffer1 = { 25, 26, 27, 28, 29, 30, 31, 32 };

            var reader = new BitsReader(buffer1);

            sbyte b  = reader.ReadInt8();
            sbyte b1 = reader.ReadInt8(1);
            sbyte b3 = reader.ReadInt8(3);
            sbyte b4 = reader.ReadInt8(4);

            short s   = reader.ReadInt16();
            short s1  = reader.ReadInt16(1);
            short s2  = reader.ReadInt16(2);
            short s4  = reader.ReadInt16(4);
            short s5  = reader.ReadInt16(5);
            short s8  = reader.ReadInt16(8);
            short s12 = reader.ReadInt16(12);

            //receive new data in buffer1
            reader.Reset(buffer1);

            int i1  = reader.ReadInt32(1);
            int i7  = reader.ReadInt32(7);
            int i24 = reader.ReadInt32(24);
            int i   = reader.ReadInt32();

            //receive new data in buffer1
            reader.Reset(buffer1);

            long l1  = reader.ReadInt64(1);
            long l3  = reader.ReadInt64(3);
            long l24 = reader.ReadInt64(24);
            long l36 = reader.ReadInt64(36);

            //receive new data in buffer1
            reader.Reset(buffer1);

            long l = reader.ReadInt64();
        }
Exemplo n.º 9
0
        public void CanConvertBitArrayToStruct()
        {
            var endianness = Endianness.LittleEndian;
            var bits       = new []
            {
                true, true, true,
                false, false, false,
                true, false, true, false, true, false, true, false, true, false, true, false, true, false, true,
                false, true, false, true, false,
                true, true, true, false, false, false,
            };
            var values = new List <uint>();

            using (var ms = new MemoryStream(new byte[] { 0xE2, 0xAA, 0xAA, 0xB8 }))
            {
                using (var br = new BitsReader(ms))
                {
                    foreach (var size in new ulong[] { 3, 3, 15, 5, 6 })
                    {
                        values.Add(br.ReadStruct <uint>(size, endianness));
                    }
                }
            }
            Assert.AreEqual(0x7, values[0]);
            Assert.AreEqual(0x0, values[1]);
            if (endianness == Endianness.BigEndian)
            {
                Assert.AreEqual(0x5555, values[2]);
            }
            else
            {
                Assert.AreEqual(0x55AA, values[2]);
            }
            Assert.AreEqual(0xA, values[3]);
            Assert.AreEqual(0x38, values[4]);
        }