Beispiel #1
0
        public override void WriteValue(BinaryWriter writer, int value)
        {
            switch (value)
            {
            case -1:
                writer.Write(DataType.SByteMinusOne);
                return;

            case 0:
                writer.Write(DataType.ByteDefault);
                return;

            case 1:
                writer.Write(DataType.Byte1);
                return;

            case 2:
                writer.Write(DataType.Byte2);
                return;

            case 3:
                writer.Write(DataType.Byte3);
                return;

            case 4:
                writer.Write(DataType.Byte4);
                return;

            case 5:
                writer.Write(DataType.Byte5);
                return;

            case 6:
                writer.Write(DataType.Byte6);
                return;

            case 7:
                writer.Write(DataType.Byte7);
                return;

            case 8:
                writer.Write(DataType.Byte8);
                return;

            case 9:
                writer.Write(DataType.Byte9);
                return;

            case 10:
                writer.Write(DataType.Byte10);
                return;

            case 11:
                writer.Write(DataType.Byte11);
                return;

            case 12:
                writer.Write(DataType.Byte12);
                return;

            case 13:
                writer.Write(DataType.Byte13);
                return;

            case 14:
                writer.Write(DataType.Byte14);
                return;

            case 15:
                writer.Write(DataType.Byte15);
                return;

            case 16:
                writer.Write(DataType.Byte16);
                return;
            }

            var decType = DataTypeHelper.GetDecreaseDataType(value);
            int objIdx;

            switch (decType)
            {
            case DataType.Int:
                #region Int Type
                objIdx = IntCache.SerializerGet(value);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefIntByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefIntUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    WriteInt(writer, DataType.Int, value);
                    IntCache.SerializerSet(value);
                }
                #endregion
                return;

            case DataType.Short:
                #region Short Type
                var v8 = (short)value;
                objIdx = ShortCache.SerializerGet(v8);
                if (objIdx > -1 && objIdx <= byte.MaxValue)
                {
                    WriteByte(writer, DataType.RefShortByte, (byte)objIdx);
                }
                else
                {
                    WriteShort(writer, DataType.Short, v8);
                    ShortCache.SerializerSet(v8);
                }
                #endregion
                return;

            case DataType.UShort:
                #region UShort Type
                var v9 = (ushort)value;
                objIdx = UShortCache.SerializerGet(v9);
                if (objIdx > -1 && objIdx <= byte.MaxValue)
                {
                    WriteByte(writer, DataType.RefUShortByte, (byte)objIdx);
                }
                else
                {
                    WriteUshort(writer, DataType.UShort, v9);
                    UShortCache.SerializerSet(v9);
                }
                #endregion
                return;

            case DataType.Byte:
                #region Byte Type
                WriteByte(writer, DataType.Byte, (byte)value);
                #endregion
                return;

            case DataType.SByte:
                #region SByte Type
                writer.Write(DataType.SByte);
                writer.Write((sbyte)value);
                #endregion
                return;
            }
        }
Beispiel #2
0
        public override void Write(BinaryWriter writer, object value)
        {
            var valueType = value?.GetType();
            var decType   = DataTypeHelper.GetDecreaseDataType(value, valueType);
            var convValue = (IConvertible)value ?? 0;
            int objIdx;

            switch (decType)
            {
            case DataType.Decimal:
                #region Decimal Type
                var v1 = convValue.ToDecimal(null);
                if (v1 == default(decimal))
                {
                    writer.Write(DataType.DecimalDefault);
                    return;
                }
                objIdx = DecimalCache.SerializerGet(v1);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefDecimalByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefDecimalUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    writer.Write(DataType.Decimal);
                    writer.Write(v1);
                    DecimalCache.SerializerSet(v1);
                }
                #endregion
                return;

            case DataType.Double:
                #region Double Type
                var v2 = convValue.ToDouble(null);
                if (Math.Abs(v2 - default(double)) < 0.0000000000001)
                {
                    writer.Write(DataType.DoubleDefault);
                    return;
                }
                objIdx = DoubleCache.SerializerGet(v2);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefDoubleByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefDoubleUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    WriteDouble(writer, DataType.Double, v2);
                    DoubleCache.SerializerSet(v2);
                }
                #endregion
                return;

            case DataType.Float:
                #region Float Type
                var v3 = convValue.ToSingle(null);
                if (Math.Abs(v3 - default(float)) < 0.0000000000001)
                {
                    writer.Write(DataType.FloatDefault);
                    return;
                }
                objIdx = FloatCache.SerializerGet(v3);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefFloatByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefFloatUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    WriteFloat(writer, DataType.Float, v3);
                    FloatCache.SerializerSet(v3);
                }
                #endregion
                return;

            case DataType.Long:
                #region Long Type
                var v4 = convValue.ToInt64(null);
                objIdx = LongCache.SerializerGet(v4);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefLongByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefLongUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    WriteLong(writer, DataType.Long, v4);
                    LongCache.SerializerSet(v4);
                }
                #endregion
                return;

            case DataType.ULong:
                #region ULong Type
                var v5 = convValue.ToUInt64(null);
                objIdx = ULongCache.SerializerGet(v5);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefULongByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefULongUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    WriteULong(writer, DataType.ULong, v5);
                    ULongCache.SerializerSet(v5);
                }
                #endregion
                return;

            case DataType.Int:
                #region Int Type
                var v6 = convValue.ToInt32(null);
                objIdx = IntCache.SerializerGet(v6);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefIntByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefIntUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    WriteInt(writer, DataType.Int, v6);
                    IntCache.SerializerSet(v6);
                }
                #endregion
                return;

            case DataType.UInt:
                #region UInt Type
                var v7 = convValue.ToUInt32(null);
                objIdx = UIntCache.SerializerGet(v7);
                if (objIdx > -1)
                {
                    if (objIdx <= byte.MaxValue)
                    {
                        WriteByte(writer, DataType.RefUIntByte, (byte)objIdx);
                    }
                    else
                    {
                        WriteUshort(writer, DataType.RefUIntUShort, (ushort)objIdx);
                    }
                }
                else
                {
                    WriteUInt(writer, DataType.UInt, v7);
                    UIntCache.SerializerSet(v7);
                }
                #endregion
                return;

            case DataType.Short:
                #region Short Type
                var v8 = convValue.ToInt16(null);
                objIdx = ShortCache.SerializerGet(v8);
                if (objIdx > -1 && objIdx <= byte.MaxValue)
                {
                    WriteByte(writer, DataType.RefShortByte, (byte)objIdx);
                }
                else
                {
                    WriteShort(writer, DataType.Short, v8);
                    ShortCache.SerializerSet(v8);
                }
                #endregion
                return;

            case DataType.UShort:
                #region UShort Type
                var v9 = convValue.ToUInt16(null);
                objIdx = UShortCache.SerializerGet(v9);
                if (objIdx > -1 && objIdx <= byte.MaxValue)
                {
                    WriteByte(writer, DataType.RefUShortByte, (byte)objIdx);
                }
                else
                {
                    WriteUshort(writer, DataType.UShort, v9);
                    UShortCache.SerializerSet(v9);
                }
                #endregion
                return;

            case DataType.Byte:
                #region Byte Type
                var v10 = convValue.ToByte(null);
                switch (v10)
                {
                case 0:
                    writer.Write(DataType.ByteDefault);
                    return;

                case 1:
                    writer.Write(DataType.Byte1);
                    return;

                case 2:
                    writer.Write(DataType.Byte2);
                    return;

                case 3:
                    writer.Write(DataType.Byte3);
                    return;

                case 4:
                    writer.Write(DataType.Byte4);
                    return;

                case 5:
                    writer.Write(DataType.Byte5);
                    return;

                case 6:
                    writer.Write(DataType.Byte6);
                    return;

                case 7:
                    writer.Write(DataType.Byte7);
                    return;

                case 8:
                    writer.Write(DataType.Byte8);
                    return;

                case 9:
                    writer.Write(DataType.Byte9);
                    return;

                case 10:
                    writer.Write(DataType.Byte10);
                    return;

                case 11:
                    writer.Write(DataType.Byte11);
                    return;

                case 12:
                    writer.Write(DataType.Byte12);
                    return;

                case 13:
                    writer.Write(DataType.Byte13);
                    return;

                case 14:
                    writer.Write(DataType.Byte14);
                    return;

                case 15:
                    writer.Write(DataType.Byte15);
                    return;

                case 16:
                    writer.Write(DataType.Byte16);
                    return;

                default:
                    WriteByte(writer, DataType.Byte, v10);
                    return;
                }

                #endregion
            case DataType.SByte:
                #region SByte Type
                var sByte = convValue.ToSByte(null);
                switch (sByte)
                {
                case -1:
                    writer.Write(DataType.SByteMinusOne);
                    return;

                default:
                    writer.Write(DataType.SByte);
                    writer.Write(sByte);
                    return;
                }
                #endregion
            }
        }