ReadString() public method

Reads a string field from the stream.
public ReadString ( ) : string
return string
Exemplo n.º 1
0
        public void MergeFrom(pb.CodedInputStream input)
        {
    #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE
            input.ReadRawMessage(this);
    #else
            uint tag;
            while ((tag = input.ReadTag()) != 0)
            {
                switch (tag)
                {
                default:
                    _unknownFields = pb.UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
                    break;

                case 10: {
                    ClientId = input.ReadString();
                    break;
                }

                case 18: {
                    MessageId = input.ReadString();
                    break;
                }

                case 24: {
                    Type = (MessageType)input.ReadEnum();
                    break;
                }

                case 34: {
                    if (time_ == null)
                    {
                        Time = new global.Google.Protobuf.WellKnownTypes.Timestamp();
                    }
                    input.ReadMessage(Time);
                    break;
                }

                case 40: {
                    Status = (MessageStatus)input.ReadEnum();
                    break;
                }

                case 50: {
                    Payload = input.ReadBytes();
                    break;
                }

                case 56: {
                    Response = (ResponseType)input.ReadEnum();
                    break;
                }
                }
            }
    #endif
        }
        public void SkipGroup_WrongEndGroupTag()
        {
            // Create an output stream with:
            // Field 1: string "field 1"
            // Start group 2
            //   Field 3: fixed int32
            // End group 4 (should give an error)
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);

            output.WriteTag(1, WireFormat.WireType.LengthDelimited);
            output.WriteString("field 1");

            // The outer group...
            output.WriteTag(2, WireFormat.WireType.StartGroup);
            output.WriteTag(3, WireFormat.WireType.Fixed32);
            output.WriteFixed32(100);
            output.WriteTag(4, WireFormat.WireType.EndGroup);
            output.Flush();
            stream.Position = 0;

            // Now act like a generated client
            var input = new CodedInputStream(stream);

            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
            Assert.AreEqual("field 1", input.ReadString());
            Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
            Assert.Throws <InvalidProtocolBufferException>(input.SkipLastField);
        }
Exemplo n.º 3
0
        public void ReadStringGreaterThanCurrentLimit()
        {
            MemoryStream      ms     = new MemoryStream();
            CodedOutputStream output = new CodedOutputStream(ms);
            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);

            output.WriteRawVarint32(tag);
            output.WriteRawVarint32(4);
            output.WriteRawBytes(new byte[4]); // Pad with a few random bytes.
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = new CodedInputStream(ms.ToArray());

            Assert.AreEqual(tag, input.ReadTag());

            // Specify limit smaller than data length
            input.PushLimit(3);
            Assert.Throws <InvalidProtocolBufferException>(() => input.ReadString());

            AssertReadFromParseContext(new ReadOnlySequence <byte>(ms.ToArray()), (ref ParseContext ctx) =>
            {
                Assert.AreEqual(tag, ctx.ReadTag());
                SegmentedBufferHelper.PushLimit(ref ctx.state, 3);
                try
                {
                    ctx.ReadString();
                    Assert.Fail();
                }
                catch (InvalidProtocolBufferException) { }
            }, true);
        }
        public void SkipGroup()
        {
            // Create an output stream with a group in:
            // Field 1: string "field 1"
            // Field 2: group containing:
            //   Field 1: fixed int32 value 100
            //   Field 2: string "ignore me"
            //   Field 3: nested group containing
            //      Field 1: fixed int64 value 1000
            // Field 3: string "field 3"
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);

            output.WriteTag(1, WireFormat.WireType.LengthDelimited);
            output.WriteString("field 1");

            // The outer group...
            output.WriteTag(2, WireFormat.WireType.StartGroup);
            output.WriteTag(1, WireFormat.WireType.Fixed32);
            output.WriteFixed32(100);
            output.WriteTag(2, WireFormat.WireType.LengthDelimited);
            output.WriteString("ignore me");
            // The nested group...
            output.WriteTag(3, WireFormat.WireType.StartGroup);
            output.WriteTag(1, WireFormat.WireType.Fixed64);
            output.WriteFixed64(1000);
            // Note: Not sure the field number is relevant for end group...
            output.WriteTag(3, WireFormat.WireType.EndGroup);

            // End the outer group
            output.WriteTag(2, WireFormat.WireType.EndGroup);

            output.WriteTag(3, WireFormat.WireType.LengthDelimited);
            output.WriteString("field 3");
            output.Flush();
            stream.Position = 0;

            // Now act like a generated client
            var input = new CodedInputStream(stream);

            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
            Assert.AreEqual("field 1", input.ReadString());
            Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
            input.SkipLastField(); // Should consume the whole group, including the nested one.
            Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDelimited), input.ReadTag());
            Assert.AreEqual("field 3", input.ReadString());
        }
Exemplo n.º 5
0
        public void WriteString_AsciiSmall_MaxUtf8SizeExceedsBuffer()
        {
            var buffer = new byte[5];
            var output = new CodedOutputStream(buffer);

            output.WriteString("ABC");

            output.Flush();

            // Verify written content
            var input = new CodedInputStream(buffer);

            Assert.AreEqual("ABC", input.ReadString());
        }
Exemplo n.º 6
0
 /// <summary>
 /// Decode a value of the given type.
 /// Should not be called directly. This interface is used by service client stubs.
 /// </summary>
 public static object Decode(ByteString value, Type type, Connection client)
 {
     var stream = new CodedInputStream (value.ToByteArray ());
     if (type == typeof(double))
         return stream.ReadDouble ();
     else if (type == typeof(float))
         return stream.ReadFloat ();
     else if (type == typeof(int))
         return stream.ReadInt32 ();
     else if (type == typeof(long))
         return stream.ReadInt64 ();
     else if (type == typeof(uint))
         return stream.ReadUInt32 ();
     else if (type == typeof(ulong))
         return stream.ReadUInt64 ();
     else if (type == typeof(bool))
         return stream.ReadBool ();
     else if (type == typeof(string))
         return stream.ReadString ();
     else if (type == typeof(byte[]))
         return stream.ReadBytes ().ToByteArray ();
     else if (type.IsEnum)
         return stream.ReadInt32 ();
     else if (typeof(RemoteObject).IsAssignableFrom (type)) {
         if (client == null)
             throw new ArgumentException ("Client not passed when decoding remote object");
         var id = stream.ReadUInt64 ();
         if (id == 0)
             return null;
         return (RemoteObject)Activator.CreateInstance (type, client, id);
     } else if (typeof(IMessage).IsAssignableFrom (type)) {
         IMessage message = (IMessage)Activator.CreateInstance (type);
         message.MergeFrom (stream);
         return message;
     } else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(IList<>))
         return DecodeList (value, type, client);
     else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(IDictionary<,>))
         return DecodeDictionary (value, type, client);
     else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(ISet<>))
         return DecodeSet (value, type, client);
     else if (type.IsGenericType &&
              (type.GetGenericTypeDefinition () == typeof(Tuple<>) ||
              type.GetGenericTypeDefinition () == typeof(Tuple<,>) ||
              type.GetGenericTypeDefinition () == typeof(Tuple<,,>) ||
              type.GetGenericTypeDefinition () == typeof(Tuple<,,,>) ||
              type.GetGenericTypeDefinition () == typeof(Tuple<,,,,>) ||
              type.GetGenericTypeDefinition () == typeof(Tuple<,,,,,>)))
         return DecodeTuple (value, type, client); // TODO: ugly handing of tuple types
     throw new ArgumentException (type + " is not a serializable type");
 }
Exemplo n.º 7
0
                public void MergeFrom(CodedInputStream input)
                {
                    while (true)
                    {
IL_F1:
                        uint num;
                        uint arg_B1_0 = ((num = input.ReadTag()) == 0u) ? 892360258u : 1848098736u;
                        while (true)
                        {
                            uint num2;
                            switch ((num2 = (arg_B1_0 ^ 1527141277u)) % 9u)
                            {
                            case 0u:
                                goto IL_F1;

                            case 1u:
                                arg_B1_0 = (((num != 16u) ? 2248849807u : 3227524151u) ^ num2 * 3730220176u);
                                continue;

                            case 3u:
                                input.SkipLastField();
                                arg_B1_0 = (num2 * 783818891u ^ 1987640322u);
                                continue;

                            case 4u:
                                arg_B1_0 = ((num == 10u) ? 319223136u : 559492368u);
                                continue;

                            case 5u:
                                arg_B1_0 = (num2 * 1269553354u ^ 293555476u);
                                continue;

                            case 6u:
                                this.IsExtension = input.ReadBool();
                                arg_B1_0         = 894606036u;
                                continue;

                            case 7u:
                                arg_B1_0 = 1848098736u;
                                continue;

                            case 8u:
                                this.NamePart_ = input.ReadString();
                                arg_B1_0       = 55025149u;
                                continue;
                            }
                            return;
                        }
                    }
                }
        public void ReadNegativeSizedStringThrowsInvalidProtocolBufferException()
        {
            MemoryStream      ms     = new MemoryStream();
            CodedOutputStream output = new CodedOutputStream(ms);

            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);

            output.WriteRawVarint32(tag);
            output.WriteLength(-1);
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = new CodedInputStream(ms);

            Assert.AreEqual(tag, input.ReadTag());
            Assert.Throws <InvalidProtocolBufferException>(() => input.ReadString());
        }
        public void ReadInvalidUtf8()
        {
            MemoryStream      ms     = new MemoryStream();
            CodedOutputStream output = new CodedOutputStream(ms);

            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);

            output.WriteRawVarint32(tag);
            output.WriteRawVarint32(1);
            output.WriteRawBytes(new byte[] { 0x80 });
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = new CodedInputStream(ms);

            Assert.AreEqual(tag, input.ReadTag());
            string text = input.ReadString();

            Assert.AreEqual('\ufffd', text[0]);
        }
Exemplo n.º 10
0
        public void WriteStringsOfDifferentSizes_Ascii()
        {
            for (int i = 1; i <= 1024; i++)
            {
                var buffer = new byte[4096];
                var output = new CodedOutputStream(buffer);
                var sb     = new StringBuilder();
                for (int j = 0; j < i; j++)
                {
                    sb.Append((j % 10).ToString()); // incrementing numbers, repeating
                }
                var s = sb.ToString();
                output.WriteString(s);

                output.Flush();

                // Verify written content
                var input = new CodedInputStream(buffer);
                Assert.AreEqual(s, input.ReadString());
            }
        }
Exemplo n.º 11
0
        public void SkipGroup_WrongEndGroupTag()
        {
            // Create an output stream with:
            // Field 1: string "field 1"
            // Start group 2
            //   Field 3: fixed int32
            // End group 4 (should give an error)
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            output.WriteTag(1, WireFormat.WireType.LengthDelimited);
            output.WriteString("field 1");

            // The outer group...
            output.WriteTag(2, WireFormat.WireType.StartGroup);
            output.WriteTag(3, WireFormat.WireType.Fixed32);
            output.WriteFixed32(100);
            output.WriteTag(4, WireFormat.WireType.EndGroup);
            output.Flush();
            stream.Position = 0;

            // Now act like a generated client
            var input = new CodedInputStream(stream);
            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
            Assert.AreEqual("field 1", input.ReadString());
            Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
            Assert.Throws<InvalidProtocolBufferException>(input.SkipLastField);
        }
Exemplo n.º 12
0
        public void SkipGroup()
        {
            // Create an output stream with a group in:
            // Field 1: string "field 1"
            // Field 2: group containing:
            //   Field 1: fixed int32 value 100
            //   Field 2: string "ignore me"
            //   Field 3: nested group containing
            //      Field 1: fixed int64 value 1000
            // Field 3: string "field 3"
            var stream = new MemoryStream();
            var output = new CodedOutputStream(stream);
            output.WriteTag(1, WireFormat.WireType.LengthDelimited);
            output.WriteString("field 1");

            // The outer group...
            output.WriteTag(2, WireFormat.WireType.StartGroup);
            output.WriteTag(1, WireFormat.WireType.Fixed32);
            output.WriteFixed32(100);
            output.WriteTag(2, WireFormat.WireType.LengthDelimited);
            output.WriteString("ignore me");
            // The nested group...
            output.WriteTag(3, WireFormat.WireType.StartGroup);
            output.WriteTag(1, WireFormat.WireType.Fixed64);
            output.WriteFixed64(1000);
            // Note: Not sure the field number is relevant for end group...
            output.WriteTag(3, WireFormat.WireType.EndGroup);

            // End the outer group
            output.WriteTag(2, WireFormat.WireType.EndGroup);

            output.WriteTag(3, WireFormat.WireType.LengthDelimited);
            output.WriteString("field 3");
            output.Flush();
            stream.Position = 0;

            // Now act like a generated client
            var input = new CodedInputStream(stream);
            Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
            Assert.AreEqual("field 1", input.ReadString());
            Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
            input.SkipLastField(); // Should consume the whole group, including the nested one.
            Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDelimited), input.ReadTag());
            Assert.AreEqual("field 3", input.ReadString());
        }
Exemplo n.º 13
0
        public void ReadInvalidUtf8()
        {
            MemoryStream ms = new MemoryStream();
            CodedOutputStream output = new CodedOutputStream(ms);

            uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
            output.WriteRawVarint32(tag);
            output.WriteRawVarint32(1);
            output.WriteRawBytes(new byte[] {0x80});
            output.Flush();
            ms.Position = 0;

            CodedInputStream input = new CodedInputStream(ms);

            Assert.AreEqual(tag, input.ReadTag());
            string text = input.ReadString();
            Assert.AreEqual('\ufffd', text[0]);
        }
Exemplo n.º 14
0
 internal string <ForString> b__0_0(CodedInputStream input)
 {
     return(input.ReadString());
 }
Exemplo n.º 15
0
                public void MergeFrom(CodedInputStream input)
                {
                    while (true)
                    {
IL_22B:
                        uint num;
                        uint arg_1C7_0 = ((num = input.ReadTag()) != 0u) ? 3338900507u : 3490427704u;
                        while (true)
                        {
                            uint num2;
                            switch ((num2 = (arg_1C7_0 ^ 3575757851u)) % 18u)
                            {
                            case 0u:
                                arg_1C7_0 = (num2 * 4161478824u ^ 2103420624u);
                                continue;

                            case 1u:
                                this.path_.AddEntriesFrom(input, SourceCodeInfo.Types.Location._repeated_path_codec);
                                arg_1C7_0 = 2227857680u;
                                continue;

                            case 2u:
                                arg_1C7_0 = ((num <= 16u) ? 2859386605u : 4283208004u);
                                continue;

                            case 3u:
                                arg_1C7_0 = (((num != 34u) ? 1520889365u : 1267782305u) ^ num2 * 2173165572u);
                                continue;

                            case 4u:
                                this.TrailingComments = input.ReadString();
                                arg_1C7_0             = 2227857680u;
                                continue;

                            case 5u:
                                arg_1C7_0 = 3338900507u;
                                continue;

                            case 6u:
                                arg_1C7_0 = (num2 * 2358789655u ^ 2159803823u);
                                continue;

                            case 8u:
                                input.SkipLastField();
                                arg_1C7_0 = 2183611349u;
                                continue;

                            case 9u:
                                arg_1C7_0 = ((num == 18u) ? 2447739934u : 2669850999u);
                                continue;

                            case 10u:
                                arg_1C7_0 = (((num != 8u) ? 3280687541u : 3841024010u) ^ num2 * 3329312757u);
                                continue;

                            case 11u:
                                this.LeadingComments = input.ReadString();
                                arg_1C7_0            = 2227857680u;
                                continue;

                            case 12u:
                                arg_1C7_0 = (((num != 26u) ? 3138895318u : 3479952318u) ^ num2 * 1481422129u);
                                continue;

                            case 13u:
                                arg_1C7_0 = (((num != 16u) ? 278902393u : 1407783438u) ^ num2 * 615822576u);
                                continue;

                            case 14u:
                                arg_1C7_0 = (num2 * 255842685u ^ 4130925702u);
                                continue;

                            case 15u:
                                goto IL_22B;

                            case 16u:
                                arg_1C7_0 = (((num != 10u) ? 1246054996u : 1063320292u) ^ num2 * 84701410u);
                                continue;

                            case 17u:
                                this.span_.AddEntriesFrom(input, SourceCodeInfo.Types.Location._repeated_span_codec);
                                arg_1C7_0 = 3200795651u;
                                continue;
                            }
                            return;
                        }
                    }
                }
Exemplo n.º 16
0
 /// <summary>
 /// Convert a Protocol Buffer value type, encoded as a byte string, to a C# value.
 /// </summary>
 public static object ReadValue(ByteString value, Type type)
 {
     if (value.Length == 0)
         throw new ArgumentException ("Value is empty");
     var stream = new CodedInputStream (value.ToByteArray ());
     if (type == typeof(double)) {
         return stream.ReadDouble ();
     } else if (type == typeof(float)) {
         return stream.ReadFloat ();
     } else if (type == typeof(int)) {
         return stream.ReadInt32 ();
     } else if (type == typeof(long)) {
         return stream.ReadInt64 ();
     } else if (type == typeof(uint)) {
         return stream.ReadUInt32 ();
     } else if (type == typeof(ulong)) {
         return stream.ReadUInt64 ();
     } else if (type == typeof(bool)) {
         return stream.ReadBool ();
     } else if (type == typeof(string)) {
         return stream.ReadString ();
     } else if (type == typeof(byte[])) {
         return stream.ReadBytes ().ToByteArray();
     }
     throw new ArgumentException (type + " is not a Protocol Buffer value type");
 }