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

@@ -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();
}