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 "gpollableoutputstream.h"
29 #include "gseekable.h"
37 * SECTION:gmemoryoutputstream
38 * @short_description: Streaming output operations on memory chunks
40 * @see_also: #GMemoryInputStream
42 * #GMemoryOutputStream is a class for using arbitrary
43 * memory chunks as output for GIO streaming output operations.
45 * As of GLib 2.34, #GMemoryOutputStream implements
46 * #GPollableOutputStream.
49 #define MIN_ARRAY_SIZE 16
56 PROP_REALLOC_FUNCTION
,
60 struct _GMemoryOutputStreamPrivate
{
62 gpointer data
; /* Write buffer */
63 gsize len
; /* Current length of the data buffer. Can change with resizing. */
64 gsize valid_len
; /* The part of data that has been written to */
65 gsize pos
; /* Current position in the stream. Distinct from valid_len,
66 because the stream is seekable. */
68 GReallocFunc realloc_fn
;
69 GDestroyNotify destroy
;
72 static void g_memory_output_stream_set_property (GObject
*object
,
76 static void g_memory_output_stream_get_property (GObject
*object
,
80 static void g_memory_output_stream_finalize (GObject
*object
);
82 static gssize
g_memory_output_stream_write (GOutputStream
*stream
,
85 GCancellable
*cancellable
,
88 static gboolean
g_memory_output_stream_close (GOutputStream
*stream
,
89 GCancellable
*cancellable
,
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 static gboolean
g_memory_output_stream_is_writable (GPollableOutputStream
*stream
);
116 static GSource
*g_memory_output_stream_create_source (GPollableOutputStream
*stream
,
117 GCancellable
*cancellable
);
119 static void g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface
*iface
);
121 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream
, g_memory_output_stream
, G_TYPE_OUTPUT_STREAM
,
122 G_ADD_PRIVATE (GMemoryOutputStream
)
123 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE
,
124 g_memory_output_stream_seekable_iface_init
);
125 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM
,
126 g_memory_output_stream_pollable_iface_init
))
130 g_memory_output_stream_class_init (GMemoryOutputStreamClass
*klass
)
132 GOutputStreamClass
*ostream_class
;
133 GObjectClass
*gobject_class
;
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
->close_async
= g_memory_output_stream_close_async
;
145 ostream_class
->close_finish
= g_memory_output_stream_close_finish
;
148 * GMemoryOutputStream:data:
150 * Pointer to buffer where data will be written.
154 g_object_class_install_property (gobject_class
,
156 g_param_spec_pointer ("data",
158 P_("Pointer to buffer where data will be written."),
159 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
160 G_PARAM_STATIC_STRINGS
));
163 * GMemoryOutputStream:size:
165 * Current size of the data buffer.
169 g_object_class_install_property (gobject_class
,
171 g_param_spec_ulong ("size",
172 P_("Data Buffer Size"),
173 P_("Current size of the data buffer."),
175 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
176 G_PARAM_STATIC_STRINGS
));
179 * GMemoryOutputStream:data-size:
181 * Size of data written to the buffer.
185 g_object_class_install_property (gobject_class
,
187 g_param_spec_ulong ("data-size",
189 P_("Size of data written to the buffer."),
192 G_PARAM_STATIC_STRINGS
));
195 * GMemoryOutputStream:realloc-function: (skip)
197 * Function with realloc semantics called to enlarge the buffer.
201 g_object_class_install_property (gobject_class
,
202 PROP_REALLOC_FUNCTION
,
203 g_param_spec_pointer ("realloc-function",
204 P_("Memory Reallocation Function"),
205 P_("Function with realloc semantics called to enlarge the buffer."),
206 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
207 G_PARAM_STATIC_STRINGS
));
210 * GMemoryOutputStream:destroy-function: (skip)
212 * Function called with the buffer as argument when the stream is destroyed.
216 g_object_class_install_property (gobject_class
,
217 PROP_DESTROY_FUNCTION
,
218 g_param_spec_pointer ("destroy-function",
219 P_("Destroy Notification Function"),
220 P_("Function called with the buffer as argument when the stream is destroyed."),
221 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
222 G_PARAM_STATIC_STRINGS
));
226 g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface
*iface
)
228 iface
->is_writable
= g_memory_output_stream_is_writable
;
229 iface
->create_source
= g_memory_output_stream_create_source
;
233 g_memory_output_stream_set_property (GObject
*object
,
238 GMemoryOutputStream
*stream
;
239 GMemoryOutputStreamPrivate
*priv
;
241 stream
= G_MEMORY_OUTPUT_STREAM (object
);
247 priv
->data
= g_value_get_pointer (value
);
250 priv
->len
= g_value_get_ulong (value
);
252 case PROP_REALLOC_FUNCTION
:
253 priv
->realloc_fn
= g_value_get_pointer (value
);
255 case PROP_DESTROY_FUNCTION
:
256 priv
->destroy
= g_value_get_pointer (value
);
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
265 g_memory_output_stream_get_property (GObject
*object
,
270 GMemoryOutputStream
*stream
;
271 GMemoryOutputStreamPrivate
*priv
;
273 stream
= G_MEMORY_OUTPUT_STREAM (object
);
279 g_value_set_pointer (value
, priv
->data
);
282 g_value_set_ulong (value
, priv
->len
);
285 g_value_set_ulong (value
, priv
->valid_len
);
287 case PROP_REALLOC_FUNCTION
:
288 g_value_set_pointer (value
, priv
->realloc_fn
);
290 case PROP_DESTROY_FUNCTION
:
291 g_value_set_pointer (value
, priv
->destroy
);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
300 g_memory_output_stream_finalize (GObject
*object
)
302 GMemoryOutputStream
*stream
;
303 GMemoryOutputStreamPrivate
*priv
;
305 stream
= G_MEMORY_OUTPUT_STREAM (object
);
309 priv
->destroy (priv
->data
);
311 G_OBJECT_CLASS (g_memory_output_stream_parent_class
)->finalize (object
);
315 g_memory_output_stream_seekable_iface_init (GSeekableIface
*iface
)
317 iface
->tell
= g_memory_output_stream_tell
;
318 iface
->can_seek
= g_memory_output_stream_can_seek
;
319 iface
->seek
= g_memory_output_stream_seek
;
320 iface
->can_truncate
= g_memory_output_stream_can_truncate
;
321 iface
->truncate_fn
= g_memory_output_stream_truncate
;
326 g_memory_output_stream_init (GMemoryOutputStream
*stream
)
328 stream
->priv
= g_memory_output_stream_get_instance_private (stream
);
329 stream
->priv
->pos
= 0;
330 stream
->priv
->valid_len
= 0;
334 * g_memory_output_stream_new: (skip)
335 * @data: (allow-none): pointer to a chunk of memory to use, or %NULL
336 * @size: the size of @data
337 * @realloc_function: (allow-none): a function with realloc() semantics (like g_realloc())
338 * to be called when @data needs to be grown, or %NULL
339 * @destroy_function: (allow-none): a function to be called on @data when the stream is
340 * finalized, or %NULL
342 * Creates a new #GMemoryOutputStream.
344 * If @data is non-%NULL, the stream will use that for its internal storage.
345 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
346 * storage when necessary. To construct a fixed-size output stream,
347 * pass %NULL as @realloc_fn.
350 * /* a stream that can grow */
351 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
353 * /* another stream that can grow */
354 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
356 * /* a fixed-size stream */
357 * data = malloc (200);
358 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
361 * Return value: A newly created #GMemoryOutputStream object.
364 g_memory_output_stream_new (gpointer data
,
366 GReallocFunc realloc_function
,
367 GDestroyNotify destroy_function
)
369 GOutputStream
*stream
;
371 stream
= g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM
,
374 "realloc-function", realloc_function
,
375 "destroy-function", destroy_function
,
382 * g_memory_output_stream_new_resizable:
384 * Creates a new #GMemoryOutputStream, using g_realloc() and g_free()
385 * for memory allocation.
390 g_memory_output_stream_new_resizable (void)
392 return g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
396 * g_memory_output_stream_get_data:
397 * @ostream: a #GMemoryOutputStream
399 * Gets any loaded data from the @ostream.
401 * Note that the returned pointer may become invalid on the next
402 * write or truncate operation on the stream.
404 * Returns: (transfer none): pointer to the stream's data
407 g_memory_output_stream_get_data (GMemoryOutputStream
*ostream
)
409 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), NULL
);
411 return ostream
->priv
->data
;
415 * g_memory_output_stream_get_size:
416 * @ostream: a #GMemoryOutputStream
418 * Gets the size of the currently allocated data area (available from
419 * g_memory_output_stream_get_data()). If the stream isn't
420 * growable (no realloc was passed to g_memory_output_stream_new()) then
421 * this is the maximum size of the stream and further writes
422 * will return %G_IO_ERROR_NO_SPACE.
424 * Note that for growable streams the returned size may become invalid on
425 * the next write or truncate operation on the stream.
427 * If you want the number of bytes currently written to the stream, use
428 * g_memory_output_stream_get_data_size().
430 * Returns: the number of bytes allocated for the data buffer
433 g_memory_output_stream_get_size (GMemoryOutputStream
*ostream
)
435 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), 0);
437 return ostream
->priv
->len
;
441 * g_memory_output_stream_get_data_size:
442 * @ostream: a #GMemoryOutputStream
444 * Returns the number of bytes from the start up
445 * to including the last byte written in the stream
446 * that has not been truncated away.
448 * Returns: the number of bytes written to the stream
453 g_memory_output_stream_get_data_size (GMemoryOutputStream
*ostream
)
455 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), 0);
457 return ostream
->priv
->valid_len
;
461 * g_memory_output_stream_steal_data:
462 * @ostream: a #GMemoryOutputStream
464 * Gets any loaded data from the @ostream. Ownership of the data
465 * is transferred to the caller; when no longer needed it must be
466 * freed using the free function set in @ostream's
467 * #GMemoryOutputStream:destroy-function property.
469 * @ostream must be closed before calling this function.
471 * Returns: (transfer full): the stream's data
476 g_memory_output_stream_steal_data (GMemoryOutputStream
*ostream
)
480 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), NULL
);
481 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream
)), NULL
);
483 data
= ostream
->priv
->data
;
484 ostream
->priv
->data
= NULL
;
490 * g_memory_output_stream_steal_as_bytes:
491 * @ostream: a #GMemoryOutputStream
493 * Returns data from the @ostream as a #GBytes. @ostream must be
494 * closed before calling this function.
496 * Returns: (transfer full): the stream's data
501 g_memory_output_stream_steal_as_bytes (GMemoryOutputStream
*ostream
)
505 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), NULL
);
506 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream
)), NULL
);
508 result
= g_bytes_new_with_free_func (ostream
->priv
->data
,
509 ostream
->priv
->valid_len
,
510 ostream
->priv
->destroy
,
511 ostream
->priv
->data
);
512 ostream
->priv
->data
= NULL
;
518 array_resize (GMemoryOutputStream
*ostream
,
520 gboolean allow_partial
,
523 GMemoryOutputStreamPrivate
*priv
;
527 priv
= ostream
->priv
;
529 if (priv
->len
== size
)
532 if (!priv
->realloc_fn
)
535 priv
->pos
< priv
->len
)
536 return TRUE
; /* Short write */
538 g_set_error_literal (error
,
541 _("Memory output stream not resizable"));
546 data
= priv
->realloc_fn (priv
->data
, size
);
548 if (size
> 0 && !data
)
551 priv
->pos
< priv
->len
)
552 return TRUE
; /* Short write */
554 g_set_error_literal (error
,
557 _("Failed to resize memory output stream"));
562 memset ((guint8
*)data
+ len
, 0, size
- len
);
567 if (priv
->len
< priv
->valid_len
)
568 priv
->valid_len
= priv
->len
;
574 g_nearest_pow (gint num
)
585 g_memory_output_stream_write (GOutputStream
*stream
,
588 GCancellable
*cancellable
,
591 GMemoryOutputStream
*ostream
;
592 GMemoryOutputStreamPrivate
*priv
;
596 ostream
= G_MEMORY_OUTPUT_STREAM (stream
);
597 priv
= ostream
->priv
;
602 /* Check for address space overflow, but only if the buffer is resizable.
603 Otherwise we just do a short write and don't worry. */
604 if (priv
->realloc_fn
&& priv
->pos
+ count
< priv
->pos
)
607 if (priv
->pos
+ count
> priv
->len
)
609 /* At least enought to fit the write, rounded up
610 for greater than linear growth.
611 TODO: This wastes a lot of memory at large stream sizes.
612 Figure out a more rational allocation strategy. */
613 new_size
= g_nearest_pow (priv
->pos
+ count
);
614 /* Check for overflow again. We have only checked if
615 pos + count > G_MAXSIZE, but it only catches the case of writing
616 more than 4GiB total on a 32-bit system. There's still the problem
617 of g_nearest_pow overflowing above 0x7fffffff, so we're
618 effectively limited to 2GiB. */
619 if (new_size
< priv
->len
)
622 new_size
= MAX (new_size
, MIN_ARRAY_SIZE
);
623 if (!array_resize (ostream
, new_size
, TRUE
, error
))
627 /* Make sure we handle short writes if the array_resize
628 only added part of the required memory */
629 count
= MIN (count
, priv
->len
- priv
->pos
);
631 dest
= (guint8
*)priv
->data
+ priv
->pos
;
632 memcpy (dest
, buffer
, count
);
635 if (priv
->pos
> priv
->valid_len
)
636 priv
->valid_len
= priv
->pos
;
641 /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
642 g_set_error_literal (error
,
645 _("Amount of memory required to process the write is "
646 "larger than available address space"));
651 g_memory_output_stream_close (GOutputStream
*stream
,
652 GCancellable
*cancellable
,
659 g_memory_output_stream_close_async (GOutputStream
*stream
,
661 GCancellable
*cancellable
,
662 GAsyncReadyCallback callback
,
667 task
= g_task_new (stream
, cancellable
, callback
, data
);
669 /* will always return TRUE */
670 g_memory_output_stream_close (stream
, cancellable
, NULL
);
672 g_task_return_boolean (task
, TRUE
);
673 g_object_unref (task
);
677 g_memory_output_stream_close_finish (GOutputStream
*stream
,
678 GAsyncResult
*result
,
681 g_return_val_if_fail (g_task_is_valid (result
, stream
), FALSE
);
683 return g_task_propagate_boolean (G_TASK (result
), error
);
687 g_memory_output_stream_tell (GSeekable
*seekable
)
689 GMemoryOutputStream
*stream
;
690 GMemoryOutputStreamPrivate
*priv
;
692 stream
= G_MEMORY_OUTPUT_STREAM (seekable
);
699 g_memory_output_stream_can_seek (GSeekable
*seekable
)
705 g_memory_output_stream_seek (GSeekable
*seekable
,
708 GCancellable
*cancellable
,
711 GMemoryOutputStream
*stream
;
712 GMemoryOutputStreamPrivate
*priv
;
715 stream
= G_MEMORY_OUTPUT_STREAM (seekable
);
721 absolute
= priv
->pos
+ offset
;
729 /* For resizable streams, we consider the end to be the data
730 * length. For fixed-sized streams, we consider the end to be the
731 * size of the buffer.
733 if (priv
->realloc_fn
)
734 absolute
= priv
->valid_len
+ offset
;
736 absolute
= priv
->len
+ offset
;
740 g_set_error_literal (error
,
742 G_IO_ERROR_INVALID_ARGUMENT
,
743 _("Invalid GSeekType supplied"));
750 g_set_error_literal (error
,
752 G_IO_ERROR_INVALID_ARGUMENT
,
753 _("Requested seek before the beginning of the stream"));
757 /* Can't seek past the end of a fixed-size stream.
759 * Note: seeking to the non-existent byte at the end of a fixed-sized
760 * stream is valid (eg: a 1-byte fixed sized stream can have position
761 * 0 or 1). Therefore '>' is what we want.
763 if (priv
->realloc_fn
== NULL
&& absolute
> priv
->len
)
765 g_set_error_literal (error
,
767 G_IO_ERROR_INVALID_ARGUMENT
,
768 _("Requested seek beyond the end of the stream"));
772 priv
->pos
= absolute
;
778 g_memory_output_stream_can_truncate (GSeekable
*seekable
)
780 GMemoryOutputStream
*ostream
;
781 GMemoryOutputStreamPrivate
*priv
;
783 ostream
= G_MEMORY_OUTPUT_STREAM (seekable
);
784 priv
= ostream
->priv
;
786 return priv
->realloc_fn
!= NULL
;
790 g_memory_output_stream_truncate (GSeekable
*seekable
,
792 GCancellable
*cancellable
,
795 GMemoryOutputStream
*ostream
= G_MEMORY_OUTPUT_STREAM (seekable
);
797 if (!array_resize (ostream
, offset
, FALSE
, error
))
804 g_memory_output_stream_is_writable (GPollableOutputStream
*stream
)
810 g_memory_output_stream_create_source (GPollableOutputStream
*stream
,
811 GCancellable
*cancellable
)
813 GSource
*base_source
, *pollable_source
;
815 base_source
= g_timeout_source_new (0);
816 pollable_source
= g_pollable_source_new_full (stream
, base_source
,
818 g_source_unref (base_source
);
820 return pollable_source
;