BmnRoot
26.10.0-233-gb9bbe585
Software framework of the BM@N experiment
Toggle main menu visibility
Loading...
Searching...
No Matches
BmnFileReaderBZip3.h
Go to the documentation of this file.
1
#pragma once
2
#ifndef BmnFileReaderBZip3_hpp
3
#define BmnFileReaderBZip3_hpp
4
5
// headers from this target
6
#include "
BmnIArchiveLibRead.h
"
7
#include "
BmnIFileReader.h
"
8
9
// std:: headers
10
#include <fstream>
11
#include <vector>
12
13
struct
bz3_state;
14
15
/*
16
* A class responsible for reading compressed BZip3 files. BZip3 library allows one to compress data very well,
17
* but decompression of compressed files is slow (including reading by this class)
18
* @note BZip3 doesn't share the interface of other implemented archive libraries, thus it doesn't use the cache
19
* manager. Still, I'm inheriting `BmnIArchiveLibRead` for consistency, while in general method `Uncompress` doesn't use
20
* the best arguments as with other libraries
21
* @warning Using this class directly can result in throws if the bzip3 library is not found during compilation.
22
* Therefore, it's always advisable to obtain appropriate read objects through
23
* `BmnCompressionCreateNewSuitableReaderForAFile`
24
*/
25
class
BmnFileReaderBZip3
26
:
public
BmnIFileReader
27
,
public
BmnIArchiveLibRead
28
{
29
public
:
30
/*
31
* A standard constructor that creates a BZip3 stream. If a filepath is provided, the stream will be opened for
32
* reading
33
* @param aFilePath A path to the input binary (BZip3) file. If provided, the file will be opened in the constructor
34
* @warning If using relative paths, be very careful about currently-working directory as it might
35
* be something unexpected based on the directory that initalized an application
36
*/
37
explicit
BmnFileReaderBZip3
(
const
std::filesystem::path& aFilePath =
""
);
38
39
// Closes the stream while destroying the instance
40
virtual
~BmnFileReaderBZip3
()
override
;
41
BmnFileReaderBZip3
(
const
BmnFileReaderBZip3
&) =
delete
;
42
BmnFileReaderBZip3
&
operator=
(
const
BmnFileReaderBZip3
&) =
delete
;
43
BmnFileReaderBZip3
(
BmnFileReaderBZip3
&&) noexcept = delete;
44
BmnFileReaderBZip3
& operator=(
BmnFileReaderBZip3
&&) noexcept = delete;
45
46
// ========== BmnIFileReader Interface ==========
47
48
/*
49
* A method that opens a binary BZip3-compressed file for reading
50
* @param aFilePath A path to the file that should be opened
51
* @warning Be very carful with "the currently-working directory" as it may not be the same as the executable path!
52
*/
53
virtual
void
Open
(const std::filesystem::path& aFilePath) override;
54
55
/*
56
* Closes the stream and resets all internal data. After calling `Close`, it's safe to reopen using the same
57
* instance of this class
58
*/
59
virtual
void
Close
() noexcept override;
60
61
/*
62
* Reads and decompresses the data while still managing caching for efficiency.
63
* @param dataToFill A buffer that will be filled with uncompressed data
64
* @param sizeToFill A number of bytes that will be filled in `dataToFill`
65
* @return A number of bytes that were filled in `dataToFill`. This value should almost always be the same as
66
* `sizeToFill` except when the end of the stream is reached (only if the user asked for more data than
67
* possible)
68
*/
69
virtual int64_t
Read
(
void
* dataToFill, int64_t sizeToFill) override;
70
71
/*
72
* A method that tells us what's the next byte to read
73
* @return A position of the next byte that should be read
74
*/
75
[[nodiscard]] virtual int64_t
Tell
() const noexcept override;
76
77
/*
78
* A method that allows us to go to another place in the file (uncompressed data) and read from that place.
79
* @param value A position in uncompressed data to which one wants to move before the next read
80
*/
81
virtual
void
Seek
(int64_t value) override;
82
83
/*
84
* Checks whether the reading stream is good
85
* @return 'true' if no decompression error has occurred and the input-file stream is good, or 'false' otherwise
86
*/
87
[[nodiscard]] virtual
bool
IsGood
() const noexcept override;
88
89
/*
90
* Check if we have reached the file. This will tell us if we have reached EOF in the uncompressed data, not
91
* in the file itself. That is, the file might be at EOF, while this method returns will return true as cache
92
* still has some data
93
* @return 'true' if both file and internal cache are completely read, 'false' otherwise
94
*/
95
[[nodiscard]] virtual
bool
IsEOF
() const noexcept override;
96
97
/*
98
* A method that clears bad flags after an error has occurred. Just calling this error will most likely
99
* not solve the problem, and you will get an error again in the next read. That is, before calling `Read`,
100
* remove the previous error source
101
*/
102
virtual
void
ClearStateFlags
() override;
103
104
/*
105
* @return 'true' if the input stream is open, or 'false' if not
106
*/
107
[[nodiscard]] virtual
bool
IsOpen
() const noexcept override;
108
109
/*
110
* @return BmnFileType::BZip3
111
*/
112
[[nodiscard]] virtual
BmnFileType
GetFileType
() const noexcept override;
113
114
// ========== BmnIArchiveLib Interface ==========
115
116
/*
117
* If an error occurred while decompressing, this method can be called to obtain a description of the error
118
* @return A description of an error that occurred while decompressing, or an empty string if no such error happened
119
*/
120
[[nodiscard]] virtual std::
string
GetLastUncompressErrorDescription
() const override;
121
122
/*
123
* A method that tells us how many uncompressed bytes were read from an input file
124
* @return A number of uncompressed bytes that passed through the BZip3
125
*/
126
[[nodiscard]] virtual
size_t
GetNumberOfDecompressedBytes
() const noexcept override;
127
128
/*
129
* A method that can be used to check if the reading stream has ended
130
* @return 'true' if we have reached EOF, 'false' otherwise
131
*/
132
[[nodiscard]] virtual
bool
HasReachedTheEndOfStream
() const noexcept override;
133
134
/*
135
* A method that checks whether the last read was good or not. If not, the stream is not good anymore
136
* @return 'true' if the stream is in a good state, 'false' otherwise
137
*/
138
[[nodiscard]] virtual
bool
IsStreamInGoodState
() const noexcept override;
139
140
protected:
141
/*
142
* A method that is called to uncompress Deflate data with BZip3
143
* @param source For this library, the same buffer is used as source and output. This, `source` is not used
144
* @param sourceTotalSize A number of bytes available in the source (output) buffer.
145
* @param numberOfReadBytes This value will not be used by this method as all input data (block) will always be
146
* read. Otherwise, an error is reported
147
* @param output A pointer to a buffer with compressed data. This method will use the compressed data and write
148
* uncompressed data back to the same buffer
149
* @param outputTotalSize The actual size of the output buffer (it must be able to accept an entire uncompressed
150
* block)
151
* @param numberOfSetOutputBytes This value is use as an input as well (an uncompressed block size). It will also be
152
* set to a number of bytes that were set while decompressing.
153
* @return There are a few options:
154
* 3. `BmnFileStreamResults::UnexpectedFail` BZip3 failed while decompressing
155
* 4. `BmnFileStreamResults::ReachedEOF` BZip3 reached the end of frame (in our case file)
156
* 5. `BmnFileStreamResults::Success` BZip3 successfully uncompressed the package
157
*/
158
virtual
BmnFileStreamResults
Uncompress
(const
void
* source,
159
const
size_t
sourceTotalSize,
160
size_t
& numberOfReadBytes,
161
void
* output,
162
const
size_t
outputTotalSize,
163
size_t
& numberOfSetOutputBytes) override;
164
165
private:
166
/*
167
* A method that handles reading the next data chunk from the input file (if in a good state) and calls the
168
* uncompress method. This method should be called only after cache buffer is full read!
169
* @return 'true' if the data block was successfully decompressed, 'false' otherwise
170
*/
171
[[nodiscard]]
bool
DecompressNextDataBlock
();
172
173
public:
174
// ========== BmnFileReaderBZip3 inlines ==========
175
176
[[nodiscard]] inline bz3_state*
GetUnderlyingLibraryStream
() const noexcept;
177
[[nodiscard]] inline
size_t
GetTotalFileSize
() const noexcept;
178
179
private:
180
bz3_state*
fBZip3Stream
;
181
/*
182
As this class has no common logic with out archive libraries, I'm keeping the logic local. If however we decide
183
to add some additional libraries with the same logic (e.g., I assume BZip2), then the interface of the class
184
should be separated from the cache manager!
185
*/
186
std::vector<uint8_t>
fCacheBuffer
;
187
std::ifstream
fFileBeingProcessed
;
188
size_t
fTotalFileSize
;
189
// This value represents the actual number of bytes that we will need to get from the IO stream
190
size_t
fRemainingBytesToRead
;
191
// This value represents the number of read bytes after decompression
192
size_t
fNumberOfReadBytes
;
193
size_t
fNumberOfValidCacheBufferBytes
;
194
size_t
fCurrentIterCacheBufferOffset
;
195
int32_t
fLastErrorCode
;
196
// 4 padding bytes
197
};
198
199
// 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
200
// ================== BmnFileReaderBZip3 inline methods ==================
201
// 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
202
203
bz3_state*
BmnFileReaderBZip3
::
GetUnderlyingLibraryStream
() const noexcept
204
{
205
return
fBZip3Stream
;
206
}
207
208
size_t
BmnFileReaderBZip3::GetTotalFileSize
() const noexcept
209
{
210
return
fTotalFileSize
;
211
}
212
213
#endif
// BmnFileReaderBZip3_hpp
BmnFileStreamResults
BmnFileStreamResults
Definition
BmnFileProcessingFlags.h:74
BmnFileType
BmnFileType
Definition
BmnFileProcessingFlags.h:16
BmnIArchiveLibRead.h
BmnIFileReader.h
BmnFileReaderBZip3::fFileBeingProcessed
std::ifstream fFileBeingProcessed
Definition
BmnFileReaderBZip3.h:187
BmnFileReaderBZip3::IsGood
virtual bool IsGood() const noexcept override
Definition
BmnFileReaderBZip3.cxx:251
BmnFileReaderBZip3::BmnFileReaderBZip3
BmnFileReaderBZip3(const BmnFileReaderBZip3 &)=delete
BmnFileReaderBZip3::GetLastUncompressErrorDescription
virtual std::string GetLastUncompressErrorDescription() const override
Definition
BmnFileReaderBZip3.cxx:283
BmnFileReaderBZip3::Read
virtual int64_t Read(void *dataToFill, int64_t sizeToFill) override
Definition
BmnFileReaderBZip3.cxx:135
BmnFileReaderBZip3::DecompressNextDataBlock
bool DecompressNextDataBlock()
Definition
BmnFileReaderBZip3.cxx:350
BmnFileReaderBZip3::GetUnderlyingLibraryStream
bz3_state * GetUnderlyingLibraryStream() const noexcept
Definition
BmnFileReaderBZip3.h:203
BmnFileReaderBZip3::fCacheBuffer
std::vector< uint8_t > fCacheBuffer
Definition
BmnFileReaderBZip3.h:186
BmnFileReaderBZip3::fNumberOfValidCacheBufferBytes
size_t fNumberOfValidCacheBufferBytes
Definition
BmnFileReaderBZip3.h:193
BmnFileReaderBZip3::fRemainingBytesToRead
size_t fRemainingBytesToRead
Definition
BmnFileReaderBZip3.h:190
BmnFileReaderBZip3::ClearStateFlags
virtual void ClearStateFlags() override
Definition
BmnFileReaderBZip3.cxx:265
BmnFileReaderBZip3::GetTotalFileSize
size_t GetTotalFileSize() const noexcept
Definition
BmnFileReaderBZip3.h:208
BmnFileReaderBZip3::BmnFileReaderBZip3
BmnFileReaderBZip3(const std::filesystem::path &aFilePath="")
Definition
BmnFileReaderBZip3.cxx:32
BmnFileReaderBZip3::GetFileType
virtual BmnFileType GetFileType() const noexcept override
Definition
BmnFileReaderBZip3.cxx:276
BmnFileReaderBZip3::Close
virtual void Close() noexcept override
Definition
BmnFileReaderBZip3.cxx:115
BmnFileReaderBZip3::Open
virtual void Open(const std::filesystem::path &aFilePath) override
Definition
BmnFileReaderBZip3.cxx:57
BmnFileReaderBZip3::BmnFileReaderBZip3
BmnFileReaderBZip3(BmnFileReaderBZip3 &&) noexcept=delete
BmnFileReaderBZip3::fCurrentIterCacheBufferOffset
size_t fCurrentIterCacheBufferOffset
Definition
BmnFileReaderBZip3.h:194
BmnFileReaderBZip3::~BmnFileReaderBZip3
virtual ~BmnFileReaderBZip3() override
Definition
BmnFileReaderBZip3.cxx:50
BmnFileReaderBZip3::fLastErrorCode
int32_t fLastErrorCode
Definition
BmnFileReaderBZip3.h:195
BmnFileReaderBZip3::HasReachedTheEndOfStream
virtual bool HasReachedTheEndOfStream() const noexcept override
Definition
BmnFileReaderBZip3.cxx:302
BmnFileReaderBZip3::IsStreamInGoodState
virtual bool IsStreamInGoodState() const noexcept override
Definition
BmnFileReaderBZip3.cxx:307
BmnFileReaderBZip3::GetNumberOfDecompressedBytes
virtual size_t GetNumberOfDecompressedBytes() const noexcept override
Definition
BmnFileReaderBZip3.cxx:297
BmnFileReaderBZip3::fNumberOfReadBytes
size_t fNumberOfReadBytes
Definition
BmnFileReaderBZip3.h:192
BmnFileReaderBZip3::Tell
virtual int64_t Tell() const noexcept override
Definition
BmnFileReaderBZip3.cxx:175
BmnFileReaderBZip3::IsEOF
virtual bool IsEOF() const noexcept override
Definition
BmnFileReaderBZip3.cxx:260
BmnFileReaderBZip3::fBZip3Stream
bz3_state * fBZip3Stream
Definition
BmnFileReaderBZip3.h:180
BmnFileReaderBZip3::operator=
BmnFileReaderBZip3 & operator=(const BmnFileReaderBZip3 &)=delete
BmnFileReaderBZip3::Seek
virtual void Seek(int64_t value) override
Definition
BmnFileReaderBZip3.cxx:180
BmnFileReaderBZip3::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
BmnFileReaderBZip3.cxx:319
BmnFileReaderBZip3::IsOpen
virtual bool IsOpen() const noexcept override
Definition
BmnFileReaderBZip3.cxx:271
BmnFileReaderBZip3::fTotalFileSize
size_t fTotalFileSize
Definition
BmnFileReaderBZip3.h:188
BmnIFileReader::BmnIFileReader
BmnIFileReader()=default
BmnIArchiveLibRead::BmnIArchiveLibRead
BmnIArchiveLibRead()=default
compression
include
BmnFileReaderBZip3.h
Generated on
for BmnRoot by
1.17.0