GMemoryOutputStream: Add API to return data as a GBytes

Matches the corresponding additions to GMemoryInputStream.

https://bugzilla.gnome.org/show_bug.cgi?id=672102
This commit is contained in:
Colin Walters 2012-05-18 10:39:05 -04:00
parent 1bedf24879
commit 44d4990442
4 changed files with 70 additions and 0 deletions

View File

@ -557,6 +557,7 @@ g_memory_output_stream_get_data
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

View File

@ -475,6 +475,34 @@ g_memory_output_stream_steal_data (GMemoryOutputStream *ostream)
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,

View File

@ -93,6 +93,9 @@ gsize g_memory_output_stream_get_size (GMemoryOutputStream *ostrea
gsize g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream);
gpointer g_memory_output_stream_steal_data (GMemoryOutputStream *ostream);
GLIB_AVAILABLE_IN_2_34
GBytes * g_memory_output_stream_steal_as_bytes (GMemoryOutputStream *ostream);
G_END_DECLS
#endif /* __G_MEMORY_OUTPUT_STREAM_H__ */

View File

@ -149,6 +149,43 @@ test_properties (void)
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[])
@ -161,6 +198,7 @@ main (int argc,
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();
}