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 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
333 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (stream), FALSE);
334 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
335 g_return_val_if_fail (format != NULL, FALSE);
337 va_start (args, format);
338 success = g_output_stream_vprintf (stream, bytes_written, cancellable,
339 error, format, args);
346 * g_output_stream_vprintf:
347 * @stream: a #GOutputStream.
348 * @bytes_written: (out): location to store the number of bytes that was
349 * written to the stream
350 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
351 * @error: location to store the error occurring, or %NULL to ignore
352 * @format: the format string. See the printf() documentation
353 * @args: the parameters to insert into the format string
355 * This is a utility function around g_output_stream_write_all(). It
356 * uses g_strdup_vprintf() to turn @format and @args into a string that
357 * is then written to @stream.
359 * See the documentation of g_output_stream_write_all() about the
360 * behavior of the actual write operation.
362 * Note that partial writes cannot be properly checked with this
363 * function due to the variable length of the written string, if you
364 * need precise control over partial write failures, you need to
365 * create you own printf()-like wrapper around g_output_stream_write()
366 * or g_output_stream_write_all().
370 * Return value: %TRUE on success, %FALSE if there was an error
373 g_output_stream_vprintf (GOutputStream *stream,
374 gsize *bytes_written,
375 GCancellable *cancellable,
383 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
384 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (stream), FALSE);
385 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
386 g_return_val_if_fail (format != NULL, FALSE);
388 text = g_strdup_vprintf (format, args);
389 success = g_output_stream_write_all (stream,
391 bytes_written, cancellable, error);
398 * g_output_stream_write_bytes:
399 * @stream: a #GOutputStream.
400 * @bytes: the #GBytes to write
401 * @cancellable: (allow-none): optional cancellable object
402 * @error: location to store the error occurring, or %NULL to ignore
404 * A wrapper function for g_output_stream_write() which takes a
405 * #GBytes as input. This can be more convenient for use by language
406 * bindings or in other cases where the refcounted nature of #GBytes
407 * is helpful over a bare pointer interface.
409 * However, note that this function <emphasis>may</emphasis> still
410 * perform partial writes, just like g_output_stream_write(). If that
411 * occurs, to continue writing, you will need to create a new #GBytes
412 * containing just the remaining bytes, using
413 * g_bytes_new_from_bytes(). Passing the same #GBytes instance
414 * multiple times potentially can result in duplicated data in the
417 * Return value: Number of bytes written, or -1 on error
420 g_output_stream_write_bytes (GOutputStream *stream,
422 GCancellable *cancellable,
428 data = g_bytes_get_data (bytes, &size);
430 return g_output_stream_write (stream,
437 * g_output_stream_flush:
438 * @stream: a #GOutputStream.
439 * @cancellable: (allow-none): optional cancellable object
440 * @error: location to store the error occurring, or %NULL to ignore
442 * Forces a write of all user-space buffered data for the given
443 * @stream. Will block during the operation. Closing the stream will
444 * implicitly cause a flush.
446 * This function is optional for inherited classes.
448 * If @cancellable is not %NULL, then the operation can be cancelled by
449 * triggering the cancellable object from another thread. If the operation
450 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
452 * Return value: %TRUE on success, %FALSE on error
455 g_output_stream_flush (GOutputStream *stream,
456 GCancellable *cancellable,
459 GOutputStreamClass *class;
462 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
464 if (!g_output_stream_set_pending (stream, error))
467 class = G_OUTPUT_STREAM_GET_CLASS (stream);
473 g_cancellable_push_current (cancellable);
475 res = class->flush (stream, cancellable, error);
478 g_cancellable_pop_current (cancellable);
481 g_output_stream_clear_pending (stream);
487 * g_output_stream_splice:
488 * @stream: a #GOutputStream.
489 * @source: a #GInputStream.
490 * @flags: a set of #GOutputStreamSpliceFlags.
491 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
492 * @error: a #GError location to store the error occurring, or %NULL to
495 * Splices an input stream into an output stream.
497 * Returns: a #gssize containing the size of the data spliced, or
498 * -1 if an error occurred. Note that if the number of bytes
499 * spliced is greater than %G_MAXSSIZE, then that will be
500 * returned, and there is no way to determine the actual number
504 g_output_stream_splice (GOutputStream *stream,
505 GInputStream *source,
506 GOutputStreamSpliceFlags flags,
507 GCancellable *cancellable,
510 GOutputStreamClass *class;
513 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
514 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
516 if (g_input_stream_is_closed (source))
518 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
519 _("Source stream is already closed"));
523 if (!g_output_stream_set_pending (stream, error))
526 class = G_OUTPUT_STREAM_GET_CLASS (stream);
529 g_cancellable_push_current (cancellable);
531 bytes_copied = class->splice (stream, source, flags, cancellable, error);
534 g_cancellable_pop_current (cancellable);
536 g_output_stream_clear_pending (stream);
542 g_output_stream_real_splice (GOutputStream *stream,
543 GInputStream *source,
544 GOutputStreamSpliceFlags flags,
545 GCancellable *cancellable,
548 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
549 gssize n_read, n_written;
551 char buffer[8192], *p;
555 if (class->write_fn == NULL)
557 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
558 _("Output stream doesn't implement write"));
566 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
579 n_written = class->write_fn (stream, p, n_read, cancellable, error);
588 bytes_copied += n_written;
591 if (bytes_copied > G_MAXSSIZE)
592 bytes_copied = G_MAXSSIZE;
598 error = NULL; /* Ignore further errors */
600 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
602 /* Don't care about errors in source here */
603 g_input_stream_close (source, cancellable, NULL);
606 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
608 /* But write errors on close are bad! */
609 res = g_output_stream_internal_close (stream, cancellable, error);
618 /* Must always be called inside
619 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
621 g_output_stream_internal_close (GOutputStream *stream,
622 GCancellable *cancellable,
625 GOutputStreamClass *class;
628 if (stream->priv->closed)
631 class = G_OUTPUT_STREAM_GET_CLASS (stream);
633 stream->priv->closing = TRUE;
636 g_cancellable_push_current (cancellable);
639 res = class->flush (stream, cancellable, error);
645 /* flushing caused the error that we want to return,
646 * but we still want to close the underlying stream if possible
649 class->close_fn (stream, cancellable, NULL);
655 res = class->close_fn (stream, cancellable, error);
659 g_cancellable_pop_current (cancellable);
661 stream->priv->closing = FALSE;
662 stream->priv->closed = TRUE;
668 * g_output_stream_close:
669 * @stream: A #GOutputStream.
670 * @cancellable: (allow-none): optional cancellable object
671 * @error: location to store the error occurring, or %NULL to ignore
673 * Closes the stream, releasing resources related to it.
675 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
676 * Closing a stream multiple times will not return an error.
678 * Closing a stream will automatically flush any outstanding buffers in the
681 * Streams will be automatically closed when the last reference
682 * is dropped, but you might want to call this function to make sure
683 * resources are released as early as possible.
685 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
686 * open after the stream is closed. See the documentation for the individual
687 * stream for details.
689 * On failure the first error that happened will be reported, but the close
690 * operation will finish as much as possible. A stream that failed to
691 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
692 * is important to check and report the error to the user, otherwise
693 * there might be a loss of data as all data might not be written.
695 * If @cancellable is not %NULL, then the operation can be cancelled by
696 * triggering the cancellable object from another thread. If the operation
697 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
698 * Cancelling a close will still leave the stream closed, but there some streams
699 * can use a faster close that doesn't block to e.g. check errors. On
700 * cancellation (as with any error) there is no guarantee that all written
701 * data will reach the target.
703 * Return value: %TRUE on success, %FALSE on failure
706 g_output_stream_close (GOutputStream *stream,
707 GCancellable *cancellable,
712 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
714 if (stream->priv->closed)
717 if (!g_output_stream_set_pending (stream, error))
720 res = g_output_stream_internal_close (stream, cancellable, error);
722 g_output_stream_clear_pending (stream);
728 async_ready_write_callback_wrapper (GObject *source_object,
732 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
733 GOutputStreamClass *class;
734 GTask *task = user_data;
736 GError *error = NULL;
738 g_output_stream_clear_pending (stream);
740 if (g_async_result_legacy_propagate_error (res, &error))
744 class = G_OUTPUT_STREAM_GET_CLASS (stream);
745 nwrote = class->write_finish (stream, res, &error);
749 g_task_return_int (task, nwrote);
751 g_task_return_error (task, error);
752 g_object_unref (task);
756 * g_output_stream_write_async:
757 * @stream: A #GOutputStream.
758 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
759 * @count: the number of bytes to write
760 * @io_priority: the io priority of the request.
761 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
762 * @callback: (scope async): callback to call when the request is satisfied
763 * @user_data: (closure): the data to pass to callback function
765 * Request an asynchronous write of @count bytes from @buffer into
766 * the stream. When the operation is finished @callback will be called.
767 * You can then call g_output_stream_write_finish() to get the result of the
770 * During an async request no other sync and async calls are allowed,
771 * and will result in %G_IO_ERROR_PENDING errors.
773 * A value of @count larger than %G_MAXSSIZE will cause a
774 * %G_IO_ERROR_INVALID_ARGUMENT error.
776 * On success, the number of bytes written will be passed to the
777 * @callback. It is not an error if this is not the same as the
778 * requested size, as it can happen e.g. on a partial I/O error,
779 * but generally we try to write as many bytes as requested.
781 * You are guaranteed that this method will never fail with
782 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
783 * method will just wait until this changes.
785 * Any outstanding I/O request with higher priority (lower numerical
786 * value) will be executed before an outstanding request with lower
787 * priority. Default priority is %G_PRIORITY_DEFAULT.
789 * The asyncronous methods have a default fallback that uses threads
790 * to implement asynchronicity, so they are optional for inheriting
791 * classes. However, if you override one you must override all.
793 * For the synchronous, blocking version of this function, see
794 * g_output_stream_write().
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);