コード例 #1
0
        static void WriteBufferedBytes(CustomBuffer customBuffer, byte[] buffer, int tag)
        {
            var lengthBuffer = Int32ToBytes(buffer.Length);

            customBuffer.Write(EncodeLength(lengthBuffer.Length, tag));
            customBuffer.Write(lengthBuffer);
            customBuffer.Write(buffer);
        }
コード例 #2
0
 public static void WriteTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan value, int tag, bool isTargetCollection)
 {
     if (value == TimeSpan.Zero && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, TimeSpanToBytes(value), tag);
 }
コード例 #3
0
 public static void WriteByteToBuffer(CustomBuffer customBuffer, byte value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, new [] { value }, tag);
 }
コード例 #4
0
 public static void WriteCharToBuffer(CustomBuffer customBuffer, char value, int tag, bool isTargetCollection)
 {
     if (value == char.MinValue && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, Int16ToBytes((short)value), tag);
 }
コード例 #5
0
 public static void WriteDecimalToBuffer(CustomBuffer customBuffer, decimal value, int tag, bool isTargetCollection)
 {
     if (value == 0m && !isTargetCollection)
     {
         return;
     }
     WriteBufferedBytes(customBuffer, DecimalToByteArray(value), tag);
 }
コード例 #6
0
 public static void WriteBoolToBuffer(CustomBuffer customBuffer, bool value, int tag, bool isTargetCollection)
 {
     if (value == false && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, value ? TrueBooleanBytes : FalseBooleanBytes, tag);
 }
コード例 #7
0
 public static void WriteInt32ToBuffer(CustomBuffer customBuffer, int value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, Int32ToBytes(value), tag);
 }
コード例 #8
0
 public static void WriteDateTimeToBuffer(CustomBuffer customBuffer, DateTime value, int tag, bool isTargetCollection)
 {
     if (value == DateTime.MinValue && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, DateTimeToByteArray(value), tag);
 }
コード例 #9
0
 public static void WriteGuidToBuffer(CustomBuffer customBuffer, Guid value, int tag, bool isTargetCollection)
 {
     if (value == Guid.Empty && !isTargetCollection)
     {
         return;
     }
     WriteBufferedBytes(customBuffer, value.ToByteArray(), tag);
 }
コード例 #10
0
 public static void WriteStringToBuffer(CustomBuffer customBuffer, string value, int tag, bool isTargetCollection)
 {
     if (value == null && !isTargetCollection)
     {
         value = NullString;
     }
     WriteBufferedBytes(customBuffer, StringToByteArray(value), tag);
 }
コード例 #11
0
 public static void WriteTypeIDFor(CustomBuffer customBuffer, Type baseType, Type type)
 {
     if (baseType == ObjectType)
     {
         return;
     }
     TypeIDByteArray[0] = TypeMapping[baseType][type];
     customBuffer.Write(TypeIDByteArray);
 }
コード例 #12
0
 public static void WriteFloatToBuffer(CustomBuffer customBuffer, float value, int tag, bool isTargetCollection)
 {
     if (value == 0f && !isTargetCollection)
     {
         return;
     }
     unsafe {
         WriteUnBufferedBytes(customBuffer, Int32ToBytes(*((int *)&value)), tag);
     }
 }
コード例 #13
0
        public static void WriteEnumToBuffer(CustomBuffer customBuffer, Enum value, int tag, bool isTargetCollection)
        {
            var enumValue = ((IConvertible)value).ToInt32(null);

            if (enumValue == 0 && !isTargetCollection)
            {
                return;
            }
            WriteUnBufferedBytes(customBuffer, Int32ToBytes(enumValue), tag);
        }
コード例 #14
0
        public static void WriteDoubleToBuffer(CustomBuffer customBuffer, double value, int tag, bool isTargetCollection)
        {
            if (value == 0d && !isTargetCollection)
            {
                return;
            }

            byte[] buffer;

            if (value == double.MaxValue)
            {
                buffer = _demicalMax;
            }
            else if (value == double.MinValue)
            {
                buffer = _demicalMin;
            }
            else
            {
                buffer = BitConverter.GetBytes(value);
                if (BitConverter.IsLittleEndian)
                {
                    var temp = buffer[0];
                    buffer[0] = buffer[7];
                    buffer[7] = temp;

                    temp      = buffer[1];
                    buffer[1] = buffer[6];
                    buffer[6] = temp;

                    temp      = buffer[2];
                    buffer[2] = buffer[5];
                    buffer[5] = temp;

                    temp      = buffer[3];
                    buffer[3] = buffer[4];
                    buffer[4] = temp;
                }
            }

            WriteUnBufferedBytes(customBuffer, buffer, tag);
        }
コード例 #15
0
        public static void WriteDoubleToBuffer(CustomBuffer customBuffer, double value, int tag, bool isTargetCollection)
        {
            if (value == 0d && !isTargetCollection) return;

            byte[] buffer;

            if (value == double.MaxValue)
                buffer = _demicalMax;
            else if (value == double.MinValue)
                buffer = _demicalMin;
            else {
                buffer = BitConverter.GetBytes(value);
                if (BitConverter.IsLittleEndian) {
                    var temp = buffer[0];
                    buffer[0] = buffer[7];
                    buffer[7] = temp;

                    temp = buffer[1];
                    buffer[1] = buffer[6];
                    buffer[6] = temp;

                    temp = buffer[2];
                    buffer[2] = buffer[5];
                    buffer[5] = temp;

                    temp = buffer[3];
                    buffer[3] = buffer[4];
                    buffer[4] = temp;
                }
            }

            WriteUnBufferedBytes(customBuffer, buffer, tag);
        }
コード例 #16
0
 static void WriteUnBufferedBytes(CustomBuffer customBuffer, byte[] buffer, int tag)
 {
     customBuffer.Write(EncodeLength(buffer.Length, tag));
     customBuffer.Write(buffer);
 }
コード例 #17
0
 public static void WriteEnumToBuffer(CustomBuffer customBuffer, Enum value, int tag, bool isTargetCollection)
 {
     var enumValue = ((IConvertible)value).ToInt32(null);
     if (enumValue == 0 && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, Int32ToBytes(enumValue), tag);
 }
コード例 #18
0
        public static void WriteObjectToBuffer(CustomBuffer customBuffer, object value, int tag, bool isTargetCollection)
        {
            if (value == null && !isTargetCollection) return;
            byte[] buffer = null;
            var type = value == null ? VoidType : value.GetType();
            if (type.IsGenericType &&
                type.GetGenericTypeDefinition() == NullableType)
                type = type.GetGenericArguments()[0];
            if (type == typeof(string)) {
                buffer = StringToByteArray((string)value);
            } else if (type == typeof(int)) {
                buffer = Int32ToBytes((int)value);
            } else if (type == typeof(byte)) {
                buffer = Int32ToBytes((byte)value);
            } else if (type == typeof(DateTime)) {
                buffer = DateTimeToByteArray((DateTime)value);
            } else if (type == typeof(bool)) {
                buffer = new byte[] { (bool)value ? (byte)1 : (byte)0 };
            } else if (type == typeof(char)) {
                buffer = Int16ToBytes((short)value);
            } else if (type == typeof(double)) {
                unsafe {
                    var dValue = (double)value;

                    if (dValue == double.MaxValue)
                        buffer = _demicalMax;
                    else if (dValue == double.MinValue)
                        buffer = _demicalMin;
                    else {
                        buffer = BitConverter.GetBytes(dValue);
                        if (BitConverter.IsLittleEndian) {
                            var temp = buffer[0];
                            buffer[0] = buffer[7];
                            buffer[7] = temp;

                            temp = buffer[1];
                            buffer[1] = buffer[6];
                            buffer[6] = temp;

                            temp = buffer[2];
                            buffer[2] = buffer[5];
                            buffer[5] = temp;

                            temp = buffer[3];
                            buffer[3] = buffer[4];
                            buffer[4] = temp;
                        }
                    }
                }
            } else if (type == typeof(short)) {
                buffer = Int16ToBytes((short)value);
            } else if (type == typeof(long)) {
                buffer = Int64ToBytes((long)value);
            } else if (type == typeof(decimal)) {
                buffer = DecimalToByteArray((decimal)value);
            } else if (type == typeof(float)) {
                unsafe {
                    var fValue = (float)value;
                    buffer = Int32ToBytes(*((int*)&fValue));
                }
            } else if (type == typeof(ushort)) {
                buffer = Int16ToBytes((short)value);
            } else if (type == typeof(uint)) {
                buffer = Int32ToBytes((int)value);
            } else if (type == typeof(int?)) {
                buffer = Int32ToBytes((value as int?) ?? 0);
            } else if (type == typeof(ulong)) {
                buffer = Int64ToBytes((long)value);
            } else if (type == typeof(Guid)) {
                buffer = ((Guid)value).ToByteArray();
            } else if (type.IsEnum) {
                buffer = Int32ToBytes(((IConvertible)value).ToInt32(null));
            } else if (type == typeof(TimeSpan)) {
                buffer = TimeSpanToBytes((TimeSpan)value);
            } else if (type == typeof(TimeSpan?)) {
                buffer = TimeSpanToBytes((value as TimeSpan?) ?? TimeSpan.Zero);
            } else if (type == VoidType) {
                buffer = new byte[0];
            }
            var buffer2 = new byte[buffer.Length + 1];
            buffer2[0] = TypeMapping[ObjectType][type];
            Buffer.BlockCopy(buffer, 0, buffer2, 1, buffer.Length);
            WriteBufferedBytes(customBuffer, buffer2, tag);
        }
コード例 #19
0
 public static void WriteNullableTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan? value, int tag, bool isTargetCollection)
 {
     WriteTimeSpanToBuffer(customBuffer, value ?? TimeSpan.Zero, tag, isTargetCollection);
 }
コード例 #20
0
 public static void WriteNullableTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan?value, int tag, bool isTargetCollection)
 {
     WriteTimeSpanToBuffer(customBuffer, value ?? TimeSpan.Zero, tag, isTargetCollection);
 }
コード例 #21
0
 public static void WriteCollectionHeader(CustomBuffer customBuffer, ICollection collection, int tag)
 {
     WriteUnBufferedBytes(customBuffer, Int32ToBytes(collection.Count), tag);
 }
コード例 #22
0
 public static void WriteCharToBuffer(CustomBuffer customBuffer, char value, int tag, bool isTargetCollection)
 {
     if (value == char.MinValue && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, Int16ToBytes((short)value), tag);
 }
コード例 #23
0
 public static void WriteByteToBuffer(CustomBuffer customBuffer, byte value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, new [] { value }, tag);
 }
コード例 #24
0
 public static void WriteDecimalToBuffer(CustomBuffer customBuffer, decimal value, int tag, bool isTargetCollection)
 {
     if (value == 0m && !isTargetCollection) return;
     WriteBufferedBytes(customBuffer, DecimalToByteArray(value), tag);
 }
コード例 #25
0
 public static void WriteCollectionHeader(CustomBuffer customBuffer, ICollection collection, int tag)
 {
     WriteUnBufferedBytes(customBuffer, Int32ToBytes(collection.Count), tag);
 }
コード例 #26
0
 public static void WriteInt16ToBuffer(CustomBuffer customBuffer, short value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, Int16ToBytes(value), tag);
 }
コード例 #27
0
 public static void WriteTypeIDFor(CustomBuffer customBuffer, Type baseType, Type type)
 {
     if (baseType == ObjectType) return;
     TypeIDByteArray[0] = TypeMapping[baseType][type];
     customBuffer.Write(TypeIDByteArray);
 }
コード例 #28
0
 public static void WriteTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan value, int tag, bool isTargetCollection)
 {
     if (value == TimeSpan.Zero && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, TimeSpanToBytes(value), tag);
 }
コード例 #29
0
 public static void WriteStringToBuffer(CustomBuffer customBuffer, string value, int tag, bool isTargetCollection)
 {
     if (value == null && !isTargetCollection) {
         value = NullString;
     }
     WriteBufferedBytes(customBuffer, StringToByteArray(value), tag);
 }
コード例 #30
0
 public static void WriteDateTimeToBuffer(CustomBuffer customBuffer, DateTime value, int tag, bool isTargetCollection)
 {
     if (value == DateTime.MinValue && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, DateTimeToByteArray(value), tag);
 }
コード例 #31
0
 public static void WriteGuidToBuffer(CustomBuffer customBuffer, Guid value, int tag, bool isTargetCollection)
 {
     if (value == Guid.Empty && !isTargetCollection) return;
     WriteBufferedBytes(customBuffer, value.ToByteArray(), tag);
 }
コード例 #32
0
 public static void WriteNullableInt32ToBuffer(CustomBuffer customBuffer, int?value, int tag, bool isTargetCollection)
 {
     WriteInt32ToBuffer(customBuffer, value ?? 0, tag, isTargetCollection);
 }
コード例 #33
0
 public static void WriteFloatToBuffer(CustomBuffer customBuffer, float value, int tag, bool isTargetCollection)
 {
     if (value == 0f && !isTargetCollection) return;
     unsafe {
         WriteUnBufferedBytes(customBuffer, Int32ToBytes(*((int*)&value)), tag);
     }
 }
コード例 #34
0
 public static void WriteUInt64ToBuffer(CustomBuffer customBuffer, ulong value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, Int64ToBytes((long)value), tag);
 }
コード例 #35
0
 public static void WriteNullableInt32ToBuffer(CustomBuffer customBuffer, int? value, int tag, bool isTargetCollection)
 {
     WriteInt32ToBuffer(customBuffer, value ?? 0, tag, isTargetCollection);
 }
コード例 #36
0
 static void WriteBufferedBytes(CustomBuffer customBuffer, byte[] buffer, int tag)
 {
     var lengthBuffer = Int32ToBytes(buffer.Length);
     customBuffer.Write(EncodeLength(lengthBuffer.Length, tag));
     customBuffer.Write(lengthBuffer);
     customBuffer.Write(buffer);
 }
コード例 #37
0
 static void WriteUnBufferedBytes(CustomBuffer customBuffer, byte[] buffer, int tag)
 {
     customBuffer.Write(EncodeLength(buffer.Length, tag));
     customBuffer.Write(buffer);
 }
コード例 #38
0
        public static void WriteObjectToBuffer(CustomBuffer customBuffer, object value, int tag, bool isTargetCollection)
        {
            if (value == null && !isTargetCollection)
            {
                return;
            }
            byte[] buffer = null;
            var    type   = value == null ? VoidType : value.GetType();

            if (type.IsGenericType &&
                type.GetGenericTypeDefinition() == NullableType)
            {
                type = type.GetGenericArguments()[0];
            }
            if (type == typeof(string))
            {
                buffer = StringToByteArray((string)value);
            }
            else if (type == typeof(int))
            {
                buffer = Int32ToBytes((int)value);
            }
            else if (type == typeof(byte))
            {
                buffer = Int32ToBytes((byte)value);
            }
            else if (type == typeof(DateTime))
            {
                buffer = DateTimeToByteArray((DateTime)value);
            }
            else if (type == typeof(bool))
            {
                buffer = new byte[] { (bool)value ? (byte)1 : (byte)0 };
            }
            else if (type == typeof(char))
            {
                buffer = Int16ToBytes((short)value);
            }
            else if (type == typeof(double))
            {
                unsafe {
                    var dValue = (double)value;

                    if (dValue == double.MaxValue)
                    {
                        buffer = _demicalMax;
                    }
                    else if (dValue == double.MinValue)
                    {
                        buffer = _demicalMin;
                    }
                    else
                    {
                        buffer = BitConverter.GetBytes(dValue);
                        if (BitConverter.IsLittleEndian)
                        {
                            var temp = buffer[0];
                            buffer[0] = buffer[7];
                            buffer[7] = temp;

                            temp      = buffer[1];
                            buffer[1] = buffer[6];
                            buffer[6] = temp;

                            temp      = buffer[2];
                            buffer[2] = buffer[5];
                            buffer[5] = temp;

                            temp      = buffer[3];
                            buffer[3] = buffer[4];
                            buffer[4] = temp;
                        }
                    }
                }
            }
            else if (type == typeof(short))
            {
                buffer = Int16ToBytes((short)value);
            }
            else if (type == typeof(long))
            {
                buffer = Int64ToBytes((long)value);
            }
            else if (type == typeof(decimal))
            {
                buffer = DecimalToByteArray((decimal)value);
            }
            else if (type == typeof(float))
            {
                unsafe {
                    var fValue = (float)value;
                    buffer = Int32ToBytes(*((int *)&fValue));
                }
            }
            else if (type == typeof(ushort))
            {
                buffer = Int16ToBytes((short)value);
            }
            else if (type == typeof(uint))
            {
                buffer = Int32ToBytes((int)value);
            }
            else if (type == typeof(int?))
            {
                buffer = Int32ToBytes((value as int?) ?? 0);
            }
            else if (type == typeof(ulong))
            {
                buffer = Int64ToBytes((long)value);
            }
            else if (type == typeof(Guid))
            {
                buffer = ((Guid)value).ToByteArray();
            }
            else if (type.IsEnum)
            {
                buffer = Int32ToBytes(((IConvertible)value).ToInt32(null));
            }
            else if (type == typeof(TimeSpan))
            {
                buffer = TimeSpanToBytes((TimeSpan)value);
            }
            else if (type == typeof(TimeSpan?))
            {
                buffer = TimeSpanToBytes((value as TimeSpan?) ?? TimeSpan.Zero);
            }
            else if (type == VoidType)
            {
                buffer = new byte[0];
            }
            var buffer2 = new byte[buffer.Length + 1];

            buffer2[0] = TypeMapping[ObjectType][type];
            Buffer.BlockCopy(buffer, 0, buffer2, 1, buffer.Length);
            WriteBufferedBytes(customBuffer, buffer2, tag);
        }
コード例 #39
0
 public static void WriteBoolToBuffer(CustomBuffer customBuffer, bool value, int tag, bool isTargetCollection)
 {
     if (value == false && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, value ? TrueBooleanBytes : FalseBooleanBytes, tag);
 }