Exemplo n.º 1
0
        /// <summary>
        /// Reads a varint from the input one byte at a time, so that it does not
        /// read any bytes after the end of the varint. If you simply wrapped the
        /// stream in a CodedInputStream and used ReadRawVarint32(Stream)
        /// then you would probably end up reading past the end of the varint since
        /// CodedInputStream buffers its input.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        internal static uint ReadRawVarint32(Stream input)
        {
            int result = 0;
            int offset = 0;

            for (; offset < 32; offset += 7)
            {
                int b = input.ReadByte();
                if (b == -1)
                {
                    throw InvalidProtocolBufferException.TruncatedMessage();
                }
                result |= (b & 0x7f) << offset;
                if ((b & 0x80) == 0)
                {
                    return((uint)result);
                }
            }
            // Keep reading up to 64 bits.
            for (; offset < 64; offset += 7)
            {
                int b = input.ReadByte();
                if (b == -1)
                {
                    throw InvalidProtocolBufferException.TruncatedMessage();
                }
                if ((b & 0x80) == 0)
                {
                    return((uint)result);
                }
            }
            throw InvalidProtocolBufferException.MalformedVarint();
        }
Exemplo n.º 2
0
 /// <summary>
 /// Read one byte from the input.
 /// </summary>
 /// <exception cref="InvalidProtocolBufferException">
 /// the end of the stream or the current limit was reached
 /// </exception>
 internal byte ReadRawByte()
 {
     if (bufferPos == bufferSize)
     {
         throw InvalidProtocolBufferException.TruncatedMessage();
     }
     return(buffer[bufferPos++]);
 }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        /// <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);
        }