gio: add GBytes-based input/output stream methods

Using a caller-supplied buffer for g_input_stream_read() doesn't
translate well to the semantics of many other languages, and using a
non-refcounted buffer for read_async() and write_async() makes it
impossible to manage the memory correctly currently in
garbage-collected languages.

Fix both of these issues by adding a new set of methods that work with
GBytes objects rather than plain buffers.

https://bugzilla.gnome.org/show_bug.cgi?id=671139
This commit is contained in:
Dan Winship
2012-04-05 09:19:17 -04:00
committed by Jasper St. Pierre
parent 4b456635e4
commit 800d6ff111
8 changed files with 428 additions and 0 deletions

View File

@@ -149,6 +149,32 @@ test_properties (void)
g_object_unref (mo);
}
static void
test_write_bytes (void)
{
GOutputStream *mo;
GBytes *bytes, *bytes2;
GError *error = NULL;
mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
"realloc-function", g_realloc,
"destroy-function", g_free,
NULL);
bytes = g_bytes_new_static ("hello world!", strlen ("hello world!") + 1);
g_output_stream_write_bytes (mo, bytes, NULL, &error);
g_assert_no_error (error);
g_output_stream_close (mo, NULL, &error);
g_assert_no_error (error);
bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
g_object_unref (mo);
g_assert (g_bytes_equal (bytes, bytes2));
g_bytes_unref (bytes);
g_bytes_unref (bytes2);
}
static void
test_steal_as_bytes (void)
{
@@ -198,6 +224,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/write-bytes", test_write_bytes);
g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);
return g_test_run();