public override void Init(SerializerMode mode) { _mode = mode; Cache?.Clear(mode); Cache16?.Clear(mode); Cache32?.Clear(mode); _useCache = (mode != SerializerMode.NoCached); }
public override string ReadValue(BinaryReader reader, byte type) { switch (type) { case DataType.StringNull: return(null); case DataType.StringEmpty: return(string.Empty); case DataType.RefStringByte: return(Cache.DeserializerGet(reader.ReadByte())); case DataType.RefStringUShort: return(Cache.DeserializerGet(reader.ReadUInt16())); case DataType.RefString32Byte: return(Cache32.DeserializerGet(reader.ReadByte())); case DataType.RefString32Byte0: return(Cache32.DeserializerGet(0)); case DataType.RefString32Byte1: return(Cache32.DeserializerGet(1)); case DataType.RefString32Byte2: return(Cache32.DeserializerGet(2)); case DataType.RefString32Byte3: return(Cache32.DeserializerGet(3)); case DataType.RefString32Byte4: return(Cache32.DeserializerGet(4)); case DataType.RefString32Byte5: return(Cache32.DeserializerGet(5)); case DataType.RefString32Byte6: return(Cache32.DeserializerGet(6)); case DataType.RefString32Byte7: return(Cache32.DeserializerGet(7)); case DataType.RefString32Byte8: return(Cache32.DeserializerGet(8)); case DataType.RefString32Byte9: return(Cache32.DeserializerGet(9)); case DataType.RefString32Byte10: return(Cache32.DeserializerGet(10)); case DataType.RefString32Byte11: return(Cache32.DeserializerGet(11)); case DataType.RefString32Byte12: return(Cache32.DeserializerGet(12)); case DataType.RefString32Byte13: return(Cache32.DeserializerGet(13)); case DataType.RefString32Byte14: return(Cache32.DeserializerGet(14)); case DataType.RefString32Byte15: return(Cache32.DeserializerGet(15)); case DataType.RefString32Byte16: return(Cache32.DeserializerGet(16)); case DataType.RefString32UShort: return(Cache32.DeserializerGet(reader.ReadUInt16())); case DataType.RefString16Byte: return(Cache16.DeserializerGet(reader.ReadByte())); case DataType.RefString16Byte0: return(Cache16.DeserializerGet(0)); case DataType.RefString16Byte1: return(Cache16.DeserializerGet(1)); case DataType.RefString16Byte2: return(Cache16.DeserializerGet(2)); case DataType.RefString16Byte3: return(Cache16.DeserializerGet(3)); case DataType.RefString16Byte4: return(Cache16.DeserializerGet(4)); case DataType.RefString16Byte5: return(Cache16.DeserializerGet(5)); case DataType.RefString16Byte6: return(Cache16.DeserializerGet(6)); case DataType.RefString16Byte7: return(Cache16.DeserializerGet(7)); case DataType.RefString16Byte8: return(Cache16.DeserializerGet(8)); case DataType.RefString16Byte9: return(Cache16.DeserializerGet(9)); case DataType.RefString16Byte10: return(Cache16.DeserializerGet(10)); case DataType.RefString16Byte11: return(Cache16.DeserializerGet(11)); case DataType.RefString16Byte12: return(Cache16.DeserializerGet(12)); case DataType.RefString16Byte13: return(Cache16.DeserializerGet(13)); case DataType.RefString16Byte14: return(Cache16.DeserializerGet(14)); case DataType.RefString16Byte15: return(Cache16.DeserializerGet(15)); case DataType.RefString16Byte16: return(Cache16.DeserializerGet(16)); case DataType.RefString16UShort: return(Cache16.DeserializerGet(reader.ReadUInt16())); } var length = 0; switch (type) { case DataType.StringLengthByte: length = reader.ReadByte(); break; case DataType.StringLengthByte1: length = 1; break; case DataType.StringLengthByte2: length = 2; break; case DataType.StringLengthByte3: length = 3; break; case DataType.StringLengthByte4: length = 4; break; case DataType.StringLengthByte5: length = 5; break; case DataType.StringLengthByte6: length = 6; break; case DataType.StringLengthByte7: length = 7; break; case DataType.StringLengthByte8: length = 8; break; case DataType.StringLengthByte9: length = 9; break; case DataType.StringLengthByte10: length = 10; break; case DataType.StringLengthByte11: length = 11; break; case DataType.StringLengthByte12: length = 12; break; case DataType.StringLengthByte13: length = 13; break; case DataType.StringLengthByte14: length = 14; break; case DataType.StringLengthByte15: length = 15; break; case DataType.StringLengthByte16: length = 16; break; case DataType.StringLengthUShort: length = reader.ReadUInt16(); break; case DataType.StringLengthInt: length = reader.ReadInt32(); break; } if (length == 0) { return(null); } var bytes = ArrayPool <byte> .Shared.Rent(length); reader.Read(bytes, 0, length); var strValue = Encoding.GetString(bytes, 0, length); ArrayPool <byte> .Shared.Return(bytes); if (!_useCache) { return(strValue); } if (length < 2) { return(strValue); } if (length == 2 && Cache.Count > byte.MaxValue) { return(strValue); } if (length > 2 && Cache.Count > ushort.MaxValue) { return(strValue); } var vLength = strValue.Length; if (vLength <= 16) { Cache16.DeserializerSet(strValue); } else if (vLength <= 32) { Cache32.DeserializerSet(strValue); } else { Cache.DeserializerSet(strValue); } return(strValue); }
public override void WriteValue(BinaryWriter writer, string value) { switch (value) { case null: writer.Write(DataType.StringNull); return; case "": writer.Write(DataType.StringEmpty); return; } var cache16 = false; var cache32 = false; if (_useCache) { var vLength = value.Length; if (vLength <= 16) { cache16 = true; var objIdx = Cache16.SerializerGet(value); if (objIdx > -1) { #region Write Reference switch (objIdx) { case 0: writer.Write(DataType.RefString16Byte0); return; case 1: writer.Write(DataType.RefString16Byte1); return; case 2: writer.Write(DataType.RefString16Byte2); return; case 3: writer.Write(DataType.RefString16Byte3); return; case 4: writer.Write(DataType.RefString16Byte4); return; case 5: writer.Write(DataType.RefString16Byte5); return; case 6: writer.Write(DataType.RefString16Byte6); return; case 7: writer.Write(DataType.RefString16Byte7); return; case 8: writer.Write(DataType.RefString16Byte8); return; case 9: writer.Write(DataType.RefString16Byte9); return; case 10: writer.Write(DataType.RefString16Byte10); return; case 11: writer.Write(DataType.RefString16Byte11); return; case 12: writer.Write(DataType.RefString16Byte12); return; case 13: writer.Write(DataType.RefString16Byte13); return; case 14: writer.Write(DataType.RefString16Byte14); return; case 15: writer.Write(DataType.RefString16Byte15); return; case 16: writer.Write(DataType.RefString16Byte16); return; default: if (objIdx <= byte.MaxValue) { WriteByte(writer, DataType.RefString16Byte, (byte)objIdx); } else { WriteUshort(writer, DataType.RefString16UShort, (ushort)objIdx); } return; } #endregion } } else if (vLength <= 32) { cache32 = true; var objIdx = Cache32.SerializerGet(value); if (objIdx > -1) { #region Write Reference switch (objIdx) { case 0: writer.Write(DataType.RefString32Byte0); return; case 1: writer.Write(DataType.RefString32Byte1); return; case 2: writer.Write(DataType.RefString32Byte2); return; case 3: writer.Write(DataType.RefString32Byte3); return; case 4: writer.Write(DataType.RefString32Byte4); return; case 5: writer.Write(DataType.RefString32Byte5); return; case 6: writer.Write(DataType.RefString32Byte6); return; case 7: writer.Write(DataType.RefString32Byte7); return; case 8: writer.Write(DataType.RefString32Byte8); return; case 9: writer.Write(DataType.RefString32Byte9); return; case 10: writer.Write(DataType.RefString32Byte10); return; case 11: writer.Write(DataType.RefString32Byte11); return; case 12: writer.Write(DataType.RefString32Byte12); return; case 13: writer.Write(DataType.RefString32Byte13); return; case 14: writer.Write(DataType.RefString32Byte14); return; case 15: writer.Write(DataType.RefString32Byte15); return; case 16: writer.Write(DataType.RefString32Byte16); return; default: if (objIdx <= byte.MaxValue) { WriteByte(writer, DataType.RefString32Byte, (byte)objIdx); } else { WriteUshort(writer, DataType.RefString32UShort, (ushort)objIdx); } return; } #endregion } } else { var objIdx = Cache.SerializerGet(value); if (objIdx > -1) { if (objIdx <= byte.MaxValue) { WriteByte(writer, DataType.RefStringByte, (byte)objIdx); } else { WriteUshort(writer, DataType.RefStringUShort, (ushort)objIdx); } return; } } } var length = Encoding.GetByteCount(value); int bytesLength; byte[] bytes; if (length < 17) { bytesLength = length + 1; bytes = ArrayPool <byte> .Shared.Rent(bytesLength); bytes[0] = (byte)(DataType.StringLengthByte1 + (length - 1)); Encoding.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; Encoding.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); Encoding.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); Encoding.GetBytes(value, 0, value.Length, bytes, 5); } writer.Write(bytes, 0, bytesLength); ArrayPool <byte> .Shared.Return(bytes); if (!_useCache) { return; } if (length < 2) { return; } if (length == 2 && Cache.Count > byte.MaxValue) { return; } if (length > 2 && Cache.Count > ushort.MaxValue) { return; } if (cache16) { Cache16.SerializerSet(value); } else if (cache32) { Cache32.SerializerSet(value); } else { Cache.SerializerSet(value); } }