Ejemplo n.º 1
0
        public void TestBitWriter()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter byteW = new BinaryWriter(ms))
                {
                    BitWriter bw = new BitWriter(byteW);

                    bw.Write(0b0, 0);
                    bw.Write(0b1, 1);
                    bw.Write(0b00, 2);
                    bw.Write(0b111, 3);

                    bw.Write(0b0000, 4);
                    bw.Write(0b11111, 5);
                    bw.Write(0b000000, 6);
                    bw.Write(0b1111111, 7);
                    bw.Write(0b00000000, 8);
                    bw.Write(0b111111111, 9);
                    bw.Write(0b0000000000, 10);
                    bw.Write(0b11111111111, 11);
                    bw.Finish();
                }

                var result = string.Join(" ", ms.ToArray().Select(b => Convert.ToString(b, 2).PadLeft(8, '0')));

                Assert.AreEqual("10011100 00111110 00000111 11110000 00001111 11111000 00000001 11111111 11000000", result);
            }
        }
Ejemplo n.º 2
0
        public void Write(BinaryWriter w)
        {
            if (IsNull)
            {
                throw new HierarchyIdException("Instance cannot be Null");
            }
            if (w == null)
            {
                throw new ArgumentNullException(nameof(w));
            }
            BitWriter bw = new BitWriter(w);

            var nodes = this._imp.GetNodes();

            for (int i = 0; i < nodes.Length; i++)
            {
                var subNodes = nodes[i];
                for (int j = 0; j < subNodes.Length; j++)
                {
                    int val = subNodes[j];

                    BitPattern p = KnownPatterns.GetPatternByValue(val);

                    bool isLast = j == (subNodes.Length - 1);

                    ulong value = p.EncodeValue(val, isLast);

                    bw.Write(value, p.BitLength);
                }
            }

            bw.Finish();
        }
 public void Finish() => w.Finish();