#pragma once #include #include template inline void WriteBin(IOutputStream* out, typename TTypeTraits::TFuncParam t) { out->Write(&t, sizeof(T)); } class TChunkedDataWriter: public IOutputStream { public: TChunkedDataWriter(IOutputStream& slave); ~TChunkedDataWriter() override; void NewBlock(); template inline void WriteBinary(typename TTypeTraits::TFuncParam t) { this->Write(&t, sizeof(T)); } void WriteFooter(); size_t GetCurrentBlockOffset() const; size_t GetBlockCount() const; protected: void DoWrite(const void* buf, size_t len) override { Slave.Write(buf, len); Offset += len; } private: static inline size_t PaddingSize(size_t size, size_t boundary) noexcept { const size_t boundaryViolation = size % boundary; return boundaryViolation == 0 ? 0 : boundary - boundaryViolation; } inline void Pad(size_t boundary) { const size_t newOffset = Offset + PaddingSize(Offset, boundary); while (Offset < newOffset) { Write('\0'); } } private: static const ui64 Version = 1; IOutputStream& Slave; size_t Offset; TVector Offsets; TVector Lengths; };