public static void Serialize_Class(object obj, BitStreamWriter writer)
 {
     if (obj == null)
     {
         writer.WriteBit(false);
         return;
     }
     writer.WriteBit(true);
     NetSerializer.Serialize(obj, writer);
 }
Beispiel #2
0
        public void ShouldNot_WriteBitsAndBytes()
        {
            var stream = new BitStream();

            stream.AllowUnalignedOperations = false;
            var writer = new BitStreamWriter(stream);
            var val    = new byte[] { 0xFF, 0x01, 0xCC, 0xAA };

            writer.WriteBit(0);
            writer.WriteBit(1);
            Assert.Throws <StreamUnalignedException>(() => writer.Write(val));
        }
 public static void Serialize(ref String value, BitStreamWriter writer)
 {
     if (value != null)
     {
         writer.WriteBit(true);
         writer.WriteString(value);
     }
     else
     {
         writer.WriteBit(false);
     }
 }
Beispiel #4
0
        public void Should_WriteBit()
        {
            var stream = new BitStream();
            var writer = new BitStreamWriter(stream);

            writer.WriteBit(0);
            writer.WriteBit(1);
            writer.WriteBit(true);
            writer.WriteBit(new Bit(1));
            writer.Flush();
            Assert.AreEqual(1, stream.Length);
            var bytes = stream.ToArray();

            // we wrote 0111 binary, which is 14
            Assert.AreEqual(14, bytes[0]);
        }
Beispiel #5
0
 //0 xxxxxxx   // val 0 - 127                 //  8 bits
 //10 xxxxxxxx // val 128 - 383               // 10 bits
 //11 xxxxxxxx0 // val 384 - 639              // 11 bits
 //11 xxxxxxxx1 xxxx0 // val 384 - 4479       // 16 bits
 //11 xxxxxxxx1 xxxx1 xxxx0 // 384 - 65919    // 21 bits
 //11 xxxxxxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx1 xxxx0  // 81 bits worst case
 public static void WriteDynamicLength(BitStreamWriter aWriter, ulong aValue)
 {
     if (aValue < 128)
     {
         aWriter.WriteBit(0);
         aWriter.WriteBits(aValue, 7);
     }
     else if (aValue < 384)
     {
         aWriter.WriteBit(1);
         aWriter.WriteBit(0);
         aWriter.WriteBits(aValue - 128UL, 8);
     }
     else
     {
         aValue -= 384UL;
         aWriter.WriteBit(1);
         aWriter.WriteBit(1);
         aWriter.WriteBits(aValue, 8);
         aValue >>= 8;
         while (aValue > 0)
         {
             aWriter.WriteBit(1);
             aWriter.WriteBits(aValue, 4);
             aValue >>= 4;
         }
         aWriter.WriteBit(0);
     }
 }
Beispiel #6
0
        public void Should_WriteBitsAndBytes()
        {
            var stream = new BitStream();

            stream.AllowUnalignedOperations = true;
            var writer = new BitStreamWriter(stream);
            var val    = new byte[] { 0xFF, 0x01, 0xCC, 0xAA };

            writer.WriteBit(0);
            writer.WriteBit(1);
            writer.Write(val);
            writer.Flush();
            Assert.AreEqual(val.Length + 1, stream.Length);
            var bytes = stream.ToArray();

            CollectionAssert.AreNotEqual(val, bytes);
            CollectionAssert.AreEqual(new byte[] { 254, 7, 48, 171, 2 }, bytes);
        }
Beispiel #7
0
 public static void WriteBool(this BitStreamWriter writer, Boolean value)
 {
     writer.WriteBit(value);
 }
Beispiel #8
0
 public void Serialize(BitStreamWriter aWriter)
 {
     aWriter.WriteBit(1);
     aWriter.WriteBits(data, 8);
 }
Beispiel #9
0
 public void Serialize(BitStreamWriter aWriter)
 {
     aWriter.WriteBit(0);
     left.Serialize(aWriter);
     right.Serialize(aWriter);
 }