示例#1
0
        public void VisitValue(string value, VisitArgs args)
        {
            if (args.Index > 0)
            {
                BinaryZPacker.Pack(_writeBuffer, args.Index);
            }

            if (value == null)
            {
                _writeBuffer.WriteByte(BinaryZPacker.Null);
                return;
            }

            var length = Encoding.GetByteCount(value);

            if (length < BinaryZPacker.VariabelLength)
            {
                _writeBuffer.WriteByte((byte)length);
            }
            else
            {
                _writeBuffer.WriteByte(BinaryZPacker.VariabelLength);
                BinaryV32Packer.PackU(_writeBuffer, (uint)value.Length);
            }

            var position = _writeBuffer.Advance(length);

            Encoding.GetBytes(value, 0, value.Length, _writeBuffer.Buffer, position);
        }
示例#2
0
        public void VisitValue(DateTime?value, VisitArgs args)
        {
            if (args.Index > 0)
            {
                BinaryZPacker.Pack(_writeBuffer, args.Index);
            }

            if (value == null)
            {
                _writeBuffer.WriteByte(BinaryZPacker.Null);
                return;
            }

            if (value.Value.Kind != DateTimeKind.Utc)
            {
                value = value.Value.ToUniversalTime();
            }

            var length = BinaryPV64Packer.GetSLength(value.Value.Ticks);

            _writeBuffer.WriteByte(length);
            var offset = _writeBuffer.Advance(length);

            BinaryPV64Packer.PackS(_writeBuffer.Buffer, offset, value.Value.Ticks, length);
        }
示例#3
0
        public void PackZTest()
        {
            const uint value  = (uint)0x0FC0D096;
            var        buffer = BinaryZPacker.Pack(value);

            Assert.True((3 & buffer[0]) == 3);
            Assert.True(((buffer[0] & 0xFC) & 0x58) == 0x58);

            var unpacked = BinaryZPacker.Unpack(buffer, 0);

            Assert.Equal(value, unpacked);
        }
示例#4
0
        public void Leave(object level, VisitArgs args)
        {
            if (args.IsRoot)
            {
                return;
            }

            if (level != null)
            {
                var reservation = _reservations.Pop();
                _writer.Write(reservation);
                BinaryZPacker.Pack(_writeBuffer, 0);
            }
        }
示例#5
0
        public void VisitValue(byte?value, VisitArgs args)
        {
            if (args.Index > 0)
            {
                BinaryZPacker.Pack(_writeBuffer, args.Index);
            }

            if (value == null)
            {
                _writeBuffer.WriteByte(BinaryZPacker.Null);
                return;
            }

            _writeBuffer.WriteByte((Byte)BinaryInformation.Byte.FixedLength);
            _writeBuffer.WriteByte(value.Value);
        }
示例#6
0
        public void VisitValue(Guid?value, VisitArgs args)
        {
            if (args.Index > 0)
            {
                BinaryZPacker.Pack(_writeBuffer, args.Index);
            }

            if (value == null)
            {
                _writeBuffer.WriteByte(BinaryZPacker.Null);
                return;
            }

            _writeBuffer.WriteByte((byte)BinaryInformation.Guid.FixedLength);
            var bytes = BinaryInformation.Guid.Converter.Convert(value.Value);

            _writeBuffer.Write(bytes, 0, bytes.Length);
        }
示例#7
0
        public void VisitValue(short?value, VisitArgs args)
        {
            if (args.Index > 0)
            {
                BinaryZPacker.Pack(_writeBuffer, args.Index);
            }

            if (value == null)
            {
                _writeBuffer.WriteByte(BinaryZPacker.Null);
                return;
            }

            var length = BinaryPV64Packer.GetSLength(value.Value);

            _writeBuffer.WriteByte(length);
            var offset = _writeBuffer.Advance(length);

            BinaryPV64Packer.PackS(_writeBuffer.Buffer, offset, value.Value, length);
        }
示例#8
0
        public void Visit(object level, VisitArgs args)
        {
            if (args.Index > 0)
            {
                BinaryZPacker.Pack(_writeBuffer, args.Index);
            }

            if (level == null)
            {
                _writeBuffer.WriteByte(BinaryZPacker.Null);
                return;
            }

            _writeBuffer.WriteByte(BinaryZPacker.VariabelLength);

            if (args.IsRoot)
            {
                return;
            }
            _reservations.Push(_writer.Reserve());
        }
示例#9
0
        public void VisitValue(byte[] value, VisitArgs args)
        {
            if (args.Index > 0)
            {
                BinaryZPacker.Pack(_writeBuffer, args.Index);
            }

            if (value == null)
            {
                _writeBuffer.WriteByte(BinaryZPacker.Null);
                return;
            }

            if (value.Length < BinaryZPacker.VariabelLength)
            {
                _writeBuffer.WriteByte((Byte)value.Length);
            }
            else
            {
                _writeBuffer.WriteByte(BinaryZPacker.VariabelLength);
                BinaryV32Packer.PackU(_writeBuffer, (uint)value.Length);
            }
            _writeBuffer.Write(value, 0, value.Length);
        }
示例#10
0
        //public void Write(WriteReservation reservation, UInt32 value)
        //{
        //    var currentPosition = _stream.Position;
        //    _stream.Seek(reservation.Position, SeekOrigin.Begin);
        //    Write(value);
        //    _stream.Seek(currentPosition, SeekOrigin.Begin);
        //}

        /// <summary>
        /// More tightly packed than the V version but the max size is 1/4 of an <see cref="UInt32"/>
        /// </summary>
        /// <param name="value">The value to pack</param>
        public void WriteZ(UInt32 value)
        {
            BinaryZPacker.Pack(_writeBuffer, value);
        }
示例#11
0
 public UInt32 ReadZ()
 {
     return(BinaryZPacker.Unpack(_stream));
 }