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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "goutputstream.h"
24 #include "gcancellable.h"
25 #include "gasyncresult.h"
27 #include "ginputstream.h"
29 #include "gioprivate.h"
31 #include "gpollableoutputstream.h"
34 * SECTION:goutputstream
35 * @short_description: Base class for implementing streaming output
38 * #GOutputStream has functions to write to a stream (g_output_stream_write()),
39 * to close a stream (g_output_stream_close()) and to flush pending writes
40 * (g_output_stream_flush()).
42 * To copy the content of an input stream to an output stream without
43 * manually handling the reads and writes, use g_output_stream_splice().
45 * All of these functions have async variants too.
48 struct _GOutputStreamPrivate {
52 GAsyncReadyCallback outstanding_callback;
55 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GOutputStream, g_output_stream, G_TYPE_OBJECT)
57 static gssize g_output_stream_real_splice (GOutputStream *stream,
59 GOutputStreamSpliceFlags flags,
60 GCancellable *cancellable,
62 static void g_output_stream_real_write_async (GOutputStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
72 static void g_output_stream_real_splice_async (GOutputStream *stream,
74 GOutputStreamSpliceFlags flags,
76 GCancellable *cancellable,
77 GAsyncReadyCallback callback,
79 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
82 static void g_output_stream_real_flush_async (GOutputStream *stream,
84 GCancellable *cancellable,
85 GAsyncReadyCallback callback,
87 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
90 static void g_output_stream_real_close_async (GOutputStream *stream,
92 GCancellable *cancellable,
93 GAsyncReadyCallback callback,
95 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
98 static gboolean g_output_stream_internal_close (GOutputStream *stream,
99 GCancellable *cancellable,
101 static void g_output_stream_internal_close_async (GOutputStream *stream,
103 GCancellable *cancellable,
104 GAsyncReadyCallback callback,
106 static gboolean g_output_stream_internal_close_finish (GOutputStream *stream,
107 GAsyncResult *result,
111 g_output_stream_dispose (GObject *object)
113 GOutputStream *stream;
115 stream = G_OUTPUT_STREAM (object);
117 if (!stream->priv->closed)
118 g_output_stream_close (stream, NULL, NULL);
120 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
124 g_output_stream_class_init (GOutputStreamClass *klass)
126 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
128 gobject_class->dispose = g_output_stream_dispose;
130 klass->splice = g_output_stream_real_splice;
132 klass->write_async = g_output_stream_real_write_async;
133 klass->write_finish = g_output_stream_real_write_finish;
134 klass->splice_async = g_output_stream_real_splice_async;
135 klass->splice_finish = g_output_stream_real_splice_finish;
136 klass->flush_async = g_output_stream_real_flush_async;
137 klass->flush_finish = g_output_stream_real_flush_finish;
138 klass->close_async = g_output_stream_real_close_async;
139 klass->close_finish = g_output_stream_real_close_finish;
143 g_output_stream_init (GOutputStream *stream)
145 stream->priv = g_output_stream_get_instance_private (stream);
149 * g_output_stream_write:
150 * @stream: a #GOutputStream.
151 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
152 * @count: the number of bytes to write
153 * @cancellable: (allow-none): optional cancellable object
154 * @error: location to store the error occurring, or %NULL to ignore
156 * Tries to write @count bytes from @buffer into the stream. Will block
157 * during the operation.
159 * If count is 0, returns 0 and does nothing. A value of @count
160 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
162 * On success, the number of bytes written to the stream is returned.
163 * It is not an error if this is not the same as the requested size, as it
164 * can happen e.g. on a partial I/O error, or if there is not enough
165 * storage in the stream. All writes block until at least one byte
166 * is written or an error occurs; 0 is never returned (unless
169 * If @cancellable is not %NULL, then the operation can be cancelled by
170 * triggering the cancellable object from another thread. If the operation
171 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
172 * operation was partially finished when the operation was cancelled the
173 * partial result will be returned, without an error.
175 * On error -1 is returned and @error is set accordingly.
179 * Return value: Number of bytes written, or -1 on error
182 g_output_stream_write (GOutputStream *stream,
185 GCancellable *cancellable,
188 GOutputStreamClass *class;
191 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
192 g_return_val_if_fail (buffer != NULL, 0);
197 if (((gssize) count) < 0)
199 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
200 _("Too large count value passed to %s"), G_STRFUNC);
204 class = G_OUTPUT_STREAM_GET_CLASS (stream);
206 if (class->write_fn == NULL)
208 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
209 _("Output stream doesn't implement write"));
213 if (!g_output_stream_set_pending (stream, error))
217 g_cancellable_push_current (cancellable);
219 res = class->write_fn (stream, buffer, count, cancellable, error);
222 g_cancellable_pop_current (cancellable);
224 g_output_stream_clear_pending (stream);
230 * g_output_stream_write_all:
231 * @stream: a #GOutputStream.
232 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
233 * @count: the number of bytes to write
234 * @bytes_written: (out): location to store the number of bytes that was
235 * written to the stream
236 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
237 * @error: location to store the error occurring, or %NULL to ignore
239 * Tries to write @count bytes from @buffer into the stream. Will block
240 * during the operation.
242 * This function is similar to g_output_stream_write(), except it tries to
243 * write as many bytes as requested, only stopping on an error.
245 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
248 * If there is an error during the operation %FALSE is returned and @error
249 * is set to indicate the error status, @bytes_written is updated to contain
250 * the number of bytes written into the stream before the error occurred.
252 * Return value: %TRUE on success, %FALSE if there was an error
255 g_output_stream_write_all (GOutputStream *stream,
258 gsize *bytes_written,
259 GCancellable *cancellable,
262 gsize _bytes_written;
265 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
266 g_return_val_if_fail (buffer != NULL, FALSE);
269 while (_bytes_written < count)
271 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
276 *bytes_written = _bytes_written;
281 g_warning ("Write returned zero without error");
283 _bytes_written += res;
287 *bytes_written = _bytes_written;
293 * g_output_stream_printf:
294 * @stream: a #GOutputStream.
295 * @bytes_written: (out): location to store the number of bytes that was
296 * written to the stream
297 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
298 * @error: location to store the error occurring, or %NULL to ignore
299 * @format: the format string. See the printf() documentation
300 * @...: the parameters to insert into the format string
302 * This is a utility function around g_output_stream_write_all(). It
303 * uses g_strdup_vprintf() to turn @format and @... into a string that
304 * is then written to @stream.
306 * See the documentation of g_output_stream_write_all() about the
307 * behavior of the actual write operation.
309 * Note that partial writes cannot be properly checked with this
310 * function due to the variable length of the written string, if you
311 * need precise control over partial write failures, you need to
312 * create you own printf()-like wrapper around g_output_stream_write()
313 * or g_output_stream_write_all().
317 * Return value: %TRUE on success, %FALSE if there was an error
320 g_output_stream_printf (GOutputStream *stream,
321 gsize *bytes_written,
322 GCancellable *cancellable,
330 va_start (args, format);
331 success = g_output_stream_vprintf (stream, bytes_written, cancellable,
332 error, format, args);
339 * g_output_stream_vprintf:
340 * @stream: a #GOutputStream.
341 * @bytes_written: (out): location to store the number of bytes that was
342 * written to the stream
343 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
344 * @error: location to store the error occurring, or %NULL to ignore
345 * @format: the format string. See the printf() documentation
346 * @args: the parameters to insert into the format string
348 * This is a utility function around g_output_stream_write_all(). It
349 * uses g_strdup_vprintf() to turn @format and @args into a string that
350 * is then written to @stream.
352 * See the documentation of g_output_stream_write_all() about the
353 * behavior of the actual write operation.
355 * Note that partial writes cannot be properly checked with this
356 * function due to the variable length of the written string, if you
357 * need precise control over partial write failures, you need to
358 * create you own printf()-like wrapper around g_output_stream_write()
359 * or g_output_stream_write_all().
363 * Return value: %TRUE on success, %FALSE if there was an error
366 g_output_stream_vprintf (GOutputStream *stream,
367 gsize *bytes_written,
368 GCancellable *cancellable,
376 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
377 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (stream), FALSE);
378 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
379 g_return_val_if_fail (format != NULL, FALSE);
381 text = g_strdup_vprintf (format, args);
382 success = g_output_stream_write_all (stream,
384 bytes_written, cancellable, error);
391 * g_output_stream_write_bytes:
392 * @stream: a #GOutputStream.
393 * @bytes: the #GBytes to write
394 * @cancellable: (allow-none): optional cancellable object
395 * @error: location to store the error occurring, or %NULL to ignore
397 * A wrapper function for g_output_stream_write() which takes a
398 * #GBytes as input. This can be more convenient for use by language
399 * bindings or in other cases where the refcounted nature of #GBytes
400 * is helpful over a bare pointer interface.
402 * However, note that this function <emphasis>may</emphasis> still
403 * perform partial writes, just like g_output_stream_write(). If that
404 * occurs, to continue writing, you will need to create a new #GBytes
405 * containing just the remaining bytes, using
406 * g_bytes_new_from_bytes(). Passing the same #GBytes instance
407 * multiple times potentially can result in duplicated data in the
410 * Return value: Number of bytes written, or -1 on error
413 g_output_stream_write_bytes (GOutputStream *stream,
415 GCancellable *cancellable,
421 data = g_bytes_get_data (bytes, &size);
423 return g_output_stream_write (stream,
430 * g_output_stream_flush:
431 * @stream: a #GOutputStream.
432 * @cancellable: (allow-none): optional cancellable object
433 * @error: location to store the error occurring, or %NULL to ignore
435 * Forces a write of all user-space buffered data for the given
436 * @stream. Will block during the operation. Closing the stream will
437 * implicitly cause a flush.
439 * This function is optional for inherited classes.
441 * If @cancellable is not %NULL, then the operation can be cancelled by
442 * triggering the cancellable object from another thread. If the operation
443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
445 * Return value: %TRUE on success, %FALSE on error
448 g_output_stream_flush (GOutputStream *stream,
449 GCancellable *cancellable,
452 GOutputStreamClass *class;
455 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
457 if (!g_output_stream_set_pending (stream, error))
460 class = G_OUTPUT_STREAM_GET_CLASS (stream);
466 g_cancellable_push_current (cancellable);
468 res = class->flush (stream, cancellable, error);
471 g_cancellable_pop_current (cancellable);
474 g_output_stream_clear_pending (stream);
480 * g_output_stream_splice:
481 * @stream: a #GOutputStream.
482 * @source: a #GInputStream.
483 * @flags: a set of #GOutputStreamSpliceFlags.
484 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
485 * @error: a #GError location to store the error occurring, or %NULL to
488 * Splices an input stream into an output stream.
490 * Returns: a #gssize containing the size of the data spliced, or
491 * -1 if an error occurred. Note that if the number of bytes
492 * spliced is greater than %G_MAXSSIZE, then that will be
493 * returned, and there is no way to determine the actual number
497 g_output_stream_splice (GOutputStream *stream,
498 GInputStream *source,
499 GOutputStreamSpliceFlags flags,
500 GCancellable *cancellable,
503 GOutputStreamClass *class;
506 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
507 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
509 if (g_input_stream_is_closed (source))
511 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
512 _("Source stream is already closed"));
516 if (!g_output_stream_set_pending (stream, error))
519 class = G_OUTPUT_STREAM_GET_CLASS (stream);
522 g_cancellable_push_current (cancellable);
524 bytes_copied = class->splice (stream, source, flags, cancellable, error);
527 g_cancellable_pop_current (cancellable);
529 g_output_stream_clear_pending (stream);
535 g_output_stream_real_splice (GOutputStream *stream,
536 GInputStream *source,
537 GOutputStreamSpliceFlags flags,
538 GCancellable *cancellable,
541 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
542 gssize n_read, n_written;
544 char buffer[8192], *p;
548 if (class->write_fn == NULL)
550 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
551 _("Output stream doesn't implement write"));
559 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
572 n_written = class->write_fn (stream, p, n_read, cancellable, error);
581 bytes_copied += n_written;
584 if (bytes_copied > G_MAXSSIZE)
585 bytes_copied = G_MAXSSIZE;
591 error = NULL; /* Ignore further errors */
593 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
595 /* Don't care about errors in source here */
596 g_input_stream_close (source, cancellable, NULL);
599 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
601 /* But write errors on close are bad! */
602 res = g_output_stream_internal_close (stream, cancellable, error);
611 /* Must always be called inside
612 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
614 g_output_stream_internal_close (GOutputStream *stream,
615 GCancellable *cancellable,
618 GOutputStreamClass *class;
621 if (stream->priv->closed)
624 class = G_OUTPUT_STREAM_GET_CLASS (stream);
626 stream->priv->closing = TRUE;
629 g_cancellable_push_current (cancellable);
632 res = class->flush (stream, cancellable, error);
638 /* flushing caused the error that we want to return,
639 * but we still want to close the underlying stream if possible
642 class->close_fn (stream, cancellable, NULL);
648 res = class->close_fn (stream, cancellable, error);
652 g_cancellable_pop_current (cancellable);
654 stream->priv->closing = FALSE;
655 stream->priv->closed = TRUE;
661 * g_output_stream_close:
662 * @stream: A #GOutputStream.
663 * @cancellable: (allow-none): optional cancellable object
664 * @error: location to store the error occurring, or %NULL to ignore
666 * Closes the stream, releasing resources related to it.
668 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
669 * Closing a stream multiple times will not return an error.
671 * Closing a stream will automatically flush any outstanding buffers in the
674 * Streams will be automatically closed when the last reference
675 * is dropped, but you might want to call this function to make sure
676 * resources are released as early as possible.
678 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
679 * open after the stream is closed. See the documentation for the individual
680 * stream for details.
682 * On failure the first error that happened will be reported, but the close
683 * operation will finish as much as possible. A stream that failed to
684 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
685 * is important to check and report the error to the user, otherwise
686 * there might be a loss of data as all data might not be written.
688 * If @cancellable is not %NULL, then the operation can be cancelled by
689 * triggering the cancellable object from another thread. If the operation
690 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
691 * Cancelling a close will still leave the stream closed, but there some streams
692 * can use a faster close that doesn't block to e.g. check errors. On
693 * cancellation (as with any error) there is no guarantee that all written
694 * data will reach the target.
696 * Return value: %TRUE on success, %FALSE on failure
699 g_output_stream_close (GOutputStream *stream,
700 GCancellable *cancellable,
705 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
707 if (stream->priv->closed)
710 if (!g_output_stream_set_pending (stream, error))
713 res = g_output_stream_internal_close (stream, cancellable, error);
715 g_output_stream_clear_pending (stream);
721 async_ready_write_callback_wrapper (GObject *source_object,
725 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
726 GOutputStreamClass *class;
727 GTask *task = user_data;
729 GError *error = NULL;
731 g_output_stream_clear_pending (stream);
733 if (g_async_result_legacy_propagate_error (res, &error))
737 class = G_OUTPUT_STREAM_GET_CLASS (stream);
738 nwrote = class->write_finish (stream, res, &error);
742 g_task_return_int (task, nwrote);
744 g_task_return_error (task, error);
745 g_object_unref (task);
749 * g_output_stream_write_async:
750 * @stream: A #GOutputStream.
751 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
752 * @count: the number of bytes to write
753 * @io_priority: the io priority of the request.
754 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
755 * @callback: (scope async): callback to call when the request is satisfied
756 * @user_data: (closure): the data to pass to callback function
758 * Request an asynchronous write of @count bytes from @buffer into
759 * the stream. When the operation is finished @callback will be called.
760 * You can then call g_output_stream_write_finish() to get the result of the
763 * During an async request no other sync and async calls are allowed,
764 * and will result in %G_IO_ERROR_PENDING errors.
766 * A value of @count larger than %G_MAXSSIZE will cause a
767 * %G_IO_ERROR_INVALID_ARGUMENT error.
769 * On success, the number of bytes written will be passed to the
770 * @callback. It is not an error if this is not the same as the
771 * requested size, as it can happen e.g. on a partial I/O error,
772 * but generally we try to write as many bytes as requested.
774 * You are guaranteed that this method will never fail with
775 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
776 * method will just wait until this changes.
778 * Any outstanding I/O request with higher priority (lower numerical
779 * value) will be executed before an outstanding request with lower
780 * priority. Default priority is %G_PRIORITY_DEFAULT.
782 * The asyncronous methods have a default fallback that uses threads
783 * to implement asynchronicity, so they are optional for inheriting
784 * classes. However, if you override one you must override all.
786 * For the synchronous, blocking version of this function, see
787 * g_output_stream_write().
789 * Note that no copy of @buffer will be made, so it must stay valid
790 * until @callback is called. See g_output_stream_write_bytes_async()
791 * for a #GBytes version that will automatically hold a reference to
792 * the contents (without copying) for the duration of the call.
795 g_output_stream_write_async (GOutputStream *stream,
799 GCancellable *cancellable,
800 GAsyncReadyCallback callback,
803 GOutputStreamClass *class;
804 GError *error = NULL;
807 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
808 g_return_if_fail (buffer != NULL);
810 task = g_task_new (stream, cancellable, callback, user_data);
811 g_task_set_source_tag (task, g_output_stream_write_async);
812 g_task_set_priority (task, io_priority);
816 g_task_return_int (task, 0);
817 g_object_unref (task);
821 if (((gssize) count) < 0)
823 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
824 _("Too large count value passed to %s"),
826 g_object_unref (task);
830 if (!g_output_stream_set_pending (stream, &error))
832 g_task_return_error (task, error);
833 g_object_unref (task);
837 class = G_OUTPUT_STREAM_GET_CLASS (stream);
839 class->write_async (stream, buffer, count, io_priority, cancellable,
840 async_ready_write_callback_wrapper, task);
844 * g_output_stream_write_finish:
845 * @stream: a #GOutputStream.
846 * @result: a #GAsyncResult.
847 * @error: a #GError location to store the error occurring, or %NULL to
850 * Finishes a stream write operation.
852 * Returns: a #gssize containing the number of bytes written to the stream.
855 g_output_stream_write_finish (GOutputStream *stream,
856 GAsyncResult *result,
859 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
860 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
861 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
863 /* @result is always the GTask created by g_output_stream_write_async();
864 * we called class->write_finish() from async_ready_write_callback_wrapper.
866 return g_task_propagate_int (G_TASK (result), error);
870 write_bytes_callback (GObject *stream,
871 GAsyncResult *result,
874 GTask *task = user_data;
875 GError *error = NULL;
878 nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
881 g_task_return_error (task, error);
883 g_task_return_int (task, nwrote);
884 g_object_unref (task);
888 * g_output_stream_write_bytes_async:
889 * @stream: A #GOutputStream.
890 * @bytes: The bytes to write
891 * @io_priority: the io priority of the request.
892 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
893 * @callback: (scope async): callback to call when the request is satisfied
894 * @user_data: (closure): the data to pass to callback function
896 * This function is similar to g_output_stream_write_async(), but
897 * takes a #GBytes as input. Due to the refcounted nature of #GBytes,
898 * this allows the stream to avoid taking a copy of the data.
900 * However, note that this function <emphasis>may</emphasis> still
901 * perform partial writes, just like g_output_stream_write_async().
902 * If that occurs, to continue writing, you will need to create a new
903 * #GBytes containing just the remaining bytes, using
904 * g_bytes_new_from_bytes(). Passing the same #GBytes instance
905 * multiple times potentially can result in duplicated data in the
908 * For the synchronous, blocking version of this function, see
909 * g_output_stream_write_bytes().
912 g_output_stream_write_bytes_async (GOutputStream *stream,
915 GCancellable *cancellable,
916 GAsyncReadyCallback callback,
923 data = g_bytes_get_data (bytes, &size);
925 task = g_task_new (stream, cancellable, callback, user_data);
926 g_task_set_task_data (task, g_bytes_ref (bytes),
927 (GDestroyNotify) g_bytes_unref);
929 g_output_stream_write_async (stream,
933 write_bytes_callback,
938 * g_output_stream_write_bytes_finish:
939 * @stream: a #GOutputStream.
940 * @result: a #GAsyncResult.
941 * @error: a #GError location to store the error occurring, or %NULL to
944 * Finishes a stream write-from-#GBytes operation.
946 * Returns: a #gssize containing the number of bytes written to the stream.
949 g_output_stream_write_bytes_finish (GOutputStream *stream,
950 GAsyncResult *result,
953 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
954 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
956 return g_task_propagate_int (G_TASK (result), error);
960 async_ready_splice_callback_wrapper (GObject *source_object,
964 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
965 GOutputStreamClass *class;
968 GError *error = NULL;
970 g_output_stream_clear_pending (stream);
972 if (g_async_result_legacy_propagate_error (res, &error))
976 class = G_OUTPUT_STREAM_GET_CLASS (stream);
977 nspliced = class->splice_finish (stream, res, &error);
981 g_task_return_int (task, nspliced);
983 g_task_return_error (task, error);
984 g_object_unref (task);
988 * g_output_stream_splice_async:
989 * @stream: a #GOutputStream.
990 * @source: a #GInputStream.
991 * @flags: a set of #GOutputStreamSpliceFlags.
992 * @io_priority: the io priority of the request.
993 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
994 * @callback: (scope async): a #GAsyncReadyCallback.
995 * @user_data: (closure): user data passed to @callback.
997 * Splices a stream asynchronously.
998 * When the operation is finished @callback will be called.
999 * You can then call g_output_stream_splice_finish() to get the
1000 * result of the operation.
1002 * For the synchronous, blocking version of this function, see
1003 * g_output_stream_splice().
1006 g_output_stream_splice_async (GOutputStream *stream,
1007 GInputStream *source,
1008 GOutputStreamSpliceFlags flags,
1010 GCancellable *cancellable,
1011 GAsyncReadyCallback callback,
1014 GOutputStreamClass *class;
1016 GError *error = NULL;
1018 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1019 g_return_if_fail (G_IS_INPUT_STREAM (source));
1021 task = g_task_new (stream, cancellable, callback, user_data);
1022 g_task_set_source_tag (task, g_output_stream_splice_async);
1023 g_task_set_priority (task, io_priority);
1024 g_task_set_task_data (task, g_object_ref (source), g_object_unref);
1026 if (g_input_stream_is_closed (source))
1028 g_task_return_new_error (task,
1029 G_IO_ERROR, G_IO_ERROR_CLOSED,
1030 _("Source stream is already closed"));
1031 g_object_unref (task);
1035 if (!g_output_stream_set_pending (stream, &error))
1037 g_task_return_error (task, error);
1038 g_object_unref (task);
1042 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1044 class->splice_async (stream, source, flags, io_priority, cancellable,
1045 async_ready_splice_callback_wrapper, task);
1049 * g_output_stream_splice_finish:
1050 * @stream: a #GOutputStream.
1051 * @result: a #GAsyncResult.
1052 * @error: a #GError location to store the error occurring, or %NULL to
1055 * Finishes an asynchronous stream splice operation.
1057 * Returns: a #gssize of the number of bytes spliced. Note that if the
1058 * number of bytes spliced is greater than %G_MAXSSIZE, then that
1059 * will be returned, and there is no way to determine the actual
1060 * number of bytes spliced.
1063 g_output_stream_splice_finish (GOutputStream *stream,
1064 GAsyncResult *result,
1067 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1068 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1069 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
1071 /* @result is always the GTask created by g_output_stream_splice_async();
1072 * we called class->splice_finish() from async_ready_splice_callback_wrapper.
1074 return g_task_propagate_int (G_TASK (result), error);
1078 async_ready_flush_callback_wrapper (GObject *source_object,
1082 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1083 GOutputStreamClass *class;
1084 GTask *task = user_data;
1086 GError *error = NULL;
1088 g_output_stream_clear_pending (stream);
1090 if (g_async_result_legacy_propagate_error (res, &error))
1094 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1095 flushed = class->flush_finish (stream, res, &error);
1099 g_task_return_boolean (task, TRUE);
1101 g_task_return_error (task, error);
1102 g_object_unref (task);
1106 * g_output_stream_flush_async:
1107 * @stream: a #GOutputStream.
1108 * @io_priority: the io priority of the request.
1109 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1110 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1111 * @user_data: (closure): the data to pass to callback function
1113 * Forces an asynchronous write of all user-space buffered data for
1114 * the given @stream.
1115 * For behaviour details see g_output_stream_flush().
1117 * When the operation is finished @callback will be
1118 * called. You can then call g_output_stream_flush_finish() to get the
1119 * result of the operation.
1122 g_output_stream_flush_async (GOutputStream *stream,
1124 GCancellable *cancellable,
1125 GAsyncReadyCallback callback,
1128 GOutputStreamClass *class;
1130 GError *error = NULL;
1132 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1134 task = g_task_new (stream, cancellable, callback, user_data);
1135 g_task_set_source_tag (task, g_output_stream_flush_async);
1136 g_task_set_priority (task, io_priority);
1138 if (!g_output_stream_set_pending (stream, &error))
1140 g_task_return_error (task, error);
1141 g_object_unref (task);
1145 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1147 if (class->flush_async == NULL)
1149 g_task_return_boolean (task, TRUE);
1150 g_object_unref (task);
1154 class->flush_async (stream, io_priority, cancellable,
1155 async_ready_flush_callback_wrapper, task);
1159 * g_output_stream_flush_finish:
1160 * @stream: a #GOutputStream.
1161 * @result: a GAsyncResult.
1162 * @error: a #GError location to store the error occurring, or %NULL to
1165 * Finishes flushing an output stream.
1167 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1170 g_output_stream_flush_finish (GOutputStream *stream,
1171 GAsyncResult *result,
1174 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1175 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1176 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
1178 /* @result is always the GTask created by g_output_stream_flush_async();
1179 * we called class->flush_finish() from async_ready_flush_callback_wrapper.
1181 return g_task_propagate_boolean (G_TASK (result), error);
1186 async_ready_close_callback_wrapper (GObject *source_object,
1190 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1191 GOutputStreamClass *class;
1192 GTask *task = user_data;
1193 GError *error = g_task_get_task_data (task);
1195 stream->priv->closing = FALSE;
1196 stream->priv->closed = TRUE;
1198 if (!error && !g_async_result_legacy_propagate_error (res, &error))
1200 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1202 class->close_finish (stream, res,
1203 error ? NULL : &error);
1207 g_task_return_error (task, error);
1209 g_task_return_boolean (task, TRUE);
1210 g_object_unref (task);
1214 async_ready_close_flushed_callback_wrapper (GObject *source_object,
1218 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1219 GOutputStreamClass *class;
1220 GTask *task = user_data;
1221 GError *error = NULL;
1223 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1225 if (!g_async_result_legacy_propagate_error (res, &error))
1227 class->flush_finish (stream, res, &error);
1230 /* propagate the possible error */
1232 g_task_set_task_data (task, error, NULL);
1234 /* we still close, even if there was a flush error */
1235 class->close_async (stream,
1236 g_task_get_priority (task),
1237 g_task_get_cancellable (task),
1238 async_ready_close_callback_wrapper, task);
1242 real_close_async_cb (GObject *source_object,
1246 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1247 GTask *task = user_data;
1248 GError *error = NULL;
1251 g_output_stream_clear_pending (stream);
1253 ret = g_output_stream_internal_close_finish (stream, res, &error);
1256 g_task_return_error (task, error);
1258 g_task_return_boolean (task, ret);
1260 g_object_unref (task);
1264 * g_output_stream_close_async:
1265 * @stream: A #GOutputStream.
1266 * @io_priority: the io priority of the request.
1267 * @cancellable: (allow-none): optional cancellable object
1268 * @callback: (scope async): callback to call when the request is satisfied
1269 * @user_data: (closure): the data to pass to callback function
1271 * Requests an asynchronous close of the stream, releasing resources
1272 * related to it. When the operation is finished @callback will be
1273 * called. You can then call g_output_stream_close_finish() to get
1274 * the result of the operation.
1276 * For behaviour details see g_output_stream_close().
1278 * The asyncronous methods have a default fallback that uses threads
1279 * to implement asynchronicity, so they are optional for inheriting
1280 * classes. However, if you override one you must override all.
1283 g_output_stream_close_async (GOutputStream *stream,
1285 GCancellable *cancellable,
1286 GAsyncReadyCallback callback,
1290 GError *error = NULL;
1292 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1294 task = g_task_new (stream, cancellable, callback, user_data);
1295 g_task_set_source_tag (task, g_output_stream_close_async);
1296 g_task_set_priority (task, io_priority);
1298 if (!g_output_stream_set_pending (stream, &error))
1300 g_task_return_error (task, error);
1301 g_object_unref (task);
1305 g_output_stream_internal_close_async (stream, io_priority, cancellable,
1306 real_close_async_cb, task);
1309 /* Must always be called inside
1310 * g_output_stream_set_pending()/g_output_stream_clear_pending().
1313 g_output_stream_internal_close_async (GOutputStream *stream,
1315 GCancellable *cancellable,
1316 GAsyncReadyCallback callback,
1319 GOutputStreamClass *class;
1322 task = g_task_new (stream, cancellable, callback, user_data);
1323 g_task_set_source_tag (task, g_output_stream_internal_close_async);
1324 g_task_set_priority (task, io_priority);
1326 if (stream->priv->closed)
1328 g_task_return_boolean (task, TRUE);
1329 g_object_unref (task);
1333 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1334 stream->priv->closing = TRUE;
1336 /* Call close_async directly if there is no need to flush, or if the flush
1337 can be done sync (in the output stream async close thread) */
1338 if (class->flush_async == NULL ||
1339 (class->flush_async == g_output_stream_real_flush_async &&
1340 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1342 class->close_async (stream, io_priority, cancellable,
1343 async_ready_close_callback_wrapper, task);
1347 /* First do an async flush, then do the async close in the callback
1348 wrapper (see async_ready_close_flushed_callback_wrapper) */
1349 class->flush_async (stream, io_priority, cancellable,
1350 async_ready_close_flushed_callback_wrapper, task);
1355 g_output_stream_internal_close_finish (GOutputStream *stream,
1356 GAsyncResult *result,
1359 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1360 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1361 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
1363 return g_task_propagate_boolean (G_TASK (result), error);
1367 * g_output_stream_close_finish:
1368 * @stream: a #GOutputStream.
1369 * @result: a #GAsyncResult.
1370 * @error: a #GError location to store the error occurring, or %NULL to
1373 * Closes an output stream.
1375 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1378 g_output_stream_close_finish (GOutputStream *stream,
1379 GAsyncResult *result,
1382 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1383 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1384 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
1386 /* @result is always the GTask created by g_output_stream_close_async();
1387 * we called class->close_finish() from async_ready_close_callback_wrapper.
1389 return g_task_propagate_boolean (G_TASK (result), error);
1393 * g_output_stream_is_closed:
1394 * @stream: a #GOutputStream.
1396 * Checks if an output stream has already been closed.
1398 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1401 g_output_stream_is_closed (GOutputStream *stream)
1403 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1405 return stream->priv->closed;
1409 * g_output_stream_is_closing:
1410 * @stream: a #GOutputStream.
1412 * Checks if an output stream is being closed. This can be
1413 * used inside e.g. a flush implementation to see if the
1414 * flush (or other i/o operation) is called from within
1415 * the closing operation.
1417 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1422 g_output_stream_is_closing (GOutputStream *stream)
1424 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1426 return stream->priv->closing;
1430 * g_output_stream_has_pending:
1431 * @stream: a #GOutputStream.
1433 * Checks if an ouput stream has pending actions.
1435 * Returns: %TRUE if @stream has pending actions.
1438 g_output_stream_has_pending (GOutputStream *stream)
1440 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1442 return stream->priv->pending;
1446 * g_output_stream_set_pending:
1447 * @stream: a #GOutputStream.
1448 * @error: a #GError location to store the error occurring, or %NULL to
1451 * Sets @stream to have actions pending. If the pending flag is
1452 * already set or @stream is closed, it will return %FALSE and set
1455 * Return value: %TRUE if pending was previously unset and is now set.
1458 g_output_stream_set_pending (GOutputStream *stream,
1461 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1463 if (stream->priv->closed)
1465 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1466 _("Stream is already closed"));
1470 if (stream->priv->pending)
1472 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1473 /* Translators: This is an error you get if there is
1474 * already an operation running against this stream when
1475 * you try to start one */
1476 _("Stream has outstanding operation"));
1480 stream->priv->pending = TRUE;
1485 * g_output_stream_clear_pending:
1486 * @stream: output stream
1488 * Clears the pending flag on @stream.
1491 g_output_stream_clear_pending (GOutputStream *stream)
1493 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1495 stream->priv->pending = FALSE;
1499 * g_output_stream_async_write_is_via_threads:
1500 * @stream: a #GOutputStream.
1502 * Checks if an ouput stream's write_async function uses threads.
1504 * Returns: %TRUE if @stream's write_async function uses threads.
1507 g_output_stream_async_write_is_via_threads (GOutputStream *stream)
1509 GOutputStreamClass *class;
1511 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1513 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1515 return (class->write_async == g_output_stream_real_write_async &&
1516 !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
1517 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
1521 /********************************************
1522 * Default implementation of async ops *
1523 ********************************************/
1527 gsize count_requested;
1528 gssize count_written;
1532 free_write_data (WriteData *op)
1534 g_slice_free (WriteData, op);
1538 write_async_thread (GTask *task,
1539 gpointer source_object,
1541 GCancellable *cancellable)
1543 GOutputStream *stream = source_object;
1544 WriteData *op = task_data;
1545 GOutputStreamClass *class;
1546 GError *error = NULL;
1547 gssize count_written;
1549 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1550 count_written = class->write_fn (stream, op->buffer, op->count_requested,
1551 cancellable, &error);
1552 if (count_written == -1)
1553 g_task_return_error (task, error);
1555 g_task_return_int (task, count_written);
1558 static void write_async_pollable (GPollableOutputStream *stream,
1562 write_async_pollable_ready (GPollableOutputStream *stream,
1565 GTask *task = user_data;
1567 write_async_pollable (stream, task);
1572 write_async_pollable (GPollableOutputStream *stream,
1575 GError *error = NULL;
1576 WriteData *op = g_task_get_task_data (task);
1577 gssize count_written;
1579 if (g_task_return_error_if_cancelled (task))
1582 count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
1583 write_nonblocking (stream, op->buffer, op->count_requested, &error);
1585 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1589 g_error_free (error);
1591 source = g_pollable_output_stream_create_source (stream,
1592 g_task_get_cancellable (task));
1593 g_task_attach_source (task, source,
1594 (GSourceFunc) write_async_pollable_ready);
1595 g_source_unref (source);
1599 if (count_written == -1)
1600 g_task_return_error (task, error);
1602 g_task_return_int (task, count_written);
1606 g_output_stream_real_write_async (GOutputStream *stream,
1610 GCancellable *cancellable,
1611 GAsyncReadyCallback callback,
1617 op = g_slice_new0 (WriteData);
1618 task = g_task_new (stream, cancellable, callback, user_data);
1619 g_task_set_check_cancellable (task, FALSE);
1620 g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
1621 op->buffer = buffer;
1622 op->count_requested = count;
1624 if (!g_output_stream_async_write_is_via_threads (stream))
1625 write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
1627 g_task_run_in_thread (task, write_async_thread);
1628 g_object_unref (task);
1632 g_output_stream_real_write_finish (GOutputStream *stream,
1633 GAsyncResult *result,
1636 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1638 return g_task_propagate_int (G_TASK (result), error);
1642 GInputStream *source;
1643 GOutputStreamSpliceFlags flags;
1652 free_splice_data (SpliceData *op)
1654 g_clear_pointer (&op->buffer, g_free);
1655 g_object_unref (op->source);
1656 g_clear_error (&op->error);
1661 real_splice_async_complete_cb (GTask *task)
1663 SpliceData *op = g_task_get_task_data (task);
1665 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
1666 !g_input_stream_is_closed (op->source))
1669 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
1670 !g_output_stream_is_closed (g_task_get_source_object (task)))
1673 if (op->error != NULL)
1675 g_task_return_error (task, op->error);
1680 g_task_return_int (task, op->bytes_copied);
1683 g_object_unref (task);
1687 real_splice_async_close_input_cb (GObject *source,
1691 GTask *task = user_data;
1693 g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
1695 real_splice_async_complete_cb (task);
1699 real_splice_async_close_output_cb (GObject *source,
1703 GTask *task = G_TASK (user_data);
1704 SpliceData *op = g_task_get_task_data (task);
1705 GError **error = (op->error == NULL) ? &op->error : NULL;
1707 g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
1709 real_splice_async_complete_cb (task);
1713 real_splice_async_complete (GTask *task)
1715 SpliceData *op = g_task_get_task_data (task);
1716 gboolean done = TRUE;
1718 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
1721 g_input_stream_close_async (op->source, g_task_get_priority (task),
1722 g_task_get_cancellable (task),
1723 real_splice_async_close_input_cb, task);
1726 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
1729 g_output_stream_internal_close_async (g_task_get_source_object (task),
1730 g_task_get_priority (task),
1731 g_task_get_cancellable (task),
1732 real_splice_async_close_output_cb,
1737 real_splice_async_complete_cb (task);
1740 static void real_splice_async_read_cb (GObject *source,
1742 gpointer user_data);
1745 real_splice_async_write_cb (GObject *source,
1749 GOutputStreamClass *class;
1750 GTask *task = G_TASK (user_data);
1751 SpliceData *op = g_task_get_task_data (task);
1754 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1756 ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
1760 real_splice_async_complete (task);
1764 op->n_written += ret;
1765 op->bytes_copied += ret;
1766 if (op->bytes_copied > G_MAXSSIZE)
1767 op->bytes_copied = G_MAXSSIZE;
1769 if (op->n_written < op->n_read)
1771 class->write_async (g_task_get_source_object (task),
1772 op->buffer + op->n_written,
1773 op->n_read - op->n_written,
1774 g_task_get_priority (task),
1775 g_task_get_cancellable (task),
1776 real_splice_async_write_cb, task);
1780 g_input_stream_read_async (op->source, op->buffer, 8192,
1781 g_task_get_priority (task),
1782 g_task_get_cancellable (task),
1783 real_splice_async_read_cb, task);
1787 real_splice_async_read_cb (GObject *source,
1791 GOutputStreamClass *class;
1792 GTask *task = G_TASK (user_data);
1793 SpliceData *op = g_task_get_task_data (task);
1796 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1798 ret = g_input_stream_read_finish (op->source, res, &op->error);
1799 if (ret == -1 || ret == 0)
1801 real_splice_async_complete (task);
1808 class->write_async (g_task_get_source_object (task), op->buffer,
1809 op->n_read, g_task_get_priority (task),
1810 g_task_get_cancellable (task),
1811 real_splice_async_write_cb, task);
1815 splice_async_thread (GTask *task,
1816 gpointer source_object,
1818 GCancellable *cancellable)
1820 GOutputStream *stream = source_object;
1821 SpliceData *op = task_data;
1822 GOutputStreamClass *class;
1823 GError *error = NULL;
1824 gssize bytes_copied;
1826 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1828 bytes_copied = class->splice (stream,
1833 if (bytes_copied == -1)
1834 g_task_return_error (task, error);
1836 g_task_return_int (task, bytes_copied);
1840 g_output_stream_real_splice_async (GOutputStream *stream,
1841 GInputStream *source,
1842 GOutputStreamSpliceFlags flags,
1844 GCancellable *cancellable,
1845 GAsyncReadyCallback callback,
1851 op = g_new0 (SpliceData, 1);
1852 task = g_task_new (stream, cancellable, callback, user_data);
1853 g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
1855 op->source = g_object_ref (source);
1857 if (g_input_stream_async_read_is_via_threads (source) &&
1858 g_output_stream_async_write_is_via_threads (stream))
1860 g_task_run_in_thread (task, splice_async_thread);
1861 g_object_unref (task);
1865 op->buffer = g_malloc (8192);
1866 g_input_stream_read_async (op->source, op->buffer, 8192,
1867 g_task_get_priority (task),
1868 g_task_get_cancellable (task),
1869 real_splice_async_read_cb, task);
1874 g_output_stream_real_splice_finish (GOutputStream *stream,
1875 GAsyncResult *result,
1878 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1880 return g_task_propagate_int (G_TASK (result), error);
1885 flush_async_thread (GTask *task,
1886 gpointer source_object,
1888 GCancellable *cancellable)
1890 GOutputStream *stream = source_object;
1891 GOutputStreamClass *class;
1893 GError *error = NULL;
1895 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1898 result = class->flush (stream, cancellable, &error);
1901 g_task_return_boolean (task, TRUE);
1903 g_task_return_error (task, error);
1907 g_output_stream_real_flush_async (GOutputStream *stream,
1909 GCancellable *cancellable,
1910 GAsyncReadyCallback callback,
1915 task = g_task_new (stream, cancellable, callback, user_data);
1916 g_task_set_priority (task, io_priority);
1917 g_task_run_in_thread (task, flush_async_thread);
1918 g_object_unref (task);
1922 g_output_stream_real_flush_finish (GOutputStream *stream,
1923 GAsyncResult *result,
1926 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1928 return g_task_propagate_boolean (G_TASK (result), error);
1932 close_async_thread (GTask *task,
1933 gpointer source_object,
1935 GCancellable *cancellable)
1937 GOutputStream *stream = source_object;
1938 GOutputStreamClass *class;
1939 GError *error = NULL;
1940 gboolean result = TRUE;
1942 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1944 /* Do a flush here if there is a flush function, and we did not have to do
1945 * an async flush before (see g_output_stream_close_async)
1947 if (class->flush != NULL &&
1948 (class->flush_async == NULL ||
1949 class->flush_async == g_output_stream_real_flush_async))
1951 result = class->flush (stream, cancellable, &error);
1954 /* Auto handling of cancelation disabled, and ignore
1955 cancellation, since we want to close things anyway, although
1956 possibly in a quick-n-dirty way. At least we never want to leak
1959 if (class->close_fn)
1961 /* Make sure to close, even if the flush failed (see sync close) */
1963 class->close_fn (stream, cancellable, NULL);
1965 result = class->close_fn (stream, cancellable, &error);
1969 g_task_return_boolean (task, TRUE);
1971 g_task_return_error (task, error);
1975 g_output_stream_real_close_async (GOutputStream *stream,
1977 GCancellable *cancellable,
1978 GAsyncReadyCallback callback,
1983 task = g_task_new (stream, cancellable, callback, user_data);
1984 g_task_set_priority (task, io_priority);
1985 g_task_run_in_thread (task, close_async_thread);
1986 g_object_unref (task);
1990 g_output_stream_real_close_finish (GOutputStream *stream,
1991 GAsyncResult *result,
1994 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1996 return g_task_propagate_boolean (G_TASK (result), error);