class Core::IO::MemoryStream

Overview

Stream backed by a block of heap memory that can grow dynamically. More…

#include <corememstream.h>

class MemoryStream:
    public Core::IO::Stream,
    public Core::IO::BufferProvider
{
public:
    // fields

    static const uint32 kDefaultGrow = 8192;

    // construction

    MemoryStream(uint32 memoryGrow = kDefaultGrow);
    MemoryStream(void* buffer, uint32 size);
    MemoryStream(const MemoryStream& ms);

    // methods

    MemoryStream& take(MemoryStream& ms);
    MemoryStream& take(Buffer& buffer);
    bool copyFrom(const MemoryStream& ms);
    bool allocateMemory(uint32 size, bool initWithZero = false);
    const Buffer& getBuffer() const;
    uint32 getMemoryGrow() const;
    void setMemoryGrow(uint32 memoryGrow);
    uint32 getBytesWritten() const;
    bool setBytesWritten(uint32 bytesWritten);
    virtual int64 getPosition();
    virtual int64 setPosition(int64 pos, int mode);
    virtual int readBytes(void* buffer, int size);
    virtual int writeBytes(const void* buffer, int size);
    virtual BufferProvider* getBufferProvider();
    virtual void moveBufferTo(Buffer& buffer);
};

Inherited Members

public:
    // fields

    static const InterfaceID kIID = FOUR_CHAR_ID('B','S','t','r');

    // methods

    virtual int64 getPosition() = 0;
    virtual int64 setPosition(int64 pos, int mode) = 0;
    virtual int readBytes(void* buffer, int size) = 0;
    virtual int writeBytes(const void* buffer, int size) = 0;
    virtual BufferProvider* getBufferProvider();
    virtual void moveBufferTo(Buffer& buffer) = 0;

Detailed Documentation

Stream backed by a block of heap memory that can grow dynamically.

Construction

MemoryStream(uint32 memoryGrow = kDefaultGrow)

[HEAVY] Stream memory grows with given amount.

MemoryStream(void* buffer, uint32 size)

[LIGHT] Wraps buffer into stream, DOES NOT copy memory.

MemoryStream(const MemoryStream& ms)

Copy constructor.

Methods

MemoryStream& take(MemoryStream& ms)

Take over memory from other memory stream.

MemoryStream& take(Buffer& buffer)

Take over memory from other buffer.

bool copyFrom(const MemoryStream& ms)

Copy data from other memory stream.

bool allocateMemory(uint32 size, bool initWithZero = false)

Allocate (and optionally initialize) internal stream memory.

const Buffer& getBuffer() const

Access underlying buffer (read-only).

uint32 getMemoryGrow() const

Get current memory grow amount.

void setMemoryGrow(uint32 memoryGrow)

Change memory grow amount.

uint32 getBytesWritten() const

Get number of bytes written to stream.

bool setBytesWritten(uint32 bytesWritten)

Manually set number of bytes written to stream.

virtual int64 getPosition()

Get current read/write position.

virtual int64 setPosition(int64 pos, int mode)

Set current read/write position.

See also:

SeekMode

virtual int readBytes(void* buffer, int size)

Read data from stream.

Returns:

number of bytes read or -1 for error

virtual int writeBytes(const void* buffer, int size)

Write data to stream.

Returns:

number of bytes written or -1 for error

virtual BufferProvider* getBufferProvider()

Provide access to underlying buffer (optional).

virtual void moveBufferTo(Buffer& buffer)

Transfer memory ownership to given buffer.