public WriteString ( string value ) : void | ||
value | string | The value to write |
return | void |
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()); }
public void WriteTo(pb.CodedOutputStream output) { #if !GOOGLE_PROTOBUF_REFSTRUCT_COMPATIBILITY_MODE output.WriteRawMessage(this); #else if (ClientId.Length != 0) { output.WriteRawTag(10); output.WriteString(ClientId); } if (MessageId.Length != 0) { output.WriteRawTag(18); output.WriteString(MessageId); } if (Type != MessageType.Undefined) { output.WriteRawTag(24); output.WriteEnum((int)Type); } if (time_ != null) { output.WriteRawTag(34); output.WriteMessage(Time); } if (Status != MessageStatus.Undefined) { output.WriteRawTag(40); output.WriteEnum((int)Status); } if (Payload.Length != 0) { output.WriteRawTag(50); output.WriteBytes(Payload); } if (Response != ResponseType.Undefined) { output.WriteRawTag(56); output.WriteEnum((int)Response); } if (_unknownFields != null) { _unknownFields.WriteTo(output); } #endif }
static ByteString EncodeObject (object value, MemoryStream buffer, CodedOutputStream stream) { buffer.SetLength (0); if (value == null) { stream.WriteUInt64 (0); } else if (value is Enum) { stream.WriteInt32 ((int)value); } else { Type type = value.GetType (); switch (Type.GetTypeCode (type)) { case TypeCode.Int32: stream.WriteInt32 ((int)value); break; case TypeCode.Int64: stream.WriteInt64 ((long)value); break; case TypeCode.UInt32: stream.WriteUInt32 ((uint)value); break; case TypeCode.UInt64: stream.WriteUInt64 ((ulong)value); break; case TypeCode.Single: stream.WriteFloat ((float)value); break; case TypeCode.Double: stream.WriteDouble ((double)value); break; case TypeCode.Boolean: stream.WriteBool ((bool)value); break; case TypeCode.String: stream.WriteString ((string)value); break; default: if (type == typeof(byte[])) stream.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (TypeUtils.IsAClassType (type)) stream.WriteUInt64 (ObjectStore.Instance.AddInstance (value)); else if (TypeUtils.IsAMessageType (type)) WriteMessage (value, stream); else if (TypeUtils.IsAListCollectionType (type)) WriteList (value, stream); else if (TypeUtils.IsADictionaryCollectionType (type)) WriteDictionary (value, stream); else if (TypeUtils.IsASetCollectionType (type)) WriteSet (value, stream); else if (TypeUtils.IsATupleCollectionType (type)) WriteTuple (value, stream); else throw new ArgumentException (type + " is not a serializable type"); break; } } stream.Flush (); return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length); }
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()); }
public void MaximumFieldNumber() { MemoryStream ms = new MemoryStream(); CodedOutputStream output = new CodedOutputStream(ms); int fieldNumber = 0x1FFFFFFF; uint tag = WireFormat.MakeTag(fieldNumber, WireFormat.WireType.LengthDelimited); output.WriteRawVarint32(tag); output.WriteString("field 1"); output.Flush(); ms.Position = 0; CodedInputStream input = new CodedInputStream(ms); Assert.AreEqual(tag, input.ReadTag()); Assert.AreEqual(fieldNumber, WireFormat.GetTagFieldNumber(tag)); }
public void IgnoreUnknownFields_RealDataStillRead() { var message = SampleMessages.CreateFullTestAllTypes(); var stream = new MemoryStream(); var output = new CodedOutputStream(stream); var unusedFieldNumber = 23456; Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber)); output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited); output.WriteString("ignore me"); message.WriteTo(output); output.Flush(); stream.Position = 0; var parsed = TestAllTypes.Parser.ParseFrom(stream); Assert.AreEqual(message, parsed); }
public void WriteTo(CodedOutputStream output) { if (UninterpretedOption.Types.NamePart.smethod_1(this.NamePart_) != 0) { goto IL_65; } goto IL_9B; uint arg_6F_0; while (true) { IL_6A: uint num; switch ((num = (arg_6F_0 ^ 2196724152u)) % 5u) { case 0u: goto IL_65; case 1u: goto IL_9B; case 2u: output.WriteRawTag(10); output.WriteString(this.NamePart_); arg_6F_0 = (num * 3710217040u ^ 1616946663u); continue; case 4u: output.WriteRawTag(16); output.WriteBool(this.IsExtension); arg_6F_0 = (num * 4058525509u ^ 2603311045u); continue; } break; } return; IL_65: arg_6F_0 = 2475431821u; goto IL_6A; IL_9B: arg_6F_0 = ((!this.IsExtension) ? 4134694002u : 2434990451u); goto IL_6A; }
public void CodedInputStream_LimitReachedRightAfterTag() { MemoryStream ms = new MemoryStream(); var cos = new CodedOutputStream(ms); cos.WriteTag(11, WireFormat.WireType.Varint); Assert.AreEqual(1, cos.Position); cos.WriteString("some extra padding"); // ensure is currentLimit distinct from the end of the buffer. cos.Flush(); var cis = new CodedInputStream(ms.ToArray()); cis.PushLimit(1); // make sure we reach the limit right after reading the tag. // we still must read the tag correctly, even though the tag is at the very end of our limited input // (which is a corner case and will most likely result in an error when trying to read value of the field // described by this tag, but it would be a logical error not to read the tag that's actually present). // See https://github.com/protocolbuffers/protobuf/pull/7289 cis.AssertNextTag(WireFormat.MakeTag(11, WireFormat.WireType.Varint)); }
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()); } }
public void DiscardUnknownFields_RealDataStillRead() { var message = SampleMessages.CreateFullTestAllTypes(); var stream = new MemoryStream(); var output = new CodedOutputStream(stream); var unusedFieldNumber = 23456; Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber)); output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited); output.WriteString("ignore me"); message.WriteTo(output); output.Flush(); MessageParsingHelpers.AssertReadingMessage( TestAllTypes.Parser, stream.ToArray(), parsed => { // TODO(jieluo): Add test back when DiscardUnknownFields API is supported. // Assert.AreEqual(message, parsed); }); }
static ByteString EncodeObject (object value, Type type, MemoryStream buffer, CodedOutputStream stream) { buffer.SetLength (0); if (value != null && !type.IsInstanceOfType (value)) throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type); if (value == null && !type.IsSubclassOf (typeof(RemoteObject)) && !IsACollectionType (type)) throw new ArgumentException ("null cannot be encoded to type " + type); if (value == null) stream.WriteUInt64 (0); else if (value is Enum) stream.WriteInt32 ((int)value); else { switch (Type.GetTypeCode (type)) { case TypeCode.Int32: stream.WriteInt32 ((int)value); break; case TypeCode.Int64: stream.WriteInt64 ((long)value); break; case TypeCode.UInt32: stream.WriteUInt32 ((uint)value); break; case TypeCode.UInt64: stream.WriteUInt64 ((ulong)value); break; case TypeCode.Single: stream.WriteFloat ((float)value); break; case TypeCode.Double: stream.WriteDouble ((double)value); break; case TypeCode.Boolean: stream.WriteBool ((bool)value); break; case TypeCode.String: stream.WriteString ((string)value); break; default: if (type.Equals (typeof(byte[]))) stream.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (IsAClassType (type)) stream.WriteUInt64 (((RemoteObject)value).id); else if (IsAMessageType (type)) ((IMessage)value).WriteTo (buffer); else if (IsAListType (type)) WriteList (value, type, buffer); else if (IsADictionaryType (type)) WriteDictionary (value, type, buffer); else if (IsASetType (type)) WriteSet (value, type, buffer); else if (IsATupleType (type)) WriteTuple (value, type, buffer); else throw new ArgumentException (type + " is not a serializable type"); break; } } stream.Flush (); return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length); }
internal void <ForString> b__0_1(CodedOutputStream output, string value) { output.WriteString(value); }
private byte[] StringToByteBuffer(string str) { int t = CodedOutputStream.ComputeTagSize(9); int s = CodedOutputStream.ComputeStringSize(str); s += t; byte[] bytes = new byte[s]; CodedOutputStream cos = new CodedOutputStream(bytes); cos.WriteTag((9 << 3) + 2); cos.WriteString(str); cos.Flush(); return bytes; }
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); }
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()); }
/// <summary> /// Encode a value of the given type. /// Should not be called directly. This interface is used by service client stubs. /// </summary> public static ByteString Encode(object value, Type type) { var stream = new MemoryStream (); var encoder = new CodedOutputStream (stream); if (value != null && !type.IsAssignableFrom (value.GetType ())) //TODO: nulls? throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type); if (type == typeof(Double)) encoder.WriteDouble ((Double)value); else if (type == typeof(Single)) encoder.WriteFloat ((Single)value); else if (type == typeof(Int32)) encoder.WriteInt32 ((Int32)value); else if (type == typeof(Int64)) encoder.WriteInt64 ((Int64)value); else if (type == typeof(UInt32)) encoder.WriteUInt32 ((UInt32)value); else if (type == typeof(UInt64)) encoder.WriteUInt64 ((UInt64)value); else if (type == typeof(Boolean)) encoder.WriteBool ((Boolean)value); else if (type == typeof(String)) encoder.WriteString ((String)value); else if (type == typeof(byte[])) encoder.WriteBytes (ByteString.CopyFrom ((byte[])value)); else if (value != null && value is Enum) encoder.WriteInt32 ((int)value); else if (type.IsSubclassOf (typeof(RemoteObject))) { if (value == null) encoder.WriteUInt64 (0); else encoder.WriteUInt64 (((RemoteObject)value)._ID); } else if (value != null && value is IMessage) { ((IMessage)value).WriteTo (stream); return ByteString.CopyFrom (stream.ToArray ()); } else if (value != null && value is IList) return EncodeList (value, type); else if (value != null && value is IDictionary) return EncodeDictionary (value, type); else if (value != null && value.GetType ().IsGenericType && value.GetType ().GetGenericTypeDefinition () == typeof(HashSet<>)) return EncodeSet (value, type); //TODO: ugly checking for set types else if (value != null && value.GetType ().IsGenericType && (value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,,>) || value.GetType ().GetGenericTypeDefinition () == typeof(Tuple<,,,,,>))) return EncodeTuple (value, type); //TODO: ugly checking for tuple types else throw new ArgumentException (type + " is not a serializable type"); encoder.Flush (); return ByteString.CopyFrom (stream.ToArray ()); }
public void MapNonContiguousEntries() { var memoryStream = new MemoryStream(); var output = new CodedOutputStream(memoryStream); // Message structure: // Entry for MapInt32Int32 // Entry for MapStringString // Entry for MapInt32Int32 // First entry var key1 = 10; var value1 = 20; output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteLength(4); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key1); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value1); // Second entry var key2 = "a"; var value2 = "b"; output.WriteTag(TestMap.MapStringStringFieldNumber, WireFormat.WireType.LengthDelimited); output.WriteLength(6); // 3 bytes per entry: tag, size, character output.WriteTag(1, WireFormat.WireType.LengthDelimited); output.WriteString(key2); output.WriteTag(2, WireFormat.WireType.LengthDelimited); output.WriteString(value2); // Third entry var key3 = 15; var value3 = 25; output.WriteTag(TestMap.MapInt32Int32FieldNumber, WireFormat.WireType.LengthDelimited); output.WriteLength(4); output.WriteTag(1, WireFormat.WireType.Varint); output.WriteInt32(key3); output.WriteTag(2, WireFormat.WireType.Varint); output.WriteInt32(value3); output.Flush(); var parsed = TestMap.Parser.ParseFrom(memoryStream.ToArray()); var expected = new TestMap { MapInt32Int32 = { { key1, value1 }, { key3, value3 } }, MapStringString = { { key2, value2 } } }; Assert.AreEqual(expected, parsed); }
public void IgnoreUnknownFields_RealDataStillRead() { var message = SampleMessages.CreateFullTestAllTypes(); var stream = new MemoryStream(); var output = new CodedOutputStream(stream); var unusedFieldNumber = 23456; Assert.IsFalse(TestAllTypes.Descriptor.Fields.InDeclarationOrder().Select(x => x.FieldNumber).Contains(unusedFieldNumber)); output.WriteTag(unusedFieldNumber, WireFormat.WireType.LengthDelimited); output.WriteString("ignore me"); message.WriteTo(output); output.Flush(); stream.Position = 0; var parsed = TestAllTypes.Parser.ParseFrom(stream); Assert.AreEqual(message, parsed); }
/// <summary> /// Convert a Protocol Buffer value type from a C# value to a byte string. /// </summary> public static ByteString WriteValue(object value, Type type) { var stream = new MemoryStream (); var encoder = new CodedOutputStream (stream); if (type == typeof(double)) encoder.WriteDouble ((double)value); else if (type == typeof(float)) encoder.WriteFloat ((float)value); else if (type == typeof(int)) encoder.WriteInt32 ((int)value); else if (type == typeof(long)) encoder.WriteInt64 ((long)value); else if (type == typeof(uint)) encoder.WriteUInt32 ((uint)value); else if (type == typeof(ulong)) encoder.WriteUInt64 ((ulong)value); else if (type == typeof(bool)) encoder.WriteBool ((bool)value); else if (type == typeof(string)) encoder.WriteString ((string)value); else if (type == typeof(byte[])) encoder.WriteBytes (ByteString.CopyFrom ((byte[])value)); else throw new ArgumentException (type + " is not a Protocol Buffer value type"); encoder.Flush (); return ByteString.CopyFrom (stream.ToArray ()); }