BmnRoot
26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnFileReaderXzLzma.h
Go to the documentation of this file.
1
#pragma once
2
#ifndef BmnFileReaderXzLzma_h
3
#define BmnFileReaderXzLzma_h
4
5
// headers from this target
6
#include "
BmnIArchiveLibRead.h
"
7
#include "
BmnIFileReader.h
"
8
#include "
BmnReaderCacheManager.h
"
9
10
// If one needs lzma_stream, just reinterpret_cast the variable from the struct below
11
struct
LzmaContext
;
12
13
/*
14
* A class responsible for reading files compressed with XZ (LZMA2 algorithm). This algorithm allows
15
* us to reach maximal compression ratios. Still, it comes with a cost - compression speeds are low,
16
* while decompression speeds are decent, but still slow relative to many other libraries.
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 XZ-LZMA library is not found during compilation.
19
* Therefore, it's always advisable to obtain appropriate read objects through
20
* `BmnCompressionCreateNewSuitableReaderForAFile`
21
*/
22
class
BmnFileReaderXzLzma
23
:
public
BmnIFileReader
24
,
public
BmnIArchiveLibRead
25
{
26
public
:
27
/*
28
* A standard constructor that creates a XZ-LZMA stream. If a filepath is provided, the stream will be opened for
29
* reading
30
* @param aFilePath A path to the input binary (XZ-LZMA) file. If provided, the file will be opened in the
31
* 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
BmnFileReaderXzLzma
(
const
std::filesystem::path& aFilePath =
""
);
36
37
// Closes the stream while destroying the instance
38
virtual
~BmnFileReaderXzLzma
()
override
;
39
BmnFileReaderXzLzma
(
const
BmnFileReaderXzLzma
&) =
delete
;
40
BmnFileReaderXzLzma
&
operator=
(
const
BmnFileReaderXzLzma
&) =
delete
;
41
BmnFileReaderXzLzma
(
BmnFileReaderXzLzma
&&) noexcept = delete;
42
BmnFileReaderXzLzma
& operator=(
BmnFileReaderXzLzma
&&) noexcept = delete;
43
44
// ========== BmnIFileReaderInterface ==========
45
46
/*
47
* A method that opens a binary XZ-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::XZ_LZMA
110
*/
111
[[nodiscard]] virtual
BmnFileType
GetFileType
() const noexcept override;
112
113
// ========== BmnIArchiveLib Interface ==========
114
115
/*
116
* A method that is called to uncompress XZ-LZMA data
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 XZ-LZMA 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` XZ-LZMA failed while decompressing
130
* 4. `BmnFileStreamResults::ReachedEOF` XZ-LZMA reached the end of frame (in our case file)
131
* 5. `BmnFileStreamResults::Success` XZ-LZMA 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 XZ-LZMA
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
// ========== BmnFileReaderXzLzma public methods ==========
166
167
/*
168
* A method that can be used to understand how much memory is used by LZMA
169
* @return Amount of memory used by an internal LZMA stream
170
*/
171
[[nodiscard]]
size_t
GetUsedMemoryByLZMA
() const noexcept;
172
173
// ========== BmnFileReaderXzLzma inlines ==========
174
175
[[nodiscard]] inline
LzmaContext
*
GetUnderlyingLibraryStream
() const noexcept;
176
// Get number of read bytes via `GetNumberOfDecompressedBytes`
177
// No need to expose `fArchiveReadManager`
178
public:
179
static
size_t
sMaximalAllowedMemory
;
180
181
private:
182
// We won't be sharing this instance so going with a raw pointer... it could've been a unique ptr as well, but
183
// nothing too important
184
LzmaContext
*
fLzmaStream
;
185
BmnReaderCacheManager
<32ull * 1024ull, 64ull * 1024ull>
fArchiveReadManager
;
186
size_t
fNumberOfReadBytes
;
187
int32_t
fLastReturnStatus
;
188
// 4 padding bytes
189
};
190
191
// 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 o
192
// ================== BmnFileReaderXzLzma inline methods ==================
193
// 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 o
194
195
LzmaContext
*
BmnFileReaderXzLzma
::
GetUnderlyingLibraryStream
() const noexcept
196
{
197
return
fLzmaStream
;
198
}
199
200
#endif
// BmnFileReaderXzLzma_h
BmnFileStreamResults
BmnFileStreamResults
Definition
BmnFileProcessingFlags.h:74
BmnFileType
BmnFileType
Definition
BmnFileProcessingFlags.h:16
BmnIArchiveLibRead.h
BmnIFileReader.h
BmnReaderCacheManager.h
BmnFileReaderXzLzma::fNumberOfReadBytes
size_t fNumberOfReadBytes
Definition
BmnFileReaderXzLzma.h:186
BmnFileReaderXzLzma::Open
virtual void Open(const std::filesystem::path &aFilePath) override
Definition
BmnFileReaderXzLzma.cxx:79
BmnFileReaderXzLzma::BmnFileReaderXzLzma
BmnFileReaderXzLzma(const BmnFileReaderXzLzma &)=delete
BmnFileReaderXzLzma::Seek
virtual void Seek(int64_t value) override
Definition
BmnFileReaderXzLzma.cxx:140
BmnFileReaderXzLzma::Tell
virtual int64_t Tell() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:134
BmnFileReaderXzLzma::operator=
BmnFileReaderXzLzma & operator=(const BmnFileReaderXzLzma &)=delete
BmnFileReaderXzLzma::Read
virtual int64_t Read(void *dataToFill, const int64_t sizeToFill) override
Definition
BmnFileReaderXzLzma.cxx:123
BmnFileReaderXzLzma::fLzmaStream
LzmaContext * fLzmaStream
Definition
BmnFileReaderXzLzma.h:184
BmnFileReaderXzLzma::BmnFileReaderXzLzma
BmnFileReaderXzLzma(BmnFileReaderXzLzma &&) noexcept=delete
BmnFileReaderXzLzma::GetLastUncompressErrorDescription
virtual std::string GetLastUncompressErrorDescription() const override
Definition
BmnFileReaderXzLzma.cxx:245
BmnFileReaderXzLzma::fLastReturnStatus
int32_t fLastReturnStatus
Definition
BmnFileReaderXzLzma.h:187
BmnFileReaderXzLzma::HasReachedTheEndOfStream
virtual bool HasReachedTheEndOfStream() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:259
BmnFileReaderXzLzma::fArchiveReadManager
BmnReaderCacheManager< 32ull *1024ull, 64ull *1024ull > fArchiveReadManager
Definition
BmnFileReaderXzLzma.h:185
BmnFileReaderXzLzma::ClearStateFlags
virtual void ClearStateFlags() override
Definition
BmnFileReaderXzLzma.cxx:191
BmnFileReaderXzLzma::BmnFileReaderXzLzma
BmnFileReaderXzLzma(const std::filesystem::path &aFilePath="")
Definition
BmnFileReaderXzLzma.cxx:60
BmnFileReaderXzLzma::GetUnderlyingLibraryStream
LzmaContext * GetUnderlyingLibraryStream() const noexcept
Definition
BmnFileReaderXzLzma.h:195
BmnFileReaderXzLzma::IsGood
virtual bool IsGood() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:177
BmnFileReaderXzLzma::Uncompress
virtual BmnFileStreamResults Uncompress(const void *source, const size_t sourceTotalSize, size_t &numberOfReadBytes, void *output, const size_t outputTotalSize, size_t &numberOfSetOutputBytes) override
Definition
BmnFileReaderXzLzma.cxx:210
BmnFileReaderXzLzma::GetUsedMemoryByLZMA
size_t GetUsedMemoryByLZMA() const noexcept
Definition
BmnFileReaderXzLzma.cxx:280
BmnFileReaderXzLzma::Close
virtual void Close() noexcept override
Definition
BmnFileReaderXzLzma.cxx:108
BmnFileReaderXzLzma::IsOpen
virtual bool IsOpen() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:198
BmnFileReaderXzLzma::GetFileType
virtual BmnFileType GetFileType() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:203
BmnFileReaderXzLzma::sMaximalAllowedMemory
static size_t sMaximalAllowedMemory
Definition
BmnFileReaderXzLzma.h:179
BmnFileReaderXzLzma::~BmnFileReaderXzLzma
virtual ~BmnFileReaderXzLzma() override
Definition
BmnFileReaderXzLzma.cxx:74
BmnFileReaderXzLzma::GetNumberOfDecompressedBytes
virtual size_t GetNumberOfDecompressedBytes() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:254
BmnFileReaderXzLzma::IsEOF
virtual bool IsEOF() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:186
BmnFileReaderXzLzma::IsStreamInGoodState
virtual bool IsStreamInGoodState() const noexcept override
Definition
BmnFileReaderXzLzma.cxx:268
BmnIFileReader::BmnIFileReader
BmnIFileReader()=default
BmnReaderCacheManager
Definition
BmnReaderCacheManager.h:27
BmnIArchiveLibRead::BmnIArchiveLibRead
BmnIArchiveLibRead()=default
LzmaContext
Definition
BmnFileReaderXzLzma.cxx:18
compression
include
BmnFileReaderXzLzma.h
Generated on
for BmnRoot by
1.17.0