BmnRoot 26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnFileReaderZLib.h
Go to the documentation of this file.
1#pragma once
2#ifndef BmnFileReaderZLib_h
3#define BmnFileReaderZLib_h
4// headers from this target
6#include "BmnIFileReader.h"
8
9struct z_stream_s;
10
11/*
12 * A class responsible for reading files compressed with ZLib (Deflate algorithm) or GZip (internally,
13 * it uses ZLib). For long, ZLib was an industry standard as it allowed for good compression ratios while
14 * still working fast. Still, nowadays, there are libraries that work faster and give better compression (ZStd).
15 * ZLib-ng (stands for "new generation") also implements the Deflate algorithm while achieving significantly
16 * faster compression/decompression (see `BmnFileReaderZLibNG`).
17 * It's advisable to allocate this class on the heap as internally it uses big std::array
18 * @warning Using this class directly can result in throws if the ZLib library is not found during compilation.
19 * Therefore, it's always advisable to obtain appropriate read objects through
20 * `BmnCompressionCreateNewSuitableReaderForAFile`
21 */
23 : public BmnIFileReader
24 , public BmnIArchiveLibRead
25{
26 public:
27 /*
28 * A standard constructor that creates a BZip3 stream. If a filepath (to a GZip or ZLib-compressed file) is
29 * provided, the stream will be opened for reading
30 * @param aFilePath A path to the input binary (GZip or ZLib-compressed) file. If provided, the file will be opened
31 * in the constructor
32 * @warning If using relative paths, be very careful about currently-working directory as it might
33 * be something unexpected based on the directory that initalized an application
34 */
35 explicit BmnFileReaderZLib(const std::filesystem::path& filePath);
36
37 // Closes the stream while destroying the instance
38 virtual ~BmnFileReaderZLib() override;
42 BmnFileReaderZLib& operator=(BmnFileReaderZLib&&) noexcept = delete;
43
44 // ========== BmnIFileReaderInterface ==========
45
46 /*
47 * A method that opens a binary ZLib-compressed file for reading
48 * @param aFilePath A path to the file that should be opened
49 * @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
50 */
51 virtual void Open(const std::filesystem::path& aFilePath) override;
52
53 /*
54 * Closes the stream and resets all internal data. After calling `Close`, it's safe to reopen using the same
55 * instance of this class
56 */
57 virtual void Close() noexcept override;
58
59 /*
60 * Reads and decompresses the data while still managing caching for efficiency.
61 * @param dataToFill A buffer that will be filled with uncompressed data
62 * @param sizeToFill A number of bytes that will be filled in `dataToFill`
63 * @return A number of bytes that were filled in `dataToFill`. This value should almost always be the same as
64 * `sizeToFill` except when the end of the stream is reached (only if the user asked for more data than
65 * possible)
66 */
67 virtual int64_t Read(void* dataToFill, const int64_t sizeToFill) override;
68
69 /*
70 * A method that tells us what's the next byte to read
71 * @return A position of the next byte that should be read
72 */
73 [[nodiscard]] virtual int64_t Tell() const noexcept override;
74
75 /*
76 * A method that allows us to go to another place in the file (uncompressed data) and read from that place.
77 * This method is NOT EFFICIENT as it still needs to uncompress data to reach the needed place!
78 * @param value A position in uncompressed data to which one wants to move before the next read
79 */
80 virtual void Seek(int64_t value) override;
81
82 /*
83 * Check whether the reading stream is good
84 * @return 'true' if no decompression error has occurred and the input-file stream is good, or 'false' otherwise
85 */
86 [[nodiscard]] virtual bool IsGood() const noexcept override;
87
88 /*
89 * Check if we have reached the file. This will tell us if we have reached EOF in the uncompressed data, not
90 * in the file itself. That is, the file might be at EOF, while this method returns will return true as cache
91 * still has some data
92 * @return 'true' if both file and internal cache are completely read, 'false' otherwise
93 */
94 [[nodiscard]] virtual bool IsEOF() const noexcept override;
95
96 /*
97 * A method that clears bad flags after an error has occurred. Just calling this error will most likely
98 * not solve the problem, and you will get an error again in the next read. That is, before calling `Read`,
99 * remove the previous error source
100 */
101 virtual void ClearStateFlags() override;
102
103 /*
104 * @return 'true' if the input stream is open, or 'false' if not
105 */
106 [[nodiscard]] virtual bool IsOpen() const noexcept override;
107
108 /*
109 * @return BmnFileType::ZLib
110 */
111 [[nodiscard]] virtual BmnFileType GetFileType() const noexcept override;
112
113 // ========== BmnIArchiveLib Interface ==========
114
115 /*
116 * A method that is called to uncompress Deflate data with ZLib
117 * @param source A pointer to a buffer with the subsequent compressed data that should be uncompressed
118 * @param sourceTotalSize A number of bytes available in the source buffer. If one has reached the EOF, it's ok to
119 * pass a 0 here
120 * @param numberOfReadBytes A number of bytes that were consumed by ZLib while decompressing. This value will be
121 * set.
122 * @param output A pointer to a buffer that will be filled with uncompressed data
123 * @param outputTotalSize A number of bytes that may be filled in the output buffer
124 * @param numberOfSetOutputBytes A number of bytes that were set while decompressing. This value will be set.
125 * @return There are a few options:
126 * 1. `BmnFileStreamResults::FileNotOpen` if the stream is not open
127 * 2. `BmnFileStreamResults::TryingToReadBadStream` if the stream previously hit an error while it wasn't
128 * cleared later
129 * 3. `BmnFileStreamResults::UnexpectedFail` ZLib failed while decompressing
130 * 4. `BmnFileStreamResults::ReachedEOF` ZLib reached the end of frame (in our case file)
131 * 5. `BmnFileStreamResults::Success` ZLib successfully uncompressed the package
132 */
133 virtual BmnFileStreamResults Uncompress(const void* source,
134 const size_t sourceTotalSize,
135 size_t& numberOfReadBytes,
136 void* output,
137 const size_t outputTotalSize,
138 size_t& numberOfSetOutputBytes) override;
139
140 /*
141 * If an error occurred while decompressing, this method can be called to obtain a description of the error
142 * @return A description of an error that occurred while decompressing, or an empty string if no such error happened
143 * @throw std::bad_alloc if an allocation error occurs
144 */
145 [[nodiscard]] virtual std::string GetLastUncompressErrorDescription() const override;
146
147 /*
148 * A method that tells us how many uncompressed bytes were read from an input file
149 * @return A number of uncompressed bytes that passed through the ZLib
150 */
151 [[nodiscard]] virtual size_t GetNumberOfDecompressedBytes() const noexcept override;
152
153 /*
154 * A method that can be used to check if the reading stream has ended
155 * @return 'true' if we have reached EOF, 'false' otherwise
156 */
157 [[nodiscard]] virtual bool HasReachedTheEndOfStream() const noexcept override;
158
159 /*
160 * A method that checks whether the last read was good or not. If not, the stream is not good anymore
161 * @return 'true' if the stream is in a good state, 'false' otherwise
162 */
163 [[nodiscard]] virtual bool IsStreamInGoodState() const noexcept override;
164
165 // ========== BmnFileReaderZLib inlines ==========
166
167 [[nodiscard]] inline z_stream_s* GetUnderlyingLibraryStream() const noexcept;
168 // Get number of read bytes via `GetNumberOfDecompressedBytes`
169 // No need to expose `fArchiveReadManager`
170
171 private:
172 z_stream_s* fZlibStream;
173 BmnReaderCacheManager<32ull * 1024ull, 64ull * 1024ull> fArchiveReadManager;
176 // 4 padding bytes
177};
178
179// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
180// ================== BmnFileReaderZLib inline methods ==================
181// o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o o
182
183[[nodiscard]] inline z_stream_s* BmnFileReaderZLib::GetUnderlyingLibraryStream() const noexcept
184{
185 return fZlibStream;
186}
187
188#endif // BmnFileReaderZLib_h
BmnFileReaderZLib & operator=(const BmnFileReaderZLib &)=delete
virtual ~BmnFileReaderZLib() override
virtual size_t GetNumberOfDecompressedBytes() const noexcept override
virtual int64_t Read(void *dataToFill, const int64_t sizeToFill) override
virtual bool IsStreamInGoodState() const noexcept override
BmnReaderCacheManager< 32ull *1024ull, 64ull *1024ull > fArchiveReadManager
virtual int64_t Tell() const noexcept override
virtual std::string GetLastUncompressErrorDescription() const override
virtual bool IsGood() const noexcept override
BmnFileReaderZLib(BmnFileReaderZLib &&) noexcept=delete
virtual void Open(const std::filesystem::path &aFilePath) override
virtual bool IsEOF() const noexcept override
virtual bool IsOpen() const noexcept override
virtual void Close() noexcept override
virtual BmnFileStreamResults Uncompress(const void *source, const size_t sourceTotalSize, size_t &numberOfReadBytes, void *output, const size_t outputTotalSize, size_t &numberOfSetOutputBytes) override
virtual BmnFileType GetFileType() const noexcept override
BmnFileReaderZLib(const std::filesystem::path &filePath)
virtual bool HasReachedTheEndOfStream() const noexcept override
z_stream_s * GetUnderlyingLibraryStream() const noexcept
virtual void Seek(int64_t value) override
virtual void ClearStateFlags() override
BmnFileReaderZLib(const BmnFileReaderZLib &)=delete
BmnIFileReader()=default
BmnIArchiveLibRead()=default