class mrpt::io::CMemoryStream

Overview

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

#include <mrpt/io/CMemoryStream.h>

class CMemoryStream: public mrpt::io::CStream
{
public:
    // construction

    CMemoryStream();
    CMemoryStream(const void* data, const uint64_t nBytesInData);

    // methods

    virtual size_t Read(void* Buffer, size_t Count);
    virtual size_t Write(const void* Buffer, size_t Count);
    void assignMemoryNotOwn(const void* data, const uint64_t nBytesInData);
    void clear();
    virtual std::string getStreamDescription() const;
    virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning);
    virtual uint64_t getTotalBytesCount() const;
    virtual uint64_t getPosition() const;
    void* getRawBufferData();
    const void* getRawBufferData() const;
    bool saveBufferToFile(const std::string& file_name);
    bool loadBufferFromFile(const std::string& file_name);
    void setAllocBlockSize(uint64_t alloc_block_size);
};

Inherited Members

public:
    // methods

    virtual size_t Read(void* Buffer, size_t Count) = 0;
    virtual size_t Write(const void* Buffer, size_t Count) = 0;
    virtual uint64_t getTotalBytesCount() const = 0;
    virtual uint64_t getPosition() const = 0;

Construction

CMemoryStream()

Default constructor.

CMemoryStream(const void* data, const uint64_t nBytesInData)

Constructor to initilize 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.

See also:

assignMemoryNotOwn

Methods

virtual size_t Read(void* Buffer, size_t Count)

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

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

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.

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

Initilize 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.

void clear()

Clears the memory buffer.

virtual std::string getStreamDescription() const

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

a filename.

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

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

Returns the total size of the internal buffer

virtual uint64_t getPosition() const

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 lenght in bytes is given by getTotalBytesCount

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

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)