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>
25 #include "goutputstream.h"
26 #include "gcancellable.h"
27 #include "gasyncresult.h"
29 #include "ginputstream.h"
31 #include "gioprivate.h"
33 #include "gpollableoutputstream.h"
36 * SECTION:goutputstream
37 * @short_description: Base class for implementing streaming output
40 * #GOutputStream has functions to write to a stream (g_output_stream_write()),
41 * to close a stream (g_output_stream_close()) and to flush pending writes
42 * (g_output_stream_flush()).
44 * To copy the content of an input stream to an output stream without
45 * manually handling the reads and writes, use g_output_stream_splice().
47 * All of these functions have async variants too.
50 struct _GOutputStreamPrivate {
54 GAsyncReadyCallback outstanding_callback;
57 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GOutputStream, g_output_stream, G_TYPE_OBJECT)
59 static gssize g_output_stream_real_splice (GOutputStream *stream,
61 GOutputStreamSpliceFlags flags,
62 GCancellable *cancellable,
64 static void g_output_stream_real_write_async (GOutputStream *stream,
68 GCancellable *cancellable,
69 GAsyncReadyCallback callback,
71 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
74 static void g_output_stream_real_splice_async (GOutputStream *stream,
76 GOutputStreamSpliceFlags flags,
78 GCancellable *cancellable,
79 GAsyncReadyCallback callback,
81 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
84 static void g_output_stream_real_flush_async (GOutputStream *stream,
86 GCancellable *cancellable,
87 GAsyncReadyCallback callback,
89 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
92 static void g_output_stream_real_close_async (GOutputStream *stream,
94 GCancellable *cancellable,
95 GAsyncReadyCallback callback,
97 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
100 static gboolean g_output_stream_internal_close (GOutputStream *stream,
101 GCancellable *cancellable,
103 static void g_output_stream_internal_close_async (GOutputStream *stream,
105 GCancellable *cancellable,
106 GAsyncReadyCallback callback,
108 static gboolean g_output_stream_internal_close_finish (GOutputStream *stream,
109 GAsyncResult *result,
113 g_output_stream_dispose (GObject *object)
115 GOutputStream *stream;
117 stream = G_OUTPUT_STREAM (object);
119 if (!stream->priv->closed)
120 g_output_stream_close (stream, NULL, NULL);
122 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
126 g_output_stream_class_init (GOutputStreamClass *klass)
128 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
130 gobject_class->dispose = g_output_stream_dispose;
132 klass->splice = g_output_stream_real_splice;
134 klass->write_async = g_output_stream_real_write_async;
135 klass->write_finish = g_output_stream_real_write_finish;
136 klass->splice_async = g_output_stream_real_splice_async;
137 klass->splice_finish = g_output_stream_real_splice_finish;
138 klass->flush_async = g_output_stream_real_flush_async;
139 klass->flush_finish = g_output_stream_real_flush_finish;
140 klass->close_async = g_output_stream_real_close_async;
141 klass->close_finish = g_output_stream_real_close_finish;
145 g_output_stream_init (GOutputStream *stream)
147 stream->priv = g_output_stream_get_instance_private (stream);
151 * g_output_stream_write:
152 * @stream: a #GOutputStream.
153 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
154 * @count: the number of bytes to write
155 * @cancellable: (allow-none): optional cancellable object
156 * @error: location to store the error occurring, or %NULL to ignore
158 * Tries to write @count bytes from @buffer into the stream. Will block
159 * during the operation.
161 * If count is 0, returns 0 and does nothing. A value of @count
162 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
164 * On success, the number of bytes written to the stream is returned.
165 * It is not an error if this is not the same as the requested size, as it
166 * can happen e.g. on a partial I/O error, or if there is not enough
167 * storage in the stream. All writes block until at least one byte
168 * is written or an error occurs; 0 is never returned (unless
171 * If @cancellable is not %NULL, then the operation can be cancelled by
172 * triggering the cancellable object from another thread. If the operation
173 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
174 * operation was partially finished when the operation was cancelled the
175 * partial result will be returned, without an error.
177 * On error -1 is returned and @error is set accordingly.
181 * Return value: Number of bytes written, or -1 on error
184 g_output_stream_write (GOutputStream *stream,
187 GCancellable *cancellable,
190 GOutputStreamClass *class;
193 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
194 g_return_val_if_fail (buffer != NULL, 0);
199 if (((gssize) count) < 0)
201 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
202 _("Too large count value passed to %s"), G_STRFUNC);
206 class = G_OUTPUT_STREAM_GET_CLASS (stream);
208 if (class->write_fn == NULL)
210 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
211 _("Output stream doesn't implement write"));
215 if (!g_output_stream_set_pending (stream, error))
219 g_cancellable_push_current (cancellable);
221 res = class->write_fn (stream, buffer, count, cancellable, error);
224 g_cancellable_pop_current (cancellable);
226 g_output_stream_clear_pending (stream);
232 * g_output_stream_write_all:
233 * @stream: a #GOutputStream.
234 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
235 * @count: the number of bytes to write
236 * @bytes_written: (out): location to store the number of bytes that was
237 * written to the stream
238 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
239 * @error: location to store the error occurring, or %NULL to ignore
241 * Tries to write @count bytes from @buffer into the stream. Will block
242 * during the operation.
244 * This function is similar to g_output_stream_write(), except it tries to
245 * write as many bytes as requested, only stopping on an error.
247 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
250 * If there is an error during the operation %FALSE is returned and @error
251 * is set to indicate the error status, @bytes_written is updated to contain
252 * the number of bytes written into the stream before the error occurred.
254 * Return value: %TRUE on success, %FALSE if there was an error
257 g_output_stream_write_all (GOutputStream *stream,
260 gsize *bytes_written,
261 GCancellable *cancellable,
264 gsize _bytes_written;
267 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
268 g_return_val_if_fail (buffer != NULL, FALSE);
271 while (_bytes_written < count)
273 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
278 *bytes_written = _bytes_written;
283 g_warning ("Write returned zero without error");
285 _bytes_written += res;
289 *bytes_written = _bytes_written;
295 * g_output_stream_printf:
296 * @stream: a #GOutputStream.
297 * @bytes_written: (out): location to store the number of bytes that was
298 * written to the stream
299 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
300 * @error: location to store the error occurring, or %NULL to ignore
301 * @format: the format string. See the printf() documentation
302 * @...: the parameters to insert into the format string
304 * This is a utility function around g_output_stream_write_all(). It
305 * uses g_strdup_vprintf() to turn @format and @... into a string that
306 * is then written to @stream.
308 * See the documentation of g_output_stream_write_all() about the
309 * behavior of the actual write operation.
311 * Note that partial writes cannot be properly checked with this
312 * function due to the variable length of the written string, if you
313 * need precise control over partial write failures, you need to
314 * create you own printf()-like wrapper around g_output_stream_write()
315 * or g_output_stream_write_all().
319 * Return value: %TRUE on success, %FALSE if there was an error
322 g_output_stream_printf (GOutputStream *stream,
323 gsize *bytes_written,
324 GCancellable *cancellable,
332 va_start (args, format);
333 success = g_output_stream_vprintf (stream, bytes_written, cancellable,
334 error, format, args);
341 * g_output_stream_vprintf:
342 * @stream: a #GOutputStream.
343 * @bytes_written: (out): location to store the number of bytes that was
344 * written to the stream
345 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
346 * @error: location to store the error occurring, or %NULL to ignore
347 * @format: the format string. See the printf() documentation
348 * @args: the parameters to insert into the format string
350 * This is a utility function around g_output_stream_write_all(). It
351 * uses g_strdup_vprintf() to turn @format and @args into a string that
352 * is then written to @stream.
354 * See the documentation of g_output_stream_write_all() about the
355 * behavior of the actual write operation.
357 * Note that partial writes cannot be properly checked with this
358 * function due to the variable length of the written string, if you
359 * need precise control over partial write failures, you need to
360 * create you own printf()-like wrapper around g_output_stream_write()
361 * or g_output_stream_write_all().
365 * Return value: %TRUE on success, %FALSE if there was an error
368 g_output_stream_vprintf (GOutputStream *stream,
369 gsize *bytes_written,
370 GCancellable *cancellable,
378 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
379 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (stream), FALSE);
380 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
381 g_return_val_if_fail (format != NULL, FALSE);
383 text = g_strdup_vprintf (format, args);
384 success = g_output_stream_write_all (stream,
386 bytes_written, cancellable, error);
393 * g_output_stream_write_bytes:
394 * @stream: a #GOutputStream.
395 * @bytes: the #GBytes to write
396 * @cancellable: (allow-none): optional cancellable object
397 * @error: location to store the error occurring, or %NULL to ignore
399 * A wrapper function for g_output_stream_write() which takes a
400 * #GBytes as input. This can be more convenient for use by language
401 * bindings or in other cases where the refcounted nature of #GBytes
402 * is helpful over a bare pointer interface.
404 * However, note that this function <emphasis>may</emphasis> still
405 * perform partial writes, just like g_output_stream_write(). If that
406 * occurs, to continue writing, you will need to create a new #GBytes
407 * containing just the remaining bytes, using
408 * g_bytes_new_from_bytes(). Passing the same #GBytes instance
409 * multiple times potentially can result in duplicated data in the
412 * Return value: Number of bytes written, or -1 on error
415 g_output_stream_write_bytes (GOutputStream *stream,
417 GCancellable *cancellable,
423 data = g_bytes_get_data (bytes, &size);
425 return g_output_stream_write (stream,
432 * g_output_stream_flush:
433 * @stream: a #GOutputStream.
434 * @cancellable: (allow-none): optional cancellable object
435 * @error: location to store the error occurring, or %NULL to ignore
437 * Forces a write of all user-space buffered data for the given
438 * @stream. Will block during the operation. Closing the stream will
439 * implicitly cause a flush.
441 * This function is optional for inherited classes.
443 * If @cancellable is not %NULL, then the operation can be cancelled by
444 * triggering the cancellable object from another thread. If the operation
445 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
447 * Return value: %TRUE on success, %FALSE on error
450 g_output_stream_flush (GOutputStream *stream,
451 GCancellable *cancellable,
454 GOutputStreamClass *class;
457 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
459 if (!g_output_stream_set_pending (stream, error))
462 class = G_OUTPUT_STREAM_GET_CLASS (stream);
468 g_cancellable_push_current (cancellable);
470 res = class->flush (stream, cancellable, error);
473 g_cancellable_pop_current (cancellable);
476 g_output_stream_clear_pending (stream);
482 * g_output_stream_splice:
483 * @stream: a #GOutputStream.
484 * @source: a #GInputStream.
485 * @flags: a set of #GOutputStreamSpliceFlags.
486 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
487 * @error: a #GError location to store the error occurring, or %NULL to
490 * Splices an input stream into an output stream.
492 * Returns: a #gssize containing the size of the data spliced, or
493 * -1 if an error occurred. Note that if the number of bytes
494 * spliced is greater than %G_MAXSSIZE, then that will be
495 * returned, and there is no way to determine the actual number
499 g_output_stream_splice (GOutputStream *stream,
500 GInputStream *source,
501 GOutputStreamSpliceFlags flags,
502 GCancellable *cancellable,
505 GOutputStreamClass *class;
508 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
509 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
511 if (g_input_stream_is_closed (source))
513 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
514 _("Source stream is already closed"));
518 if (!g_output_stream_set_pending (stream, error))
521 class = G_OUTPUT_STREAM_GET_CLASS (stream);
524 g_cancellable_push_current (cancellable);
526 bytes_copied = class->splice (stream, source, flags, cancellable, error);
529 g_cancellable_pop_current (cancellable);
531 g_output_stream_clear_pending (stream);
537 g_output_stream_real_splice (GOutputStream *stream,
538 GInputStream *source,
539 GOutputStreamSpliceFlags flags,
540 GCancellable *cancellable,
543 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
544 gssize n_read, n_written;
546 char buffer[8192], *p;
550 if (class->write_fn == NULL)
552 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
553 _("Output stream doesn't implement write"));
561 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
574 n_written = class->write_fn (stream, p, n_read, cancellable, error);
583 bytes_copied += n_written;
586 if (bytes_copied > G_MAXSSIZE)
587 bytes_copied = G_MAXSSIZE;
593 error = NULL; /* Ignore further errors */
595 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
597 /* Don't care about errors in source here */
598 g_input_stream_close (source, cancellable, NULL);
601 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
603 /* But write errors on close are bad! */
604 res = g_output_stream_internal_close (stream, cancellable, error);
613 /* Must always be called inside
614 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
616 g_output_stream_internal_close (GOutputStream *stream,
617 GCancellable *cancellable,
620 GOutputStreamClass *class;
623 if (stream->priv->closed)
626 class = G_OUTPUT_STREAM_GET_CLASS (stream);
628 stream->priv->closing = TRUE;
631 g_cancellable_push_current (cancellable);
634 res = class->flush (stream, cancellable, error);
640 /* flushing caused the error that we want to return,
641 * but we still want to close the underlying stream if possible
644 class->close_fn (stream, cancellable, NULL);
650 res = class->close_fn (stream, cancellable, error);
654 g_cancellable_pop_current (cancellable);
656 stream->priv->closing = FALSE;
657 stream->priv->closed = TRUE;
663 * g_output_stream_close:
664 * @stream: A #GOutputStream.
665 * @cancellable: (allow-none): optional cancellable object
666 * @error: location to store the error occurring, or %NULL to ignore
668 * Closes the stream, releasing resources related to it.
670 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
671 * Closing a stream multiple times will not return an error.
673 * Closing a stream will automatically flush any outstanding buffers in the
676 * Streams will be automatically closed when the last reference
677 * is dropped, but you might want to call this function to make sure
678 * resources are released as early as possible.
680 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
681 * open after the stream is closed. See the documentation for the individual
682 * stream for details.
684 * On failure the first error that happened will be reported, but the close
685 * operation will finish as much as possible. A stream that failed to
686 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
687 * is important to check and report the error to the user, otherwise
688 * there might be a loss of data as all data might not be written.
690 * If @cancellable is not %NULL, then the operation can be cancelled by
691 * triggering the cancellable object from another thread. If the operation
692 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
693 * Cancelling a close will still leave the stream closed, but there some streams
694 * can use a faster close that doesn't block to e.g. check errors. On
695 * cancellation (as with any error) there is no guarantee that all written
696 * data will reach the target.
698 * Return value: %TRUE on success, %FALSE on failure
701 g_output_stream_close (GOutputStream *stream,
702 GCancellable *cancellable,
707 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
709 if (stream->priv->closed)
712 if (!g_output_stream_set_pending (stream, error))
715 res = g_output_stream_internal_close (stream, cancellable, error);
717 g_output_stream_clear_pending (stream);
723 async_ready_write_callback_wrapper (GObject *source_object,
727 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
728 GOutputStreamClass *class;
729 GTask *task = user_data;
731 GError *error = NULL;
733 g_output_stream_clear_pending (stream);
735 if (g_async_result_legacy_propagate_error (res, &error))
739 class = G_OUTPUT_STREAM_GET_CLASS (stream);
740 nwrote = class->write_finish (stream, res, &error);
744 g_task_return_int (task, nwrote);
746 g_task_return_error (task, error);
747 g_object_unref (task);
751 * g_output_stream_write_async:
752 * @stream: A #GOutputStream.
753 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
754 * @count: the number of bytes to write
755 * @io_priority: the io priority of the request.
756 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
757 * @callback: (scope async): callback to call when the request is satisfied
758 * @user_data: (closure): the data to pass to callback function
760 * Request an asynchronous write of @count bytes from @buffer into
761 * the stream. When the operation is finished @callback will be called.
762 * You can then call g_output_stream_write_finish() to get the result of the
765 * During an async request no other sync and async calls are allowed,
766 * and will result in %G_IO_ERROR_PENDING errors.
768 * A value of @count larger than %G_MAXSSIZE will cause a
769 * %G_IO_ERROR_INVALID_ARGUMENT error.
771 * On success, the number of bytes written will be passed to the
772 * @callback. It is not an error if this is not the same as the
773 * requested size, as it can happen e.g. on a partial I/O error,
774 * but generally we try to write as many bytes as requested.
776 * You are guaranteed that this method will never fail with
777 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
778 * method will just wait until this changes.
780 * Any outstanding I/O request with higher priority (lower numerical
781 * value) will be executed before an outstanding request with lower
782 * priority. Default priority is %G_PRIORITY_DEFAULT.
784 * The asyncronous methods have a default fallback that uses threads
785 * to implement asynchronicity, so they are optional for inheriting
786 * classes. However, if you override one you must override all.
788 * For the synchronous, blocking version of this function, see
789 * g_output_stream_write().
791 * <warning><para>No copy of @buffer will be made, so it must stay valid until
792 * @callback is called. See g_output_stream_write_bytes_async() for a #GBytes
793 * version that will automatically hold a reference to the contents (without
794 * copying) for the duration of the call.</para></warning>
797 g_output_stream_write_async (GOutputStream *stream,
801 GCancellable *cancellable,
802 GAsyncReadyCallback callback,
805 GOutputStreamClass *class;
806 GError *error = NULL;
809 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
810 g_return_if_fail (buffer != NULL);
812 task = g_task_new (stream, cancellable, callback, user_data);
813 g_task_set_source_tag (task, g_output_stream_write_async);
814 g_task_set_priority (task, io_priority);
818 g_task_return_int (task, 0);
819 g_object_unref (task);
823 if (((gssize) count) < 0)
825 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
826 _("Too large count value passed to %s"),
828 g_object_unref (task);
832 if (!g_output_stream_set_pending (stream, &error))
834 g_task_return_error (task, error);
835 g_object_unref (task);
839 class = G_OUTPUT_STREAM_GET_CLASS (stream);
841 class->write_async (stream, buffer, count, io_priority, cancellable,
842 async_ready_write_callback_wrapper, task);
846 * g_output_stream_write_finish:
847 * @stream: a #GOutputStream.
848 * @result: a #GAsyncResult.
849 * @error: a #GError location to store the error occurring, or %NULL to
852 * Finishes a stream write operation.
854 * Returns: a #gssize containing the number of bytes written to the stream.
857 g_output_stream_write_finish (GOutputStream *stream,
858 GAsyncResult *result,
861 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
862 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
863 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
865 /* @result is always the GTask created by g_output_stream_write_async();
866 * we called class->write_finish() from async_ready_write_callback_wrapper.
868 return g_task_propagate_int (G_TASK (result), error);
872 write_bytes_callback (GObject *stream,
873 GAsyncResult *result,
876 GTask *task = user_data;
877 GError *error = NULL;
880 nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
883 g_task_return_error (task, error);
885 g_task_return_int (task, nwrote);
886 g_object_unref (task);
890 * g_output_stream_write_bytes_async:
891 * @stream: A #GOutputStream.
892 * @bytes: The bytes to write
893 * @io_priority: the io priority of the request.
894 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
895 * @callback: (scope async): callback to call when the request is satisfied
896 * @user_data: (closure): the data to pass to callback function
898 * This function is similar to g_output_stream_write_async(), but
899 * takes a #GBytes as input. Due to the refcounted nature of #GBytes,
900 * this allows the stream to avoid taking a copy of the data.
902 * However, note that this function <emphasis>may</emphasis> still
903 * perform partial writes, just like g_output_stream_write_async().
904 * If that occurs, to continue writing, you will need to create a new
905 * #GBytes containing just the remaining bytes, using
906 * g_bytes_new_from_bytes(). Passing the same #GBytes instance
907 * multiple times potentially can result in duplicated data in the
910 * For the synchronous, blocking version of this function, see
911 * g_output_stream_write_bytes().
914 g_output_stream_write_bytes_async (GOutputStream *stream,
917 GCancellable *cancellable,
918 GAsyncReadyCallback callback,
925 data = g_bytes_get_data (bytes, &size);
927 task = g_task_new (stream, cancellable, callback, user_data);
928 g_task_set_task_data (task, g_bytes_ref (bytes),
929 (GDestroyNotify) g_bytes_unref);
931 g_output_stream_write_async (stream,
935 write_bytes_callback,
940 * g_output_stream_write_bytes_finish:
941 * @stream: a #GOutputStream.
942 * @result: a #GAsyncResult.
943 * @error: a #GError location to store the error occurring, or %NULL to
946 * Finishes a stream write-from-#GBytes operation.
948 * Returns: a #gssize containing the number of bytes written to the stream.
951 g_output_stream_write_bytes_finish (GOutputStream *stream,
952 GAsyncResult *result,
955 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
956 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
958 return g_task_propagate_int (G_TASK (result), error);
962 async_ready_splice_callback_wrapper (GObject *source_object,
966 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
967 GOutputStreamClass *class;
970 GError *error = NULL;
972 g_output_stream_clear_pending (stream);
974 if (g_async_result_legacy_propagate_error (res, &error))
978 class = G_OUTPUT_STREAM_GET_CLASS (stream);
979 nspliced = class->splice_finish (stream, res, &error);
983 g_task_return_int (task, nspliced);
985 g_task_return_error (task, error);
986 g_object_unref (task);
990 * g_output_stream_splice_async:
991 * @stream: a #GOutputStream.
992 * @source: a #GInputStream.
993 * @flags: a set of #GOutputStreamSpliceFlags.
994 * @io_priority: the io priority of the request.
995 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
996 * @callback: (scope async): a #GAsyncReadyCallback.
997 * @user_data: (closure): user data passed to @callback.
999 * Splices a stream asynchronously.
1000 * When the operation is finished @callback will be called.
1001 * You can then call g_output_stream_splice_finish() to get the
1002 * result of the operation.
1004 * For the synchronous, blocking version of this function, see
1005 * g_output_stream_splice().
1008 g_output_stream_splice_async (GOutputStream *stream,
1009 GInputStream *source,
1010 GOutputStreamSpliceFlags flags,
1012 GCancellable *cancellable,
1013 GAsyncReadyCallback callback,
1016 GOutputStreamClass *class;
1018 GError *error = NULL;
1020 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1021 g_return_if_fail (G_IS_INPUT_STREAM (source));
1023 task = g_task_new (stream, cancellable, callback, user_data);
1024 g_task_set_source_tag (task, g_output_stream_splice_async);
1025 g_task_set_priority (task, io_priority);
1026 g_task_set_task_data (task, g_object_ref (source), g_object_unref);
1028 if (g_input_stream_is_closed (source))
1030 g_task_return_new_error (task,
1031 G_IO_ERROR, G_IO_ERROR_CLOSED,
1032 _("Source stream is already closed"));
1033 g_object_unref (task);
1037 if (!g_output_stream_set_pending (stream, &error))
1039 g_task_return_error (task, error);
1040 g_object_unref (task);
1044 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1046 class->splice_async (stream, source, flags, io_priority, cancellable,
1047 async_ready_splice_callback_wrapper, task);
1051 * g_output_stream_splice_finish:
1052 * @stream: a #GOutputStream.
1053 * @result: a #GAsyncResult.
1054 * @error: a #GError location to store the error occurring, or %NULL to
1057 * Finishes an asynchronous stream splice operation.
1059 * Returns: a #gssize of the number of bytes spliced. Note that if the
1060 * number of bytes spliced is greater than %G_MAXSSIZE, then that
1061 * will be returned, and there is no way to determine the actual
1062 * number of bytes spliced.
1065 g_output_stream_splice_finish (GOutputStream *stream,
1066 GAsyncResult *result,
1069 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1070 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1071 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
1073 /* @result is always the GTask created by g_output_stream_splice_async();
1074 * we called class->splice_finish() from async_ready_splice_callback_wrapper.
1076 return g_task_propagate_int (G_TASK (result), error);
1080 async_ready_flush_callback_wrapper (GObject *source_object,
1084 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1085 GOutputStreamClass *class;
1086 GTask *task = user_data;
1088 GError *error = NULL;
1090 g_output_stream_clear_pending (stream);
1092 if (g_async_result_legacy_propagate_error (res, &error))
1096 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1097 flushed = class->flush_finish (stream, res, &error);
1101 g_task_return_boolean (task, TRUE);
1103 g_task_return_error (task, error);
1104 g_object_unref (task);
1108 * g_output_stream_flush_async:
1109 * @stream: a #GOutputStream.
1110 * @io_priority: the io priority of the request.
1111 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1112 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1113 * @user_data: (closure): the data to pass to callback function
1115 * Forces an asynchronous write of all user-space buffered data for
1116 * the given @stream.
1117 * For behaviour details see g_output_stream_flush().
1119 * When the operation is finished @callback will be
1120 * called. You can then call g_output_stream_flush_finish() to get the
1121 * result of the operation.
1124 g_output_stream_flush_async (GOutputStream *stream,
1126 GCancellable *cancellable,
1127 GAsyncReadyCallback callback,
1130 GOutputStreamClass *class;
1132 GError *error = NULL;
1134 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1136 task = g_task_new (stream, cancellable, callback, user_data);
1137 g_task_set_source_tag (task, g_output_stream_flush_async);
1138 g_task_set_priority (task, io_priority);
1140 if (!g_output_stream_set_pending (stream, &error))
1142 g_task_return_error (task, error);
1143 g_object_unref (task);
1147 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1149 if (class->flush_async == NULL)
1151 g_task_return_boolean (task, TRUE);
1152 g_object_unref (task);
1156 class->flush_async (stream, io_priority, cancellable,
1157 async_ready_flush_callback_wrapper, task);
1161 * g_output_stream_flush_finish:
1162 * @stream: a #GOutputStream.
1163 * @result: a GAsyncResult.
1164 * @error: a #GError location to store the error occurring, or %NULL to
1167 * Finishes flushing an output stream.
1169 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1172 g_output_stream_flush_finish (GOutputStream *stream,
1173 GAsyncResult *result,
1176 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1177 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1178 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
1180 /* @result is always the GTask created by g_output_stream_flush_async();
1181 * we called class->flush_finish() from async_ready_flush_callback_wrapper.
1183 return g_task_propagate_boolean (G_TASK (result), error);
1188 async_ready_close_callback_wrapper (GObject *source_object,
1192 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1193 GOutputStreamClass *class;
1194 GTask *task = user_data;
1195 GError *error = g_task_get_task_data (task);
1197 stream->priv->closing = FALSE;
1198 stream->priv->closed = TRUE;
1200 if (!error && !g_async_result_legacy_propagate_error (res, &error))
1202 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1204 class->close_finish (stream, res,
1205 error ? NULL : &error);
1209 g_task_return_error (task, error);
1211 g_task_return_boolean (task, TRUE);
1212 g_object_unref (task);
1216 async_ready_close_flushed_callback_wrapper (GObject *source_object,
1220 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1221 GOutputStreamClass *class;
1222 GTask *task = user_data;
1223 GError *error = NULL;
1225 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1227 if (!g_async_result_legacy_propagate_error (res, &error))
1229 class->flush_finish (stream, res, &error);
1232 /* propagate the possible error */
1234 g_task_set_task_data (task, error, NULL);
1236 /* we still close, even if there was a flush error */
1237 class->close_async (stream,
1238 g_task_get_priority (task),
1239 g_task_get_cancellable (task),
1240 async_ready_close_callback_wrapper, task);
1244 real_close_async_cb (GObject *source_object,
1248 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1249 GTask *task = user_data;
1250 GError *error = NULL;
1253 g_output_stream_clear_pending (stream);
1255 ret = g_output_stream_internal_close_finish (stream, res, &error);
1258 g_task_return_error (task, error);
1260 g_task_return_boolean (task, ret);
1262 g_object_unref (task);
1266 * g_output_stream_close_async:
1267 * @stream: A #GOutputStream.
1268 * @io_priority: the io priority of the request.
1269 * @cancellable: (allow-none): optional cancellable object
1270 * @callback: (scope async): callback to call when the request is satisfied
1271 * @user_data: (closure): the data to pass to callback function
1273 * Requests an asynchronous close of the stream, releasing resources
1274 * related to it. When the operation is finished @callback will be
1275 * called. You can then call g_output_stream_close_finish() to get
1276 * the result of the operation.
1278 * For behaviour details see g_output_stream_close().
1280 * The asyncronous methods have a default fallback that uses threads
1281 * to implement asynchronicity, so they are optional for inheriting
1282 * classes. However, if you override one you must override all.
1285 g_output_stream_close_async (GOutputStream *stream,
1287 GCancellable *cancellable,
1288 GAsyncReadyCallback callback,
1292 GError *error = NULL;
1294 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1296 task = g_task_new (stream, cancellable, callback, user_data);
1297 g_task_set_source_tag (task, g_output_stream_close_async);
1298 g_task_set_priority (task, io_priority);
1300 if (!g_output_stream_set_pending (stream, &error))
1302 g_task_return_error (task, error);
1303 g_object_unref (task);
1307 g_output_stream_internal_close_async (stream, io_priority, cancellable,
1308 real_close_async_cb, task);
1311 /* Must always be called inside
1312 * g_output_stream_set_pending()/g_output_stream_clear_pending().
1315 g_output_stream_internal_close_async (GOutputStream *stream,
1317 GCancellable *cancellable,
1318 GAsyncReadyCallback callback,
1321 GOutputStreamClass *class;
1324 task = g_task_new (stream, cancellable, callback, user_data);
1325 g_task_set_source_tag (task, g_output_stream_internal_close_async);
1326 g_task_set_priority (task, io_priority);
1328 if (stream->priv->closed)
1330 g_task_return_boolean (task, TRUE);
1331 g_object_unref (task);
1335 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1336 stream->priv->closing = TRUE;
1338 /* Call close_async directly if there is no need to flush, or if the flush
1339 can be done sync (in the output stream async close thread) */
1340 if (class->flush_async == NULL ||
1341 (class->flush_async == g_output_stream_real_flush_async &&
1342 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1344 class->close_async (stream, io_priority, cancellable,
1345 async_ready_close_callback_wrapper, task);
1349 /* First do an async flush, then do the async close in the callback
1350 wrapper (see async_ready_close_flushed_callback_wrapper) */
1351 class->flush_async (stream, io_priority, cancellable,
1352 async_ready_close_flushed_callback_wrapper, task);
1357 g_output_stream_internal_close_finish (GOutputStream *stream,
1358 GAsyncResult *result,
1361 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1362 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1363 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
1365 return g_task_propagate_boolean (G_TASK (result), error);
1369 * g_output_stream_close_finish:
1370 * @stream: a #GOutputStream.
1371 * @result: a #GAsyncResult.
1372 * @error: a #GError location to store the error occurring, or %NULL to
1375 * Closes an output stream.
1377 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1380 g_output_stream_close_finish (GOutputStream *stream,
1381 GAsyncResult *result,
1384 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1385 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1386 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
1388 /* @result is always the GTask created by g_output_stream_close_async();
1389 * we called class->close_finish() from async_ready_close_callback_wrapper.
1391 return g_task_propagate_boolean (G_TASK (result), error);
1395 * g_output_stream_is_closed:
1396 * @stream: a #GOutputStream.
1398 * Checks if an output stream has already been closed.
1400 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1403 g_output_stream_is_closed (GOutputStream *stream)
1405 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1407 return stream->priv->closed;
1411 * g_output_stream_is_closing:
1412 * @stream: a #GOutputStream.
1414 * Checks if an output stream is being closed. This can be
1415 * used inside e.g. a flush implementation to see if the
1416 * flush (or other i/o operation) is called from within
1417 * the closing operation.
1419 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1424 g_output_stream_is_closing (GOutputStream *stream)
1426 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1428 return stream->priv->closing;
1432 * g_output_stream_has_pending:
1433 * @stream: a #GOutputStream.
1435 * Checks if an ouput stream has pending actions.
1437 * Returns: %TRUE if @stream has pending actions.
1440 g_output_stream_has_pending (GOutputStream *stream)
1442 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1444 return stream->priv->pending;
1448 * g_output_stream_set_pending:
1449 * @stream: a #GOutputStream.
1450 * @error: a #GError location to store the error occurring, or %NULL to
1453 * Sets @stream to have actions pending. If the pending flag is
1454 * already set or @stream is closed, it will return %FALSE and set
1457 * Return value: %TRUE if pending was previously unset and is now set.
1460 g_output_stream_set_pending (GOutputStream *stream,
1463 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1465 if (stream->priv->closed)
1467 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1468 _("Stream is already closed"));
1472 if (stream->priv->pending)
1474 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1475 /* Translators: This is an error you get if there is
1476 * already an operation running against this stream when
1477 * you try to start one */
1478 _("Stream has outstanding operation"));
1482 stream->priv->pending = TRUE;
1487 * g_output_stream_clear_pending:
1488 * @stream: output stream
1490 * Clears the pending flag on @stream.
1493 g_output_stream_clear_pending (GOutputStream *stream)
1495 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1497 stream->priv->pending = FALSE;
1501 * g_output_stream_async_write_is_via_threads:
1502 * @stream: a #GOutputStream.
1504 * Checks if an ouput stream's write_async function uses threads.
1506 * Returns: %TRUE if @stream's write_async function uses threads.
1509 g_output_stream_async_write_is_via_threads (GOutputStream *stream)
1511 GOutputStreamClass *class;
1513 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1515 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1517 return (class->write_async == g_output_stream_real_write_async &&
1518 !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
1519 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
1523 /********************************************
1524 * Default implementation of async ops *
1525 ********************************************/
1529 gsize count_requested;
1530 gssize count_written;
1534 free_write_data (WriteData *op)
1536 g_slice_free (WriteData, op);
1540 write_async_thread (GTask *task,
1541 gpointer source_object,
1543 GCancellable *cancellable)
1545 GOutputStream *stream = source_object;
1546 WriteData *op = task_data;
1547 GOutputStreamClass *class;
1548 GError *error = NULL;
1549 gssize count_written;
1551 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1552 count_written = class->write_fn (stream, op->buffer, op->count_requested,
1553 cancellable, &error);
1554 if (count_written == -1)
1555 g_task_return_error (task, error);
1557 g_task_return_int (task, count_written);
1560 static void write_async_pollable (GPollableOutputStream *stream,
1564 write_async_pollable_ready (GPollableOutputStream *stream,
1567 GTask *task = user_data;
1569 write_async_pollable (stream, task);
1574 write_async_pollable (GPollableOutputStream *stream,
1577 GError *error = NULL;
1578 WriteData *op = g_task_get_task_data (task);
1579 gssize count_written;
1581 if (g_task_return_error_if_cancelled (task))
1584 count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
1585 write_nonblocking (stream, op->buffer, op->count_requested, &error);
1587 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1591 g_error_free (error);
1593 source = g_pollable_output_stream_create_source (stream,
1594 g_task_get_cancellable (task));
1595 g_task_attach_source (task, source,
1596 (GSourceFunc) write_async_pollable_ready);
1597 g_source_unref (source);
1601 if (count_written == -1)
1602 g_task_return_error (task, error);
1604 g_task_return_int (task, count_written);
1608 g_output_stream_real_write_async (GOutputStream *stream,
1612 GCancellable *cancellable,
1613 GAsyncReadyCallback callback,
1619 op = g_slice_new0 (WriteData);
1620 task = g_task_new (stream, cancellable, callback, user_data);
1621 g_task_set_check_cancellable (task, FALSE);
1622 g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
1623 op->buffer = buffer;
1624 op->count_requested = count;
1626 if (!g_output_stream_async_write_is_via_threads (stream))
1627 write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
1629 g_task_run_in_thread (task, write_async_thread);
1630 g_object_unref (task);
1634 g_output_stream_real_write_finish (GOutputStream *stream,
1635 GAsyncResult *result,
1638 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1640 return g_task_propagate_int (G_TASK (result), error);
1644 GInputStream *source;
1645 GOutputStreamSpliceFlags flags;
1654 free_splice_data (SpliceData *op)
1656 g_clear_pointer (&op->buffer, g_free);
1657 g_object_unref (op->source);
1658 g_clear_error (&op->error);
1663 real_splice_async_complete_cb (GTask *task)
1665 SpliceData *op = g_task_get_task_data (task);
1667 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
1668 !g_input_stream_is_closed (op->source))
1671 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
1672 !g_output_stream_is_closed (g_task_get_source_object (task)))
1675 if (op->error != NULL)
1677 g_task_return_error (task, op->error);
1682 g_task_return_int (task, op->bytes_copied);
1685 g_object_unref (task);
1689 real_splice_async_close_input_cb (GObject *source,
1693 GTask *task = user_data;
1695 g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
1697 real_splice_async_complete_cb (task);
1701 real_splice_async_close_output_cb (GObject *source,
1705 GTask *task = G_TASK (user_data);
1706 SpliceData *op = g_task_get_task_data (task);
1707 GError **error = (op->error == NULL) ? &op->error : NULL;
1709 g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
1711 real_splice_async_complete_cb (task);
1715 real_splice_async_complete (GTask *task)
1717 SpliceData *op = g_task_get_task_data (task);
1718 gboolean done = TRUE;
1720 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
1723 g_input_stream_close_async (op->source, g_task_get_priority (task),
1724 g_task_get_cancellable (task),
1725 real_splice_async_close_input_cb, task);
1728 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
1731 g_output_stream_internal_close_async (g_task_get_source_object (task),
1732 g_task_get_priority (task),
1733 g_task_get_cancellable (task),
1734 real_splice_async_close_output_cb,
1739 real_splice_async_complete_cb (task);
1742 static void real_splice_async_read_cb (GObject *source,
1744 gpointer user_data);
1747 real_splice_async_write_cb (GObject *source,
1751 GOutputStreamClass *class;
1752 GTask *task = G_TASK (user_data);
1753 SpliceData *op = g_task_get_task_data (task);
1756 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1758 ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
1762 real_splice_async_complete (task);
1766 op->n_written += ret;
1767 op->bytes_copied += ret;
1768 if (op->bytes_copied > G_MAXSSIZE)
1769 op->bytes_copied = G_MAXSSIZE;
1771 if (op->n_written < op->n_read)
1773 class->write_async (g_task_get_source_object (task),
1774 op->buffer + op->n_written,
1775 op->n_read - op->n_written,
1776 g_task_get_priority (task),
1777 g_task_get_cancellable (task),
1778 real_splice_async_write_cb, task);
1782 g_input_stream_read_async (op->source, op->buffer, 8192,
1783 g_task_get_priority (task),
1784 g_task_get_cancellable (task),
1785 real_splice_async_read_cb, task);
1789 real_splice_async_read_cb (GObject *source,
1793 GOutputStreamClass *class;
1794 GTask *task = G_TASK (user_data);
1795 SpliceData *op = g_task_get_task_data (task);
1798 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1800 ret = g_input_stream_read_finish (op->source, res, &op->error);
1801 if (ret == -1 || ret == 0)
1803 real_splice_async_complete (task);
1810 class->write_async (g_task_get_source_object (task), op->buffer,
1811 op->n_read, g_task_get_priority (task),
1812 g_task_get_cancellable (task),
1813 real_splice_async_write_cb, task);
1817 splice_async_thread (GTask *task,
1818 gpointer source_object,
1820 GCancellable *cancellable)
1822 GOutputStream *stream = source_object;
1823 SpliceData *op = task_data;
1824 GOutputStreamClass *class;
1825 GError *error = NULL;
1826 gssize bytes_copied;
1828 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1830 bytes_copied = class->splice (stream,
1835 if (bytes_copied == -1)
1836 g_task_return_error (task, error);
1838 g_task_return_int (task, bytes_copied);
1842 g_output_stream_real_splice_async (GOutputStream *stream,
1843 GInputStream *source,
1844 GOutputStreamSpliceFlags flags,
1846 GCancellable *cancellable,
1847 GAsyncReadyCallback callback,
1853 op = g_new0 (SpliceData, 1);
1854 task = g_task_new (stream, cancellable, callback, user_data);
1855 g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
1857 op->source = g_object_ref (source);
1859 if (g_input_stream_async_read_is_via_threads (source) &&
1860 g_output_stream_async_write_is_via_threads (stream))
1862 g_task_run_in_thread (task, splice_async_thread);
1863 g_object_unref (task);
1867 op->buffer = g_malloc (8192);
1868 g_input_stream_read_async (op->source, op->buffer, 8192,
1869 g_task_get_priority (task),
1870 g_task_get_cancellable (task),
1871 real_splice_async_read_cb, task);
1876 g_output_stream_real_splice_finish (GOutputStream *stream,
1877 GAsyncResult *result,
1880 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1882 return g_task_propagate_int (G_TASK (result), error);
1887 flush_async_thread (GTask *task,
1888 gpointer source_object,
1890 GCancellable *cancellable)
1892 GOutputStream *stream = source_object;
1893 GOutputStreamClass *class;
1895 GError *error = NULL;
1897 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1900 result = class->flush (stream, cancellable, &error);
1903 g_task_return_boolean (task, TRUE);
1905 g_task_return_error (task, error);
1909 g_output_stream_real_flush_async (GOutputStream *stream,
1911 GCancellable *cancellable,
1912 GAsyncReadyCallback callback,
1917 task = g_task_new (stream, cancellable, callback, user_data);
1918 g_task_set_priority (task, io_priority);
1919 g_task_run_in_thread (task, flush_async_thread);
1920 g_object_unref (task);
1924 g_output_stream_real_flush_finish (GOutputStream *stream,
1925 GAsyncResult *result,
1928 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1930 return g_task_propagate_boolean (G_TASK (result), error);
1934 close_async_thread (GTask *task,
1935 gpointer source_object,
1937 GCancellable *cancellable)
1939 GOutputStream *stream = source_object;
1940 GOutputStreamClass *class;
1941 GError *error = NULL;
1942 gboolean result = TRUE;
1944 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1946 /* Do a flush here if there is a flush function, and we did not have to do
1947 * an async flush before (see g_output_stream_close_async)
1949 if (class->flush != NULL &&
1950 (class->flush_async == NULL ||
1951 class->flush_async == g_output_stream_real_flush_async))
1953 result = class->flush (stream, cancellable, &error);
1956 /* Auto handling of cancelation disabled, and ignore
1957 cancellation, since we want to close things anyway, although
1958 possibly in a quick-n-dirty way. At least we never want to leak
1961 if (class->close_fn)
1963 /* Make sure to close, even if the flush failed (see sync close) */
1965 class->close_fn (stream, cancellable, NULL);
1967 result = class->close_fn (stream, cancellable, &error);
1971 g_task_return_boolean (task, TRUE);
1973 g_task_return_error (task, error);
1977 g_output_stream_real_close_async (GOutputStream *stream,
1979 GCancellable *cancellable,
1980 GAsyncReadyCallback callback,
1985 task = g_task_new (stream, cancellable, callback, user_data);
1986 g_task_set_priority (task, io_priority);
1987 g_task_run_in_thread (task, close_async_thread);
1988 g_object_unref (task);
1992 g_output_stream_real_close_finish (GOutputStream *stream,
1993 GAsyncResult *result,
1996 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1998 return g_task_propagate_boolean (G_TASK (result), error);