GMemoryOutputStream

## GMemoryOutputStream GMemoryOutputStream — Streaming output operations on memory chunks |

## Synopsis

    #include <gio/gio.h>

    gpointer            (*GReallocFunc)                     (gpointer data,
                                                             gsize size);
                        GMemoryOutputStream;
    GOutputStream *     g_memory_output_stream_new          (gpointer data,
                                                             gsize size,
                                                             GReallocFunc realloc_function,
                                                             GDestroyNotify destroy_function);
    gpointer            g_memory_output_stream_get_data     (GMemoryOutputStream *ostream);
    gsize               g_memory_output_stream_get_size     (GMemoryOutputStream *ostream);
    gsize               g_memory_output_stream_get_data_size
                                                            (GMemoryOutputStream *ostream);
    gpointer            g_memory_output_stream_steal_data   (GMemoryOutputStream *ostream);

## Object Hierarchy

      GObject
       +----GOutputStream
             +----GMemoryOutputStream

## Implemented Interfaces

GMemoryOutputStream implements GSeekable.

## Properties

      "data"                     gpointer              : Read / Write / Construct Only
      "data-size"                gulong                : Read
      "destroy-function"         gpointer              : Read / Write / Construct Only
      "realloc-function"         gpointer              : Read / Write / Construct Only
      "size"                     gulong                : Read / Write / Construct Only

## Description

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

## Details

### GReallocFunc ()

    gpointer            (*GReallocFunc)                     (gpointer data,
                                                             gsize size);

Changes the size of the memory block pointed to by `data` to `size` bytes.

The function should have the same semantics as `realloc()`.

`data` : | memory block to reallocate
`size` : | size to reallocate `data` to
Returns : | a pointer to the reallocated memory

### GMemoryOutputStream

    typedef struct _GMemoryOutputStream GMemoryOutputStream;

Implements GOutputStream for arbitrary memory chunks.

### g_memory_output_stream_new ()

    GOutputStream *     g_memory_output_stream_new          (gpointer data,
                                                             gsize size,
                                                             GReallocFunc realloc_function,
                                                             GDestroyNotify destroy_function);

Creates a new GMemoryOutputStream.

If `data` is non-`NULL`, the stream will use that for its internal storage. If `realloc_fn` is non-`NULL`, it will be used for resizing the internal storage when necessary. To construct a fixed-size output stream, pass `NULL` as `realloc_fn`.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    /* a stream that can grow */
    stream = g_memory_output_stream_new (NULL, 0, realloc, free);

    /* another stream that can grow */
    stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);

    /* a fixed-size stream */
    data = malloc (200);
    stream3 = g_memory_output_stream_new (data, 200, NULL, free);

`data` : | pointer to a chunk of memory to use, or `NULL`. [allow-none]
`size` : | the size of `data`
`realloc_function` : | a function with `realloc()` semantics (like `g_realloc()`) to be called when `data` needs to be grown, or `NULL`. [allow-none]
`destroy_function` : | a function to be called on `data` when the stream is finalized, or `NULL`. [allow-none]
Returns : | A newly created GMemoryOutputStream object.

### g_memory_output_stream_get_data ()

    gpointer            g_memory_output_stream_get_data     (GMemoryOutputStream *ostream);

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.

`ostream` : | a GMemoryOutputStream
Returns : | pointer to the stream's data. [transfer none]

### g_memory_output_stream_get_size ()

    gsize               g_memory_output_stream_get_size     (GMemoryOutputStream *ostream);

Gets the size of the currently allocated data area (available from `g_memory_output_stream_get_data()`). If the stream isn't growable (no realloc was passed to `g_memory_output_stream_new()`) then this is the maximum size of the stream and further writes will return `G_IO_ERROR_NO_SPACE`.

Note that for growable streams the returned size may become invalid on the next write or truncate operation on the stream.

If you want the number of bytes currently written to the stream, use `g_memory_output_stream_get_data_size()`.

`ostream` : | a GMemoryOutputStream
Returns : | the number of bytes allocated for the data buffer

### g_memory_output_stream_get_data_size ()

    gsize               g_memory_output_stream_get_data_size
                                                            (GMemoryOutputStream *ostream);

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

`ostream` : | a GMemoryOutputStream
Returns : | the number of bytes written to the stream

Since 2.18

### g_memory_output_stream_steal_data ()

    gpointer            g_memory_output_stream_steal_data   (GMemoryOutputStream *ostream);

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 "destroy-function" property.

`ostream` must be closed before calling this function.

`ostream` : | a GMemoryOutputStream
Returns : | the stream's data. [transfer full]

Since 2.26

## Property Details

### The `"data"` property

      "data"                     gpointer              : Read / Write / Construct Only

Pointer to buffer where data will be written.

Since 2.24

### The `"data-size"` property

      "data-size"                gulong                : Read

Size of data written to the buffer.

Since 2.24

### The `"destroy-function"` property

      "destroy-function"         gpointer              : Read / Write / Construct Only

Function called with the buffer as argument when the stream is destroyed.

Since 2.24

### The `"realloc-function"` property

      "realloc-function"         gpointer              : Read / Write / Construct Only

Function with realloc semantics called to enlarge the buffer.

Since 2.24

### The `"size"` property

      "size"                     gulong                : Read / Write / Construct Only

Current size of the data buffer.

Since 2.24

## See Also

GMemoryInputStream