1 /* 2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_WRITER_H_ 6 #define _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_WRITER_H_ 7 8 9 #include <Array.h> 10 #include <package/hpkg/PackageFileHeapAccessorBase.h> 11 12 13 class BCompressionParameters; 14 15 namespace BPrivate { 16 template<typename Value> class RangeArray; 17 } 18 19 20 namespace BPackageKit { 21 22 namespace BHPKG { 23 24 25 class BDataReader; 26 class BErrorOutput; 27 28 29 namespace BPrivate { 30 31 32 class PackageFileHeapReader; 33 34 35 class PackageFileHeapWriter : public PackageFileHeapAccessorBase { 36 public: 37 PackageFileHeapWriter(BErrorOutput* errorOutput, 38 BPositionIO* file, off_t heapOffset, 39 CompressionAlgorithmOwner* 40 compressionAlgorithm, 41 DecompressionAlgorithmOwner* 42 decompressionAlgorithm); 43 ~PackageFileHeapWriter(); 44 45 void Init(); 46 void Reinit(PackageFileHeapReader* heapReader); 47 48 status_t AddData(BDataReader& dataReader, off_t size, 49 uint64& _offset); 50 void AddDataThrows(const void* buffer, size_t size); 51 void RemoveDataRanges( 52 const ::BPrivate::RangeArray<uint64>& 53 ranges); 54 // doesn't truncate the file 55 status_t Finish(); 56 57 protected: 58 virtual status_t ReadAndDecompressChunk(size_t chunkIndex, 59 void* compressedDataBuffer, 60 void* uncompressedDataBuffer); 61 62 private: 63 struct Chunk; 64 struct ChunkSegment; 65 struct ChunkBuffer; 66 67 friend struct ChunkBuffer; 68 69 private: 70 void _Uninit(); 71 72 status_t _FlushPendingData(); 73 status_t _WriteChunk(const void* data, size_t size, 74 bool mayCompress); 75 status_t _WriteDataCompressed(const void* data, 76 size_t size); 77 status_t _WriteDataUncompressed(const void* data, 78 size_t size); 79 80 void _PushChunks(ChunkBuffer& chunkBuffer, 81 uint64 startOffset, uint64 endOffset); 82 void _UnwriteLastPartialChunk(); 83 84 private: 85 void* fPendingDataBuffer; 86 void* fCompressedDataBuffer; 87 size_t fPendingDataSize; 88 Array<uint64> fOffsets; 89 CompressionAlgorithmOwner* fCompressionAlgorithm; 90 }; 91 92 93 } // namespace BPrivate 94 95 } // namespace BHPKG 96 97 } // namespace BPackageKit 98 99 100 #endif // _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_WRITER_H_ 101