[glib/wip/gsubprocess: 2/3] GMemoryOutputStream: Add API to return data as a GBytes



commit 40bcef95911899692145cb14533067208fec983d
Author: Colin Walters <walters verbum org>
Date:   Fri May 18 10:39:05 2012 -0400

    GMemoryOutputStream: Add API to return data as a GBytes
    
    Matches the corresponding additions to GMemoryInputStream.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=672102

 gio/gio.symbols                  |    1 +
 gio/gmemoryoutputstream.c        |   28 ++++++++++++++++++++++++++++
 gio/gmemoryoutputstream.h        |    3 +++
 gio/tests/memory-output-stream.c |   38 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 0 deletions(-)
---
diff --git a/gio/gio.symbols b/gio/gio.symbols
index 3dbbb5d..34c954f 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -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
diff --git a/gio/gmemoryoutputstream.c b/gio/gmemoryoutputstream.c
index 5a62fbb..378bf3c 100644
--- a/gio/gmemoryoutputstream.c
+++ b/gio/gmemoryoutputstream.c
@@ -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,
diff --git a/gio/gmemoryoutputstream.h b/gio/gmemoryoutputstream.h
index bcdc164..faf0364 100644
--- a/gio/gmemoryoutputstream.h
+++ b/gio/gmemoryoutputstream.h
@@ -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__ */
diff --git a/gio/tests/memory-output-stream.c b/gio/tests/memory-output-stream.c
index 01baaf5..6f4b881 100644
--- a/gio/tests/memory-output-stream.c
+++ b/gio/tests/memory-output-stream.c
@@ -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();
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]