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,
242 * g_memory_output_stream_set_free_data:
248 g_memory_output_stream_set_free_data (GMemoryOutputStream *ostream,
251 GMemoryOutputStreamPrivate *priv;
253 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
255 priv = ostream->priv;
257 priv->free_data = free_data;
261 * g_memory_output_stream_set_max_size:
267 g_memory_output_stream_set_max_size (GMemoryOutputStream *ostream,
270 GMemoryOutputStreamPrivate *priv;
272 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
274 priv = ostream->priv;
276 priv->max_size = max_size;
278 if (priv->max_size > 0 &&
279 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;
288 g_object_notify (G_OBJECT (ostream), "size-limit");
292 g_memory_output_stream_set_property (GObject *object,
297 GMemoryOutputStream *ostream;
298 GMemoryOutputStreamPrivate *priv;
302 ostream = G_MEMORY_OUTPUT_STREAM (object);
303 priv = ostream->priv;
309 if (priv->data && priv->free_data) {
310 g_byte_array_free (priv->data, TRUE);
313 data = g_value_get_pointer (value);
316 data = g_byte_array_new ();
317 priv->free_data = TRUE;
319 priv->free_data = FALSE;
324 g_object_notify (G_OBJECT (ostream), "data");
327 case PROP_FREE_ARRAY:
328 priv->free_data = g_value_get_boolean (value);
331 case PROP_SIZE_LIMIT:
332 max_size = g_value_get_uint (value);
333 g_memory_output_stream_set_max_size (ostream, max_size);
337 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343 g_memory_output_stream_get_property (GObject *object,
348 GMemoryOutputStream *ostream;
349 GMemoryOutputStreamPrivate *priv;
351 ostream = G_MEMORY_OUTPUT_STREAM (object);
352 priv = ostream->priv;
357 g_value_set_pointer (value, priv->data);
360 case PROP_FREE_ARRAY:
361 g_value_set_boolean (value, priv->free_data);
364 case PROP_SIZE_LIMIT:
365 g_value_set_uint (value, priv->max_size);
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
375 * g_memory_output_stream_get_data:
378 * Returns: #GByteArray of the stream's data.
381 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
383 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
385 return ostream->priv->data;
390 array_check_boundary (GMemoryOutputStream *stream,
394 GMemoryOutputStreamPrivate *priv;
398 if (! priv->max_size) {
402 if (priv->max_size < size || size > G_MAXUINT) {
407 "Reached maximum data array limit");
416 array_resize (GMemoryOutputStream *stream,
420 GMemoryOutputStreamPrivate *priv;
425 if (! array_check_boundary (stream, size, error))
429 if (priv->data->len == size)
430 return priv->data->len - priv->pos;
433 old_len = priv->data->len;
434 g_byte_array_set_size (priv->data, size);
436 if (size > old_len && priv->pos > old_len)
437 memset (priv->data->data + priv->pos, 0, size - old_len);
440 return priv->data->len - priv->pos;
444 g_memory_output_stream_write (GOutputStream *stream,
447 GCancellable *cancellable,
450 GMemoryOutputStream *ostream;
451 GMemoryOutputStreamPrivate *priv;
456 ostream = G_MEMORY_OUTPUT_STREAM (stream);
457 priv = ostream->priv;
459 /* count < 0 is ensured by GOutputStream */
461 n = MIN (count, priv->data->len - priv->pos);
465 new_size = priv->pos + count;
467 if (priv->max_size > 0)
469 new_size = MIN (new_size, priv->max_size);
472 n = array_resize (ostream, new_size, error);
479 "Reached maximum data array limit");
488 dest = priv->data->data + priv->pos;
489 memcpy (dest, buffer, n);
496 g_memory_output_stream_close (GOutputStream *stream,
497 GCancellable *cancellable,
500 GMemoryOutputStream *ostream;
501 GMemoryOutputStreamPrivate *priv;
503 ostream = G_MEMORY_OUTPUT_STREAM (stream);
504 priv = ostream->priv;
510 g_memory_output_stream_write_async (GOutputStream *stream,
514 GCancellable *cancellable,
515 GAsyncReadyCallback callback,
518 GSimpleAsyncResult *simple;
521 nwritten = g_memory_output_stream_write (stream,
528 simple = g_simple_async_result_new (G_OBJECT (stream),
531 g_memory_output_stream_write_async);
533 g_simple_async_result_set_op_res_gssize (simple, nwritten);
534 g_simple_async_result_complete_in_idle (simple);
535 g_object_unref (simple);
540 g_memory_output_stream_write_finish (GOutputStream *stream,
541 GAsyncResult *result,
544 GSimpleAsyncResult *simple;
547 simple = G_SIMPLE_ASYNC_RESULT (result);
549 g_assert (g_simple_async_result_get_source_tag (simple) ==
550 g_memory_output_stream_write_async);
552 nwritten = g_simple_async_result_get_op_res_gssize (simple);
557 g_memory_output_stream_close_async (GOutputStream *stream,
559 GCancellable *cancellable,
560 GAsyncReadyCallback callback,
563 GSimpleAsyncResult *simple;
565 simple = g_simple_async_result_new (G_OBJECT (stream),
568 g_memory_output_stream_close_async);
571 /* will always return TRUE */
572 g_memory_output_stream_close (stream, cancellable, NULL);
574 g_simple_async_result_complete_in_idle (simple);
575 g_object_unref (simple);
580 g_memory_output_stream_close_finish (GOutputStream *stream,
581 GAsyncResult *result,
584 GSimpleAsyncResult *simple;
586 simple = G_SIMPLE_ASYNC_RESULT (result);
588 g_assert (g_simple_async_result_get_source_tag (simple) ==
589 g_memory_output_stream_close_async);
596 g_memory_output_stream_tell (GSeekable *seekable)
598 GMemoryOutputStream *stream;
599 GMemoryOutputStreamPrivate *priv;
601 stream = G_MEMORY_OUTPUT_STREAM (seekable);
608 g_memory_output_stream_can_seek (GSeekable *seekable)
614 g_memory_output_stream_seek (GSeekable *seekable,
617 GCancellable *cancellable,
620 GMemoryOutputStream *stream;
621 GMemoryOutputStreamPrivate *priv;
624 stream = G_MEMORY_OUTPUT_STREAM (seekable);
630 absolute = priv->pos + offset;
638 absolute = priv->data->len + offset;
644 G_IO_ERROR_INVALID_ARGUMENT,
645 "Invalid GSeekType supplied");
653 G_IO_ERROR_INVALID_ARGUMENT,
654 "Invalid seek request");
658 if (! array_check_boundary (stream, absolute, error))
661 priv->pos = absolute;
667 g_memory_output_stream_can_truncate (GSeekable *seekable)
673 g_memory_output_stream_truncate (GSeekable *seekable,
675 GCancellable *cancellable,
678 GMemoryOutputStream *ostream;
679 GMemoryOutputStreamPrivate *priv;
681 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
682 priv = ostream->priv;
684 if (array_resize (ostream, offset, error) < 0)
690 #define __G_MEMORY_OUTPUT_STREAM_C__
691 #include "gioaliasdef.c"
693 /* vim: ts=2 sw=2 et */