GMemoryOutputStream: Add API to return data as a GBytes
authorColin Walters <walters@verbum.org>
Fri, 18 May 2012 14:39:05 +0000 (10:39 -0400)
committerColin Walters <walters@verbum.org>
Mon, 21 May 2012 17:45:15 +0000 (13:45 -0400)
Matches the corresponding additions to GMemoryInputStream.

https://bugzilla.gnome.org/show_bug.cgi?id=672102

gio/gio.symbols
gio/gmemoryoutputstream.c
gio/gmemoryoutputstream.h
gio/tests/memory-output-stream.c

index 3dbbb5d..34c954f 100644 (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
index 5a62fbb..378bf3c 100644 (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,
index bcdc164..faf0364 100644 (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__ */
index 01baaf5..6f4b881 100644 (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();
 }