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 = g_memory_output_stream_write;
135 ostream_class->close = 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 = 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
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:
247 g_memory_output_stream_set_free_data (GMemoryOutputStream *ostream,
250 GMemoryOutputStreamPrivate *priv;
252 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
254 priv = ostream->priv;
256 priv->free_data = free_data;
260 * g_memory_output_stream_set_max_size:
266 g_memory_output_stream_set_max_size (GMemoryOutputStream *ostream,
269 GMemoryOutputStreamPrivate *priv;
271 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
273 priv = ostream->priv;
275 priv->max_size = max_size;
277 if (priv->max_size > 0 &&
278 priv->max_size < priv->data->len)
281 g_byte_array_set_size (priv->data, priv->max_size);
283 if (priv->pos > priv->max_size)
284 priv->pos = priv->max_size;
287 g_object_notify (G_OBJECT (ostream), "size-limit");
291 g_memory_output_stream_set_property (GObject *object,
296 GMemoryOutputStream *ostream;
297 GMemoryOutputStreamPrivate *priv;
301 ostream = G_MEMORY_OUTPUT_STREAM (object);
302 priv = ostream->priv;
308 if (priv->data && priv->free_data)
309 g_byte_array_free (priv->data, TRUE);
311 data = g_value_get_pointer (value);
315 data = g_byte_array_new ();
316 priv->free_data = TRUE;
319 priv->free_data = FALSE;
323 g_object_notify (G_OBJECT (ostream), "data");
326 case PROP_FREE_ARRAY:
327 priv->free_data = g_value_get_boolean (value);
330 case PROP_SIZE_LIMIT:
331 max_size = g_value_get_uint (value);
332 g_memory_output_stream_set_max_size (ostream, max_size);
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 g_memory_output_stream_get_property (GObject *object,
347 GMemoryOutputStream *ostream;
348 GMemoryOutputStreamPrivate *priv;
350 ostream = G_MEMORY_OUTPUT_STREAM (object);
351 priv = ostream->priv;
356 g_value_set_pointer (value, priv->data);
359 case PROP_FREE_ARRAY:
360 g_value_set_boolean (value, priv->free_data);
363 case PROP_SIZE_LIMIT:
364 g_value_set_uint (value, priv->max_size);
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374 * g_memory_output_stream_get_data:
377 * Returns: #GByteArray of the stream's data.
380 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
382 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
384 return ostream->priv->data;
389 array_check_boundary (GMemoryOutputStream *stream,
393 GMemoryOutputStreamPrivate *priv;
400 if (priv->max_size < size || size > G_MAXUINT)
405 "Reached maximum data array limit");
414 array_resize (GMemoryOutputStream *stream,
418 GMemoryOutputStreamPrivate *priv;
423 if (! array_check_boundary (stream, size, error))
427 if (priv->data->len == size)
428 return priv->data->len - priv->pos;
431 old_len = priv->data->len;
432 g_byte_array_set_size (priv->data, size);
434 if (size > old_len && priv->pos > old_len)
435 memset (priv->data->data + priv->pos, 0, size - old_len);
437 return priv->data->len - priv->pos;
441 g_memory_output_stream_write (GOutputStream *stream,
444 GCancellable *cancellable,
447 GMemoryOutputStream *ostream;
448 GMemoryOutputStreamPrivate *priv;
453 ostream = G_MEMORY_OUTPUT_STREAM (stream);
454 priv = ostream->priv;
456 /* count < 0 is ensured by GOutputStream */
458 n = MIN (count, priv->data->len - priv->pos);
462 new_size = priv->pos + count;
464 if (priv->max_size > 0)
465 new_size = MIN (new_size, priv->max_size);
467 n = array_resize (ostream, new_size, error);
474 "Reached maximum data array limit");
481 dest = priv->data->data + priv->pos;
482 memcpy (dest, buffer, n);
489 g_memory_output_stream_close (GOutputStream *stream,
490 GCancellable *cancellable,
493 GMemoryOutputStream *ostream;
494 GMemoryOutputStreamPrivate *priv;
496 ostream = G_MEMORY_OUTPUT_STREAM (stream);
497 priv = ostream->priv;
503 g_memory_output_stream_write_async (GOutputStream *stream,
507 GCancellable *cancellable,
508 GAsyncReadyCallback callback,
511 GSimpleAsyncResult *simple;
514 nwritten = g_memory_output_stream_write (stream,
521 simple = g_simple_async_result_new (G_OBJECT (stream),
524 g_memory_output_stream_write_async);
526 g_simple_async_result_set_op_res_gssize (simple, nwritten);
527 g_simple_async_result_complete_in_idle (simple);
528 g_object_unref (simple);
532 g_memory_output_stream_write_finish (GOutputStream *stream,
533 GAsyncResult *result,
536 GSimpleAsyncResult *simple;
539 simple = G_SIMPLE_ASYNC_RESULT (result);
541 g_assert (g_simple_async_result_get_source_tag (simple) ==
542 g_memory_output_stream_write_async);
544 nwritten = g_simple_async_result_get_op_res_gssize (simple);
549 g_memory_output_stream_close_async (GOutputStream *stream,
551 GCancellable *cancellable,
552 GAsyncReadyCallback callback,
555 GSimpleAsyncResult *simple;
557 simple = g_simple_async_result_new (G_OBJECT (stream),
560 g_memory_output_stream_close_async);
563 /* will always return TRUE */
564 g_memory_output_stream_close (stream, cancellable, NULL);
566 g_simple_async_result_complete_in_idle (simple);
567 g_object_unref (simple);
571 g_memory_output_stream_close_finish (GOutputStream *stream,
572 GAsyncResult *result,
575 GSimpleAsyncResult *simple;
577 simple = G_SIMPLE_ASYNC_RESULT (result);
579 g_assert (g_simple_async_result_get_source_tag (simple) ==
580 g_memory_output_stream_close_async);
586 g_memory_output_stream_tell (GSeekable *seekable)
588 GMemoryOutputStream *stream;
589 GMemoryOutputStreamPrivate *priv;
591 stream = G_MEMORY_OUTPUT_STREAM (seekable);
598 g_memory_output_stream_can_seek (GSeekable *seekable)
604 g_memory_output_stream_seek (GSeekable *seekable,
607 GCancellable *cancellable,
610 GMemoryOutputStream *stream;
611 GMemoryOutputStreamPrivate *priv;
614 stream = G_MEMORY_OUTPUT_STREAM (seekable);
620 absolute = priv->pos + offset;
628 absolute = priv->data->len + offset;
634 G_IO_ERROR_INVALID_ARGUMENT,
635 "Invalid GSeekType supplied");
644 G_IO_ERROR_INVALID_ARGUMENT,
645 "Invalid seek request");
649 if (!array_check_boundary (stream, absolute, error))
652 priv->pos = absolute;
658 g_memory_output_stream_can_truncate (GSeekable *seekable)
664 g_memory_output_stream_truncate (GSeekable *seekable,
666 GCancellable *cancellable,
669 GMemoryOutputStream *ostream;
670 GMemoryOutputStreamPrivate *priv;
672 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
673 priv = ostream->priv;
675 if (array_resize (ostream, offset, error) < 0)
681 #define __G_MEMORY_OUTPUT_STREAM_C__
682 #include "gioaliasdef.c"