Google.Protobuf.CodedOutputStream.WriteFloat C# (CSharp) Method

WriteFloat() public method

Writes a float field value, without a tag, to the stream.
public WriteFloat ( float value ) : void
value float The value to write
return void
        public void WriteFloat(float value)
        {
            byte[] rawBytes = BitConverter.GetBytes(value);
            if (!BitConverter.IsLittleEndian)
            {
                ByteArray.Reverse(rawBytes);
            }

            if (limit - position >= 4)
            {
                buffer[position++] = rawBytes[0];
                buffer[position++] = rawBytes[1];
                buffer[position++] = rawBytes[2];
                buffer[position++] = rawBytes[3];
            }
            else
            {
                WriteRawBytes(rawBytes, 0, 4);
            }
        }

Usage Example

Example #1
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);
 }
All Usage Examples Of Google.Protobuf.CodedOutputStream::WriteFloat