Fix up includes in section docs
[platform/upstream/glib.git] / gio / gbufferedoutputstream.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  * Author: Christian Kellner <gicmo@gnome.org> 
21  */
22
23 #include <config.h>
24 #include "gbufferedoutputstream.h"
25 #include "goutputstream.h"
26 #include "gsimpleasyncresult.h"
27 #include "string.h"
28 #include "glibintl.h"
29
30 #include <gioalias.h>
31
32 /**
33  * SECTION:gbufferedoutputstream
34  * @short_description: Buffered Output Stream
35  * @include: gio.h
36  * @see_also: #GFilterOutputStream, #GOutputStream
37  * 
38  * Buffered output stream implements #GFilterOutputStream and provides 
39  * for buffered writes. 
40  * 
41  * By default, #GBufferedOutputStream's buffer size is set at 4 kilobytes.
42  * 
43  * To create a buffered output stream, use g_buffered_output_stream_new(), 
44  * or g_buffered_output_stream_new_sized() to specify the buffer's size 
45  * at construction.
46  * 
47  * To get the size of a buffer within a buffered input stream, use 
48  * g_buffered_output_stream_get_buffer_size(). To change the size of a 
49  * buffered output stream's buffer, use 
50  * g_buffered_output_stream_set_buffer_size(). Note that the buffer's 
51  * size cannot be reduced below the size of the data within the buffer.
52  **/
53
54 #define DEFAULT_BUFFER_SIZE 4096
55
56 struct _GBufferedOutputStreamPrivate {
57   guint8 *buffer; 
58   gsize   len;
59   goffset pos;
60   gboolean auto_grow;
61 };
62
63 enum {
64   PROP_0,
65   PROP_BUFSIZE,
66   PROP_AUTO_GROW
67 };
68
69 static void     g_buffered_output_stream_set_property (GObject      *object,
70                                                        guint         prop_id,
71                                                        const GValue *value,
72                                                        GParamSpec   *pspec);
73
74 static void     g_buffered_output_stream_get_property (GObject    *object,
75                                                        guint       prop_id,
76                                                        GValue     *value,
77                                                        GParamSpec *pspec);
78 static void     g_buffered_output_stream_finalize     (GObject *object);
79
80
81 static gssize   g_buffered_output_stream_write        (GOutputStream *stream,
82                                                        const void    *buffer,
83                                                        gsize          count,
84                                                        GCancellable  *cancellable,
85                                                        GError       **error);
86 static gboolean g_buffered_output_stream_flush        (GOutputStream    *stream,
87                                                        GCancellable  *cancellable,
88                                                        GError          **error);
89 static gboolean g_buffered_output_stream_close        (GOutputStream  *stream,
90                                                        GCancellable   *cancellable,
91                                                        GError        **error);
92
93 static void     g_buffered_output_stream_write_async  (GOutputStream        *stream,
94                                                        const void           *buffer,
95                                                        gsize                 count,
96                                                        int                   io_priority,
97                                                        GCancellable         *cancellable,
98                                                        GAsyncReadyCallback   callback,
99                                                        gpointer              data);
100 static gssize   g_buffered_output_stream_write_finish (GOutputStream        *stream,
101                                                        GAsyncResult         *result,
102                                                        GError              **error);
103 static void     g_buffered_output_stream_flush_async  (GOutputStream        *stream,
104                                                        int                   io_priority,
105                                                        GCancellable         *cancellable,
106                                                        GAsyncReadyCallback   callback,
107                                                        gpointer              data);
108 static gboolean g_buffered_output_stream_flush_finish (GOutputStream        *stream,
109                                                        GAsyncResult         *result,
110                                                        GError              **error);
111 static void     g_buffered_output_stream_close_async  (GOutputStream        *stream,
112                                                        int                   io_priority,
113                                                        GCancellable         *cancellable,
114                                                        GAsyncReadyCallback   callback,
115                                                        gpointer              data);
116 static gboolean g_buffered_output_stream_close_finish (GOutputStream        *stream,
117                                                        GAsyncResult         *result,
118                                                        GError              **error);
119
120 G_DEFINE_TYPE (GBufferedOutputStream,
121                g_buffered_output_stream,
122                G_TYPE_FILTER_OUTPUT_STREAM)
123
124
125 static void
126 g_buffered_output_stream_class_init (GBufferedOutputStreamClass *klass)
127 {
128   GObjectClass *object_class;
129   GOutputStreamClass *ostream_class;
130  
131   g_type_class_add_private (klass, sizeof (GBufferedOutputStreamPrivate));
132
133   object_class = G_OBJECT_CLASS (klass);
134   object_class->get_property = g_buffered_output_stream_get_property;
135   object_class->set_property = g_buffered_output_stream_set_property;
136   object_class->finalize     = g_buffered_output_stream_finalize;
137
138   ostream_class = G_OUTPUT_STREAM_CLASS (klass);
139   ostream_class->write_fn = g_buffered_output_stream_write;
140   ostream_class->flush = g_buffered_output_stream_flush;
141   ostream_class->close_fn = g_buffered_output_stream_close;
142   ostream_class->write_async  = g_buffered_output_stream_write_async;
143   ostream_class->write_finish = g_buffered_output_stream_write_finish;
144   ostream_class->flush_async  = g_buffered_output_stream_flush_async;
145   ostream_class->flush_finish = g_buffered_output_stream_flush_finish;
146   ostream_class->close_async  = g_buffered_output_stream_close_async;
147   ostream_class->close_finish = g_buffered_output_stream_close_finish;
148
149   g_object_class_install_property (object_class,
150                                    PROP_BUFSIZE,
151                                    g_param_spec_uint ("buffer-size",
152                                                       P_("Buffer Size"),
153                                                       P_("The size of the backend buffer"),
154                                                       1,
155                                                       G_MAXUINT,
156                                                       DEFAULT_BUFFER_SIZE,
157                                                       G_PARAM_READWRITE|G_PARAM_CONSTRUCT|
158                                                       G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
159
160   g_object_class_install_property (object_class,
161                                    PROP_AUTO_GROW,
162                                    g_param_spec_boolean ("auto-grow",
163                                                          P_("Auto-grow"),
164                                                          P_("Whether the buffer should automatically grow"),
165                                                          FALSE,
166                                                          G_PARAM_READWRITE|
167                                                          G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
168
169 }
170
171 /**
172  * g_buffered_output_stream_get_buffer_size:
173  * @stream: a #GBufferedOutputStream.
174  * 
175  * Gets the size of the buffer in the @stream.
176  * 
177  * Returns: the current size of the buffer.
178  **/
179 gsize
180 g_buffered_output_stream_get_buffer_size (GBufferedOutputStream *stream)
181 {
182   g_return_val_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream), -1);
183
184   return stream->priv->len;
185 }
186
187 /**
188  * g_buffered_output_stream_set_buffer_size:
189  * @stream: a #GBufferedOutputStream.
190  * @size: a #gsize.
191  *
192  * Sets the size of the internal buffer to @size.
193  **/    
194 void
195 g_buffered_output_stream_set_buffer_size (GBufferedOutputStream *stream,
196                                           gsize                  size)
197 {
198   GBufferedOutputStreamPrivate *priv;
199   guint8 *buffer;
200   
201   g_return_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream));
202
203   priv = stream->priv;
204   
205   if (size == priv->len)
206     return;
207
208   if (priv->buffer)
209     {
210       size = MAX (size, priv->pos);
211
212       buffer = g_malloc (size);
213       memcpy (buffer, priv->buffer, priv->pos);
214       g_free (priv->buffer);
215       priv->buffer = buffer;
216       priv->len = size;
217       /* Keep old pos */
218     }
219   else
220     {
221       priv->buffer = g_malloc (size);
222       priv->len = size;
223       priv->pos = 0;
224     }
225
226   g_object_notify (G_OBJECT (stream), "buffer-size");
227 }
228
229 /**
230  * g_buffered_output_stream_get_auto_grow:
231  * @stream: a #GBufferedOutputStream.
232  * 
233  * Checks if the buffer automatically grows as data is added.
234  * 
235  * Returns: %TRUE if the @stream's buffer automatically grows,
236  * %FALSE otherwise.
237  **/  
238 gboolean
239 g_buffered_output_stream_get_auto_grow (GBufferedOutputStream *stream)
240 {
241   g_return_val_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream), FALSE);
242
243   return stream->priv->auto_grow;
244 }
245
246 /**
247  * g_buffered_output_stream_set_auto_grow:
248  * @stream: a #GBufferedOutputStream.
249  * @auto_grow: a #gboolean.
250  *
251  * Sets whether or not the @stream's buffer should automatically grow.
252  * If @auto_grow is true, then each write will just make the buffer
253  * larger, and you must manually flush the buffer to actually write out
254  * the data to the underlying stream.
255  **/
256 void
257 g_buffered_output_stream_set_auto_grow (GBufferedOutputStream *stream,
258                                         gboolean               auto_grow)
259 {
260   GBufferedOutputStreamPrivate *priv;
261   g_return_if_fail (G_IS_BUFFERED_OUTPUT_STREAM (stream));
262   priv = stream->priv;
263   auto_grow = auto_grow != FALSE;
264   if (priv->auto_grow != auto_grow)
265     {
266       priv->auto_grow = auto_grow;
267       g_object_notify (G_OBJECT (stream), "auto-grow");
268     }
269 }
270
271 static void
272 g_buffered_output_stream_set_property (GObject      *object,
273                                        guint         prop_id,
274                                        const GValue *value,
275                                        GParamSpec   *pspec)
276 {
277   GBufferedOutputStream *stream;
278
279   stream = G_BUFFERED_OUTPUT_STREAM (object);
280
281   switch (prop_id) 
282     {
283     case PROP_BUFSIZE:
284       g_buffered_output_stream_set_buffer_size (stream, g_value_get_uint (value));
285       break;    
286
287     case PROP_AUTO_GROW:
288       g_buffered_output_stream_set_auto_grow (stream, g_value_get_boolean (value));
289       break;
290
291     default:
292       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293       break;
294     }
295
296 }
297
298 static void
299 g_buffered_output_stream_get_property (GObject    *object,
300                                        guint       prop_id,
301                                        GValue     *value,
302                                        GParamSpec *pspec)
303 {
304   GBufferedOutputStream *buffered_stream;
305   GBufferedOutputStreamPrivate *priv;
306
307   buffered_stream = G_BUFFERED_OUTPUT_STREAM (object);
308   priv = buffered_stream->priv;
309
310   switch (prop_id)
311     {
312     case PROP_BUFSIZE:
313       g_value_set_uint (value, priv->len);
314       break;
315
316     case PROP_AUTO_GROW:
317       g_value_set_boolean (value, priv->auto_grow);
318       break;
319
320     default:
321       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322       break;
323     }
324
325 }
326
327 static void
328 g_buffered_output_stream_finalize (GObject *object)
329 {
330   GBufferedOutputStream *stream;
331   GBufferedOutputStreamPrivate *priv;
332
333   stream = G_BUFFERED_OUTPUT_STREAM (object);
334   priv = stream->priv;
335
336   g_free (priv->buffer);
337
338   if (G_OBJECT_CLASS (g_buffered_output_stream_parent_class)->finalize)
339     (*G_OBJECT_CLASS (g_buffered_output_stream_parent_class)->finalize) (object);
340 }
341
342 static void
343 g_buffered_output_stream_init (GBufferedOutputStream *stream)
344 {
345   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
346                                               G_TYPE_BUFFERED_OUTPUT_STREAM,
347                                               GBufferedOutputStreamPrivate);
348
349 }
350
351 /**
352  * g_buffered_output_stream_new:
353  * @base_stream: a #GOutputStream.
354  * 
355  * Creates a new buffered output stream for a base stream.
356  * 
357  * Returns: a #GOutputStream for the given @base_stream.
358  **/  
359 GOutputStream *
360 g_buffered_output_stream_new (GOutputStream *base_stream)
361 {
362   GOutputStream *stream;
363
364   g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
365
366   stream = g_object_new (G_TYPE_BUFFERED_OUTPUT_STREAM,
367                          "base-stream", base_stream,
368                          NULL);
369
370   return stream;
371 }
372
373 /**
374  * g_buffered_output_stream_new_sized:
375  * @base_stream: a #GOutputStream.
376  * @size: a #gsize.
377  * 
378  * Creates a new buffered output stream with a given buffer size.
379  * 
380  * Returns: a #GOutputStream with an internal buffer set to @size.
381  **/  
382 GOutputStream *
383 g_buffered_output_stream_new_sized (GOutputStream *base_stream,
384                                     guint          size)
385 {
386   GOutputStream *stream;
387
388   g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
389
390   stream = g_object_new (G_TYPE_BUFFERED_OUTPUT_STREAM,
391                          "base-stream", base_stream,
392                          "buffer-size", size,
393                          NULL);
394
395   return stream;
396 }
397
398 static gboolean
399 flush_buffer (GBufferedOutputStream  *stream,
400               GCancellable           *cancellable,
401               GError                 **error)
402 {
403   GBufferedOutputStreamPrivate *priv;
404   GOutputStream                *base_stream;
405   gboolean                      res;
406   gsize                         bytes_written;
407   gsize                         count;
408
409   priv = stream->priv;
410   bytes_written = 0;
411   base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;
412
413   g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), FALSE);
414
415   res = g_output_stream_write_all (base_stream,
416                                    priv->buffer,
417                                    priv->pos,
418                                    &bytes_written,
419                                    cancellable,
420                                    error);
421
422   count = priv->pos - bytes_written;
423
424   if (count > 0)
425     g_memmove (priv->buffer, priv->buffer + bytes_written, count);
426   
427   priv->pos -= bytes_written;
428
429   return res;
430 }
431
432 static gssize
433 g_buffered_output_stream_write  (GOutputStream *stream,
434                                  const void    *buffer,
435                                  gsize          count,
436                                  GCancellable  *cancellable,
437                                  GError       **error)
438 {
439   GBufferedOutputStream        *bstream;
440   GBufferedOutputStreamPrivate *priv;
441   gboolean res;
442   gsize    n;
443   gsize new_size;
444
445   bstream = G_BUFFERED_OUTPUT_STREAM (stream);
446   priv = bstream->priv;
447
448   n = priv->len - priv->pos;
449
450   if (priv->auto_grow && n < count)
451     {
452       new_size = MAX (priv->len * 2, priv->len + count);
453       g_buffered_output_stream_set_buffer_size (bstream, new_size);
454     }
455   else if (n == 0)
456     {
457       res = flush_buffer (bstream, cancellable, error);
458       
459       if (res == FALSE)
460         return -1;
461     }
462
463   n = priv->len - priv->pos;
464   
465   count = MIN (count, n);
466   memcpy (priv->buffer + priv->pos, buffer, count);
467   priv->pos += count;
468
469   return count;
470 }
471
472 static gboolean
473 g_buffered_output_stream_flush (GOutputStream  *stream,
474                                 GCancellable   *cancellable,
475                                 GError        **error)
476 {
477   GBufferedOutputStream *bstream;
478   GBufferedOutputStreamPrivate *priv;
479   GOutputStream                *base_stream;
480   gboolean res;
481
482   bstream = G_BUFFERED_OUTPUT_STREAM (stream);
483   priv = bstream->priv;
484   base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;
485
486   res = flush_buffer (bstream, cancellable, error);
487
488   if (res == FALSE)
489     return FALSE;
490
491   res = g_output_stream_flush (base_stream, cancellable, error);
492
493   return res;
494 }
495
496 static gboolean
497 g_buffered_output_stream_close (GOutputStream  *stream,
498                                 GCancellable   *cancellable,
499                                 GError        **error)
500 {
501   GBufferedOutputStream        *bstream;
502   GBufferedOutputStreamPrivate *priv;
503   GOutputStream                *base_stream;
504   gboolean                      res;
505
506   bstream = G_BUFFERED_OUTPUT_STREAM (stream);
507   priv = bstream->priv;
508   base_stream = G_FILTER_OUTPUT_STREAM (bstream)->base_stream;
509
510   res = flush_buffer (bstream, cancellable, error);
511
512   /* report the first error but still close the stream */
513   if (res)
514     res = g_output_stream_close (base_stream, cancellable, error); 
515   else
516     g_output_stream_close (base_stream, cancellable, NULL); 
517
518   return res;
519 }
520
521 /* ************************** */
522 /* Async stuff implementation */
523 /* ************************** */
524
525 /* TODO: This should be using the base class async ops, not threads */
526
527 typedef struct {
528
529   guint flush_stream : 1;
530   guint close_stream : 1;
531
532 } FlushData;
533
534 static void
535 free_flush_data (gpointer data)
536 {
537   g_slice_free (FlushData, data);
538 }
539
540 /* This function is used by all three (i.e. 
541  * _write, _flush, _close) functions since
542  * all of them will need to flush the buffer
543  * and so closing and writing is just a special
544  * case of flushing + some addition stuff */
545 static void
546 flush_buffer_thread (GSimpleAsyncResult *result,
547                      GObject            *object,
548                      GCancellable       *cancellable)
549 {
550   GBufferedOutputStream *stream;
551   GOutputStream *base_stream;
552   FlushData     *fdata;
553   gboolean       res;
554   GError        *error = NULL;
555
556   stream = G_BUFFERED_OUTPUT_STREAM (object);
557   fdata = g_simple_async_result_get_op_res_gpointer (result);
558   base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;
559
560   res = flush_buffer (stream, cancellable, &error);
561
562   /* if flushing the buffer didn't work don't even bother
563    * to flush the stream but just report that error */
564   if (res && fdata->flush_stream)
565     res = g_output_stream_flush (base_stream, cancellable, &error);
566
567   if (fdata->close_stream) 
568     {
569      
570       /* if flushing the buffer or the stream returned 
571        * an error report that first error but still try 
572        * close the stream */
573       if (res == FALSE)
574         g_output_stream_close (base_stream, cancellable, NULL);
575       else 
576         res = g_output_stream_close (base_stream, cancellable, &error);
577     }
578
579   if (res == FALSE)
580     {
581       g_simple_async_result_set_from_error (result, error);
582       g_error_free (error);
583     }
584 }
585
586 typedef struct {
587     
588   FlushData fdata;
589
590   gsize  count;
591   const void  *buffer;
592
593 } WriteData;
594
595 static void 
596 free_write_data (gpointer data)
597 {
598   g_slice_free (WriteData, data);
599 }
600
601 static void
602 g_buffered_output_stream_write_async (GOutputStream        *stream,
603                                       const void           *buffer,
604                                       gsize                 count,
605                                       int                   io_priority,
606                                       GCancellable         *cancellable,
607                                       GAsyncReadyCallback   callback,
608                                       gpointer              data)
609 {
610   GBufferedOutputStream *buffered_stream;
611   GBufferedOutputStreamPrivate *priv;
612   GSimpleAsyncResult *res;
613   WriteData *wdata;
614
615   buffered_stream = G_BUFFERED_OUTPUT_STREAM (stream);
616   priv = buffered_stream->priv;
617
618   wdata = g_slice_new (WriteData);
619   wdata->count  = count;
620   wdata->buffer = buffer;
621
622   res = g_simple_async_result_new (G_OBJECT (stream),
623                                    callback,
624                                    data,
625                                    g_buffered_output_stream_write_async);
626
627   g_simple_async_result_set_op_res_gpointer (res, wdata, free_write_data);
628
629   /* if we have space left directly call the
630    * callback (from idle) otherwise schedule a buffer 
631    * flush in the thread. In both cases the actual
632    * copying of the data to the buffer will be done in
633    * the write_finish () func since that should
634    * be fast enough */
635   if (priv->len - priv->pos > 0)
636     {
637       g_simple_async_result_complete_in_idle (res);
638     }
639   else
640     {
641       wdata->fdata.flush_stream = FALSE;
642       wdata->fdata.close_stream = FALSE;
643       g_simple_async_result_run_in_thread (res, 
644                                            flush_buffer_thread, 
645                                            io_priority,
646                                            cancellable);
647       g_object_unref (res);
648     }
649 }
650
651 static gssize
652 g_buffered_output_stream_write_finish (GOutputStream        *stream,
653                                        GAsyncResult         *result,
654                                        GError              **error)
655 {
656   GBufferedOutputStreamPrivate *priv;
657   GBufferedOutputStream        *buffered_stream;
658   GSimpleAsyncResult *simple;
659   WriteData          *wdata;
660   gssize              count;
661
662   simple = G_SIMPLE_ASYNC_RESULT (result);
663   buffered_stream = G_BUFFERED_OUTPUT_STREAM (stream);
664   priv = buffered_stream->priv;
665
666   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == 
667             g_buffered_output_stream_write_async);
668
669   wdata = g_simple_async_result_get_op_res_gpointer (simple);
670
671   /* Now do the real copying of data to the buffer */
672   count = priv->len - priv->pos; 
673   count = MIN (wdata->count, count);
674
675   memcpy (priv->buffer + priv->pos, wdata->buffer, count);
676   
677   priv->pos += count;
678
679   return count;
680 }
681
682 static void
683 g_buffered_output_stream_flush_async (GOutputStream        *stream,
684                                       int                   io_priority,
685                                       GCancellable         *cancellable,
686                                       GAsyncReadyCallback   callback,
687                                       gpointer              data)
688 {
689   GSimpleAsyncResult *res;
690   FlushData          *fdata;
691
692   fdata = g_slice_new (FlushData);
693   fdata->flush_stream = TRUE;
694   fdata->close_stream = FALSE;
695
696   res = g_simple_async_result_new (G_OBJECT (stream),
697                                    callback,
698                                    data,
699                                    g_buffered_output_stream_flush_async);
700
701   g_simple_async_result_set_op_res_gpointer (res, fdata, free_flush_data);
702
703   g_simple_async_result_run_in_thread (res, 
704                                        flush_buffer_thread, 
705                                        io_priority,
706                                        cancellable);
707   g_object_unref (res);
708 }
709
710 static gboolean
711 g_buffered_output_stream_flush_finish (GOutputStream        *stream,
712                                        GAsyncResult         *result,
713                                        GError              **error)
714 {
715   GSimpleAsyncResult *simple;
716
717   simple = G_SIMPLE_ASYNC_RESULT (result);
718
719   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == 
720             g_buffered_output_stream_flush_async);
721
722   return TRUE;
723 }
724
725 static void
726 g_buffered_output_stream_close_async (GOutputStream        *stream,
727                                       int                   io_priority,
728                                       GCancellable         *cancellable,
729                                       GAsyncReadyCallback   callback,
730                                       gpointer              data)
731 {
732   GSimpleAsyncResult *res;
733   FlushData          *fdata;
734
735   fdata = g_slice_new (FlushData);
736   fdata->close_stream = TRUE;
737
738   res = g_simple_async_result_new (G_OBJECT (stream),
739                                    callback,
740                                    data,
741                                    g_buffered_output_stream_close_async);
742
743   g_simple_async_result_set_op_res_gpointer (res, fdata, free_flush_data);
744
745   g_simple_async_result_run_in_thread (res, 
746                                        flush_buffer_thread, 
747                                        io_priority,
748                                        cancellable);
749   g_object_unref (res);
750 }
751
752 static gboolean
753 g_buffered_output_stream_close_finish (GOutputStream        *stream,
754                                        GAsyncResult         *result,
755                                        GError              **error)
756 {
757   GSimpleAsyncResult *simple;
758
759   simple = G_SIMPLE_ASYNC_RESULT (result);
760
761   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == 
762             g_buffered_output_stream_flush_async);
763
764   return TRUE;
765 }
766
767 #define __G_BUFFERED_OUTPUT_STREAM_C__
768 #include "gioaliasdef.c"