public Guid ReadGuid() { ReadLength(); if (bufferSize - bufferPos >= CodedOutputStream.GuidSize) { ReadTag(); ByteArray.Copy(buffer, bufferPos, guidBuffer, 0, 8); bufferPos += 8; ReadTag(); ByteArray.Copy(buffer, bufferPos, guidBuffer, 8, 8); bufferPos += 8; return(new Guid(guidBuffer)); } throw InvalidProtocolBufferException.SizeLimitExceeded(); }
/// <summary> /// Reads a raw varint from the stream. /// </summary> internal ulong ReadRawVarint64() { int shift = 0; ulong result = 0; while (shift < 64) { byte b = ReadRawByte(); result |= (ulong)(b & 0x7F) << shift; if ((b & 0x80) == 0) { return(result); } shift += 7; } throw InvalidProtocolBufferException.MalformedVarint(); }
private uint ReadTagSlow() { if (IsAtEnd) { return(0); } uint tag = ReadRawVarint32(); if (tag == 0) { // If we actually read zero, that's not a valid tag. throw InvalidProtocolBufferException.InvalidTag(); } return(tag); }
/// <summary> /// Reads a fixed size of bytes from the input. /// </summary> /// <exception cref="InvalidProtocolBufferException"> /// the end of the stream or the current limit was reached /// </exception> internal byte[] ReadRawBytes(int size) { if (size < 0) { throw InvalidProtocolBufferException.NegativeSize(); } if (size > bufferSize - bufferPos) { throw InvalidProtocolBufferException.TruncatedMessage(); } // We have all the bytes we need already. byte[] bytes = new byte[size]; ByteArray.Copy(buffer, bufferPos, bytes, 0, size); bufferPos += size; return(bytes); }
/// <summary> /// Reads a field tag, returning the tag of 0 for "end of stream". /// </summary> /// <remarks> /// 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. /// </remarks> /// <returns>The next field tag, or 0 for end of stream. (0 is never a valid tag.)</returns> public uint ReadTag() { // Optimize for the incredibly common case of having at least one byte left in the buffer, // and this byte being enough to get the tag. This will be true for fields up to 31. if (bufferPos + 1 <= bufferSize) { uint tag = buffer[bufferPos]; if (tag < 128) { bufferPos++; if (tag == 0) { // If we actually read zero, that's not a valid tag. throw InvalidProtocolBufferException.InvalidTag(); } return(tag); } } return(ReadTagSlow()); }
/// <summary> /// Reads a string field from the stream. /// </summary> public string ReadString() { int length = ReadLength(); // No need to read any data for an empty string. if (length == 0) { return(""); } if (length > bufferSize - bufferPos) { throw InvalidProtocolBufferException.TruncatedMessage(); } // Fast path: We already have the bytes in a contiguous buffer, so // just copy directly from it. var result = CodedOutputStream.Utf8Encoding.GetString(buffer, bufferPos, length); bufferPos += length; return(result); }