Google.Protobuf.CodedInputStream.ReadTag C# (CSharp) Method

ReadTag() public method

Reads a field tag, returning the tag of 0 for "end of stream".
If this method returns 0, it doesn't necessarily mean the end of all the data in this CodedInputStream; it may be the end of the logical stream for an embedded message, for example.
public ReadTag ( ) : uint
return uint
        public uint ReadTag()
        {
            if (hasNextTag)
            {
                lastTag = nextTag;
                hasNextTag = false;
                return lastTag;
            }

            // Optimize for the incredibly common case of having at least two bytes left in the buffer,
            // and those two bytes being enough to get the tag. This will be true for fields up to 4095.
            if (bufferPos + 2 <= bufferSize)
            {
                int tmp = buffer[bufferPos++];
                if (tmp < 128)
                {
                    lastTag = (uint)tmp;
                }
                else
                {
                    int result = tmp & 0x7f;
                    if ((tmp = buffer[bufferPos++]) < 128)
                    {
                        result |= tmp << 7;
                        lastTag = (uint) result;
                    }
                    else
                    {
                        // Nope, rewind and go the potentially slow route.
                        bufferPos -= 2;
                        lastTag = ReadRawVarint32();
                    }
                }
            }
            else
            {
                if (IsAtEnd)
                {
                    lastTag = 0;
                    return 0; // This is the only case in which we return 0.
                }

                lastTag = ReadRawVarint32();
            }
            if (lastTag == 0)
            {
                // If we actually read zero, that's not a valid tag.
                throw InvalidProtocolBufferException.InvalidTag();
            }
            return lastTag;
        }

Usage Example

Example #1
0
            internal static T Read <T>(CodedInputStream input, FieldCodec <T> codec)
            {
                int length   = input.ReadLength();
                int oldLimit = input.PushLimit(length);

                uint tag;
                T    value = codec.DefaultValue;

                while (input.ReadTag(out tag))
                {
                    if (tag == 0)
                    {
                        throw InvalidProtocolBufferException.InvalidTag();
                    }
                    if (tag == codec.Tag)
                    {
                        value = codec.Read(input);
                    }
                    if (WireFormat.IsEndGroupTag(tag))
                    {
                        break;
                    }
                }
                input.CheckLastTagWas(0);
                input.PopLimit(oldLimit);

                return(value);
            }
All Usage Examples Of Google.Protobuf.CodedInputStream::ReadTag