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);
        }
 public static void WriteTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan value, int tag, bool isTargetCollection)
 {
     if (value == TimeSpan.Zero && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, TimeSpanToBytes(value), tag);
 }
 public static void WriteByteToBuffer(CustomBuffer customBuffer, byte value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, new [] { value }, tag);
 }
 public static void WriteCharToBuffer(CustomBuffer customBuffer, char value, int tag, bool isTargetCollection)
 {
     if (value == char.MinValue && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, Int16ToBytes((short)value), tag);
 }
 public static void WriteDecimalToBuffer(CustomBuffer customBuffer, decimal value, int tag, bool isTargetCollection)
 {
     if (value == 0m && !isTargetCollection)
     {
         return;
     }
     WriteBufferedBytes(customBuffer, DecimalToByteArray(value), tag);
 }
 public static void WriteBoolToBuffer(CustomBuffer customBuffer, bool value, int tag, bool isTargetCollection)
 {
     if (value == false && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, value ? TrueBooleanBytes : FalseBooleanBytes, tag);
 }
 public static void WriteInt32ToBuffer(CustomBuffer customBuffer, int value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, Int32ToBytes(value), tag);
 }
 public static void WriteDateTimeToBuffer(CustomBuffer customBuffer, DateTime value, int tag, bool isTargetCollection)
 {
     if (value == DateTime.MinValue && !isTargetCollection)
     {
         return;
     }
     WriteUnBufferedBytes(customBuffer, DateTimeToByteArray(value), tag);
 }
 public static void WriteGuidToBuffer(CustomBuffer customBuffer, Guid value, int tag, bool isTargetCollection)
 {
     if (value == Guid.Empty && !isTargetCollection)
     {
         return;
     }
     WriteBufferedBytes(customBuffer, value.ToByteArray(), tag);
 }
 public static void WriteStringToBuffer(CustomBuffer customBuffer, string value, int tag, bool isTargetCollection)
 {
     if (value == null && !isTargetCollection)
     {
         value = NullString;
     }
     WriteBufferedBytes(customBuffer, StringToByteArray(value), tag);
 }
 public static void WriteTypeIDFor(CustomBuffer customBuffer, Type baseType, Type type)
 {
     if (baseType == ObjectType)
     {
         return;
     }
     TypeIDByteArray[0] = TypeMapping[baseType][type];
     customBuffer.Write(TypeIDByteArray);
 }
 public static void WriteFloatToBuffer(CustomBuffer customBuffer, float value, int tag, bool isTargetCollection)
 {
     if (value == 0f && !isTargetCollection)
     {
         return;
     }
     unsafe {
         WriteUnBufferedBytes(customBuffer, Int32ToBytes(*((int *)&value)), tag);
     }
 }
        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);
        }
        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);
        }
        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);
        }
 static void WriteUnBufferedBytes(CustomBuffer customBuffer, byte[] buffer, int tag)
 {
     customBuffer.Write(EncodeLength(buffer.Length, tag));
     customBuffer.Write(buffer);
 }
 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);
 }
        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);
        }
 public static void WriteNullableTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan? value, int tag, bool isTargetCollection)
 {
     WriteTimeSpanToBuffer(customBuffer, value ?? TimeSpan.Zero, tag, isTargetCollection);
 }
 public static void WriteNullableTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan?value, int tag, bool isTargetCollection)
 {
     WriteTimeSpanToBuffer(customBuffer, value ?? TimeSpan.Zero, tag, isTargetCollection);
 }
 public static void WriteCollectionHeader(CustomBuffer customBuffer, ICollection collection, int tag)
 {
     WriteUnBufferedBytes(customBuffer, Int32ToBytes(collection.Count), tag);
 }
 public static void WriteCharToBuffer(CustomBuffer customBuffer, char value, int tag, bool isTargetCollection)
 {
     if (value == char.MinValue && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, Int16ToBytes((short)value), tag);
 }
 public static void WriteByteToBuffer(CustomBuffer customBuffer, byte value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, new [] { value }, tag);
 }
 public static void WriteDecimalToBuffer(CustomBuffer customBuffer, decimal value, int tag, bool isTargetCollection)
 {
     if (value == 0m && !isTargetCollection) return;
     WriteBufferedBytes(customBuffer, DecimalToByteArray(value), tag);
 }
 public static void WriteCollectionHeader(CustomBuffer customBuffer, ICollection collection, int tag)
 {
     WriteUnBufferedBytes(customBuffer, Int32ToBytes(collection.Count), tag);
 }
 public static void WriteInt16ToBuffer(CustomBuffer customBuffer, short value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, Int16ToBytes(value), tag);
 }
 public static void WriteTypeIDFor(CustomBuffer customBuffer, Type baseType, Type type)
 {
     if (baseType == ObjectType) return;
     TypeIDByteArray[0] = TypeMapping[baseType][type];
     customBuffer.Write(TypeIDByteArray);
 }
 public static void WriteTimeSpanToBuffer(CustomBuffer customBuffer, TimeSpan value, int tag, bool isTargetCollection)
 {
     if (value == TimeSpan.Zero && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, TimeSpanToBytes(value), tag);
 }
 public static void WriteStringToBuffer(CustomBuffer customBuffer, string value, int tag, bool isTargetCollection)
 {
     if (value == null && !isTargetCollection) {
         value = NullString;
     }
     WriteBufferedBytes(customBuffer, StringToByteArray(value), tag);
 }
 public static void WriteDateTimeToBuffer(CustomBuffer customBuffer, DateTime value, int tag, bool isTargetCollection)
 {
     if (value == DateTime.MinValue && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, DateTimeToByteArray(value), tag);
 }
 public static void WriteGuidToBuffer(CustomBuffer customBuffer, Guid value, int tag, bool isTargetCollection)
 {
     if (value == Guid.Empty && !isTargetCollection) return;
     WriteBufferedBytes(customBuffer, value.ToByteArray(), tag);
 }
 public static void WriteNullableInt32ToBuffer(CustomBuffer customBuffer, int?value, int tag, bool isTargetCollection)
 {
     WriteInt32ToBuffer(customBuffer, value ?? 0, tag, isTargetCollection);
 }
 public static void WriteFloatToBuffer(CustomBuffer customBuffer, float value, int tag, bool isTargetCollection)
 {
     if (value == 0f && !isTargetCollection) return;
     unsafe {
         WriteUnBufferedBytes(customBuffer, Int32ToBytes(*((int*)&value)), tag);
     }
 }
 public static void WriteUInt64ToBuffer(CustomBuffer customBuffer, ulong value, int tag, bool isTargetCollection)
 {
     if (value == 0 && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, Int64ToBytes((long)value), tag);
 }
 public static void WriteNullableInt32ToBuffer(CustomBuffer customBuffer, int? value, int tag, bool isTargetCollection)
 {
     WriteInt32ToBuffer(customBuffer, value ?? 0, tag, isTargetCollection);
 }
 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);
 }
 static void WriteUnBufferedBytes(CustomBuffer customBuffer, byte[] buffer, int tag)
 {
     customBuffer.Write(EncodeLength(buffer.Length, tag));
     customBuffer.Write(buffer);
 }
        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);
        }
 public static void WriteBoolToBuffer(CustomBuffer customBuffer, bool value, int tag, bool isTargetCollection)
 {
     if (value == false && !isTargetCollection) return;
     WriteUnBufferedBytes(customBuffer, value ? TrueBooleanBytes : FalseBooleanBytes, tag);
 }