Example #1
0
        public void WriteNull()
        {
            const byte nullNull = 0x0F;

            PrepareValue();
            _containerStack.IncreaseCurrentContainerLength(1);
            _dataBuffer.WriteByte(nullNull);
            FinishValue();
        }
Example #2
0
        /// <summary>
        /// Pop a container from the container stack and link the previous container sequence with the length
        /// and sequence of the popped container
        /// </summary>
        private void PopContainer()
        {
            var popped = _containerStack.Pop();

            if (_containerStack.Count == 0)
            {
                return;
            }


            var wrappedList = _dataBuffer.Wrapup();

            Debug.Assert(ReferenceEquals(wrappedList, popped.Sequence));

            var outer = _containerStack.Peek();

            //write the tid|len byte and (maybe) the length into the length buffer
            //clear the length segments, no worry
            _lengthSegments.Clear();
            _lengthBuffer.StartStreak(_lengthSegments);
            byte tidByte;

            switch (popped.Type)
            {
            case ContainerType.Sequence:
                tidByte = TidListByte;
                break;

            case ContainerType.Struct:
                tidByte = TidStructByte;
                break;

            case ContainerType.Annotation:
                tidByte = TidTypeDeclByte;
                break;

            case ContainerType.Timestamp:
                tidByte = TidTimestampByte;
                break;

            case ContainerType.BigDecimal:
                tidByte = TidDecimalByte;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var wholeContainerLength = popped.Length;

            if (wholeContainerLength <= 0xD)
            {
                //fit in the tid byte
                tidByte |= (byte)wholeContainerLength;
                _containerStack.IncreaseCurrentContainerLength(1 + wholeContainerLength);
                _lengthBuffer.WriteByte(tidByte);
            }
            else
            {
                tidByte |= BinaryConstants.LnIsVarLen;
                _lengthBuffer.WriteByte(tidByte);
                var lengthBytes = _lengthBuffer.WriteVarUint(popped.Length);
                _containerStack.IncreaseCurrentContainerLength(1 + lengthBytes + wholeContainerLength);
            }

            _lengthBuffer.Wrapup();
            foreach (var t in _lengthSegments)
            {
                outer.Sequence.Add(t);
            }

            outer.Sequence.AddRange(wrappedList);
            _dataBuffer.StartStreak(outer.Sequence);
        }