Skip to content

Gio.MemoryOutputStream

class — extends OutputStream, PollableOutputStream, Seekable

GMemoryOutputStream is a class for using arbitrary memory chunks as output for GIO streaming output operations.

As of GLib 2.34, GMemoryOutputStream trivially implements PollableOutputStream: it always polls as ready.

Constructors

new_resizable

@classmethod
def new_resizable(cls) -> OutputStream

Creates a new MemoryOutputStream, using GLib.realloc and GLib.free for memory allocation.

Methods

get_data

def get_data(self) -> int | None

Gets any loaded data from the ostream.

Note that the returned pointer may become invalid on the next write or truncate operation on the stream.

get_data_size

def get_data_size(self) -> int

Returns the number of bytes from the start up to including the last byte written in the stream that has not been truncated away.

get_size

def get_size(self) -> int

Gets the size of the currently allocated data area (available from MemoryOutputStream.get_data).

You probably don't want to use this function on resizable streams. See MemoryOutputStream.get_data_size instead. For resizable streams the size returned by this function is an implementation detail and may be change at any time in response to operations on the stream.

If the stream is fixed-sized (ie: no realloc was passed to g_memory_output_stream_new()) then this is the maximum size of the stream and further writes will return IOErrorEnum.NO_SPACE.

In any case, if you want the number of bytes currently written to the stream, use MemoryOutputStream.get_data_size.

steal_as_bytes

def steal_as_bytes(self) -> bytes

Returns data from the ostream as a GLib.Bytes. ostream must be closed before calling this function.

steal_data

def steal_data(self) -> int | None

Gets any loaded data from the ostream. Ownership of the data is transferred to the caller; when no longer needed it must be freed using the free function set in ostream's MemoryOutputStream:destroy-function property.

ostream must be closed before calling this function.

Properties

data

data: int  # read/write

Pointer to buffer where data will be written.

data_size

data_size: int  # read-only

Size of data written to the buffer.

size

size: int  # read/write

Current size of the data buffer.