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.
21 * Christian Kellner <gicmo@gnome.org>
22 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
26 #include "gmemoryoutputstream.h"
27 #include "goutputstream.h"
28 #include "gseekable.h"
29 #include "gsimpleasyncresult.h"
36 * SECTION:gmemoryoutputstream
37 * @short_description: Streaming output operations on memory chunks
39 * @see_also: #GMemoryInputStream
41 * #GMemoryOutputStream is a class for using arbitrary
42 * memory chunks as output for GIO streaming output operations.
46 #define MIN_ARRAY_SIZE 16
53 PROP_REALLOC_FUNCTION,
57 struct _GMemoryOutputStreamPrivate {
59 gpointer data; /* Write buffer */
60 gsize len; /* Current length of the data buffer. Can change with resizing. */
61 gsize valid_len; /* The part of data that has been written to */
62 gsize pos; /* Current position in the stream. Distinct from valid_len,
63 because the stream is seekable. */
65 GReallocFunc realloc_fn;
66 GDestroyNotify destroy;
69 static void g_memory_output_stream_set_property (GObject *object,
73 static void g_memory_output_stream_get_property (GObject *object,
77 static void g_memory_output_stream_finalize (GObject *object);
79 static gssize g_memory_output_stream_write (GOutputStream *stream,
82 GCancellable *cancellable,
85 static gboolean g_memory_output_stream_close (GOutputStream *stream,
86 GCancellable *cancellable,
89 static void g_memory_output_stream_write_async (GOutputStream *stream,
93 GCancellable *cancellable,
94 GAsyncReadyCallback callback,
96 static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
99 static void g_memory_output_stream_close_async (GOutputStream *stream,
101 GCancellable *cancellable,
102 GAsyncReadyCallback callback,
104 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
105 GAsyncResult *result,
108 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
109 static goffset g_memory_output_stream_tell (GSeekable *seekable);
110 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
111 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
114 GCancellable *cancellable,
116 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
117 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
119 GCancellable *cancellable,
122 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
123 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
124 g_memory_output_stream_seekable_iface_init))
128 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
130 GOutputStreamClass *ostream_class;
131 GObjectClass *gobject_class;
133 g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
135 gobject_class = G_OBJECT_CLASS (klass);
136 gobject_class->set_property = g_memory_output_stream_set_property;
137 gobject_class->get_property = g_memory_output_stream_get_property;
138 gobject_class->finalize = g_memory_output_stream_finalize;
140 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
142 ostream_class->write_fn = g_memory_output_stream_write;
143 ostream_class->close_fn = g_memory_output_stream_close;
144 ostream_class->write_async = g_memory_output_stream_write_async;
145 ostream_class->write_finish = g_memory_output_stream_write_finish;
146 ostream_class->close_async = g_memory_output_stream_close_async;
147 ostream_class->close_finish = g_memory_output_stream_close_finish;
150 * GMemoryOutputStream:data:
152 * Pointer to buffer where data will be written.
156 g_object_class_install_property (gobject_class,
158 g_param_spec_pointer ("data",
160 P_("Pointer to buffer where data will be written."),
161 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
162 G_PARAM_STATIC_STRINGS));
165 * GMemoryOutputStream:size:
167 * Current size of the data buffer.
171 g_object_class_install_property (gobject_class,
173 g_param_spec_ulong ("size",
174 P_("Data Buffer Size"),
175 P_("Current size of the data buffer."),
177 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
178 G_PARAM_STATIC_STRINGS));
181 * GMemoryOutputStream:data-size:
183 * Size of data written to the buffer.
187 g_object_class_install_property (gobject_class,
189 g_param_spec_ulong ("data-size",
191 P_("Size of data written to the buffer."),
194 G_PARAM_STATIC_STRINGS));
197 * GMemoryOutputStream:realloc-function: (skip)
199 * Function with realloc semantics called to enlarge the buffer.
203 g_object_class_install_property (gobject_class,
204 PROP_REALLOC_FUNCTION,
205 g_param_spec_pointer ("realloc-function",
206 P_("Memory Reallocation Function"),
207 P_("Function with realloc semantics called to enlarge the buffer."),
208 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
209 G_PARAM_STATIC_STRINGS));
212 * GMemoryOutputStream:destroy-function: (skip)
214 * Function called with the buffer as argument when the stream is destroyed.
218 g_object_class_install_property (gobject_class,
219 PROP_DESTROY_FUNCTION,
220 g_param_spec_pointer ("destroy-function",
221 P_("Destroy Notification Function"),
222 P_("Function called with the buffer as argument when the stream is destroyed."),
223 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
224 G_PARAM_STATIC_STRINGS));
228 g_memory_output_stream_set_property (GObject *object,
233 GMemoryOutputStream *stream;
234 GMemoryOutputStreamPrivate *priv;
236 stream = G_MEMORY_OUTPUT_STREAM (object);
242 priv->data = g_value_get_pointer (value);
245 priv->len = g_value_get_ulong (value);
247 case PROP_REALLOC_FUNCTION:
248 priv->realloc_fn = g_value_get_pointer (value);
250 case PROP_DESTROY_FUNCTION:
251 priv->destroy = g_value_get_pointer (value);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 g_memory_output_stream_get_property (GObject *object,
265 GMemoryOutputStream *stream;
266 GMemoryOutputStreamPrivate *priv;
268 stream = G_MEMORY_OUTPUT_STREAM (object);
274 g_value_set_pointer (value, priv->data);
277 g_value_set_ulong (value, priv->len);
280 g_value_set_ulong (value, priv->valid_len);
282 case PROP_REALLOC_FUNCTION:
283 g_value_set_pointer (value, priv->realloc_fn);
285 case PROP_DESTROY_FUNCTION:
286 g_value_set_pointer (value, priv->destroy);
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 g_memory_output_stream_finalize (GObject *object)
297 GMemoryOutputStream *stream;
298 GMemoryOutputStreamPrivate *priv;
300 stream = G_MEMORY_OUTPUT_STREAM (object);
304 priv->destroy (priv->data);
306 G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
310 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
312 iface->tell = g_memory_output_stream_tell;
313 iface->can_seek = g_memory_output_stream_can_seek;
314 iface->seek = g_memory_output_stream_seek;
315 iface->can_truncate = g_memory_output_stream_can_truncate;
316 iface->truncate_fn = g_memory_output_stream_truncate;
321 g_memory_output_stream_init (GMemoryOutputStream *stream)
323 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
324 G_TYPE_MEMORY_OUTPUT_STREAM,
325 GMemoryOutputStreamPrivate);
326 stream->priv->pos = 0;
327 stream->priv->valid_len = 0;
331 * g_memory_output_stream_new: (skip)
332 * @data: pointer to a chunk of memory to use, or %NULL
333 * @size: the size of @data
334 * @realloc_function: a function with realloc() semantics (like g_realloc())
335 * to be called when @data needs to be grown, or %NULL
336 * @destroy_function: a function to be called on @data when the stream is
337 * finalized, or %NULL
339 * Creates a new #GMemoryOutputStream.
341 * If @data is non-%NULL, the stream will use that for its internal storage.
342 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
343 * storage when necessary. To construct a fixed-size output stream,
344 * pass %NULL as @realloc_fn.
347 * /* a stream that can grow */
348 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
350 * /* another stream that can grow */
351 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
353 * /* a fixed-size stream */
354 * data = malloc (200);
355 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
358 * Return value: A newly created #GMemoryOutputStream object.
361 g_memory_output_stream_new (gpointer data,
363 GReallocFunc realloc_function,
364 GDestroyNotify destroy_function)
366 GOutputStream *stream;
368 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
371 "realloc-function", realloc_function,
372 "destroy-function", destroy_function,
379 * g_memory_output_stream_get_data:
380 * @ostream: a #GMemoryOutputStream
382 * Gets any loaded data from the @ostream.
384 * Note that the returned pointer may become invalid on the next
385 * write or truncate operation on the stream.
387 * Returns: (transfer none): pointer to the stream's data
390 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
392 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
394 return ostream->priv->data;
398 * g_memory_output_stream_get_size:
399 * @ostream: a #GMemoryOutputStream
401 * Gets the size of the currently allocated data area (available from
402 * g_memory_output_stream_get_data()). If the stream isn't
403 * growable (no realloc was passed to g_memory_output_stream_new()) then
404 * this is the maximum size of the stream and further writes
405 * will return %G_IO_ERROR_NO_SPACE.
407 * Note that for growable streams the returned size may become invalid on
408 * the next write or truncate operation on the stream.
410 * If you want the number of bytes currently written to the stream, use
411 * g_memory_output_stream_get_data_size().
413 * Returns: the number of bytes allocated for the data buffer
416 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
418 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
420 return ostream->priv->len;
424 * g_memory_output_stream_get_data_size:
425 * @ostream: a #GMemoryOutputStream
427 * Returns the number of bytes from the start up
428 * to including the last byte written in the stream
429 * that has not been truncated away.
431 * Returns: the number of bytes written to the stream
436 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
438 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
440 return ostream->priv->valid_len;
444 * g_memory_output_stream_steal_data:
445 * @ostream: a #GMemoryOutputStream
447 * Gets any loaded data from the @ostream. Ownership of the data
448 * is transferred to the caller; when no longer needed it must be
449 * freed using the free function set in @ostream's
450 * #GMemoryOutputStream:destroy-function property.
452 * @ostream must be closed before calling this function.
454 * Returns: (transfer full): the stream's data
459 g_memory_output_stream_steal_data (GMemoryOutputStream *ostream)
463 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
464 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
466 data = ostream->priv->data;
467 ostream->priv->data = NULL;
473 array_resize (GMemoryOutputStream *ostream,
475 gboolean allow_partial,
478 GMemoryOutputStreamPrivate *priv;
482 priv = ostream->priv;
484 if (priv->len == size)
487 if (!priv->realloc_fn)
490 priv->pos < priv->len)
491 return TRUE; /* Short write */
493 g_set_error_literal (error,
496 _("Memory output stream not resizable"));
501 data = priv->realloc_fn (priv->data, size);
503 if (size > 0 && !data)
506 priv->pos < priv->len)
507 return TRUE; /* Short write */
509 g_set_error_literal (error,
512 _("Failed to resize memory output stream"));
517 memset ((guint8 *)data + len, 0, size - len);
522 if (priv->len < priv->valid_len)
523 priv->valid_len = priv->len;
529 g_nearest_pow (gint num)
540 g_memory_output_stream_write (GOutputStream *stream,
543 GCancellable *cancellable,
546 GMemoryOutputStream *ostream;
547 GMemoryOutputStreamPrivate *priv;
551 ostream = G_MEMORY_OUTPUT_STREAM (stream);
552 priv = ostream->priv;
557 /* Check for address space overflow, but only if the buffer is resizable.
558 Otherwise we just do a short write and don't worry. */
559 if (priv->realloc_fn && priv->pos + count < priv->pos)
562 if (priv->pos + count > priv->len)
564 /* At least enought to fit the write, rounded up
565 for greater than linear growth.
566 TODO: This wastes a lot of memory at large stream sizes.
567 Figure out a more rational allocation strategy. */
568 new_size = g_nearest_pow (priv->pos + count);
569 /* Check for overflow again. We have only checked if
570 pos + count > G_MAXSIZE, but it only catches the case of writing
571 more than 4GiB total on a 32-bit system. There's still the problem
572 of g_nearest_pow overflowing above 0x7fffffff, so we're
573 effectively limited to 2GiB. */
574 if (new_size < priv->len)
577 new_size = MAX (new_size, MIN_ARRAY_SIZE);
578 if (!array_resize (ostream, new_size, TRUE, error))
582 /* Make sure we handle short writes if the array_resize
583 only added part of the required memory */
584 count = MIN (count, priv->len - priv->pos);
586 dest = (guint8 *)priv->data + priv->pos;
587 memcpy (dest, buffer, count);
590 if (priv->pos > priv->valid_len)
591 priv->valid_len = priv->pos;
596 /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
597 g_set_error_literal (error,
600 _("Amount of memory required to process the write is "
601 "larger than available address space"));
606 g_memory_output_stream_close (GOutputStream *stream,
607 GCancellable *cancellable,
614 g_memory_output_stream_write_async (GOutputStream *stream,
618 GCancellable *cancellable,
619 GAsyncReadyCallback callback,
622 GSimpleAsyncResult *simple;
625 nwritten = g_memory_output_stream_write (stream,
631 simple = g_simple_async_result_new (G_OBJECT (stream),
634 g_memory_output_stream_write_async);
636 g_simple_async_result_set_op_res_gssize (simple, nwritten);
637 g_simple_async_result_complete_in_idle (simple);
638 g_object_unref (simple);
642 g_memory_output_stream_write_finish (GOutputStream *stream,
643 GAsyncResult *result,
646 GSimpleAsyncResult *simple;
649 simple = G_SIMPLE_ASYNC_RESULT (result);
651 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
652 g_memory_output_stream_write_async);
654 nwritten = g_simple_async_result_get_op_res_gssize (simple);
660 g_memory_output_stream_close_async (GOutputStream *stream,
662 GCancellable *cancellable,
663 GAsyncReadyCallback callback,
666 GSimpleAsyncResult *simple;
668 simple = g_simple_async_result_new (G_OBJECT (stream),
671 g_memory_output_stream_close_async);
674 /* will always return TRUE */
675 g_memory_output_stream_close (stream, cancellable, NULL);
677 g_simple_async_result_complete_in_idle (simple);
678 g_object_unref (simple);
682 g_memory_output_stream_close_finish (GOutputStream *stream,
683 GAsyncResult *result,
686 GSimpleAsyncResult *simple;
688 simple = G_SIMPLE_ASYNC_RESULT (result);
690 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
691 g_memory_output_stream_close_async);
697 g_memory_output_stream_tell (GSeekable *seekable)
699 GMemoryOutputStream *stream;
700 GMemoryOutputStreamPrivate *priv;
702 stream = G_MEMORY_OUTPUT_STREAM (seekable);
709 g_memory_output_stream_can_seek (GSeekable *seekable)
715 g_memory_output_stream_seek (GSeekable *seekable,
718 GCancellable *cancellable,
721 GMemoryOutputStream *stream;
722 GMemoryOutputStreamPrivate *priv;
725 stream = G_MEMORY_OUTPUT_STREAM (seekable);
731 absolute = priv->pos + offset;
739 absolute = priv->len + offset;
743 g_set_error_literal (error,
745 G_IO_ERROR_INVALID_ARGUMENT,
746 _("Invalid GSeekType supplied"));
753 g_set_error_literal (error,
755 G_IO_ERROR_INVALID_ARGUMENT,
756 _("Requested seek before the beginning of the stream"));
760 if (absolute > priv->len)
762 g_set_error_literal (error,
764 G_IO_ERROR_INVALID_ARGUMENT,
765 _("Requested seek beyond the end of the stream"));
769 priv->pos = absolute;
775 g_memory_output_stream_can_truncate (GSeekable *seekable)
777 GMemoryOutputStream *ostream;
778 GMemoryOutputStreamPrivate *priv;
780 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
781 priv = ostream->priv;
783 return priv->realloc_fn != NULL;
787 g_memory_output_stream_truncate (GSeekable *seekable,
789 GCancellable *cancellable,
792 GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
794 if (!array_resize (ostream, offset, FALSE, error))