예제 #1
0
        public virtual byte[] CreateExtras()
        {
            var        extras      = new byte[8];
            var        format      = (byte)GetFormat();
            const byte compression = (byte)Compression.None;

            Converter.SetBit(ref extras[0], 0, Converter.GetBit(format, 0));
            Converter.SetBit(ref extras[0], 1, Converter.GetBit(format, 1));
            Converter.SetBit(ref extras[0], 2, Converter.GetBit(format, 2));
            Converter.SetBit(ref extras[0], 3, Converter.GetBit(format, 3));
            Converter.SetBit(ref extras[0], 4, false);
            Converter.SetBit(ref extras[0], 5, Converter.GetBit(compression, 0));
            Converter.SetBit(ref extras[0], 6, Converter.GetBit(compression, 1));
            Converter.SetBit(ref extras[0], 7, Converter.GetBit(compression, 2));

            var typeCode = (ushort)Type.GetTypeCode(typeof(T));

            Converter.FromUInt16(typeCode, extras, 2);
            Converter.FromUInt32(Expires, extras, 4);

            Format      = (DataFormat)format;
            Compression = compression;

            Flags.DataFormat  = Format;
            Flags.Compression = Compression;
            Flags.TypeCode    = (TypeCode)typeCode;

            return(extras);
        }
        /// <summary>
        /// Writes a <see cref="UInt16" /> to a new buffer.
        /// </summary>
        /// <param name="converter">The converter.</param>
        /// <param name="value">The value.</param>
        /// <param name="useNbo">If <c>true</c> will make most significant byte first.</param>
        public static byte[] FromUInt16(this IByteConverter converter, ushort value, bool useNbo)
        {
            var buffer = new byte[sizeof(ushort)];

            converter.FromUInt16(value, buffer, useNbo);

            return(buffer);
        }
예제 #3
0
        public byte[] Serialize <T>(T value)
        {
            var bytes    = new byte[] { };
            var typeCode = Type.GetTypeCode(typeof(T));

            switch (typeCode)
            {
            case TypeCode.Empty:
            case TypeCode.DBNull:
            case TypeCode.String:
            case TypeCode.Char:
                _converter.FromString(Convert.ToString(value), ref bytes, 0);
                break;

            case TypeCode.Int16:
                _converter.FromInt16(Convert.ToInt16(value), ref bytes, 0);
                break;

            case TypeCode.UInt16:
                _converter.FromUInt16(Convert.ToUInt16(value), ref bytes, 0);
                break;

            case TypeCode.Int32:
                _converter.FromInt32(Convert.ToInt32(value), ref bytes, 0);
                break;

            case TypeCode.UInt32:
                _converter.FromUInt32(Convert.ToUInt32(value), ref bytes, 0);
                break;

            case TypeCode.Int64:
                _converter.FromInt64(Convert.ToInt64(value), ref bytes, 0);
                break;

            case TypeCode.UInt64:
                _converter.FromUInt64(Convert.ToUInt64(value), ref bytes, 0);
                break;

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
            case TypeCode.DateTime:
            case TypeCode.Boolean:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Object:
                bytes = SerializeAsJson(value);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(bytes);
        }
 /// <summary>
 /// Writes a <see cref="UInt16" /> to a new buffer, using network byte order.
 /// </summary>
 /// <param name="converter">The converter.</param>
 /// <param name="value">The value.</param>
 public static byte[] FromUInt16(this IByteConverter converter, ushort value)
 {
     return(converter.FromUInt16(value, true));
 }
 /// <summary>
 /// Writes a <see cref="UInt16"/> to a buffer, using network byte order.
 /// </summary>
 /// <param name="converter">The converter.</param>
 /// <param name="value">The value.</param>
 /// <param name="buffer">The buffer.</param>
 public static void FromUInt16(this IByteConverter converter, ushort value, Span <byte> buffer)
 {
     converter.FromUInt16(value, buffer, true);
 }