Пример #1
0
 internal static void WriteTagWithLength(int length, EnumPattern topPattern, EnumPattern bottomPattern)
 {
     if (length > 63 || length < 0)
     {
         var bytes = LittleEndianToNetworkOrderBitConverter.GetBytes(length);
         WriteBottomPatternAndBytes(bottomPattern, bytes);
     }
     else
     {
         _buffer.WriteByte((byte)((int)topPattern | length));
     }
 }
Пример #2
0
        private void SerializeValue(object value)
        {
            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.String:
                Writer.WriteTagWithLength(Encoding.UTF8.GetBytes(value as string).Length, EnumPattern.STRING_SHORT_PATTERN, EnumPattern.STRING_PATTERN);
                Writer.Write(Encoding.UTF8.GetBytes(value as string));
                break;

            case TypeCode.Int32:
                Writer.WriteTagWithLength((int)value, EnumPattern.UNSIGNED_INT_SHORT_PATTERN, EnumPattern.INT_PATTERN);
                break;

            case TypeCode.UInt32:
                Writer.WriteBottomPatternAndBytes(EnumPattern.UNSIGNED_INT_PATTERN, LittleEndianToNetworkOrderBitConverter.GetBytes((uint)value));
                break;

            case TypeCode.Int64:
                Writer.WriteLongPattern(EnumPattern.LONG_SHORT_PATTERN, EnumPattern.LONG_PATTERN, LittleEndianToNetworkOrderBitConverter.GetBytes((long)value));
                break;

            case TypeCode.UInt64:
                Writer.WriteLongPattern(EnumPattern.UNSIGNED_LONG_SHORT_PATTERN, EnumPattern.UNSIGNED_LONG_PATTERN, LittleEndianToNetworkOrderBitConverter.GetBytes((ulong)value));
                break;

            case TypeCode.Double:
                Writer.Write(EnumPattern.DOUBLE_PATTERN);
                Writer.Write(LittleEndianToNetworkOrderBitConverter.GetBytes((double)value));
                break;

            case TypeCode.Single:
                Writer.Write(EnumPattern.FLOAT_PATTERN);
                Writer.Write(LittleEndianToNetworkOrderBitConverter.GetBytes((float)value));
                break;

            case TypeCode.Boolean:
                Writer.Write((bool)value ? EnumPattern.BOOLEAN_TRUE_PATTERN : EnumPattern.BOOLEAN_FALSE_PATTERN);
                break;

            case TypeCode.Object:
                if (!(value is byte[]))
                {
                    break;
                }

                var array = (byte[])value;

                Writer.WriteTagWithLength(array.Length, EnumPattern.BYTE_ARRAY_SHORT_PATTERN, EnumPattern.BYTE_ARRAY_PATTERN);
                Writer.Write(array);
                break;
            }
        }