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
37 * @see_also: #GMemoryInputStream
39 * #GMemoryOutputStream is a class for using arbitrary
40 * memory chunks as output for GIO streaming output operations.
44 struct _GMemoryOutputStreamPrivate {
60 static void g_memory_output_stream_finalize (GObject *object);
62 static void g_memory_output_stream_set_property (GObject *object,
67 static void g_memory_output_stream_get_property (GObject *object,
72 static gssize g_memory_output_stream_write (GOutputStream *stream,
75 GCancellable *cancellable,
78 static gboolean g_memory_output_stream_close (GOutputStream *stream,
79 GCancellable *cancellable,
82 static void g_memory_output_stream_write_async (GOutputStream *stream,
86 GCancellable *cancellable,
87 GAsyncReadyCallback callback,
89 static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
92 static void g_memory_output_stream_close_async (GOutputStream *stream,
94 GCancellable *cancellable,
95 GAsyncReadyCallback callback,
97 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
101 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
102 static goffset g_memory_output_stream_tell (GSeekable *seekable);
103 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
104 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
107 GCancellable *cancellable,
109 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
110 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
112 GCancellable *cancellable,
115 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
116 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
117 g_memory_output_stream_seekable_iface_init))
121 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
123 GOutputStreamClass *ostream_class;
124 GObjectClass *gobject_class;
126 g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
128 gobject_class = G_OBJECT_CLASS (klass);
129 gobject_class->finalize = g_memory_output_stream_finalize;
130 gobject_class->get_property = g_memory_output_stream_get_property;
131 gobject_class->set_property = g_memory_output_stream_set_property;
133 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
135 ostream_class->write_fn = g_memory_output_stream_write;
136 ostream_class->close_fn = g_memory_output_stream_close;
137 ostream_class->write_async = g_memory_output_stream_write_async;
138 ostream_class->write_finish = g_memory_output_stream_write_finish;
139 ostream_class->close_async = g_memory_output_stream_close_async;
140 ostream_class->close_finish = g_memory_output_stream_close_finish;
142 g_object_class_install_property (gobject_class,
144 g_param_spec_pointer ("data",
145 P_("Data byte array"),
146 P_("The byte array used as internal storage."),
147 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
148 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
150 g_object_class_install_property (gobject_class,
152 g_param_spec_boolean ("free-array",
153 P_("Free array data"),
154 P_("Wether or not the interal array should be free on close."),
157 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
158 g_object_class_install_property (gobject_class,
160 g_param_spec_uint ("size-limit",
162 P_("Maximum amount of bytes that can be written to the stream."),
167 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
173 g_memory_output_stream_finalize (GObject *object)
175 GMemoryOutputStream *stream;
177 stream = G_MEMORY_OUTPUT_STREAM (object);
179 if (stream->priv->free_data)
180 g_byte_array_free (stream->priv->data, TRUE);
182 if (G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize)
183 (*G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize) (object);
187 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
189 iface->tell = g_memory_output_stream_tell;
190 iface->can_seek = g_memory_output_stream_can_seek;
191 iface->seek = g_memory_output_stream_seek;
192 iface->can_truncate = g_memory_output_stream_can_truncate;
193 iface->truncate_fn = g_memory_output_stream_truncate;
198 g_memory_output_stream_init (GMemoryOutputStream *stream)
200 GMemoryOutputStreamPrivate *priv;
202 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
203 G_TYPE_MEMORY_OUTPUT_STREAM,
204 GMemoryOutputStreamPrivate);
211 * g_memory_output_stream_new:
212 * @data: a #GByteArray.
214 * Creates a new #GMemoryOutputStream. If @data is non-%NULL it will use
215 * that for its internal storage otherwise it will create a new #GByteArray.
216 * In both cases the internal #GByteArray can later be accessed through the
217 * "data" property, or with g_memory_output_stream_get_data().
219 * Note: The new stream will not take ownership of the supplied
220 * @data so you have to free it yourself after use or explicitly
221 * ask for it be freed on close by setting the "free-array"
224 * Return value: A newly created #GMemoryOutputStream object.
227 g_memory_output_stream_new (GByteArray *data)
229 GOutputStream *stream;
232 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM, NULL);
234 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
242 * g_memory_output_stream_set_free_data:
243 * @ostream: a #GMemoryOutputStream.
244 * @free_data: a #gboolean. If %TRUE, frees the data within @stream.
246 * Sets if the data within the @stream should be freed when the stream
250 g_memory_output_stream_set_free_data (GMemoryOutputStream *ostream,
253 GMemoryOutputStreamPrivate *priv;
255 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
257 priv = ostream->priv;
259 priv->free_data = free_data;
263 * g_memory_output_stream_set_max_size:
264 * @ostream: a #GMemoryOutputStream.
265 * @max_size: a #guint to set as the maximum stream size.
267 * Sets a size limit on the data contained within the output stream.
270 g_memory_output_stream_set_max_size (GMemoryOutputStream *ostream,
273 GMemoryOutputStreamPrivate *priv;
275 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
277 priv = ostream->priv;
279 priv->max_size = max_size;
281 if (priv->max_size > 0 &&
282 priv->max_size < priv->data->len)
285 g_byte_array_set_size (priv->data, priv->max_size);
287 if (priv->pos > priv->max_size)
288 priv->pos = priv->max_size;
291 g_object_notify (G_OBJECT (ostream), "size-limit");
295 g_memory_output_stream_set_property (GObject *object,
300 GMemoryOutputStream *ostream;
301 GMemoryOutputStreamPrivate *priv;
305 ostream = G_MEMORY_OUTPUT_STREAM (object);
306 priv = ostream->priv;
312 if (priv->data && priv->free_data)
313 g_byte_array_free (priv->data, TRUE);
315 data = g_value_get_pointer (value);
319 data = g_byte_array_new ();
320 priv->free_data = TRUE;
323 priv->free_data = FALSE;
327 g_object_notify (G_OBJECT (ostream), "data");
330 case PROP_FREE_ARRAY:
331 priv->free_data = g_value_get_boolean (value);
334 case PROP_SIZE_LIMIT:
335 max_size = g_value_get_uint (value);
336 g_memory_output_stream_set_max_size (ostream, max_size);
340 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 g_memory_output_stream_get_property (GObject *object,
351 GMemoryOutputStream *ostream;
352 GMemoryOutputStreamPrivate *priv;
354 ostream = G_MEMORY_OUTPUT_STREAM (object);
355 priv = ostream->priv;
360 g_value_set_pointer (value, priv->data);
363 case PROP_FREE_ARRAY:
364 g_value_set_boolean (value, priv->free_data);
367 case PROP_SIZE_LIMIT:
368 g_value_set_uint (value, priv->max_size);
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
378 * g_memory_output_stream_get_data:
379 * @ostream: a #GMemoryOutputStream
381 * Gets any loaded data from the @ostream.
383 * Returns: #GByteArray of the stream's data.
386 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
388 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
390 return ostream->priv->data;
395 array_check_boundary (GMemoryOutputStream *stream,
399 GMemoryOutputStreamPrivate *priv;
406 if (priv->max_size < size || size > G_MAXUINT)
411 "Reached maximum data array limit");
420 array_resize (GMemoryOutputStream *stream,
424 GMemoryOutputStreamPrivate *priv;
429 if (! array_check_boundary (stream, size, error))
433 if (priv->data->len == size)
434 return priv->data->len - priv->pos;
437 old_len = priv->data->len;
438 g_byte_array_set_size (priv->data, size);
440 if (size > old_len && priv->pos > old_len)
441 memset (priv->data->data + priv->pos, 0, size - old_len);
443 return priv->data->len - priv->pos;
447 g_memory_output_stream_write (GOutputStream *stream,
450 GCancellable *cancellable,
453 GMemoryOutputStream *ostream;
454 GMemoryOutputStreamPrivate *priv;
459 ostream = G_MEMORY_OUTPUT_STREAM (stream);
460 priv = ostream->priv;
462 /* count < 0 is ensured by GOutputStream */
464 n = MIN (count, priv->data->len - priv->pos);
468 new_size = priv->pos + count;
470 if (priv->max_size > 0)
471 new_size = MIN (new_size, priv->max_size);
473 n = array_resize (ostream, new_size, error);
480 "Reached maximum data array limit");
487 dest = priv->data->data + priv->pos;
488 memcpy (dest, buffer, n);
495 g_memory_output_stream_close (GOutputStream *stream,
496 GCancellable *cancellable,
499 GMemoryOutputStream *ostream;
500 GMemoryOutputStreamPrivate *priv;
502 ostream = G_MEMORY_OUTPUT_STREAM (stream);
503 priv = ostream->priv;
509 g_memory_output_stream_write_async (GOutputStream *stream,
513 GCancellable *cancellable,
514 GAsyncReadyCallback callback,
517 GSimpleAsyncResult *simple;
520 nwritten = g_memory_output_stream_write (stream,
527 simple = g_simple_async_result_new (G_OBJECT (stream),
530 g_memory_output_stream_write_async);
532 g_simple_async_result_set_op_res_gssize (simple, nwritten);
533 g_simple_async_result_complete_in_idle (simple);
534 g_object_unref (simple);
538 g_memory_output_stream_write_finish (GOutputStream *stream,
539 GAsyncResult *result,
542 GSimpleAsyncResult *simple;
545 simple = G_SIMPLE_ASYNC_RESULT (result);
547 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
548 g_memory_output_stream_write_async);
550 nwritten = g_simple_async_result_get_op_res_gssize (simple);
555 g_memory_output_stream_close_async (GOutputStream *stream,
557 GCancellable *cancellable,
558 GAsyncReadyCallback callback,
561 GSimpleAsyncResult *simple;
563 simple = g_simple_async_result_new (G_OBJECT (stream),
566 g_memory_output_stream_close_async);
569 /* will always return TRUE */
570 g_memory_output_stream_close (stream, cancellable, NULL);
572 g_simple_async_result_complete_in_idle (simple);
573 g_object_unref (simple);
577 g_memory_output_stream_close_finish (GOutputStream *stream,
578 GAsyncResult *result,
581 GSimpleAsyncResult *simple;
583 simple = G_SIMPLE_ASYNC_RESULT (result);
585 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
586 g_memory_output_stream_close_async);
592 g_memory_output_stream_tell (GSeekable *seekable)
594 GMemoryOutputStream *stream;
595 GMemoryOutputStreamPrivate *priv;
597 stream = G_MEMORY_OUTPUT_STREAM (seekable);
604 g_memory_output_stream_can_seek (GSeekable *seekable)
610 g_memory_output_stream_seek (GSeekable *seekable,
613 GCancellable *cancellable,
616 GMemoryOutputStream *stream;
617 GMemoryOutputStreamPrivate *priv;
620 stream = G_MEMORY_OUTPUT_STREAM (seekable);
626 absolute = priv->pos + offset;
634 absolute = priv->data->len + offset;
640 G_IO_ERROR_INVALID_ARGUMENT,
641 "Invalid GSeekType supplied");
650 G_IO_ERROR_INVALID_ARGUMENT,
651 "Invalid seek request");
655 if (!array_check_boundary (stream, absolute, error))
658 priv->pos = absolute;
664 g_memory_output_stream_can_truncate (GSeekable *seekable)
670 g_memory_output_stream_truncate (GSeekable *seekable,
672 GCancellable *cancellable,
675 GMemoryOutputStream *ostream;
676 GMemoryOutputStreamPrivate *priv;
678 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
679 priv = ostream->priv;
681 if (array_resize (ostream, offset, error) < 0)
687 #define __G_MEMORY_OUTPUT_STREAM_C__
688 #include "gioaliasdef.c"