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"
32 * SECTION:gmemoryoutputstream
33 * @short_description: streaming output operations on memory chunks
34 * @see_also: #GMemoryInputStream
36 * #GMemoryOutputStream is a class for using arbitrary
37 * memory chunks as output for GIO streaming output operations.
41 struct _GMemoryOutputStreamPrivate {
57 static void g_memory_output_stream_finalize (GObject *object);
59 static void g_memory_output_stream_set_property (GObject *object,
64 static void g_memory_output_stream_get_property (GObject *object,
69 static gssize g_memory_output_stream_write (GOutputStream *stream,
72 GCancellable *cancellable,
75 static gboolean g_memory_output_stream_close (GOutputStream *stream,
76 GCancellable *cancellable,
79 static void g_memory_output_stream_write_async (GOutputStream *stream,
83 GCancellable *cancellable,
84 GAsyncReadyCallback callback,
86 static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
89 static void g_memory_output_stream_close_async (GOutputStream *stream,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
94 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
98 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
99 static goffset g_memory_output_stream_tell (GSeekable *seekable);
100 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
101 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
104 GCancellable *cancellable,
106 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
107 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
109 GCancellable *cancellable,
112 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
113 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
114 g_memory_output_stream_seekable_iface_init))
118 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
120 GOutputStreamClass *ostream_class;
121 GObjectClass *gobject_class;
123 g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
125 gobject_class = G_OBJECT_CLASS (klass);
126 gobject_class->finalize = g_memory_output_stream_finalize;
127 gobject_class->get_property = g_memory_output_stream_get_property;
128 gobject_class->set_property = g_memory_output_stream_set_property;
130 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
132 ostream_class->write = g_memory_output_stream_write;
133 ostream_class->close = g_memory_output_stream_close;
134 ostream_class->write_async = g_memory_output_stream_write_async;
135 ostream_class->write_finish = g_memory_output_stream_write_finish;
136 ostream_class->close_async = g_memory_output_stream_close_async;
137 ostream_class->close_finish = g_memory_output_stream_close_finish;
139 g_object_class_install_property (gobject_class,
141 g_param_spec_pointer ("data",
142 P_("Data byte array"),
143 P_("The byte array used as internal storage."),
144 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
145 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
147 g_object_class_install_property (gobject_class,
149 g_param_spec_boolean ("free-array",
150 P_("Free array data"),
151 P_("Wether or not the interal array should be free on close."),
154 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
155 g_object_class_install_property (gobject_class,
157 g_param_spec_uint ("size-limit",
159 P_("Maximum amount of bytes that can be written to the stream."),
164 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
170 g_memory_output_stream_finalize (GObject *object)
172 GMemoryOutputStream *stream;
174 stream = G_MEMORY_OUTPUT_STREAM (object);
176 if (stream->priv->free_data)
177 g_byte_array_free (stream->priv->data, TRUE);
179 if (G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize)
180 (*G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize) (object);
184 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
186 iface->tell = g_memory_output_stream_tell;
187 iface->can_seek = g_memory_output_stream_can_seek;
188 iface->seek = g_memory_output_stream_seek;
189 iface->can_truncate = g_memory_output_stream_can_truncate;
190 iface->truncate = g_memory_output_stream_truncate;
195 g_memory_output_stream_init (GMemoryOutputStream *stream)
197 GMemoryOutputStreamPrivate *priv;
199 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
200 G_TYPE_MEMORY_OUTPUT_STREAM,
201 GMemoryOutputStreamPrivate);
208 * g_memory_output_stream_new:
209 * @data: a #GByteArray.
211 * Creates a new #GMemoryOutputStream. If @data is non-%NULL it will use
212 * that for its internal storage otherwise it will create a new #GByteArray.
213 * In both cases the internal #GByteArray can later be accessed through the
216 * Note: The new stream will not take ownership of the supplied
217 * @data so you have to free it yourself after use or explicitly
218 * ask for it be freed on close by setting the "free-array"
221 * Return value: A newly created #GMemoryOutputStream object.
224 g_memory_output_stream_new (GByteArray *data)
226 GOutputStream *stream;
229 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM, NULL);
231 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
240 * g_memory_output_stream_set_free_data:
246 g_memory_output_stream_set_free_data (GMemoryOutputStream *ostream,
249 GMemoryOutputStreamPrivate *priv;
251 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
253 priv = ostream->priv;
255 priv->free_data = free_data;
259 * g_memory_output_stream_set_max_size:
265 g_memory_output_stream_set_max_size (GMemoryOutputStream *ostream,
268 GMemoryOutputStreamPrivate *priv;
270 g_return_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream));
272 priv = ostream->priv;
274 priv->max_size = max_size;
276 if (priv->max_size > 0 &&
277 priv->max_size < priv->data->len) {
279 g_byte_array_set_size (priv->data, priv->max_size);
281 if (priv->pos > priv->max_size) {
282 priv->pos = priv->max_size;
286 g_object_notify (G_OBJECT (ostream), "size-limit");
290 g_memory_output_stream_set_property (GObject *object,
295 GMemoryOutputStream *ostream;
296 GMemoryOutputStreamPrivate *priv;
300 ostream = G_MEMORY_OUTPUT_STREAM (object);
301 priv = ostream->priv;
307 if (priv->data && priv->free_data) {
308 g_byte_array_free (priv->data, TRUE);
311 data = g_value_get_pointer (value);
314 data = g_byte_array_new ();
315 priv->free_data = TRUE;
317 priv->free_data = FALSE;
322 g_object_notify (G_OBJECT (ostream), "data");
325 case PROP_FREE_ARRAY:
326 priv->free_data = g_value_get_boolean (value);
329 case PROP_SIZE_LIMIT:
330 max_size = g_value_get_uint (value);
331 g_memory_output_stream_set_max_size (ostream, max_size);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 g_memory_output_stream_get_property (GObject *object,
346 GMemoryOutputStream *ostream;
347 GMemoryOutputStreamPrivate *priv;
349 ostream = G_MEMORY_OUTPUT_STREAM (object);
350 priv = ostream->priv;
355 g_value_set_pointer (value, priv->data);
358 case PROP_FREE_ARRAY:
359 g_value_set_boolean (value, priv->free_data);
362 case PROP_SIZE_LIMIT:
363 g_value_set_uint (value, priv->max_size);
367 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
373 * g_memory_output_stream_get_data:
376 * Returns: #GByteArray of the stream's data.
379 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
381 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
383 return ostream->priv->data;
388 array_check_boundary (GMemoryOutputStream *stream,
392 GMemoryOutputStreamPrivate *priv;
396 if (! priv->max_size) {
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);
438 return priv->data->len - priv->pos;
442 g_memory_output_stream_write (GOutputStream *stream,
445 GCancellable *cancellable,
448 GMemoryOutputStream *ostream;
449 GMemoryOutputStreamPrivate *priv;
454 ostream = G_MEMORY_OUTPUT_STREAM (stream);
455 priv = ostream->priv;
457 /* count < 0 is ensured by GOutputStream */
459 n = MIN (count, priv->data->len - priv->pos);
463 new_size = priv->pos + count;
465 if (priv->max_size > 0)
467 new_size = MIN (new_size, priv->max_size);
470 n = array_resize (ostream, new_size, error);
477 "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);
538 g_memory_output_stream_write_finish (GOutputStream *stream,
539 GAsyncResult *result,
542 GSimpleAsyncResult *simple;
545 simple = G_SIMPLE_ASYNC_RESULT (result);
547 g_assert (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);
578 g_memory_output_stream_close_finish (GOutputStream *stream,
579 GAsyncResult *result,
582 GSimpleAsyncResult *simple;
584 simple = G_SIMPLE_ASYNC_RESULT (result);
586 g_assert (g_simple_async_result_get_source_tag (simple) ==
587 g_memory_output_stream_close_async);
594 g_memory_output_stream_tell (GSeekable *seekable)
596 GMemoryOutputStream *stream;
597 GMemoryOutputStreamPrivate *priv;
599 stream = G_MEMORY_OUTPUT_STREAM (seekable);
606 g_memory_output_stream_can_seek (GSeekable *seekable)
612 g_memory_output_stream_seek (GSeekable *seekable,
615 GCancellable *cancellable,
618 GMemoryOutputStream *stream;
619 GMemoryOutputStreamPrivate *priv;
622 stream = G_MEMORY_OUTPUT_STREAM (seekable);
628 absolute = priv->pos + offset;
636 absolute = priv->data->len + offset;
642 G_IO_ERROR_INVALID_ARGUMENT,
643 "Invalid GSeekType supplied");
651 G_IO_ERROR_INVALID_ARGUMENT,
652 "Invalid seek request");
656 if (! array_check_boundary (stream, absolute, error))
659 priv->pos = absolute;
665 g_memory_output_stream_can_truncate (GSeekable *seekable)
671 g_memory_output_stream_truncate (GSeekable *seekable,
673 GCancellable *cancellable,
676 GMemoryOutputStream *ostream;
677 GMemoryOutputStreamPrivate *priv;
679 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
680 priv = ostream->priv;
682 if (array_resize (ostream, offset, error) < 0)
688 /* vim: ts=2 sw=2 et */