Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gmemoryoutputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors:
21  *   Christian Kellner <gicmo@gnome.org>
22  *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
23  */
24
25 #include "config.h"
26 #include "gmemoryoutputstream.h"
27 #include "goutputstream.h"
28 #include "gseekable.h"
29 #include "gsimpleasyncresult.h"
30 #include "gioerror.h"
31 #include "string.h"
32 #include "glibintl.h"
33
34 #include "gioalias.h"
35
36 /**
37  * SECTION:gmemoryoutputstream
38  * @short_description: Streaming output operations on memory chunks
39  * @include: gio/gio.h
40  * @see_also: #GMemoryInputStream
41  *
42  * #GMemoryOutputStream is a class for using arbitrary
43  * memory chunks as output for GIO streaming output operations.
44  *
45  */
46
47 #define MIN_ARRAY_SIZE  16
48
49 enum {
50   PROP_0,
51   PROP_DATA,
52   PROP_SIZE,
53   PROP_DATA_SIZE,
54   PROP_REALLOC_FUNCTION,
55   PROP_DESTROY_FUNCTION
56 };
57
58 struct _GMemoryOutputStreamPrivate {
59   
60   gpointer       data; /* Write buffer */
61   gsize          len; /* Current length of the data buffer. Can change with resizing. */
62   gsize          valid_len; /* The part of data that has been written to */
63   gsize          pos; /* Current position in the stream. Distinct from valid_len,
64                          because the stream is seekable. */
65
66   GReallocFunc   realloc_fn;
67   GDestroyNotify destroy;
68 };
69
70 static void     g_memory_output_stream_set_property (GObject      *object,
71                                                      guint         prop_id,
72                                                      const GValue *value,
73                                                      GParamSpec   *pspec);
74 static void     g_memory_output_stream_get_property (GObject      *object,
75                                                      guint         prop_id,
76                                                      GValue       *value,
77                                                      GParamSpec   *pspec);
78 static void     g_memory_output_stream_finalize     (GObject      *object);
79
80 static gssize   g_memory_output_stream_write       (GOutputStream *stream,
81                                                     const void    *buffer,
82                                                     gsize          count,
83                                                     GCancellable  *cancellable,
84                                                     GError       **error);
85
86 static gboolean g_memory_output_stream_close       (GOutputStream  *stream,
87                                                     GCancellable   *cancellable,
88                                                     GError        **error);
89
90 static void     g_memory_output_stream_write_async  (GOutputStream        *stream,
91                                                      const void           *buffer,
92                                                      gsize                 count,
93                                                      int                   io_priority,
94                                                      GCancellable         *cancellable,
95                                                      GAsyncReadyCallback   callback,
96                                                      gpointer              data);
97 static gssize   g_memory_output_stream_write_finish (GOutputStream        *stream,
98                                                      GAsyncResult         *result,
99                                                      GError              **error);
100 static void     g_memory_output_stream_close_async  (GOutputStream        *stream,
101                                                      int                   io_priority,
102                                                      GCancellable         *cancellable,
103                                                      GAsyncReadyCallback   callback,
104                                                      gpointer              data);
105 static gboolean g_memory_output_stream_close_finish (GOutputStream        *stream,
106                                                      GAsyncResult         *result,
107                                                      GError              **error);
108
109 static void     g_memory_output_stream_seekable_iface_init (GSeekableIface  *iface);
110 static goffset  g_memory_output_stream_tell                (GSeekable       *seekable);
111 static gboolean g_memory_output_stream_can_seek            (GSeekable       *seekable);
112 static gboolean g_memory_output_stream_seek                (GSeekable       *seekable,
113                                                            goffset          offset,
114                                                            GSeekType        type,
115                                                            GCancellable    *cancellable,
116                                                            GError         **error);
117 static gboolean g_memory_output_stream_can_truncate        (GSeekable       *seekable);
118 static gboolean g_memory_output_stream_truncate            (GSeekable       *seekable,
119                                                            goffset          offset,
120                                                            GCancellable    *cancellable,
121                                                            GError         **error);
122
123 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
124                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
125                                                 g_memory_output_stream_seekable_iface_init))
126
127
128 static void
129 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
130 {
131   GOutputStreamClass *ostream_class;
132   GObjectClass *gobject_class;
133
134   g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
135
136   gobject_class = G_OBJECT_CLASS (klass);
137   gobject_class->set_property = g_memory_output_stream_set_property;
138   gobject_class->get_property = g_memory_output_stream_get_property;
139   gobject_class->finalize     = g_memory_output_stream_finalize;
140
141   ostream_class = G_OUTPUT_STREAM_CLASS (klass);
142
143   ostream_class->write_fn = g_memory_output_stream_write;
144   ostream_class->close_fn = g_memory_output_stream_close;
145   ostream_class->write_async  = g_memory_output_stream_write_async;
146   ostream_class->write_finish = g_memory_output_stream_write_finish;
147   ostream_class->close_async  = g_memory_output_stream_close_async;
148   ostream_class->close_finish = g_memory_output_stream_close_finish;
149
150   /**
151    * GMemoryOutputStream:data:
152    *
153    * Pointer to buffer where data will be written.
154    *
155    * Since: 2.24
156    **/
157   g_object_class_install_property (gobject_class,
158                                    PROP_DATA,
159                                    g_param_spec_pointer ("data",
160                                                          P_("Data Buffer"),
161                                                          P_("Pointer to buffer where data will be written."),
162                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
163                                                          G_PARAM_STATIC_STRINGS));
164
165   /**
166    * GMemoryOutputStream:size:
167    *
168    * Current size of the data buffer.
169    *
170    * Since: 2.24
171    **/
172   g_object_class_install_property (gobject_class,
173                                    PROP_SIZE,
174                                    g_param_spec_ulong ("size",
175                                                        P_("Data Buffer Size"),
176                                                        P_("Current size of the data buffer."),
177                                                        0, G_MAXULONG, 0,
178                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
179                                                        G_PARAM_STATIC_STRINGS));
180
181   /**
182    * GMemoryOutputStream:data-size:
183    *
184    * Size of data written to the buffer.
185    *
186    * Since: 2.24
187    **/
188   g_object_class_install_property (gobject_class,
189                                    PROP_DATA_SIZE,
190                                    g_param_spec_ulong ("data-size",
191                                                        P_("Data Size"),
192                                                        P_("Size of data written to the buffer."),
193                                                        0, G_MAXULONG, 0,
194                                                        G_PARAM_READABLE |
195                                                        G_PARAM_STATIC_STRINGS));
196
197   /**
198    * GMemoryOutputStream:realloc-function:
199    *
200    * Function with realloc semantics called to enlarge the buffer.
201    *
202    * Since: 2.24
203    **/
204   g_object_class_install_property (gobject_class,
205                                    PROP_REALLOC_FUNCTION,
206                                    g_param_spec_pointer ("realloc-function",
207                                                          P_("Memory Reallocation Function"),
208                                                          P_("Function with realloc semantics called to enlarge the buffer."),
209                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
210                                                          G_PARAM_STATIC_STRINGS));
211
212   /**
213    * GMemoryOutputStream:destroy-function:
214    *
215    * Function called with the buffer as argument when the stream is destroyed.
216    *
217    * Since: 2.24
218    **/
219   g_object_class_install_property (gobject_class,
220                                    PROP_DESTROY_FUNCTION,
221                                    g_param_spec_pointer ("destroy-function",
222                                                          P_("Destroy Notification Function"),
223                                                          P_("Function called with the buffer as argument when the stream is destroyed."),
224                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
225                                                          G_PARAM_STATIC_STRINGS));
226 }
227
228 static void
229 g_memory_output_stream_set_property (GObject      *object,
230                                      guint         prop_id,
231                                      const GValue *value,
232                                      GParamSpec   *pspec)
233 {
234   GMemoryOutputStream        *stream;
235   GMemoryOutputStreamPrivate *priv;
236
237   stream = G_MEMORY_OUTPUT_STREAM (object);
238   priv = stream->priv;
239
240   switch (prop_id)
241     {
242     case PROP_DATA:
243       priv->data = g_value_get_pointer (value);
244       break;
245     case PROP_SIZE:
246       priv->len = g_value_get_ulong (value);
247       break;
248     case PROP_REALLOC_FUNCTION:
249       priv->realloc_fn = g_value_get_pointer (value);
250       break;
251     case PROP_DESTROY_FUNCTION:
252       priv->destroy = g_value_get_pointer (value);
253       break;
254     default:
255       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256       break;
257     }
258 }
259
260 static void
261 g_memory_output_stream_get_property (GObject      *object,
262                                      guint         prop_id,
263                                      GValue       *value,
264                                      GParamSpec   *pspec)
265 {
266   GMemoryOutputStream        *stream;
267   GMemoryOutputStreamPrivate *priv;
268
269   stream = G_MEMORY_OUTPUT_STREAM (object);
270   priv = stream->priv;
271
272   switch (prop_id)
273     {
274     case PROP_DATA:
275       g_value_set_pointer (value, priv->data);
276       break;
277     case PROP_SIZE:
278       g_value_set_ulong (value, priv->len);
279       break;
280     case PROP_DATA_SIZE:
281       g_value_set_ulong (value, priv->valid_len);
282       break;
283     case PROP_REALLOC_FUNCTION:
284       g_value_set_pointer (value, priv->realloc_fn);
285       break;
286     case PROP_DESTROY_FUNCTION:
287       g_value_set_pointer (value, priv->destroy);
288       break;
289     default:
290       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291       break;
292     }
293 }
294
295 static void
296 g_memory_output_stream_finalize (GObject *object)
297 {
298   GMemoryOutputStream        *stream;
299   GMemoryOutputStreamPrivate *priv;
300
301   stream = G_MEMORY_OUTPUT_STREAM (object);
302   priv = stream->priv;
303   
304   if (priv->destroy)
305     priv->destroy (priv->data);
306
307   G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
308 }
309
310 static void
311 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
312 {
313   iface->tell         = g_memory_output_stream_tell;
314   iface->can_seek     = g_memory_output_stream_can_seek;
315   iface->seek         = g_memory_output_stream_seek;
316   iface->can_truncate = g_memory_output_stream_can_truncate;
317   iface->truncate_fn  = g_memory_output_stream_truncate;
318 }
319
320
321 static void
322 g_memory_output_stream_init (GMemoryOutputStream *stream)
323 {
324   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
325                                               G_TYPE_MEMORY_OUTPUT_STREAM,
326                                               GMemoryOutputStreamPrivate);
327   stream->priv->pos = 0;
328   stream->priv->valid_len = 0;
329 }
330
331 /**
332  * g_memory_output_stream_new:
333  * @data: pointer to a chunk of memory to use, or %NULL
334  * @size: the size of @data
335  * @realloc_function: a function with realloc() semantics (like g_realloc())
336  *     to be called when @data needs to be grown, or %NULL
337  * @destroy_function: a function to be called on @data when the stream is
338  *     finalized, or %NULL
339  *
340  * Creates a new #GMemoryOutputStream.
341  *
342  * If @data is non-%NULL, the stream  will use that for its internal storage.
343  * If @realloc_fn is non-%NULL, it will be used for resizing the internal
344  * storage when necessary. To construct a fixed-size output stream,
345  * pass %NULL as @realloc_fn.
346  *
347  * |[
348  * /&ast; a stream that can grow &ast;/
349  * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
350  *
351  * /&ast; another stream that can grow &ast;/
352  * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
353  *
354  * /&ast; a fixed-size stream &ast;/
355  * data = malloc (200);
356  * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
357  * ]|
358  *
359  * Return value: A newly created #GMemoryOutputStream object.
360  **/
361 GOutputStream *
362 g_memory_output_stream_new (gpointer       data,
363                             gsize          size,
364                             GReallocFunc   realloc_function,
365                             GDestroyNotify destroy_function)
366 {
367   GOutputStream *stream;
368
369   stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
370                          "data", data,
371                          "size", size,
372                          "realloc-function", realloc_function,
373                          "destroy-function", destroy_function,
374                          NULL);
375
376   return stream;
377 }
378
379 /**
380  * g_memory_output_stream_get_data:
381  * @ostream: a #GMemoryOutputStream
382  *
383  * Gets any loaded data from the @ostream.
384  *
385  * Note that the returned pointer may become invalid on the next
386  * write or truncate operation on the stream.
387  *
388  * Returns: pointer to the stream's data
389  **/
390 gpointer
391 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
392 {
393   g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
394
395   return ostream->priv->data;
396 }
397
398 /**
399  * g_memory_output_stream_get_size:
400  * @ostream: a #GMemoryOutputStream
401  *
402  * Gets the size of the currently allocated data area (availible from
403  * g_memory_output_stream_get_data()). If the stream isn't
404  * growable (no realloc was passed to g_memory_output_stream_new()) then
405  * this is the maximum size of the stream and further writes
406  * will return %G_IO_ERROR_NO_SPACE.
407  *
408  * Note that for growable streams the returned size may become invalid on
409  * the next write or truncate operation on the stream.
410  *
411  * If you want the number of bytes currently written to the stream, use
412  * g_memory_output_stream_get_data_size().
413  *
414  * Returns: the number of bytes allocated for the data buffer
415  */
416 gsize
417 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
418 {
419   g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
420
421   return ostream->priv->len;
422 }
423
424 /**
425  * g_memory_output_stream_get_data_size:
426  * @ostream: a #GMemoryOutputStream
427  *
428  * Returns the number of bytes from the start up
429  * to including the last byte written in the stream
430  * that has not been truncated away.
431  *
432  * Returns: the number of bytes written to the stream
433  *
434  * Since: 2.18
435  */
436 gsize
437 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
438 {
439   g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
440
441   return ostream->priv->valid_len;
442 }
443
444 static gboolean
445 array_resize (GMemoryOutputStream  *ostream,
446               gsize                 size,
447               gboolean              allow_partial,
448               GError              **error)
449 {
450   GMemoryOutputStreamPrivate *priv;
451   gpointer data;
452   gsize len;
453
454   priv = ostream->priv;
455
456   if (priv->len == size)
457     return TRUE;
458
459   if (!priv->realloc_fn)
460     {
461       if (allow_partial &&
462           priv->pos < priv->len)
463         return TRUE; /* Short write */
464
465       g_set_error_literal (error,
466                            G_IO_ERROR,
467                            G_IO_ERROR_NO_SPACE,
468                            _("Memory output stream not resizable"));
469       return FALSE;
470     }
471
472   len = priv->len;
473   data = priv->realloc_fn (priv->data, size);
474
475   if (size > 0 && !data)
476     {
477       if (allow_partial &&
478           priv->pos < priv->len)
479         return TRUE; /* Short write */
480
481       g_set_error_literal (error,
482                            G_IO_ERROR,
483                            G_IO_ERROR_NO_SPACE,
484                            _("Failed to resize memory output stream"));
485       return FALSE;
486     }
487
488   if (size > len)
489     memset ((guint8 *)data + len, 0, size - len);
490
491   priv->data = data;
492   priv->len = size;
493
494   if (priv->len < priv->valid_len)
495     priv->valid_len = priv->len;
496
497   return TRUE;
498 }
499
500 static gint
501 g_nearest_pow (gint num)
502 {
503   gint n = 1;
504
505   while (n < num)
506     n <<= 1;
507
508   return n;
509 }
510
511 static gssize
512 g_memory_output_stream_write (GOutputStream  *stream,
513                               const void     *buffer,
514                               gsize           count,
515                               GCancellable   *cancellable,
516                               GError        **error)
517 {
518   GMemoryOutputStream        *ostream;
519   GMemoryOutputStreamPrivate *priv;
520   guint8   *dest;
521   gsize new_size;
522
523   ostream = G_MEMORY_OUTPUT_STREAM (stream);
524   priv = ostream->priv;
525
526   if (count == 0)
527     return 0;
528
529   /* Check for address space overflow, but only if the buffer is resizable.
530      Otherwise we just do a short write and don't worry. */
531   if (priv->realloc_fn && priv->pos + count < priv->pos)
532     goto overflow;
533
534   if (priv->pos + count > priv->len)
535     {
536       /* At least enought to fit the write, rounded up
537              for greater than linear growth.
538          TODO: This wastes a lot of memory at large stream sizes.
539                Figure out a more rational allocation strategy. */
540       new_size = g_nearest_pow (priv->pos + count);
541       /* Check for overflow again. We have only checked if
542          pos + count > G_MAXSIZE, but it only catches the case of writing
543          more than 4GiB total on a 32-bit system. There's still the problem
544          of g_nearest_pow overflowing above 0x7fffffff, so we're
545          effectively limited to 2GiB. */
546       if (new_size < priv->len)
547         goto overflow;
548
549       new_size = MAX (new_size, MIN_ARRAY_SIZE);
550       if (!array_resize (ostream, new_size, TRUE, error))
551         return -1;
552     }
553
554   /* Make sure we handle short writes if the array_resize
555      only added part of the required memory */
556   count = MIN (count, priv->len - priv->pos);
557
558   dest = (guint8 *)priv->data + priv->pos;
559   memcpy (dest, buffer, count);
560   priv->pos += count;
561
562   if (priv->pos > priv->valid_len)
563     priv->valid_len = priv->pos;
564
565   return count;
566
567  overflow:
568   /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
569   g_set_error_literal (error,
570                        G_IO_ERROR,
571                        G_IO_ERROR_NO_SPACE,
572                        _("Amount of memory required to process the write is "
573                          "larger than available address space"));
574   return -1;
575 }
576
577 static gboolean
578 g_memory_output_stream_close (GOutputStream  *stream,
579                               GCancellable   *cancellable,
580                               GError        **error)
581 {
582   return TRUE;
583 }
584
585 static void
586 g_memory_output_stream_write_async (GOutputStream       *stream,
587                                     const void          *buffer,
588                                     gsize                count,
589                                     int                  io_priority,
590                                     GCancellable        *cancellable,
591                                     GAsyncReadyCallback  callback,
592                                     gpointer             data)
593 {
594   GSimpleAsyncResult *simple;
595   gssize nwritten;
596
597   nwritten = g_memory_output_stream_write (stream,
598                                            buffer,
599                                            count,
600                                            cancellable,
601                                            NULL);
602
603   simple = g_simple_async_result_new (G_OBJECT (stream),
604                                       callback,
605                                       data,
606                                       g_memory_output_stream_write_async);
607
608   g_simple_async_result_set_op_res_gssize (simple, nwritten);
609   g_simple_async_result_complete_in_idle (simple);
610   g_object_unref (simple);
611 }
612
613 static gssize
614 g_memory_output_stream_write_finish (GOutputStream  *stream,
615                                      GAsyncResult   *result,
616                                      GError        **error)
617 {
618   GSimpleAsyncResult *simple;
619   gssize nwritten;
620
621   simple = G_SIMPLE_ASYNC_RESULT (result);
622
623   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
624                   g_memory_output_stream_write_async);
625
626   nwritten = g_simple_async_result_get_op_res_gssize (simple);
627
628   return nwritten;
629 }
630
631 static void
632 g_memory_output_stream_close_async (GOutputStream       *stream,
633                                     int                  io_priority,
634                                     GCancellable        *cancellable,
635                                     GAsyncReadyCallback  callback,
636                                     gpointer             data)
637 {
638   GSimpleAsyncResult *simple;
639
640   simple = g_simple_async_result_new (G_OBJECT (stream),
641                                       callback,
642                                       data,
643                                       g_memory_output_stream_close_async);
644
645
646   /* will always return TRUE */
647   g_memory_output_stream_close (stream, cancellable, NULL);
648
649   g_simple_async_result_complete_in_idle (simple);
650   g_object_unref (simple);
651 }
652
653 static gboolean
654 g_memory_output_stream_close_finish (GOutputStream  *stream,
655                                      GAsyncResult   *result,
656                                      GError        **error)
657 {
658   GSimpleAsyncResult *simple;
659
660   simple = G_SIMPLE_ASYNC_RESULT (result);
661
662   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
663                   g_memory_output_stream_close_async);
664
665   return TRUE;
666 }
667
668 static goffset
669 g_memory_output_stream_tell (GSeekable *seekable)
670 {
671   GMemoryOutputStream *stream;
672   GMemoryOutputStreamPrivate *priv;
673
674   stream = G_MEMORY_OUTPUT_STREAM (seekable);
675   priv = stream->priv;
676
677   return priv->pos;
678 }
679
680 static gboolean
681 g_memory_output_stream_can_seek (GSeekable *seekable)
682 {
683   return TRUE;
684 }
685
686 static gboolean
687 g_memory_output_stream_seek (GSeekable    *seekable,
688                             goffset        offset,
689                             GSeekType      type,
690                             GCancellable  *cancellable,
691                             GError       **error)
692 {
693   GMemoryOutputStream        *stream;
694   GMemoryOutputStreamPrivate *priv;
695   goffset absolute;
696
697   stream = G_MEMORY_OUTPUT_STREAM (seekable);
698   priv = stream->priv;
699
700   switch (type)
701     {
702     case G_SEEK_CUR:
703       absolute = priv->pos + offset;
704       break;
705
706     case G_SEEK_SET:
707       absolute = offset;
708       break;
709
710     case G_SEEK_END:
711       absolute = priv->len + offset;
712       break;
713
714     default:
715       g_set_error_literal (error,
716                            G_IO_ERROR,
717                            G_IO_ERROR_INVALID_ARGUMENT,
718                            _("Invalid GSeekType supplied"));
719
720       return FALSE;
721     }
722
723   if (absolute < 0)
724     {
725       g_set_error_literal (error,
726                            G_IO_ERROR,
727                            G_IO_ERROR_INVALID_ARGUMENT,
728                            _("Requested seek before the beginning of the stream"));
729       return FALSE;
730     }
731
732   if (absolute > priv->len)
733     {
734       g_set_error_literal (error,
735                            G_IO_ERROR,
736                            G_IO_ERROR_INVALID_ARGUMENT,
737                            _("Requested seek beyond the end of the stream"));
738       return FALSE;
739     }
740
741   priv->pos = absolute;
742
743   return TRUE;
744 }
745
746 static gboolean
747 g_memory_output_stream_can_truncate (GSeekable *seekable)
748 {
749   GMemoryOutputStream *ostream;
750   GMemoryOutputStreamPrivate *priv;
751
752   ostream = G_MEMORY_OUTPUT_STREAM (seekable);
753   priv = ostream->priv;
754
755   return priv->realloc_fn != NULL;
756 }
757
758 static gboolean
759 g_memory_output_stream_truncate (GSeekable     *seekable,
760                                  goffset        offset,
761                                  GCancellable  *cancellable,
762                                  GError       **error)
763 {
764   GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
765
766   if (!array_resize (ostream, offset, FALSE, error))
767     return FALSE;
768
769   return TRUE;
770 }
771
772 #define __G_MEMORY_OUTPUT_STREAM_C__
773 #include "gioaliasdef.c"