1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "goutputstream.h"
25 #include "gcancellable.h"
26 #include "gasyncresult.h"
27 #include "gsimpleasyncresult.h"
28 #include "ginputstream.h"
34 * SECTION:goutputstream
35 * @short_description: Base class for implementing streaming output
38 * GOutputStream has functions to write to a stream (g_output_stream_write()),
39 * to close a stream (g_output_stream_close()) and to flush pending writes
40 * (g_output_stream_flush()).
42 * To copy the content of an input stream to an output stream without
43 * manually handling the reads and writes, use g_output_stream_splice().
45 * All of these functions have async variants too.
48 G_DEFINE_ABSTRACT_TYPE (GOutputStream
, g_output_stream
, G_TYPE_OBJECT
);
50 struct _GOutputStreamPrivate
{
54 GAsyncReadyCallback outstanding_callback
;
57 static gssize
g_output_stream_real_splice (GOutputStream
*stream
,
59 GOutputStreamSpliceFlags flags
,
60 GCancellable
*cancellable
,
62 static void g_output_stream_real_write_async (GOutputStream
*stream
,
66 GCancellable
*cancellable
,
67 GAsyncReadyCallback callback
,
69 static gssize
g_output_stream_real_write_finish (GOutputStream
*stream
,
72 static void g_output_stream_real_splice_async (GOutputStream
*stream
,
74 GOutputStreamSpliceFlags flags
,
76 GCancellable
*cancellable
,
77 GAsyncReadyCallback callback
,
79 static gssize
g_output_stream_real_splice_finish (GOutputStream
*stream
,
82 static void g_output_stream_real_flush_async (GOutputStream
*stream
,
84 GCancellable
*cancellable
,
85 GAsyncReadyCallback callback
,
87 static gboolean
g_output_stream_real_flush_finish (GOutputStream
*stream
,
90 static void g_output_stream_real_close_async (GOutputStream
*stream
,
92 GCancellable
*cancellable
,
93 GAsyncReadyCallback callback
,
95 static gboolean
g_output_stream_real_close_finish (GOutputStream
*stream
,
98 static gboolean
_g_output_stream_close_internal (GOutputStream
*stream
,
99 GCancellable
*cancellable
,
103 g_output_stream_finalize (GObject
*object
)
105 G_OBJECT_CLASS (g_output_stream_parent_class
)->finalize (object
);
109 g_output_stream_dispose (GObject
*object
)
111 GOutputStream
*stream
;
113 stream
= G_OUTPUT_STREAM (object
);
115 if (!stream
->priv
->closed
)
116 g_output_stream_close (stream
, NULL
, NULL
);
118 G_OBJECT_CLASS (g_output_stream_parent_class
)->dispose (object
);
122 g_output_stream_class_init (GOutputStreamClass
*klass
)
124 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
126 g_type_class_add_private (klass
, sizeof (GOutputStreamPrivate
));
128 gobject_class
->finalize
= g_output_stream_finalize
;
129 gobject_class
->dispose
= g_output_stream_dispose
;
131 klass
->splice
= g_output_stream_real_splice
;
133 klass
->write_async
= g_output_stream_real_write_async
;
134 klass
->write_finish
= g_output_stream_real_write_finish
;
135 klass
->splice_async
= g_output_stream_real_splice_async
;
136 klass
->splice_finish
= g_output_stream_real_splice_finish
;
137 klass
->flush_async
= g_output_stream_real_flush_async
;
138 klass
->flush_finish
= g_output_stream_real_flush_finish
;
139 klass
->close_async
= g_output_stream_real_close_async
;
140 klass
->close_finish
= g_output_stream_real_close_finish
;
144 g_output_stream_init (GOutputStream
*stream
)
146 stream
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (stream
,
147 G_TYPE_OUTPUT_STREAM
,
148 GOutputStreamPrivate
);
152 * g_output_stream_write:
153 * @stream: a #GOutputStream.
154 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
155 * @count: the number of bytes to write
156 * @cancellable: (allow-none): optional cancellable object
157 * @error: location to store the error occurring, or %NULL to ignore
159 * Tries to write @count bytes from @buffer into the stream. Will block
160 * during the operation.
162 * If count is 0, returns 0 and does nothing. A value of @count
163 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
165 * On success, the number of bytes written to the stream is returned.
166 * It is not an error if this is not the same as the requested size, as it
167 * can happen e.g. on a partial I/O error, or if there is not enough
168 * storage in the stream. All writes block until at least one byte
169 * is written or an error occurs; 0 is never returned (unless
172 * If @cancellable is not NULL, then the operation can be cancelled by
173 * triggering the cancellable object from another thread. If the operation
174 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
175 * operation was partially finished when the operation was cancelled the
176 * partial result will be returned, without an error.
178 * On error -1 is returned and @error is set accordingly.
182 * Return value: Number of bytes written, or -1 on error
185 g_output_stream_write (GOutputStream
*stream
,
188 GCancellable
*cancellable
,
191 GOutputStreamClass
*class;
194 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), -1);
195 g_return_val_if_fail (buffer
!= NULL
, 0);
200 if (((gssize
) count
) < 0)
202 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
203 _("Too large count value passed to %s"), G_STRFUNC
);
207 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
209 if (class->write_fn
== NULL
)
211 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
212 _("Output stream doesn't implement write"));
216 if (!g_output_stream_set_pending (stream
, error
))
220 g_cancellable_push_current (cancellable
);
222 res
= class->write_fn (stream
, buffer
, count
, cancellable
, error
);
225 g_cancellable_pop_current (cancellable
);
227 g_output_stream_clear_pending (stream
);
233 * g_output_stream_write_all:
234 * @stream: a #GOutputStream.
235 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
236 * @count: the number of bytes to write
237 * @bytes_written: (out): location to store the number of bytes that was
238 * written to the stream
239 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
240 * @error: location to store the error occurring, or %NULL to ignore
242 * Tries to write @count bytes from @buffer into the stream. Will block
243 * during the operation.
245 * This function is similar to g_output_stream_write(), except it tries to
246 * write as many bytes as requested, only stopping on an error.
248 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
251 * If there is an error during the operation FALSE is returned and @error
252 * is set to indicate the error status, @bytes_written is updated to contain
253 * the number of bytes written into the stream before the error occurred.
255 * Return value: %TRUE on success, %FALSE if there was an error
258 g_output_stream_write_all (GOutputStream
*stream
,
261 gsize
*bytes_written
,
262 GCancellable
*cancellable
,
265 gsize _bytes_written
;
268 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
269 g_return_val_if_fail (buffer
!= NULL
, FALSE
);
272 while (_bytes_written
< count
)
274 res
= g_output_stream_write (stream
, (char *)buffer
+ _bytes_written
, count
- _bytes_written
,
279 *bytes_written
= _bytes_written
;
284 g_warning ("Write returned zero without error");
286 _bytes_written
+= res
;
290 *bytes_written
= _bytes_written
;
296 * g_output_stream_flush:
297 * @stream: a #GOutputStream.
298 * @cancellable: (allow-none): optional cancellable object
299 * @error: location to store the error occurring, or %NULL to ignore
301 * Forces a write of all user-space buffered data for the given
302 * @stream. Will block during the operation. Closing the stream will
303 * implicitly cause a flush.
305 * This function is optional for inherited classes.
307 * If @cancellable is not %NULL, then the operation can be cancelled by
308 * triggering the cancellable object from another thread. If the operation
309 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
311 * Return value: %TRUE on success, %FALSE on error
314 g_output_stream_flush (GOutputStream
*stream
,
315 GCancellable
*cancellable
,
318 GOutputStreamClass
*class;
321 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
323 if (!g_output_stream_set_pending (stream
, error
))
326 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
332 g_cancellable_push_current (cancellable
);
334 res
= class->flush (stream
, cancellable
, error
);
337 g_cancellable_pop_current (cancellable
);
340 g_output_stream_clear_pending (stream
);
346 * g_output_stream_splice:
347 * @stream: a #GOutputStream.
348 * @source: a #GInputStream.
349 * @flags: a set of #GOutputStreamSpliceFlags.
350 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
351 * @error: a #GError location to store the error occurring, or %NULL to
354 * Splices an input stream into an output stream.
356 * Returns: a #gssize containing the size of the data spliced, or
357 * -1 if an error occurred. Note that if the number of bytes
358 * spliced is greater than %G_MAXSSIZE, then that will be
359 * returned, and there is no way to determine the actual number
363 g_output_stream_splice (GOutputStream
*stream
,
364 GInputStream
*source
,
365 GOutputStreamSpliceFlags flags
,
366 GCancellable
*cancellable
,
369 GOutputStreamClass
*class;
372 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), -1);
373 g_return_val_if_fail (G_IS_INPUT_STREAM (source
), -1);
375 if (g_input_stream_is_closed (source
))
377 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_CLOSED
,
378 _("Source stream is already closed"));
382 if (!g_output_stream_set_pending (stream
, error
))
385 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
388 g_cancellable_push_current (cancellable
);
390 bytes_copied
= class->splice (stream
, source
, flags
, cancellable
, error
);
393 g_cancellable_pop_current (cancellable
);
395 g_output_stream_clear_pending (stream
);
401 g_output_stream_real_splice (GOutputStream
*stream
,
402 GInputStream
*source
,
403 GOutputStreamSpliceFlags flags
,
404 GCancellable
*cancellable
,
407 GOutputStreamClass
*class = G_OUTPUT_STREAM_GET_CLASS (stream
);
408 gssize n_read
, n_written
;
410 char buffer
[8192], *p
;
414 if (class->write_fn
== NULL
)
416 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
417 _("Output stream doesn't implement write"));
425 n_read
= g_input_stream_read (source
, buffer
, sizeof (buffer
), cancellable
, error
);
438 n_written
= class->write_fn (stream
, p
, n_read
, cancellable
, error
);
447 bytes_copied
+= n_written
;
450 if (bytes_copied
> G_MAXSSIZE
)
451 bytes_copied
= G_MAXSSIZE
;
457 error
= NULL
; /* Ignore further errors */
459 if (flags
& G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE
)
461 /* Don't care about errors in source here */
462 g_input_stream_close (source
, cancellable
, NULL
);
465 if (flags
& G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET
)
467 /* But write errors on close are bad! */
468 res
= _g_output_stream_close_internal (stream
, cancellable
, error
);
477 /* Must always be called inside
478 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
480 _g_output_stream_close_internal (GOutputStream
*stream
,
481 GCancellable
*cancellable
,
484 GOutputStreamClass
*class;
487 if (stream
->priv
->closed
)
490 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
492 stream
->priv
->closing
= TRUE
;
495 g_cancellable_push_current (cancellable
);
498 res
= class->flush (stream
, cancellable
, error
);
504 /* flushing caused the error that we want to return,
505 * but we still want to close the underlying stream if possible
508 class->close_fn (stream
, cancellable
, NULL
);
514 res
= class->close_fn (stream
, cancellable
, error
);
518 g_cancellable_pop_current (cancellable
);
520 stream
->priv
->closing
= FALSE
;
521 stream
->priv
->closed
= TRUE
;
527 * g_output_stream_close:
528 * @stream: A #GOutputStream.
529 * @cancellable: (allow-none): optional cancellable object
530 * @error: location to store the error occurring, or %NULL to ignore
532 * Closes the stream, releasing resources related to it.
534 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
535 * Closing a stream multiple times will not return an error.
537 * Closing a stream will automatically flush any outstanding buffers in the
540 * Streams will be automatically closed when the last reference
541 * is dropped, but you might want to call this function to make sure
542 * resources are released as early as possible.
544 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
545 * open after the stream is closed. See the documentation for the individual
546 * stream for details.
548 * On failure the first error that happened will be reported, but the close
549 * operation will finish as much as possible. A stream that failed to
550 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
551 * is important to check and report the error to the user, otherwise
552 * there might be a loss of data as all data might not be written.
554 * If @cancellable is not NULL, then the operation can be cancelled by
555 * triggering the cancellable object from another thread. If the operation
556 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
557 * Cancelling a close will still leave the stream closed, but there some streams
558 * can use a faster close that doesn't block to e.g. check errors. On
559 * cancellation (as with any error) there is no guarantee that all written
560 * data will reach the target.
562 * Return value: %TRUE on success, %FALSE on failure
565 g_output_stream_close (GOutputStream
*stream
,
566 GCancellable
*cancellable
,
571 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
573 if (stream
->priv
->closed
)
576 if (!g_output_stream_set_pending (stream
, error
))
579 res
= _g_output_stream_close_internal (stream
, cancellable
, error
);
581 g_output_stream_clear_pending (stream
);
587 async_ready_callback_wrapper (GObject
*source_object
,
591 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
593 g_output_stream_clear_pending (stream
);
594 if (stream
->priv
->outstanding_callback
)
595 (*stream
->priv
->outstanding_callback
) (source_object
, res
, user_data
);
596 g_object_unref (stream
);
601 GCancellable
*cancellable
;
607 async_ready_close_callback_wrapper (GObject
*source_object
,
611 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
612 CloseUserData
*data
= user_data
;
614 stream
->priv
->closing
= FALSE
;
615 stream
->priv
->closed
= TRUE
;
617 g_output_stream_clear_pending (stream
);
619 if (stream
->priv
->outstanding_callback
)
621 if (data
->flush_error
!= NULL
)
623 GSimpleAsyncResult
*err
;
625 err
= g_simple_async_result_new_take_error (source_object
,
626 stream
->priv
->outstanding_callback
,
629 data
->flush_error
= NULL
;
631 (*stream
->priv
->outstanding_callback
) (source_object
,
632 G_ASYNC_RESULT (err
),
634 g_object_unref (err
);
638 (*stream
->priv
->outstanding_callback
) (source_object
,
644 g_object_unref (stream
);
646 if (data
->cancellable
)
647 g_object_unref (data
->cancellable
);
649 if (data
->flush_error
)
650 g_error_free (data
->flush_error
);
652 g_slice_free (CloseUserData
, data
);
656 async_ready_close_flushed_callback_wrapper (GObject
*source_object
,
660 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
661 GOutputStreamClass
*class;
662 CloseUserData
*data
= user_data
;
663 GSimpleAsyncResult
*simple
;
665 /* propagate the possible error */
666 if (G_IS_SIMPLE_ASYNC_RESULT (res
))
668 simple
= G_SIMPLE_ASYNC_RESULT (res
);
669 g_simple_async_result_propagate_error (simple
, &data
->flush_error
);
672 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
674 /* we still close, even if there was a flush error */
675 class->close_async (stream
, data
->io_priority
, data
->cancellable
,
676 async_ready_close_callback_wrapper
, user_data
);
680 * g_output_stream_write_async:
681 * @stream: A #GOutputStream.
682 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
683 * @count: the number of bytes to write
684 * @io_priority: the io priority of the request.
685 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
686 * @callback: (scope async): callback to call when the request is satisfied
687 * @user_data: (closure): the data to pass to callback function
689 * Request an asynchronous write of @count bytes from @buffer into
690 * the stream. When the operation is finished @callback will be called.
691 * You can then call g_output_stream_write_finish() to get the result of the
694 * During an async request no other sync and async calls are allowed,
695 * and will result in %G_IO_ERROR_PENDING errors.
697 * A value of @count larger than %G_MAXSSIZE will cause a
698 * %G_IO_ERROR_INVALID_ARGUMENT error.
700 * On success, the number of bytes written will be passed to the
701 * @callback. It is not an error if this is not the same as the
702 * requested size, as it can happen e.g. on a partial I/O error,
703 * but generally we try to write as many bytes as requested.
705 * You are guaranteed that this method will never fail with
706 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
707 * method will just wait until this changes.
709 * Any outstanding I/O request with higher priority (lower numerical
710 * value) will be executed before an outstanding request with lower
711 * priority. Default priority is %G_PRIORITY_DEFAULT.
713 * The asyncronous methods have a default fallback that uses threads
714 * to implement asynchronicity, so they are optional for inheriting
715 * classes. However, if you override one you must override all.
717 * For the synchronous, blocking version of this function, see
718 * g_output_stream_write().
721 g_output_stream_write_async (GOutputStream
*stream
,
725 GCancellable
*cancellable
,
726 GAsyncReadyCallback callback
,
729 GOutputStreamClass
*class;
730 GSimpleAsyncResult
*simple
;
731 GError
*error
= NULL
;
733 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
734 g_return_if_fail (buffer
!= NULL
);
738 simple
= g_simple_async_result_new (G_OBJECT (stream
),
741 g_output_stream_write_async
);
742 g_simple_async_result_complete_in_idle (simple
);
743 g_object_unref (simple
);
747 if (((gssize
) count
) < 0)
749 g_simple_async_report_error_in_idle (G_OBJECT (stream
),
752 G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
753 _("Too large count value passed to %s"),
758 if (!g_output_stream_set_pending (stream
, &error
))
760 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
767 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
769 stream
->priv
->outstanding_callback
= callback
;
770 g_object_ref (stream
);
771 class->write_async (stream
, buffer
, count
, io_priority
, cancellable
,
772 async_ready_callback_wrapper
, user_data
);
776 * g_output_stream_write_finish:
777 * @stream: a #GOutputStream.
778 * @result: a #GAsyncResult.
779 * @error: a #GError location to store the error occurring, or %NULL to
782 * Finishes a stream write operation.
784 * Returns: a #gssize containing the number of bytes written to the stream.
787 g_output_stream_write_finish (GOutputStream
*stream
,
788 GAsyncResult
*result
,
791 GSimpleAsyncResult
*simple
;
792 GOutputStreamClass
*class;
794 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), -1);
795 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), -1);
797 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
799 simple
= G_SIMPLE_ASYNC_RESULT (result
);
800 if (g_simple_async_result_propagate_error (simple
, error
))
803 /* Special case writes of 0 bytes */
804 if (g_simple_async_result_get_source_tag (simple
) == g_output_stream_write_async
)
808 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
809 return class->write_finish (stream
, result
, error
);
813 GInputStream
*source
;
815 GAsyncReadyCallback callback
;
819 async_ready_splice_callback_wrapper (GObject
*source_object
,
823 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
824 SpliceUserData
*data
= _data
;
826 g_output_stream_clear_pending (stream
);
829 (*data
->callback
) (source_object
, res
, data
->user_data
);
831 g_object_unref (stream
);
832 g_object_unref (data
->source
);
837 * g_output_stream_splice_async:
838 * @stream: a #GOutputStream.
839 * @source: a #GInputStream.
840 * @flags: a set of #GOutputStreamSpliceFlags.
841 * @io_priority: the io priority of the request.
842 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
843 * @callback: (scope async): a #GAsyncReadyCallback.
844 * @user_data: (closure): user data passed to @callback.
846 * Splices a stream asynchronously.
847 * When the operation is finished @callback will be called.
848 * You can then call g_output_stream_splice_finish() to get the
849 * result of the operation.
851 * For the synchronous, blocking version of this function, see
852 * g_output_stream_splice().
855 g_output_stream_splice_async (GOutputStream
*stream
,
856 GInputStream
*source
,
857 GOutputStreamSpliceFlags flags
,
859 GCancellable
*cancellable
,
860 GAsyncReadyCallback callback
,
863 GOutputStreamClass
*class;
864 SpliceUserData
*data
;
865 GError
*error
= NULL
;
867 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
868 g_return_if_fail (G_IS_INPUT_STREAM (source
));
870 if (g_input_stream_is_closed (source
))
872 g_simple_async_report_error_in_idle (G_OBJECT (stream
),
875 G_IO_ERROR
, G_IO_ERROR_CLOSED
,
876 _("Source stream is already closed"));
880 if (!g_output_stream_set_pending (stream
, &error
))
882 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
889 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
891 data
= g_new0 (SpliceUserData
, 1);
892 data
->callback
= callback
;
893 data
->user_data
= user_data
;
894 data
->source
= g_object_ref (source
);
896 g_object_ref (stream
);
897 class->splice_async (stream
, source
, flags
, io_priority
, cancellable
,
898 async_ready_splice_callback_wrapper
, data
);
902 * g_output_stream_splice_finish:
903 * @stream: a #GOutputStream.
904 * @result: a #GAsyncResult.
905 * @error: a #GError location to store the error occurring, or %NULL to
908 * Finishes an asynchronous stream splice operation.
910 * Returns: a #gssize of the number of bytes spliced. Note that if the
911 * number of bytes spliced is greater than %G_MAXSSIZE, then that
912 * will be returned, and there is no way to determine the actual
913 * number of bytes spliced.
916 g_output_stream_splice_finish (GOutputStream
*stream
,
917 GAsyncResult
*result
,
920 GSimpleAsyncResult
*simple
;
921 GOutputStreamClass
*class;
923 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), -1);
924 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), -1);
926 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
928 simple
= G_SIMPLE_ASYNC_RESULT (result
);
929 if (g_simple_async_result_propagate_error (simple
, error
))
933 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
934 return class->splice_finish (stream
, result
, error
);
938 * g_output_stream_flush_async:
939 * @stream: a #GOutputStream.
940 * @io_priority: the io priority of the request.
941 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
942 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
943 * @user_data: (closure): the data to pass to callback function
945 * Forces an asynchronous write of all user-space buffered data for
947 * For behaviour details see g_output_stream_flush().
949 * When the operation is finished @callback will be
950 * called. You can then call g_output_stream_flush_finish() to get the
951 * result of the operation.
954 g_output_stream_flush_async (GOutputStream
*stream
,
956 GCancellable
*cancellable
,
957 GAsyncReadyCallback callback
,
960 GOutputStreamClass
*class;
961 GSimpleAsyncResult
*simple
;
962 GError
*error
= NULL
;
964 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
966 if (!g_output_stream_set_pending (stream
, &error
))
968 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
975 stream
->priv
->outstanding_callback
= callback
;
976 g_object_ref (stream
);
978 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
980 if (class->flush_async
== NULL
)
982 simple
= g_simple_async_result_new (G_OBJECT (stream
),
983 async_ready_callback_wrapper
,
985 g_output_stream_flush_async
);
986 g_simple_async_result_complete_in_idle (simple
);
987 g_object_unref (simple
);
991 class->flush_async (stream
, io_priority
, cancellable
,
992 async_ready_callback_wrapper
, user_data
);
996 * g_output_stream_flush_finish:
997 * @stream: a #GOutputStream.
998 * @result: a GAsyncResult.
999 * @error: a #GError location to store the error occurring, or %NULL to
1002 * Finishes flushing an output stream.
1004 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1007 g_output_stream_flush_finish (GOutputStream
*stream
,
1008 GAsyncResult
*result
,
1011 GSimpleAsyncResult
*simple
;
1012 GOutputStreamClass
*klass
;
1014 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1015 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
1017 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
1019 simple
= G_SIMPLE_ASYNC_RESULT (result
);
1020 if (g_simple_async_result_propagate_error (simple
, error
))
1023 /* Special case default implementation */
1024 if (g_simple_async_result_get_source_tag (simple
) == g_output_stream_flush_async
)
1028 klass
= G_OUTPUT_STREAM_GET_CLASS (stream
);
1029 return klass
->flush_finish (stream
, result
, error
);
1034 * g_output_stream_close_async:
1035 * @stream: A #GOutputStream.
1036 * @io_priority: the io priority of the request.
1037 * @cancellable: (allow-none): optional cancellable object
1038 * @callback: (scope async): callback to call when the request is satisfied
1039 * @user_data: (closure): the data to pass to callback function
1041 * Requests an asynchronous close of the stream, releasing resources
1042 * related to it. When the operation is finished @callback will be
1043 * called. You can then call g_output_stream_close_finish() to get
1044 * the result of the operation.
1046 * For behaviour details see g_output_stream_close().
1048 * The asyncronous methods have a default fallback that uses threads
1049 * to implement asynchronicity, so they are optional for inheriting
1050 * classes. However, if you override one you must override all.
1053 g_output_stream_close_async (GOutputStream
*stream
,
1055 GCancellable
*cancellable
,
1056 GAsyncReadyCallback callback
,
1059 GOutputStreamClass
*class;
1060 GSimpleAsyncResult
*simple
;
1061 GError
*error
= NULL
;
1062 CloseUserData
*data
;
1064 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
1066 if (stream
->priv
->closed
)
1068 simple
= g_simple_async_result_new (G_OBJECT (stream
),
1071 g_output_stream_close_async
);
1072 g_simple_async_result_complete_in_idle (simple
);
1073 g_object_unref (simple
);
1077 if (!g_output_stream_set_pending (stream
, &error
))
1079 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
1086 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
1087 stream
->priv
->closing
= TRUE
;
1088 stream
->priv
->outstanding_callback
= callback
;
1089 g_object_ref (stream
);
1091 data
= g_slice_new0 (CloseUserData
);
1093 if (cancellable
!= NULL
)
1094 data
->cancellable
= g_object_ref (cancellable
);
1096 data
->io_priority
= io_priority
;
1097 data
->user_data
= user_data
;
1099 /* Call close_async directly if there is no need to flush, or if the flush
1100 can be done sync (in the output stream async close thread) */
1101 if (class->flush_async
== NULL
||
1102 (class->flush_async
== g_output_stream_real_flush_async
&&
1103 (class->flush
== NULL
|| class->close_async
== g_output_stream_real_close_async
)))
1105 class->close_async (stream
, io_priority
, cancellable
,
1106 async_ready_close_callback_wrapper
, data
);
1110 /* First do an async flush, then do the async close in the callback
1111 wrapper (see async_ready_close_flushed_callback_wrapper) */
1112 class->flush_async (stream
, io_priority
, cancellable
,
1113 async_ready_close_flushed_callback_wrapper
, data
);
1118 * g_output_stream_close_finish:
1119 * @stream: a #GOutputStream.
1120 * @result: a #GAsyncResult.
1121 * @error: a #GError location to store the error occurring, or %NULL to
1124 * Closes an output stream.
1126 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1129 g_output_stream_close_finish (GOutputStream
*stream
,
1130 GAsyncResult
*result
,
1133 GSimpleAsyncResult
*simple
;
1134 GOutputStreamClass
*class;
1136 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1137 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
1139 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
1141 simple
= G_SIMPLE_ASYNC_RESULT (result
);
1142 if (g_simple_async_result_propagate_error (simple
, error
))
1145 /* Special case already closed */
1146 if (g_simple_async_result_get_source_tag (simple
) == g_output_stream_close_async
)
1150 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
1151 return class->close_finish (stream
, result
, error
);
1155 * g_output_stream_is_closed:
1156 * @stream: a #GOutputStream.
1158 * Checks if an output stream has already been closed.
1160 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1163 g_output_stream_is_closed (GOutputStream
*stream
)
1165 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), TRUE
);
1167 return stream
->priv
->closed
;
1171 * g_output_stream_is_closing:
1172 * @stream: a #GOutputStream.
1174 * Checks if an output stream is being closed. This can be
1175 * used inside e.g. a flush implementation to see if the
1176 * flush (or other i/o operation) is called from within
1177 * the closing operation.
1179 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1184 g_output_stream_is_closing (GOutputStream
*stream
)
1186 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), TRUE
);
1188 return stream
->priv
->closing
;
1192 * g_output_stream_has_pending:
1193 * @stream: a #GOutputStream.
1195 * Checks if an ouput stream has pending actions.
1197 * Returns: %TRUE if @stream has pending actions.
1200 g_output_stream_has_pending (GOutputStream
*stream
)
1202 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1204 return stream
->priv
->pending
;
1208 * g_output_stream_set_pending:
1209 * @stream: a #GOutputStream.
1210 * @error: a #GError location to store the error occurring, or %NULL to
1213 * Sets @stream to have actions pending. If the pending flag is
1214 * already set or @stream is closed, it will return %FALSE and set
1217 * Return value: %TRUE if pending was previously unset and is now set.
1220 g_output_stream_set_pending (GOutputStream
*stream
,
1223 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1225 if (stream
->priv
->closed
)
1227 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_CLOSED
,
1228 _("Stream is already closed"));
1232 if (stream
->priv
->pending
)
1234 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_PENDING
,
1235 /* Translators: This is an error you get if there is
1236 * already an operation running against this stream when
1237 * you try to start one */
1238 _("Stream has outstanding operation"));
1242 stream
->priv
->pending
= TRUE
;
1247 * g_output_stream_clear_pending:
1248 * @stream: output stream
1250 * Clears the pending flag on @stream.
1253 g_output_stream_clear_pending (GOutputStream
*stream
)
1255 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
1257 stream
->priv
->pending
= FALSE
;
1261 /********************************************
1262 * Default implementation of async ops *
1263 ********************************************/
1267 gsize count_requested
;
1268 gssize count_written
;
1272 write_async_thread (GSimpleAsyncResult
*res
,
1274 GCancellable
*cancellable
)
1277 GOutputStreamClass
*class;
1278 GError
*error
= NULL
;
1280 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1281 op
= g_simple_async_result_get_op_res_gpointer (res
);
1282 op
->count_written
= class->write_fn (G_OUTPUT_STREAM (object
), op
->buffer
, op
->count_requested
,
1283 cancellable
, &error
);
1284 if (op
->count_written
== -1)
1285 g_simple_async_result_take_error (res
, error
);
1289 g_output_stream_real_write_async (GOutputStream
*stream
,
1293 GCancellable
*cancellable
,
1294 GAsyncReadyCallback callback
,
1297 GSimpleAsyncResult
*res
;
1300 op
= g_new0 (WriteData
, 1);
1301 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_write_async
);
1302 g_simple_async_result_set_op_res_gpointer (res
, op
, g_free
);
1303 op
->buffer
= buffer
;
1304 op
->count_requested
= count
;
1306 g_simple_async_result_run_in_thread (res
, write_async_thread
, io_priority
, cancellable
);
1307 g_object_unref (res
);
1311 g_output_stream_real_write_finish (GOutputStream
*stream
,
1312 GAsyncResult
*result
,
1315 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1318 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_output_stream_real_write_async
);
1319 op
= g_simple_async_result_get_op_res_gpointer (simple
);
1320 return op
->count_written
;
1324 GInputStream
*source
;
1325 GOutputStreamSpliceFlags flags
;
1326 gssize bytes_copied
;
1330 splice_async_thread (GSimpleAsyncResult
*result
,
1332 GCancellable
*cancellable
)
1335 GOutputStreamClass
*class;
1336 GError
*error
= NULL
;
1337 GOutputStream
*stream
;
1339 stream
= G_OUTPUT_STREAM (object
);
1340 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1341 op
= g_simple_async_result_get_op_res_gpointer (result
);
1343 op
->bytes_copied
= class->splice (stream
,
1348 if (op
->bytes_copied
== -1)
1349 g_simple_async_result_take_error (result
, error
);
1353 g_output_stream_real_splice_async (GOutputStream
*stream
,
1354 GInputStream
*source
,
1355 GOutputStreamSpliceFlags flags
,
1357 GCancellable
*cancellable
,
1358 GAsyncReadyCallback callback
,
1361 GSimpleAsyncResult
*res
;
1364 op
= g_new0 (SpliceData
, 1);
1365 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_splice_async
);
1366 g_simple_async_result_set_op_res_gpointer (res
, op
, g_free
);
1368 op
->source
= source
;
1370 /* TODO: In the case where both source and destintion have
1371 non-threadbased async calls we can use a true async copy here */
1373 g_simple_async_result_run_in_thread (res
, splice_async_thread
, io_priority
, cancellable
);
1374 g_object_unref (res
);
1378 g_output_stream_real_splice_finish (GOutputStream
*stream
,
1379 GAsyncResult
*result
,
1382 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1385 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_output_stream_real_splice_async
);
1386 op
= g_simple_async_result_get_op_res_gpointer (simple
);
1387 return op
->bytes_copied
;
1392 flush_async_thread (GSimpleAsyncResult
*res
,
1394 GCancellable
*cancellable
)
1396 GOutputStreamClass
*class;
1398 GError
*error
= NULL
;
1400 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1403 result
= class->flush (G_OUTPUT_STREAM (object
), cancellable
, &error
);
1406 g_simple_async_result_take_error (res
, error
);
1410 g_output_stream_real_flush_async (GOutputStream
*stream
,
1412 GCancellable
*cancellable
,
1413 GAsyncReadyCallback callback
,
1416 GSimpleAsyncResult
*res
;
1418 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_write_async
);
1420 g_simple_async_result_run_in_thread (res
, flush_async_thread
, io_priority
, cancellable
);
1421 g_object_unref (res
);
1425 g_output_stream_real_flush_finish (GOutputStream
*stream
,
1426 GAsyncResult
*result
,
1433 close_async_thread (GSimpleAsyncResult
*res
,
1435 GCancellable
*cancellable
)
1437 GOutputStreamClass
*class;
1438 GError
*error
= NULL
;
1439 gboolean result
= TRUE
;
1441 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1443 /* Do a flush here if there is a flush function, and we did not have to do
1444 an async flush before (see g_output_stream_close_async) */
1445 if (class->flush
!= NULL
&&
1446 (class->flush_async
== NULL
||
1447 class->flush_async
== g_output_stream_real_flush_async
))
1449 result
= class->flush (G_OUTPUT_STREAM (object
), cancellable
, &error
);
1452 /* Auto handling of cancelation disabled, and ignore
1453 cancellation, since we want to close things anyway, although
1454 possibly in a quick-n-dirty way. At least we never want to leak
1457 if (class->close_fn
)
1459 /* Make sure to close, even if the flush failed (see sync close) */
1461 class->close_fn (G_OUTPUT_STREAM (object
), cancellable
, NULL
);
1463 result
= class->close_fn (G_OUTPUT_STREAM (object
), cancellable
, &error
);
1466 g_simple_async_result_take_error (res
, error
);
1471 g_output_stream_real_close_async (GOutputStream
*stream
,
1473 GCancellable
*cancellable
,
1474 GAsyncReadyCallback callback
,
1477 GSimpleAsyncResult
*res
;
1479 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_close_async
);
1481 g_simple_async_result_set_handle_cancellation (res
, FALSE
);
1483 g_simple_async_result_run_in_thread (res
, close_async_thread
, io_priority
, cancellable
);
1484 g_object_unref (res
);
1488 g_output_stream_real_close_finish (GOutputStream
*stream
,
1489 GAsyncResult
*result
,
1492 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1493 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_output_stream_real_close_async
);