class MemoryDataStream

Inherits: DataStream

Data stream for handling data from memory.

Data is stored in a memory block that is either owned by the stream (freed when stream goes out of scope), or owned externally.

Public

Constructors

MemoryDataStream

MemoryDataStream()

Initializes an empty memory stream.

As data is written the stream will grow its internal memory storage automatically.

MemoryDataStream

MemoryDataStream(size_t capacity)

Initializes a stream with some initial capacity.

If more bytes than capacity is written, the stream will grow its internal memory storage.

capacity
Number of bytes to initially allocate for the internal memory storage.

MemoryDataStream

MemoryDataStream(void *memory, size_t size)

Wrap an existing memory chunk in a stream.

memory
Memory to wrap the data stream around.
size
Size of the memory chunk in bytes.

MemoryDataStream

MemoryDataStream(const MemoryDataStream &other)

Create a stream which pre-buffers the contents of another stream.

Data from the other buffer will be entirely read and stored in an internal buffer.

MemoryDataStream

MemoryDataStream(const SPtr<DataStream> &other)

Create a stream which pre-buffers the contents of another stream.

Data from the other buffer will be entirely read and stored in an internal buffer.

MemoryDataStream

MemoryDataStream(MemoryDataStream &&other)

Inherits the data from the provided stream, invalidating the source stream.

Methods

~MemoryDataStream

~MemoryDataStream() noexcept

Data

uint8_t *Data() const

Get a pointer to the start of the memory block this stream holds.

Cursor

uint8_t *Cursor() const

Get a pointer to the current position in the memory block this stream holds.

IsFile

bool IsFile() const override

Checks whether the stream reads/writes from a file system.

Read

size_t Read(void *data, size_t byteCount) const override

Read the requisite number of bytes from the stream, stopping at the end of the file.

Advances the read pointer.

data
Pre-allocated buffer to read the data into.
byteCount
Number of bytes to read.

Returns: Number of bytes actually read.

Write

size_t Write(const void *data, size_t byteCount) override

Write the requisite number of bytes to the stream and advance the write pointer.

data
Buffer containing bytes to write.
byteCount
Number of bytes to write.

Returns: Number of bytes actually written.

Skip

size_t Skip(size_t byteCount) override

Skip a defined number of bytes.

Returns the actual number of bytes skipped.

Seek

size_t Seek(size_t position) override

Repositions the read or write cursor to the specified byte.

Returns the actual byte the cursor has been placed at, in case end has been reached earlier

Tell

size_t Tell() const override

Returns the current cursor byte offset from beginning.

Eof

bool Eof() const override

Returns true if the stream has reached the end.

Clone

SPtr<DataStream> Clone(bool copyData = true) const override

Creates a copy of this stream.

copyData
If true the internal stream data will be copied as well, otherwise it will just reference the data from the original stream (in which case the caller must ensure the original stream outlives the clone). This is not relevant for file streams.

Flush

bool Flush() override

Flushes the stream, writing any buffer data to the destination.

Returns false if some error occurred and data was not written correctly. Does not need to be called if stream was just read from.

Close

bool Close() override

Closes the stream.

This makes further operations invalid. Executes a Flush() before closing, and passes the return value from Flush().

DisownMemory

uint8_t *DisownMemory()

Disowns the internal memory buffer, ensuring it wont be released when the stream goes out of scope.

The caller becomes responsible for freeing the internal data buffer.

Operators

operator=

MemoryDataStream &operator=(const MemoryDataStream &other)

operator=

Protected

Methods

ReallocateBuffer

void ReallocateBuffer(size_t byteCount)

Reallocates the internal buffer making enough room for .

EnsureEnoughSpace

size_t EnsureEnoughSpace(size_t size)

Ensures the underlying buffer has enough space to store bytes.

This will be calculated starting from the current offset. If the new size exceeds current capacity and buffer supports resizing, it will be increased to fit the new size. If the buffer cannot be resized, the amount of bytes that fit into the current buffer will be returned by this method.

Fields

mData

uint8_t * mData

mCursor

uint8_t * mCursor

mEnd

uint8_t * mEnd

mCapacity

size_t mCapacity

mOwnsMemory

bool mOwnsMemory