public void WriteValue(BinaryWriter writer, TimeSpan value) { if (value == default(TimeSpan)) { writer.Write(DataType.TimeSpanDefault); return; } var objIdx = _cache.SerializerGet(value); if (objIdx > -1) { if (objIdx <= byte.MaxValue) { WriteHelper.WriteByte(writer, DataType.RefTimeSpanByte, (byte)objIdx); } else { WriteHelper.WriteUshort(writer, DataType.RefTimeSpanUShort, (ushort)objIdx); } } else { var longBinary = value.Ticks; WriteHelper.WriteLong(writer, DataType.TimeSpan, longBinary); _cache.SerializerSet(value); } }
public void WriteValue(BinaryWriter writer, Guid value) { if (value == default(Guid)) { writer.Write(DataType.GuidDefault); return; } var objIdx = _cache.SerializerGet(value); if (objIdx > -1) { if (objIdx <= byte.MaxValue) { WriteHelper.WriteByte(writer, DataType.RefGuidByte, (byte)objIdx); } else { WriteHelper.WriteUshort(writer, DataType.RefGuidUShort, (ushort)objIdx); } } else { writer.Write(DataType.Guid); var bytes = value.ToByteArray(); writer.Write(bytes, 0, bytes.Length); _cache.SerializerSet(value); } }
public void WriteValue(BinaryWriter writer, DateTime value) { if (value == default(DateTime)) { writer.Write(DataType.DateTimeDefault); return; } var objIdx = _cache.SerializerGet(value); if (objIdx > -1) { if (objIdx <= byte.MaxValue) { WriteHelper.WriteByte(writer, DataType.RefDateTimeByte, (byte)objIdx); } else { WriteHelper.WriteUshort(writer, DataType.RefDateTimeUShort, (ushort)objIdx); } } else { var longBinary = value.ToBinary(); WriteHelper.WriteLong(writer, DataType.DateTime, longBinary); _cache.SerializerSet(value); } }
public void WriteValue(BinaryWriter writer, string value) { if (value == null) { writer.Write(DataType.StringNull); return; } if (value == string.Empty) { writer.Write(DataType.StringEmpty); return; } var vLength = value.Length; if (vLength > 2) { if (vLength <= 8) { var objIdx = _cache8.SerializerGet(value); if (objIdx > -1) { if (objIdx < 16) { writer.Write((byte)(DataType.RefString8Byte0 + objIdx)); } else if (objIdx <= byte.MaxValue) { WriteHelper.WriteByte(writer, DataType.RefString8Byte, (byte)objIdx); } else { WriteHelper.WriteUshort(writer, DataType.RefString8UShort, (ushort)objIdx); } return; } _cache8.SerializerSet(value); } else if (vLength <= 16) { var objIdx = _cache16.SerializerGet(value); if (objIdx > -1) { if (objIdx < 16) { writer.Write((byte)(DataType.RefString16Byte0 + objIdx)); } else if (objIdx <= byte.MaxValue) { WriteHelper.WriteByte(writer, DataType.RefString16Byte, (byte)objIdx); } else { WriteHelper.WriteUshort(writer, DataType.RefString16UShort, (ushort)objIdx); } return; } _cache16.SerializerSet(value); } else if (vLength <= 32) { var objIdx = _cache32.SerializerGet(value); if (objIdx > -1) { if (objIdx < 16) { writer.Write((byte)(DataType.RefString32Byte0 + objIdx)); } else if (objIdx <= byte.MaxValue) { WriteHelper.WriteByte(writer, DataType.RefString32Byte, (byte)objIdx); } else { WriteHelper.WriteUshort(writer, DataType.RefString32UShort, (ushort)objIdx); } return; } _cache32.SerializerSet(value); } else { var objIdx = _cache.SerializerGet(value); if (objIdx > -1) { if (objIdx <= byte.MaxValue) { WriteHelper.WriteByte(writer, DataType.RefStringByte, (byte)objIdx); } else { WriteHelper.WriteUshort(writer, DataType.RefStringUShort, (ushort)objIdx); } return; } _cache.SerializerSet(value); } } var length = DefaultUTF8Encoding.GetByteCount(value); int bytesLength; byte[] bytes; if (length < 21) { bytesLength = length + 1; bytes = ArrayPool <byte> .Shared.Rent(bytesLength); bytes[0] = (byte)(DataType.StringLengthByte1 + (length - 1)); DefaultUTF8Encoding.GetBytes(value, 0, value.Length, bytes, 1); } else if (length <= byte.MaxValue) { bytesLength = length + 2; bytes = ArrayPool <byte> .Shared.Rent(bytesLength); bytes[0] = DataType.StringLengthByte; bytes[1] = (byte)length; DefaultUTF8Encoding.GetBytes(value, 0, value.Length, bytes, 2); } else if (length <= ushort.MaxValue) { bytesLength = length + 3; bytes = ArrayPool <byte> .Shared.Rent(bytesLength); bytes[0] = DataType.StringLengthUShort; bytes[1] = (byte)(ushort)length; bytes[2] = (byte)(((ushort)length) >> 8); DefaultUTF8Encoding.GetBytes(value, 0, value.Length, bytes, 3); } else { bytesLength = length + 5; bytes = ArrayPool <byte> .Shared.Rent(bytesLength); bytes[0] = DataType.StringLengthInt; bytes[1] = (byte)length; bytes[2] = (byte)(length >> 8); bytes[3] = (byte)(length >> 16); bytes[4] = (byte)(length >> 24); DefaultUTF8Encoding.GetBytes(value, 0, value.Length, bytes, 5); } writer.Write(bytes, 0, bytesLength); ArrayPool <byte> .Shared.Return(bytes); }