g_memory_output_stream_get_data_size
g_memory_output_stream_get_size
g_memory_output_stream_steal_data
+g_memory_output_stream_steal_as_bytes
g_mount_operation_get_type
g_mount_operation_new
g_mount_operation_get_username
return data;
}
+/**
+ * g_memory_output_stream_steal_as_bytes:
+ * @ostream: a #GMemoryOutputStream
+ *
+ * Returns data from the @ostream as a #GBytes. @ostream must be
+ * closed before calling this function.
+ *
+ * Returns: (transfer full): the stream's data
+ *
+ * Since: 2.34
+ **/
+GBytes *
+g_memory_output_stream_steal_as_bytes (GMemoryOutputStream *ostream)
+{
+ GBytes *result;
+
+ g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
+ g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
+
+ result = g_bytes_new_with_free_func (ostream->priv->data,
+ ostream->priv->valid_len,
+ ostream->priv->destroy,
+ ostream->priv->data);
+ ostream->priv->data = NULL;
+
+ return result;
+}
+
static gboolean
array_resize (GMemoryOutputStream *ostream,
gsize size,
g_object_unref (mo);
}
+static void
+test_steal_as_bytes (void)
+{
+ GOutputStream *mo;
+ GDataOutputStream *o;
+ GError *error = NULL;
+ GBytes *bytes;
+ gsize size;
+
+ mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
+ "realloc-function", g_realloc,
+ "destroy-function", g_free,
+ NULL);
+ o = g_data_output_stream_new (mo);
+
+ g_data_output_stream_put_string (o, "hello ", NULL, &error);
+ g_assert_no_error (error);
+
+ g_data_output_stream_put_string (o, "world!", NULL, &error);
+ g_assert_no_error (error);
+
+ g_data_output_stream_put_byte (o, '\0', NULL, &error);
+ g_assert_no_error (error);
+
+ g_output_stream_close ((GOutputStream*) o, NULL, &error);
+ g_assert_no_error (error);
+
+ bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
+ g_object_unref (mo);
+
+ g_assert_cmpint (g_bytes_get_size (bytes), ==, strlen ("hello world!") + 1);
+ g_assert_cmpstr (g_bytes_get_data (bytes, &size), ==, "hello world!");
+
+ g_bytes_unref (bytes);
+ g_object_unref (o);
+}
+
int
main (int argc,
char *argv[])
g_test_add_func ("/memory-output-stream/seek", test_seek);
g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
g_test_add_func ("/memory-output-stream/properties", test_properties);
+ g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);
return g_test_run();
}