void WriteFirstByte <T>(ref BitTarget target, T data, CompressedDataInfo dataInfo) where T : INumberContainer { bool isExtendedByte = target.FreeBits < 4; bool byteWillHaveFreeSpace = dataInfo.HeaderLen < target.FreeBits; // Write the header target.WriteInteger(dataInfo.Header, dataInfo.HeaderLen); // Handle extended starts (yyy-xxxxxxxx) if (isExtendedByte) { // Write any free "y"s. if (byteWillHaveFreeSpace) { target.WriteInteger((byte)(data.ShiftRight(dataInfo.BitsToGo) >> 8), target.FreeBits); } // The next byte will definitely have some free space, as we can not physically fill all of the remaining "xxxxxxxx"s with the header. // Ensure we're definitely ready for the next byte. if (target.FreeBits == 0) { target.Apply(); } byteWillHaveFreeSpace = true; } if (byteWillHaveFreeSpace) { target.WriteInteger((byte)data.ShiftRight(dataInfo.BitsToGo), target.FreeBits); } target.Apply(); }
public void SerializeItem(object?obj, MapItemInfo item, ref BitTarget header) { if (obj == null) { header.WriteBitOff(); header.Apply(); } else { SerializePossibleNullableItem(obj, item, ref header); } }
void WriteCompressed <T>(T data, ref BitTarget target) where T : INumberContainer { if (target.FreeBits == 0) { target.Apply(); } if (Settings.LazyCompressedWriting) { WriteCompressedLazyFast(data, ref target); } else { WriteCompressedSlow(data, ref target); } }
void SerializeConverter(object obj, Converter converter, ref BitTarget header, bool skipHeader) { Type?actualType = obj.GetType(); bool appliedHeader = true; // Write the null and inheritance bits. bool sameType = true; if (!converter.IsValueItemType && !skipHeader) { sameType = WriteHeaderNullAndInheritance(actualType, converter, ref header); appliedHeader = false; } // Write and get the info for a version, if necessary if (!HandleVersionNumber(converter, out VersionInfo info, ref header)) { appliedHeader = true; } // Handle inheritance if needed. if (info._inheritanceInfo != null && !sameType) { SerializeActualType(obj, info._inheritanceInfo, converter.ItemType, actualType, ref header); return; } // Apply the header if it's not being used and hasn't already been applied. if (!info.UsesHeader && !appliedHeader) { header.Apply(); } var serializeInfo = new Converter.SerializeInfo(obj, actualType, info); converter.Serialize(in serializeInfo, ref header); }