Plasma Engine  2.0
Loading...
Searching...
No Matches
CompressedStreamZstd.h
1
2#pragma once
3
4#include <Foundation/Basics.h>
5#include <Foundation/Containers/DynamicArray.h>
6#include <Foundation/IO/Stream.h>
7
8#ifdef BUILDSYSTEM_ENABLE_ZSTD_SUPPORT
9
13class PL_FOUNDATION_DLL plCompressedStreamReaderZstd : public plStreamReader
14{
15public:
16 plCompressedStreamReaderZstd(); // [tested]
17
19 plCompressedStreamReaderZstd(plStreamReader* pInputStream); // [tested]
20
21 ~plCompressedStreamReaderZstd(); // [tested]
22
27 void SetInputStream(plStreamReader* pInputStream); // [tested]
28
33 virtual plUInt64 ReadBytes(void* pReadBuffer, plUInt64 uiBytesToRead) override; // [tested]
34
35private:
36 plResult RefillReadCache();
37
38 // local declaration to reduce #include dependencies
39 struct InBufferImpl
40 {
41 const void* src;
42 size_t size;
43 size_t pos;
44 };
45
46 bool m_bReachedEnd = false;
47 plDynamicArray<plUInt8> m_CompressedCache;
48 plStreamReader* m_pInputStream = nullptr;
49 /*ZSTD_DStream*/ void* m_pZstdDStream = nullptr;
50 /*ZSTD_inBuffer*/ InBufferImpl m_InBuffer;
51};
52
61class PL_FOUNDATION_DLL plCompressedStreamWriterZstd final : public plStreamWriter
62{
63public:
65 enum class Compression
66 {
67 Fastest = 1,
68 Fast = 5,
69 Average = 10,
70 High = 15,
71 Highest = 18, // officially up to 22, but with 20 I already encountered never-ending compression times inside zstd :(
72 Default = Fastest
74 };
75
76 plCompressedStreamWriterZstd();
77
79 plCompressedStreamWriterZstd(plStreamWriter* pOutputStream, plUInt32 uiMaxNumWorkerThreads, Compression ratio = Compression::Default, plUInt32 uiCompressionCacheSizeKB = 4); // [tested]
80
82 ~plCompressedStreamWriterZstd(); // [tested]
83
95 void SetOutputStream(plStreamWriter* pOutputStream, plUInt32 uiMaxNumWorkerThreads, Compression ratio = Compression::Default, plUInt32 uiCompressionCacheSizeKB = 4); // [tested]
96
100 virtual plResult WriteBytes(const void* pWriteBuffer, plUInt64 uiBytesToWrite) override; // [tested]
101
108 plResult FinishCompressedStream(); // [tested]
109
111 plUInt64 GetUncompressedSize() const { return m_uiUncompressedSize; } // [tested]
112
119 plUInt64 GetCompressedSize() const { return m_uiCompressedSize; } // [tested]
120
124 plUInt64 GetWrittenBytes() const { return m_uiWrittenBytes; } // [tested]
125
133 virtual plResult Flush() override; // [tested]
134
135private:
136 plResult FlushWriteCache();
137
138 plUInt64 m_uiUncompressedSize = 0;
139 plUInt64 m_uiCompressedSize = 0;
140 plUInt64 m_uiWrittenBytes = 0;
141
142 // local declaration to reduce #include dependencies
143 struct OutBufferImpl
144 {
145 void* dst;
146 size_t size;
147 size_t pos;
148 };
149
150 plStreamWriter* m_pOutputStream = nullptr;
151 /*ZSTD_CStream*/ void* m_pZstdCStream = nullptr;
152 /*ZSTD_outBuffer*/ OutBufferImpl m_OutBuffer;
153
154 plDynamicArray<plUInt8> m_CompressedCache;
155};
156
157#endif // BUILDSYSTEM_ENABLE_ZSTD_SUPPORT
Definition DynamicArray.h:81
Interface for binary in (read) streams.
Definition Stream.h:22
virtual plUInt64 ReadBytes(void *pReadBuffer, plUInt64 uiBytesToRead)=0
Reads a raw number of bytes into the read buffer, this is the only method which has to be implemented...
Interface for binary out (write) streams.
Definition Stream.h:107
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54