Ejemplo n.º 1
0
        public T[] ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <T[], TInput>(ref reader, field));
            }

            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);

            T[]  result  = null;
            uint fieldId = 0;
            var  length  = 0;
            var  index   = 0;

            while (true)
            {
                var header = reader.ReadFieldHeader();
                if (header.IsEndBaseOrEndObject)
                {
                    break;
                }

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    length = Int32Codec.ReadValue(ref reader, header);
                    result = new T[length];
                    ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
                    break;

                case 1:
                    if (result is null)
                    {
                        return(ThrowLengthFieldMissing());
                    }

                    if (index >= length)
                    {
                        return(ThrowIndexOutOfRangeException(length));
                    }

                    result[index] = _fieldCodec.ReadValue(ref reader, header);
                    ++index;
                    break;

                default:
                    reader.ConsumeUnknownField(header);
                    break;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        List <T> IFieldCodec <List <T> > .ReadValue <TInput>(ref Reader <TInput> reader, Field field)
        {
            if (field.WireType == WireType.Reference)
            {
                return(ReferenceCodec.ReadReference <List <T>, TInput>(ref reader, field));
            }

            if (field.WireType != WireType.TagDelimited)
            {
                ThrowUnsupportedWireTypeException(field);
            }

            var      placeholderReferenceId = ReferenceCodec.CreateRecordPlaceholder(reader.Session);
            List <T> result  = null;
            uint     fieldId = 0;
            var      length  = 0;
            var      index   = 0;

            while (true)
            {
                var header = reader.ReadFieldHeader();
                if (header.IsEndBaseOrEndObject)
                {
                    break;
                }

                fieldId += header.FieldIdDelta;
                switch (fieldId)
                {
                case 0:
                    length          = Int32Codec.ReadValue(ref reader, header);
                    result          = _activator.Create(length);
                    result.Capacity = length;
                    ReferenceCodec.RecordObject(reader.Session, result, placeholderReferenceId);
                    break;

                case 1:
                    if (result is null)
                    {
                        ThrowLengthFieldMissing();
                    }

                    if (index >= length)
                    {
                        ThrowIndexOutOfRangeException(length);
                    }
                    // ReSharper disable once PossibleNullReferenceException
                    result.Add(_fieldCodec.ReadValue(ref reader, header));
                    ++index;
                    break;

                default:
                    reader.ConsumeUnknownField(header);
                    break;
                }
            }

            return(result);
        }