Ejemplo n.º 1
0
        public override object Read(BinaryReader reader, byte type)
        {
            switch (type)
            {
                #region Decimal
            case DataType.Decimal:
                var v1 = reader.ReadDecimal();
                DecimalCache.DeserializerSet(v1);
                return(v1);

            case DataType.DecimalDefault:
                return(default(decimal));

            case DataType.RefDecimalByte:
                return(DecimalCache.DeserializerGet(reader.ReadByte()));

            case DataType.RefDecimalUShort:
                return(DecimalCache.DeserializerGet(reader.ReadUInt16()));

                #endregion

                #region Double
            case DataType.Double:
                var v2 = reader.ReadDouble();
                DoubleCache.DeserializerSet(v2);
                return(v2);

            case DataType.DoubleDefault:
                return(default(double));

            case DataType.RefDoubleByte:
                return(DoubleCache.DeserializerGet(reader.ReadByte()));

            case DataType.RefDoubleUShort:
                return(DoubleCache.DeserializerGet(reader.ReadUInt16()));

                #endregion

                #region Float
            case DataType.Float:
                var v3 = reader.ReadSingle();
                FloatCache.DeserializerSet(v3);
                return(v3);

            case DataType.FloatDefault:
                return(default(float));

            case DataType.RefFloatByte:
                return(FloatCache.DeserializerGet(reader.ReadByte()));

            case DataType.RefFloatUShort:
                return(FloatCache.DeserializerGet(reader.ReadUInt16()));

                #endregion

                #region Long
            case DataType.Long:
                var v4 = reader.ReadInt64();
                LongCache.DeserializerSet(v4);
                return(v4);

            case DataType.RefLongByte:
                return(LongCache.DeserializerGet(reader.ReadByte()));

            case DataType.RefLongUShort:
                return(LongCache.DeserializerGet(reader.ReadUInt16()));

                #endregion

                #region ULong
            case DataType.ULong:
                var v5 = reader.ReadUInt64();
                ULongCache.DeserializerSet(v5);
                return(v5);

            case DataType.RefULongByte:
                return(ULongCache.DeserializerGet(reader.ReadByte()));

            case DataType.RefULongUShort:
                return(ULongCache.DeserializerGet(reader.ReadUInt16()));

                #endregion

                #region Int
            case DataType.Int:
                var v6 = reader.ReadInt32();
                IntCache.DeserializerSet(v6);
                return(v6);

            case DataType.RefIntByte:
                return(IntCache.DeserializerGet(reader.ReadByte()));

            case DataType.RefIntUShort:
                return(IntCache.DeserializerGet(reader.ReadUInt16()));

                #endregion

                #region UInt
            case DataType.UInt:
                var v7 = reader.ReadUInt32();
                UIntCache.DeserializerSet(v7);
                return(v7);

            case DataType.RefUIntByte:
                return(UIntCache.DeserializerGet(reader.ReadByte()));

            case DataType.RefUIntUShort:
                return(UIntCache.DeserializerGet(reader.ReadUInt16()));

                #endregion

                #region Short
            case DataType.Short:
                var v8 = reader.ReadInt16();
                ShortCache.DeserializerSet(v8);
                return(v8);

            case DataType.RefShortByte:
                return(ShortCache.DeserializerGet(reader.ReadByte()));

                #endregion

                #region UShort
            case DataType.UShort:
                var v9 = reader.ReadUInt16();
                UShortCache.DeserializerSet(v9);
                return(v9);

            case DataType.RefUShortByte:
                return(UShortCache.DeserializerGet(reader.ReadByte()));

                #endregion

                #region Byte
            case DataType.Byte:
                return(reader.ReadByte());

            case DataType.ByteDefault:
                return((byte)0);

            case DataType.Byte1:
                return((byte)1);

            case DataType.Byte2:
                return((byte)2);

            case DataType.Byte3:
                return((byte)3);

            case DataType.Byte4:
                return((byte)4);

            case DataType.Byte5:
                return((byte)5);

            case DataType.Byte6:
                return((byte)6);

            case DataType.Byte7:
                return((byte)7);

            case DataType.Byte8:
                return((byte)8);

            case DataType.Byte9:
                return((byte)9);

            case DataType.Byte10:
                return((byte)10);

            case DataType.Byte11:
                return((byte)11);

            case DataType.Byte12:
                return((byte)12);

            case DataType.Byte13:
                return((byte)13);

            case DataType.Byte14:
                return((byte)14);

            case DataType.Byte15:
                return((byte)15);

            case DataType.Byte16:
                return((byte)16);

                #endregion

            //SByte
            case DataType.SByte:
                return(reader.ReadSByte());

            case DataType.SByteMinusOne:
                return(-1);
            }
            return(0);
        }
Ejemplo n.º 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
            }
        }