コード例 #1
0
 internal static void WriteTagWithLength(int length, Pattern topPattern, Pattern 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, Pattern.STRING_SHORT_PATTERN, Pattern.STRING_PATTERN);
                Writer.Write(Encoding.UTF8.GetBytes(value as string));
                break;

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

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

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

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

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

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

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

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

                var array = (byte[])value;

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