1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Christian Kellner <gicmo@gnome.org>
24 #include "gmemoryoutputstream.h"
25 #include "goutputstream.h"
26 #include "gseekable.h"
27 #include "gsimpleasyncresult.h"
34 * SECTION:gmemoryoutputstream
35 * @short_description: Streaming output operations on memory chunks
36 * @see_also: #GMemoryInputStream
38 * #GMemoryOutputStream is a class for using arbitrary
39 * memory chunks as output for GIO streaming output operations.
43 struct _GMemoryOutputStreamPrivate {
59 static void g_memory_output_stream_finalize (GObject *object);
61 static void g_memory_output_stream_set_property (GObject *object,
66 static void g_memory_output_stream_get_property (GObject *object,
71 static gssize g_memory_output_stream_write (GOutputStream *stream,
74 GCancellable *cancellable,
77 static gboolean g_memory_output_stream_close (GOutputStream *stream,
78 GCancellable *cancellable,
81 static void g_memory_output_stream_write_async (GOutputStream *stream,
85 GCancellable *cancellable,
86 GAsyncReadyCallback callback,
88 static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
91 static void g_memory_output_stream_close_async (GOutputStream *stream,
93 GCancellable *cancellable,
94 GAsyncReadyCallback callback,
96 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
100 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
101 static goffset g_memory_output_stream_tell (GSeekable *seekable);
102 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
103 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
106 GCancellable *cancellable,
108 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
109 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
111 GCancellable *cancellable,
114 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
115 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
116 g_memory_output_stream_seekable_iface_init))
120 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
122 GOutputStreamClass *ostream_class;
123 GObjectClass *gobject_class;
125 g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
127 gobject_class = G_OBJECT_CLASS (klass);
128 gobject_class->finalize = g_memory_output_stream_finalize;
129 gobject_class->get_property = g_memory_output_stream_get_property;
130 gobject_class->set_property = g_memory_output_stream_set_property;
132 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
134 ostream_class->write_fn = g_memory_output_stream_write;
135 ostream_class->close_fn = g_memory_output_stream_close;
136 ostream_class->write_async = g_memory_output_stream_write_async;
137 ostream_class->write_finish = g_memory_output_stream_write_finish;
138 ostream_class->close_async = g_memory_output_stream_close_async;
139 ostream_class->close_finish = g_memory_output_stream_close_finish;
141 g_object_class_install_property (gobject_class,
143 g_param_spec_pointer ("data",
144 P_("Data byte array"),
145 P_("The byte array used as internal storage."),
146 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
147 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
149 g_object_class_install_property (gobject_class,
151 g_param_spec_boolean ("free-array",
152 P_("Free array data"),
153 P_("Wether or not the interal array should be free on close."),
156 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
157 g_object_class_install_property (gobject_class,
159 g_param_spec_uint ("size-limit",
161 P_("Maximum amount of bytes that can be written to the stream."),
166 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
172 g_memory_output_stream_finalize (GObject *object)
174 GMemoryOutputStream *stream;
176 stream = G_MEMORY_OUTPUT_STREAM (object);
178 if (stream->priv->free_data)
179 g_byte_array_free (stream->priv->data, TRUE);
181 if (G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize)
182 (*G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize) (object);
186 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
188 iface->tell = g_memory_output_stream_tell;
189 iface->can_seek = g_memory_output_stream_can_seek;
190 iface->seek = g_memory_output_stream_seek;
191 iface->can_truncate = g_memory_output_stream_can_truncate;
192 iface->truncate_fn = g_memory_output_stream_truncate;
197 g_memory_output_stream_init (GMemoryOutputStream *stream)
199 GMemoryOutputStreamPrivate *priv;
201 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
202 G_TYPE_MEMORY_OUTPUT_STREAM,
203 GMemoryOutputStreamPrivate);
210 * g_memory_output_stream_new:
211 * @data: a #GByteArray.
213 * Creates a new #GMemoryOutputStream. If @data is non-%NULL it will use
214 * that for its internal storage otherwise it will create a new #GByteArray.
215 * In both cases the internal #GByteArray can later be accessed through the
216 * "data" property, or with g_memory_output_stream_get_data().
218 * Note: The new stream will not take ownership of the supplied
219 * @data so you have to free it yourself after use or explicitly
220 * ask for it be freed on close by setting the "free-array"
223 * Return value: A newly created #GMemoryOutputStream object.
226 g_memory_output_stream_new (GByteArray *data)
228 GOutputStream *stream;
231 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM, NULL);
233 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
241 * g_memory_output_stream_set_free_data:
242 * @ostream: a #GMemoryOutputStream.
243 * @free_data: a #gboolean. If %TRUE, frees the data within @stream.
245 * Sets if the data within the @stream should be freed when the stream
249 g_memory_output_stream_set_free_data (GMemoryOutputStream *ostream,
252 GMemoryOutputStreamPrivate *priv;
254 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
256 priv = ostream->priv;
258 priv->free_data = free_data;
262 * g_memory_output_stream_set_max_size:
263 * @ostream: a #GMemoryOutputStream.
264 * @max_size: a #guint to set as the maximum stream size.
266 * Sets a size limit on the data contained within the output stream.
269 g_memory_output_stream_set_max_size (GMemoryOutputStream *ostream,
272 GMemoryOutputStreamPrivate *priv;
274 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
276 priv = ostream->priv;
278 priv->max_size = max_size;
280 if (priv->max_size > 0 &&
281 priv->max_size < priv->data->len)
284 g_byte_array_set_size (priv->data, priv->max_size);
286 if (priv->pos > priv->max_size)
287 priv->pos = priv->max_size;
290 g_object_notify (G_OBJECT (ostream), "size-limit");
294 g_memory_output_stream_set_property (GObject *object,
299 GMemoryOutputStream *ostream;
300 GMemoryOutputStreamPrivate *priv;
304 ostream = G_MEMORY_OUTPUT_STREAM (object);
305 priv = ostream->priv;
311 if (priv->data && priv->free_data)
312 g_byte_array_free (priv->data, TRUE);
314 data = g_value_get_pointer (value);
318 data = g_byte_array_new ();
319 priv->free_data = TRUE;
322 priv->free_data = FALSE;
326 g_object_notify (G_OBJECT (ostream), "data");
329 case PROP_FREE_ARRAY:
330 priv->free_data = g_value_get_boolean (value);
333 case PROP_SIZE_LIMIT:
334 max_size = g_value_get_uint (value);
335 g_memory_output_stream_set_max_size (ostream, max_size);
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 g_memory_output_stream_get_property (GObject *object,
350 GMemoryOutputStream *ostream;
351 GMemoryOutputStreamPrivate *priv;
353 ostream = G_MEMORY_OUTPUT_STREAM (object);
354 priv = ostream->priv;
359 g_value_set_pointer (value, priv->data);
362 case PROP_FREE_ARRAY:
363 g_value_set_boolean (value, priv->free_data);
366 case PROP_SIZE_LIMIT:
367 g_value_set_uint (value, priv->max_size);
371 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
377 * g_memory_output_stream_get_data:
378 * @ostream: a #GMemoryOutputStream
380 * Gets any loaded data from the @ostream.
382 * Returns: #GByteArray of the stream's data.
385 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
387 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
389 return ostream->priv->data;
394 array_check_boundary (GMemoryOutputStream *stream,
398 GMemoryOutputStreamPrivate *priv;
405 if (priv->max_size < size || size > G_MAXUINT)
410 "Reached maximum data array limit");
419 array_resize (GMemoryOutputStream *stream,
423 GMemoryOutputStreamPrivate *priv;
428 if (! array_check_boundary (stream, size, error))
432 if (priv->data->len == size)
433 return priv->data->len - priv->pos;
436 old_len = priv->data->len;
437 g_byte_array_set_size (priv->data, size);
439 if (size > old_len && priv->pos > old_len)
440 memset (priv->data->data + priv->pos, 0, size - old_len);
442 return priv->data->len - priv->pos;
446 g_memory_output_stream_write (GOutputStream *stream,
449 GCancellable *cancellable,
452 GMemoryOutputStream *ostream;
453 GMemoryOutputStreamPrivate *priv;
458 ostream = G_MEMORY_OUTPUT_STREAM (stream);
459 priv = ostream->priv;
461 /* count < 0 is ensured by GOutputStream */
463 n = MIN (count, priv->data->len - priv->pos);
467 new_size = priv->pos + count;
469 if (priv->max_size > 0)
470 new_size = MIN (new_size, priv->max_size);
472 n = array_resize (ostream, new_size, error);
479 "Reached maximum data array limit");
486 dest = priv->data->data + priv->pos;
487 memcpy (dest, buffer, n);
494 g_memory_output_stream_close (GOutputStream *stream,
495 GCancellable *cancellable,
498 GMemoryOutputStream *ostream;
499 GMemoryOutputStreamPrivate *priv;
501 ostream = G_MEMORY_OUTPUT_STREAM (stream);
502 priv = ostream->priv;
508 g_memory_output_stream_write_async (GOutputStream *stream,
512 GCancellable *cancellable,
513 GAsyncReadyCallback callback,
516 GSimpleAsyncResult *simple;
519 nwritten = g_memory_output_stream_write (stream,
526 simple = g_simple_async_result_new (G_OBJECT (stream),
529 g_memory_output_stream_write_async);
531 g_simple_async_result_set_op_res_gssize (simple, nwritten);
532 g_simple_async_result_complete_in_idle (simple);
533 g_object_unref (simple);
537 g_memory_output_stream_write_finish (GOutputStream *stream,
538 GAsyncResult *result,
541 GSimpleAsyncResult *simple;
544 simple = G_SIMPLE_ASYNC_RESULT (result);
546 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
547 g_memory_output_stream_write_async);
549 nwritten = g_simple_async_result_get_op_res_gssize (simple);
554 g_memory_output_stream_close_async (GOutputStream *stream,
556 GCancellable *cancellable,
557 GAsyncReadyCallback callback,
560 GSimpleAsyncResult *simple;
562 simple = g_simple_async_result_new (G_OBJECT (stream),
565 g_memory_output_stream_close_async);
568 /* will always return TRUE */
569 g_memory_output_stream_close (stream, cancellable, NULL);
571 g_simple_async_result_complete_in_idle (simple);
572 g_object_unref (simple);
576 g_memory_output_stream_close_finish (GOutputStream *stream,
577 GAsyncResult *result,
580 GSimpleAsyncResult *simple;
582 simple = G_SIMPLE_ASYNC_RESULT (result);
584 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
585 g_memory_output_stream_close_async);
591 g_memory_output_stream_tell (GSeekable *seekable)
593 GMemoryOutputStream *stream;
594 GMemoryOutputStreamPrivate *priv;
596 stream = G_MEMORY_OUTPUT_STREAM (seekable);
603 g_memory_output_stream_can_seek (GSeekable *seekable)
609 g_memory_output_stream_seek (GSeekable *seekable,
612 GCancellable *cancellable,
615 GMemoryOutputStream *stream;
616 GMemoryOutputStreamPrivate *priv;
619 stream = G_MEMORY_OUTPUT_STREAM (seekable);
625 absolute = priv->pos + offset;
633 absolute = priv->data->len + offset;
639 G_IO_ERROR_INVALID_ARGUMENT,
640 "Invalid GSeekType supplied");
649 G_IO_ERROR_INVALID_ARGUMENT,
650 "Invalid seek request");
654 if (!array_check_boundary (stream, absolute, error))
657 priv->pos = absolute;
663 g_memory_output_stream_can_truncate (GSeekable *seekable)
669 g_memory_output_stream_truncate (GSeekable *seekable,
671 GCancellable *cancellable,
674 GMemoryOutputStream *ostream;
675 GMemoryOutputStreamPrivate *priv;
677 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
678 priv = ostream->priv;
680 if (array_resize (ostream, offset, error) < 0)
686 #define __G_MEMORY_OUTPUT_STREAM_C__
687 #include "gioaliasdef.c"