WriteBytes() публичный Метод

Write a byte string, without a tag, to the stream. The data is length-prefixed.
public WriteBytes ( ByteString value ) : void
value ByteString The value to write
Результат void
Пример #1
0
 /// <summary>
 /// Serializes the field, including the field number, and writes it to
 /// <paramref name="output"/>
 /// </summary>
 /// <param name="fieldNumber">The unknown field number.</param>
 /// <param name="output">The CodedOutputStream to write to.</param>
 internal void WriteTo(int fieldNumber, CodedOutputStream output)
 {
     if (varintList != null)
     {
         foreach (ulong value in varintList)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.Varint);
             output.WriteUInt64(value);
         }
     }
     if (fixed32List != null)
     {
         foreach (uint value in fixed32List)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
             output.WriteFixed32(value);
         }
     }
     if (fixed64List != null)
     {
         foreach (ulong value in fixed64List)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
             output.WriteFixed64(value);
         }
     }
     if (lengthDelimitedList != null)
     {
         foreach (ByteString value in lengthDelimitedList)
         {
             output.WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
             output.WriteBytes(value);
         }
     }
 }
Пример #2
0
 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 TestSlowPathAvoidance()
        {
            using (var ms = new MemoryStream())
            {
                CodedOutputStream output = new CodedOutputStream(ms);
                output.WriteTag(1, WireFormat.WireType.LengthDelimited);
                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
                output.WriteTag(2, WireFormat.WireType.LengthDelimited);
                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
                output.Flush();

                ms.Position = 0;
                CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0, false);

                uint tag = input.ReadTag();
                Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
                Assert.AreEqual(100, input.ReadBytes().Length);

                tag = input.ReadTag();
                Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
                Assert.AreEqual(100, input.ReadBytes().Length);
            }
        }
Пример #4
0
     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
     }
Пример #5
0
        public void TestCodedInputOutputPosition()
        {
            byte[] content = new byte[110];
            for (int i = 0; i < content.Length; i++)
            {
                content[i] = (byte)i;
            }

            byte[] child = new byte[120];
            {
                MemoryStream      ms   = new MemoryStream(child);
                CodedOutputStream cout = new CodedOutputStream(ms, 20);
                // Field 11: numeric value: 500
                cout.WriteTag(11, WireFormat.WireType.Varint);
                Assert.AreEqual(1, cout.Position);
                cout.WriteInt32(500);
                Assert.AreEqual(3, cout.Position);
                //Field 12: length delimited 120 bytes
                cout.WriteTag(12, WireFormat.WireType.LengthDelimited);
                Assert.AreEqual(4, cout.Position);
                cout.WriteBytes(ByteString.CopyFrom(content));
                Assert.AreEqual(115, cout.Position);
                // Field 13: fixed numeric value: 501
                cout.WriteTag(13, WireFormat.WireType.Fixed32);
                Assert.AreEqual(116, cout.Position);
                cout.WriteSFixed32(501);
                Assert.AreEqual(120, cout.Position);
                cout.Flush();
            }

            byte[] bytes = new byte[130];
            {
                CodedOutputStream cout = new CodedOutputStream(bytes);
                // Field 1: numeric value: 500
                cout.WriteTag(1, WireFormat.WireType.Varint);
                Assert.AreEqual(1, cout.Position);
                cout.WriteInt32(500);
                Assert.AreEqual(3, cout.Position);
                //Field 2: length delimited 120 bytes
                cout.WriteTag(2, WireFormat.WireType.LengthDelimited);
                Assert.AreEqual(4, cout.Position);
                cout.WriteBytes(ByteString.CopyFrom(child));
                Assert.AreEqual(125, cout.Position);
                // Field 3: fixed numeric value: 500
                cout.WriteTag(3, WireFormat.WireType.Fixed32);
                Assert.AreEqual(126, cout.Position);
                cout.WriteSFixed32(501);
                Assert.AreEqual(130, cout.Position);
                cout.Flush();
            }
            // Now test Input stream:
            {
                CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50], 0, 0);
                Assert.AreEqual(0, cin.Position);
                // Field 1:
                uint tag = cin.ReadTag();
                Assert.AreEqual(1, tag >> 3);
                Assert.AreEqual(1, cin.Position);
                Assert.AreEqual(500, cin.ReadInt32());
                Assert.AreEqual(3, cin.Position);
                //Field 2:
                tag = cin.ReadTag();
                Assert.AreEqual(2, tag >> 3);
                Assert.AreEqual(4, cin.Position);
                int childlen = cin.ReadLength();
                Assert.AreEqual(120, childlen);
                Assert.AreEqual(5, cin.Position);
                int oldlimit = cin.PushLimit((int)childlen);
                Assert.AreEqual(5, cin.Position);
                // Now we are reading child message
                {
                    // Field 11: numeric value: 500
                    tag = cin.ReadTag();
                    Assert.AreEqual(11, tag >> 3);
                    Assert.AreEqual(6, cin.Position);
                    Assert.AreEqual(500, cin.ReadInt32());
                    Assert.AreEqual(8, cin.Position);
                    //Field 12: length delimited 120 bytes
                    tag = cin.ReadTag();
                    Assert.AreEqual(12, tag >> 3);
                    Assert.AreEqual(9, cin.Position);
                    ByteString bstr = cin.ReadBytes();
                    Assert.AreEqual(110, bstr.Length);
                    Assert.AreEqual((byte)109, bstr[109]);
                    Assert.AreEqual(120, cin.Position);
                    // Field 13: fixed numeric value: 501
                    tag = cin.ReadTag();
                    Assert.AreEqual(13, tag >> 3);
                    // ROK - Previously broken here, this returned 126 failing to account for bufferSizeAfterLimit
                    Assert.AreEqual(121, cin.Position);
                    Assert.AreEqual(501, cin.ReadSFixed32());
                    Assert.AreEqual(125, cin.Position);
                    Assert.IsTrue(cin.IsAtEnd);
                }
                cin.PopLimit(oldlimit);
                Assert.AreEqual(125, cin.Position);
                // Field 3: fixed numeric value: 501
                tag = cin.ReadTag();
                Assert.AreEqual(3, tag >> 3);
                Assert.AreEqual(126, cin.Position);
                Assert.AreEqual(501, cin.ReadSFixed32());
                Assert.AreEqual(130, cin.Position);
                Assert.IsTrue(cin.IsAtEnd);
            }
        }
Пример #6
0
        public void TestSlowPathAvoidance()
        {
            using (var ms = new MemoryStream())
            {
                CodedOutputStream output = new CodedOutputStream(ms);
                output.WriteTag(1, WireFormat.WireType.LengthDelimited);
                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
                output.WriteTag(2, WireFormat.WireType.LengthDelimited);
                output.WriteBytes(ByteString.CopyFrom(new byte[100]));
                output.Flush();

                ms.Position = 0;
                CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0);

                uint tag = input.ReadTag();
                Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
                Assert.AreEqual(100, input.ReadBytes().Length);

                tag = input.ReadTag();
                Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
                Assert.AreEqual(100, input.ReadBytes().Length);
            }
        }
Пример #7
0
        public void TestCodedInputOutputPosition()
        {
            byte[] content = new byte[110];
            for (int i = 0; i < content.Length; i++)
                content[i] = (byte)i;

            byte[] child = new byte[120];
            {
                MemoryStream ms = new MemoryStream(child);
                CodedOutputStream cout = new CodedOutputStream(ms, 20);
                // Field 11: numeric value: 500
                cout.WriteTag(11, WireFormat.WireType.Varint);
                Assert.AreEqual(1, cout.Position);
                cout.WriteInt32(500);
                Assert.AreEqual(3, cout.Position);
                //Field 12: length delimited 120 bytes
                cout.WriteTag(12, WireFormat.WireType.LengthDelimited);
                Assert.AreEqual(4, cout.Position);
                cout.WriteBytes(ByteString.CopyFrom(content));
                Assert.AreEqual(115, cout.Position);
                // Field 13: fixed numeric value: 501
                cout.WriteTag(13, WireFormat.WireType.Fixed32);
                Assert.AreEqual(116, cout.Position);
                cout.WriteSFixed32(501);
                Assert.AreEqual(120, cout.Position);
                cout.Flush();
            }

            byte[] bytes = new byte[130];
            {
                CodedOutputStream cout = new CodedOutputStream(bytes);
                // Field 1: numeric value: 500
                cout.WriteTag(1, WireFormat.WireType.Varint);
                Assert.AreEqual(1, cout.Position);
                cout.WriteInt32(500);
                Assert.AreEqual(3, cout.Position);
                //Field 2: length delimited 120 bytes
                cout.WriteTag(2, WireFormat.WireType.LengthDelimited);
                Assert.AreEqual(4, cout.Position);
                cout.WriteBytes(ByteString.CopyFrom(child));
                Assert.AreEqual(125, cout.Position);
                // Field 3: fixed numeric value: 500
                cout.WriteTag(3, WireFormat.WireType.Fixed32);
                Assert.AreEqual(126, cout.Position);
                cout.WriteSFixed32(501);
                Assert.AreEqual(130, cout.Position);
                cout.Flush();
            }
            // Now test Input stream:
            {
                CodedInputStream cin = new CodedInputStream(new MemoryStream(bytes), new byte[50], 0, 0);
                Assert.AreEqual(0, cin.Position);
                // Field 1:
                uint tag = cin.ReadTag();
                Assert.AreEqual(1, tag >> 3);
                Assert.AreEqual(1, cin.Position);
                Assert.AreEqual(500, cin.ReadInt32());
                Assert.AreEqual(3, cin.Position);
                //Field 2:
                tag = cin.ReadTag();
                Assert.AreEqual(2, tag >> 3);
                Assert.AreEqual(4, cin.Position);
                int childlen = cin.ReadLength();
                Assert.AreEqual(120, childlen);
                Assert.AreEqual(5, cin.Position);
                int oldlimit = cin.PushLimit((int)childlen);
                Assert.AreEqual(5, cin.Position);
                // Now we are reading child message
                {
                    // Field 11: numeric value: 500
                    tag = cin.ReadTag();
                    Assert.AreEqual(11, tag >> 3);
                    Assert.AreEqual(6, cin.Position);
                    Assert.AreEqual(500, cin.ReadInt32());
                    Assert.AreEqual(8, cin.Position);
                    //Field 12: length delimited 120 bytes
                    tag = cin.ReadTag();
                    Assert.AreEqual(12, tag >> 3);
                    Assert.AreEqual(9, cin.Position);
                    ByteString bstr = cin.ReadBytes();
                    Assert.AreEqual(110, bstr.Length);
                    Assert.AreEqual((byte) 109, bstr[109]);
                    Assert.AreEqual(120, cin.Position);
                    // Field 13: fixed numeric value: 501
                    tag = cin.ReadTag();
                    Assert.AreEqual(13, tag >> 3);
                    // ROK - Previously broken here, this returned 126 failing to account for bufferSizeAfterLimit
                    Assert.AreEqual(121, cin.Position);
                    Assert.AreEqual(501, cin.ReadSFixed32());
                    Assert.AreEqual(125, cin.Position);
                    Assert.IsTrue(cin.IsAtEnd);
                }
                cin.PopLimit(oldlimit);
                Assert.AreEqual(125, cin.Position);
                // Field 3: fixed numeric value: 501
                tag = cin.ReadTag();
                Assert.AreEqual(3, tag >> 3);
                Assert.AreEqual(126, cin.Position);
                Assert.AreEqual(501, cin.ReadSFixed32());
                Assert.AreEqual(130, cin.Position);
                Assert.IsTrue(cin.IsAtEnd);
            }
        }
Пример #8
0
 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);
 }
Пример #9
0
 /// <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 ());
 }
Пример #10
0
 internal void <ForBytes> b__1_1(CodedOutputStream output, ByteString value)
 {
     output.WriteBytes(value);
 }
Пример #11
0
 /// <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 ());
 }