17 #ifndef CPP_MATE_READ_STREAM_HPP
18 #define CPP_MATE_READ_STREAM_HPP
20 #include <CppMate/BinaryData.hpp>
21 #include <CppMate/BinarySink.hpp>
22 #include <CppMate/ByteOrder.hpp>
23 #include <CppMate/Checkers.hpp>
49 _sharedData(std::move(data)),
50 _data(_sharedData.get()),
51 _binary(_data ? _data->
getData() : nullptr),
52 _size(_data ? _data->
getSize() : 0),
68 _binary(_data ? _data->
getData() : nullptr),
69 _size(_data ? _data->
getSize() : 0),
87 uint64_t
getSize()
const {
return _size; }
93 uint64_t
getAvailable()
const {
return _position < _size ? _size - _position : 0; }
123 void seek(uint64_t offset,
bool forward =
true) {
setPosition(forward ? _position + offset : _position - offset); }
130 inline void seek(uint64_t count = 1) {
seek(
sizeof(T) * count); }
151 inline bool canRead(uint64_t count = 1)
const {
return canRead(
sizeof(T) * count); }
160 if (_isNativeByteOrder ||
sizeof(T) < 2) {
161 result = *
reinterpret_cast<const T *
>(_binary + _position);
163 auto const resultPtr =
reinterpret_cast<uint8_t *
>(&result);
166 resultPtr[0] = *(_binary + _position + 1);
167 resultPtr[1] = *(_binary + _position);
170 resultPtr[0] = *(_binary + _position + 3);
171 resultPtr[1] = *(_binary + _position + 2);
172 resultPtr[2] = *(_binary + _position + 1);
173 resultPtr[3] = *(_binary + _position);
176 resultPtr[0] = *(_binary + _position + 7);
177 resultPtr[1] = *(_binary + _position + 6);
178 resultPtr[2] = *(_binary + _position + 5);
179 resultPtr[3] = *(_binary + _position + 4);
180 resultPtr[4] = *(_binary + _position + 3);
181 resultPtr[5] = *(_binary + _position + 2);
182 resultPtr[6] = *(_binary + _position + 1);
183 resultPtr[7] = *(_binary + _position);
186 for (
auto i = 0u; i <
sizeof(T); i++) {
187 resultPtr[i] = *(_binary + _position + (
sizeof(T) - i - 1));
202 std::string result(length,
'\0');
203 memcpy(&result[0], _binary + _position, length);
215 uint64_t
readBytes(uint8_t* buffer, uint64_t bufferSize) {
217 const auto count = std::min(
getAvailable(), bufferSize);
218 memcpy(buffer, _binary + _position,
static_cast<size_t>(count));
229 sink.
write(_binary + _position, bytes);
247 bool operator==(
const ReadStream& other)
const {
return _data == other._data && _position == other._position && _byteOrder == other._byteOrder; }
257 std::shared_ptr<BinaryData> _sharedData;
259 const uint8_t* _binary;
260 const uint64_t _size;
262 const bool _isNativeByteOrder;
268 result = stream.
read<T>();
273 inline uint8_t ReadStream::read<uint8_t>()
275 return _binary[_position++];
279 inline char ReadStream::read<char>()
281 return static_cast<char>(_binary[_position++]);
Represents interface of abstract binary data.
Definition: BinaryData.hpp:34
Represents low-level interface of abstract binary sink.
Definition: BinarySink.hpp:36
virtual void write(const void *data, uint64_t size)=0
Writes data into the sink.
Represents the byte stream reader with custom byte order.
Definition: ReadStream.hpp:39
void reset()
Resets stream position to begin of the source data.
Definition: ReadStream.hpp:116
void setPosition(uint64_t position)
Sets a new absolute position of the stream.
Definition: ReadStream.hpp:111
bool operator!=(const ReadStream &other) const
operator !=
Definition: ReadStream.hpp:254
std::string readString(uint64_t length)
Reads string from the stream.
Definition: ReadStream.hpp:201
uint64_t getAvailable() const
Returns number of bytes available to read.
Definition: ReadStream.hpp:93
uint64_t readBytes(uint8_t *buffer, uint64_t bufferSize)
Reads a particular number of bytes from the stream.
Definition: ReadStream.hpp:215
bool operator==(const ReadStream &other) const
operator ==
Definition: ReadStream.hpp:247
void seekAlign(unsigned align)
Changes position of stream to the closest position divisible by the provided parameter.
Definition: ReadStream.hpp:136
void seek(uint64_t offset, bool forward=true)
Changes position of the stream by increase the offset value with the current position....
Definition: ReadStream.hpp:123
ReadStream(const BinaryData *data, ByteOrder byteOrder=ByteOrder::Unknown)
Constructor.
Definition: ReadStream.hpp:66
T read()
Reads item from the stream. The method doesn't validate bounds, see canRead.
Definition: ReadStream.hpp:158
void transfer(BinarySink &sink, uint64_t bytes)
Transfer arbitrary block of bytes from stream to binary sink.
Definition: ReadStream.hpp:228
uint64_t getPosition() const
Returns current position of the stream.
Definition: ReadStream.hpp:105
ReadStream(std::shared_ptr< BinaryData > data, ByteOrder byteOrder=ByteOrder::Unknown)
Constructor.
Definition: ReadStream.hpp:48
bool canRead(uint64_t count=1) const
Checks the ability to read a specified number of items.
Definition: ReadStream.hpp:151
uint64_t getSize() const
Returns size of the stream.
Definition: ReadStream.hpp:87
ByteOrder getByteOrder() const
Returns byte order of the stream.
Definition: ReadStream.hpp:99
const BinaryData * getData() const
Returns binary data of stream.
Definition: ReadStream.hpp:81
bool canRead(uint64_t bytes) const
Checks the ability to read a specified number of bytes.
Definition: ReadStream.hpp:143
void seek(uint64_t count=1)
Seeks stream over a specified number of items.
Definition: ReadStream.hpp:130
friend ReadStream & operator>>(ReadStream &stream, T &result)
operator >>
Definition: ReadStream.hpp:267
Definition: BinaryData.hpp:28
ByteOrder CPP_MATE_LIB_API getNativeByteOrder()
Returns platform byte order.
ReadStream & operator>>(ReadStream &stream, T &result)
Definition: ReadStream.hpp:267
ByteOrder
The byte order type.
Definition: ByteOrder.hpp:29
@ Unknown
Unknown byte order.