writer.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <util/generic/vector.h>
  3. #include <util/stream/output.h>
  4. template <typename T>
  5. inline void WriteBin(IOutputStream* out, typename TTypeTraits<T>::TFuncParam t) {
  6. out->Write(&t, sizeof(T));
  7. }
  8. class TChunkedDataWriter: public IOutputStream {
  9. public:
  10. TChunkedDataWriter(IOutputStream& slave);
  11. ~TChunkedDataWriter() override;
  12. void NewBlock();
  13. template <typename T>
  14. inline void WriteBinary(typename TTypeTraits<T>::TFuncParam t) {
  15. this->Write(&t, sizeof(T));
  16. }
  17. void WriteFooter();
  18. size_t GetCurrentBlockOffset() const;
  19. size_t GetBlockCount() const;
  20. protected:
  21. void DoWrite(const void* buf, size_t len) override {
  22. Slave.Write(buf, len);
  23. Offset += len;
  24. }
  25. private:
  26. static inline size_t PaddingSize(size_t size, size_t boundary) noexcept {
  27. const size_t boundaryViolation = size % boundary;
  28. return boundaryViolation == 0 ? 0 : boundary - boundaryViolation;
  29. }
  30. inline void Pad(size_t boundary) {
  31. const size_t newOffset = Offset + PaddingSize(Offset, boundary);
  32. while (Offset < newOffset) {
  33. Write('\0');
  34. }
  35. }
  36. private:
  37. static const ui64 Version = 1;
  38. IOutputStream& Slave;
  39. size_t Offset;
  40. TVector<ui64> Offsets;
  41. TVector<ui64> Lengths;
  42. };