Class CMemoryStream

Inheritance Relationships

Base Type

Class Documentation

class CMemoryStream : public mrpt::io::CStream

This CStream derived class allow using a memory buffer as a CStream. This class is useful for storing any required set of variables or objects, and then read them to other objects, or storing them to a file, for example.

See also

CStream

Public Functions

virtual size_t Read(void *Buffer, size_t Count) override

Introduces a pure virtual method responsible for reading from the stream.

virtual size_t Write(const void *Buffer, size_t Count) override

Introduces a pure virtual method responsible for writing to the stream. Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.

CMemoryStream() = default

Default constructor

CMemoryStream(const void *data, const uint64_t nBytesInData)

Constructor to Initialize the data in the stream from a block of memory (which is copied), and sets the current stream position at the beginning of the data.

void assignMemoryNotOwn(const void *data, const uint64_t nBytesInData)

Initialize the data in the stream from a block of memory which is NEITHER OWNED NOR COPIED by the object, so it must exist during the whole live of the object. After assigning a block of data with this method, the object becomes “read-only”, so further attempts to change the size of the buffer will raise an exception. This method resets the write and read positions to the beginning.

~CMemoryStream() override

Destructor

void clear()

Clears the memory buffer.

virtual std::string getStreamDescription() const override

Returns a human-friendly description of the stream, e.g. a filename.

virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) override

Introduces a pure virtual method for moving to a specified position in the streamed resource. he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values:

  • sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.

  • sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset.

  • sFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file.

Returns:

Seek returns the new value of the Position property.

virtual uint64_t getTotalBytesCount() const override

Returns the total size of the internal buffer

virtual uint64_t getPosition() const override

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one

void *getRawBufferData()

Method for getting a pointer to the raw stored data. The length in bytes is given by getTotalBytesCount

const void *getRawBufferData() const
bool saveBufferToFile(const std::string &file_name)

Saves the entire buffer to a file

Returns:

true on success

bool loadBufferFromFile(const std::string &file_name)

Loads the entire buffer from a file

Returns:

true on success

inline void setAllocBlockSize(uint64_t alloc_block_size)

Change the size of the additional memory block that is reserved whenever the current block runs too short (default=0x10000 bytes)

Protected Functions

void resize(uint64_t newSize)

Resizes the internal buffer size.

Protected Attributes

void_ptr_noncopy m_memory = {nullptr}

Internal data

uint64_t m_size = {0}
uint64_t m_position = {0}
uint64_t m_bytesWritten = {0}
uint64_t m_alloc_block_size = {0x1000}
bool m_read_only = {false}

If the memory block does not belong to the object.