PackageFileHeapWriter.h revision 84297881
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
13namespace BPrivate {
14	template<typename Value> class RangeArray;
15}
16
17
18namespace BPackageKit {
19
20namespace BHPKG {
21
22
23class BDataReader;
24class BErrorOutput;
25
26
27namespace BPrivate {
28
29
30class PackageFileHeapReader;
31
32
33class PackageFileHeapWriter : public PackageFileHeapAccessorBase {
34public:
35								PackageFileHeapWriter(BErrorOutput* errorOutput,
36									int fd, off_t heapOffset,
37									int32 compressionLevel);
38								~PackageFileHeapWriter();
39
40			void				Init();
41			void				Reinit(PackageFileHeapReader* heapReader);
42
43			status_t			AddData(BDataReader& dataReader, off_t size,
44									uint64& _offset);
45			void				AddDataThrows(const void* buffer, size_t size);
46			void				RemoveDataRanges(
47									const ::BPrivate::RangeArray<uint64>&
48										ranges);
49									// doesn't truncate the file
50			status_t			Finish();
51
52protected:
53	virtual	status_t			ReadAndDecompressChunk(size_t chunkIndex,
54									void* compressedDataBuffer,
55									void* uncompressedDataBuffer);
56
57private:
58			struct Chunk;
59			struct ChunkSegment;
60			struct ChunkBuffer;
61
62			friend struct ChunkBuffer;
63
64private:
65			void				_Uninit();
66
67			status_t			_FlushPendingData();
68			status_t			_WriteChunk(const void* data, size_t size,
69									bool mayCompress);
70			status_t			_WriteDataCompressed(const void* data,
71									size_t size);
72			status_t			_WriteDataUncompressed(const void* data,
73									size_t size);
74
75			void				_PushChunks(ChunkBuffer& chunkBuffer,
76									uint64 startOffset, uint64 endOffset);
77			void				_UnwriteLastPartialChunk();
78
79private:
80			void*				fPendingDataBuffer;
81			void*				fCompressedDataBuffer;
82			size_t				fPendingDataSize;
83			Array<uint64>		fOffsets;
84			int32				fCompressionLevel;
85};
86
87
88}	// namespace BPrivate
89
90}	// namespace BHPKG
91
92}	// namespace BPackageKit
93
94
95#endif	// _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_WRITER_H_
96