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 * Returns: 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 * Returns: %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 * Returns: %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 * Returns: %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 may still perform partial writes,
403 * just like g_output_stream_write(). If that occurs, to continue
404 * writing, you will need to create a new #GBytes containing just the
405 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
406 * #GBytes instance multiple times potentially can result in duplicated
407 * data in the output stream.
409 * Returns: Number of bytes written, or -1 on error
412 g_output_stream_write_bytes (GOutputStream *stream,
414 GCancellable *cancellable,
420 data = g_bytes_get_data (bytes, &size);
422 return g_output_stream_write (stream,
429 * g_output_stream_flush:
430 * @stream: a #GOutputStream.
431 * @cancellable: (allow-none): optional cancellable object
432 * @error: location to store the error occurring, or %NULL to ignore
434 * Forces a write of all user-space buffered data for the given
435 * @stream. Will block during the operation. Closing the stream will
436 * implicitly cause a flush.
438 * This function is optional for inherited classes.
440 * If @cancellable is not %NULL, then the operation can be cancelled by
441 * triggering the cancellable object from another thread. If the operation
442 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
444 * Returns: %TRUE on success, %FALSE on error
447 g_output_stream_flush (GOutputStream *stream,
448 GCancellable *cancellable,
451 GOutputStreamClass *class;
454 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
456 if (!g_output_stream_set_pending (stream, error))
459 class = G_OUTPUT_STREAM_GET_CLASS (stream);
465 g_cancellable_push_current (cancellable);
467 res = class->flush (stream, cancellable, error);
470 g_cancellable_pop_current (cancellable);
473 g_output_stream_clear_pending (stream);
479 * g_output_stream_splice:
480 * @stream: a #GOutputStream.
481 * @source: a #GInputStream.
482 * @flags: a set of #GOutputStreamSpliceFlags.
483 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
484 * @error: a #GError location to store the error occurring, or %NULL to
487 * Splices an input stream into an output stream.
489 * Returns: a #gssize containing the size of the data spliced, or
490 * -1 if an error occurred. Note that if the number of bytes
491 * spliced is greater than %G_MAXSSIZE, then that will be
492 * returned, and there is no way to determine the actual number
496 g_output_stream_splice (GOutputStream *stream,
497 GInputStream *source,
498 GOutputStreamSpliceFlags flags,
499 GCancellable *cancellable,
502 GOutputStreamClass *class;
505 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
506 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
508 if (g_input_stream_is_closed (source))
510 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
511 _("Source stream is already closed"));
515 if (!g_output_stream_set_pending (stream, error))
518 class = G_OUTPUT_STREAM_GET_CLASS (stream);
521 g_cancellable_push_current (cancellable);
523 bytes_copied = class->splice (stream, source, flags, cancellable, error);
526 g_cancellable_pop_current (cancellable);
528 g_output_stream_clear_pending (stream);
534 g_output_stream_real_splice (GOutputStream *stream,
535 GInputStream *source,
536 GOutputStreamSpliceFlags flags,
537 GCancellable *cancellable,
540 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
541 gssize n_read, n_written;
543 char buffer[8192], *p;
547 if (class->write_fn == NULL)
549 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
550 _("Output stream doesn't implement write"));
558 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
571 n_written = class->write_fn (stream, p, n_read, cancellable, error);
580 bytes_copied += n_written;
583 if (bytes_copied > G_MAXSSIZE)
584 bytes_copied = G_MAXSSIZE;
590 error = NULL; /* Ignore further errors */
592 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
594 /* Don't care about errors in source here */
595 g_input_stream_close (source, cancellable, NULL);
598 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
600 /* But write errors on close are bad! */
601 res = g_output_stream_internal_close (stream, cancellable, error);
610 /* Must always be called inside
611 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
613 g_output_stream_internal_close (GOutputStream *stream,
614 GCancellable *cancellable,
617 GOutputStreamClass *class;
620 if (stream->priv->closed)
623 class = G_OUTPUT_STREAM_GET_CLASS (stream);
625 stream->priv->closing = TRUE;
628 g_cancellable_push_current (cancellable);
631 res = class->flush (stream, cancellable, error);
637 /* flushing caused the error that we want to return,
638 * but we still want to close the underlying stream if possible
641 class->close_fn (stream, cancellable, NULL);
647 res = class->close_fn (stream, cancellable, error);
651 g_cancellable_pop_current (cancellable);
653 stream->priv->closing = FALSE;
654 stream->priv->closed = TRUE;
660 * g_output_stream_close:
661 * @stream: A #GOutputStream.
662 * @cancellable: (allow-none): optional cancellable object
663 * @error: location to store the error occurring, or %NULL to ignore
665 * Closes the stream, releasing resources related to it.
667 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
668 * Closing a stream multiple times will not return an error.
670 * Closing a stream will automatically flush any outstanding buffers in the
673 * Streams will be automatically closed when the last reference
674 * is dropped, but you might want to call this function to make sure
675 * resources are released as early as possible.
677 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
678 * open after the stream is closed. See the documentation for the individual
679 * stream for details.
681 * On failure the first error that happened will be reported, but the close
682 * operation will finish as much as possible. A stream that failed to
683 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
684 * is important to check and report the error to the user, otherwise
685 * there might be a loss of data as all data might not be written.
687 * If @cancellable is not %NULL, then the operation can be cancelled by
688 * triggering the cancellable object from another thread. If the operation
689 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
690 * Cancelling a close will still leave the stream closed, but there some streams
691 * can use a faster close that doesn't block to e.g. check errors. On
692 * cancellation (as with any error) there is no guarantee that all written
693 * data will reach the target.
695 * Returns: %TRUE on success, %FALSE on failure
698 g_output_stream_close (GOutputStream *stream,
699 GCancellable *cancellable,
704 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
706 if (stream->priv->closed)
709 if (!g_output_stream_set_pending (stream, error))
712 res = g_output_stream_internal_close (stream, cancellable, error);
714 g_output_stream_clear_pending (stream);
720 async_ready_write_callback_wrapper (GObject *source_object,
724 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
725 GOutputStreamClass *class;
726 GTask *task = user_data;
728 GError *error = NULL;
730 g_output_stream_clear_pending (stream);
732 if (g_async_result_legacy_propagate_error (res, &error))
736 class = G_OUTPUT_STREAM_GET_CLASS (stream);
737 nwrote = class->write_finish (stream, res, &error);
741 g_task_return_int (task, nwrote);
743 g_task_return_error (task, error);
744 g_object_unref (task);
748 * g_output_stream_write_async:
749 * @stream: A #GOutputStream.
750 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
751 * @count: the number of bytes to write
752 * @io_priority: the io priority of the request.
753 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
754 * @callback: (scope async): callback to call when the request is satisfied
755 * @user_data: (closure): the data to pass to callback function
757 * Request an asynchronous write of @count bytes from @buffer into
758 * the stream. When the operation is finished @callback will be called.
759 * You can then call g_output_stream_write_finish() to get the result of the
762 * During an async request no other sync and async calls are allowed,
763 * and will result in %G_IO_ERROR_PENDING errors.
765 * A value of @count larger than %G_MAXSSIZE will cause a
766 * %G_IO_ERROR_INVALID_ARGUMENT error.
768 * On success, the number of bytes written will be passed to the
769 * @callback. It is not an error if this is not the same as the
770 * requested size, as it can happen e.g. on a partial I/O error,
771 * but generally we try to write as many bytes as requested.
773 * You are guaranteed that this method will never fail with
774 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
775 * method will just wait until this changes.
777 * Any outstanding I/O request with higher priority (lower numerical
778 * value) will be executed before an outstanding request with lower
779 * priority. Default priority is %G_PRIORITY_DEFAULT.
781 * The asyncronous methods have a default fallback that uses threads
782 * to implement asynchronicity, so they are optional for inheriting
783 * classes. However, if you override one you must override all.
785 * For the synchronous, blocking version of this function, see
786 * g_output_stream_write().
788 * Note that no copy of @buffer will be made, so it must stay valid
789 * until @callback is called. See g_output_stream_write_bytes_async()
790 * for a #GBytes version that will automatically hold a reference to
791 * the contents (without copying) for the duration of the call.
794 g_output_stream_write_async (GOutputStream *stream,
798 GCancellable *cancellable,
799 GAsyncReadyCallback callback,
802 GOutputStreamClass *class;
803 GError *error = NULL;
806 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
807 g_return_if_fail (buffer != NULL);
809 task = g_task_new (stream, cancellable, callback, user_data);
810 g_task_set_source_tag (task, g_output_stream_write_async);
811 g_task_set_priority (task, io_priority);
815 g_task_return_int (task, 0);
816 g_object_unref (task);
820 if (((gssize) count) < 0)
822 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
823 _("Too large count value passed to %s"),
825 g_object_unref (task);
829 if (!g_output_stream_set_pending (stream, &error))
831 g_task_return_error (task, error);
832 g_object_unref (task);
836 class = G_OUTPUT_STREAM_GET_CLASS (stream);
838 class->write_async (stream, buffer, count, io_priority, cancellable,
839 async_ready_write_callback_wrapper, task);
843 * g_output_stream_write_finish:
844 * @stream: a #GOutputStream.
845 * @result: a #GAsyncResult.
846 * @error: a #GError location to store the error occurring, or %NULL to
849 * Finishes a stream write operation.
851 * Returns: a #gssize containing the number of bytes written to the stream.
854 g_output_stream_write_finish (GOutputStream *stream,
855 GAsyncResult *result,
858 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
859 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
860 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
862 /* @result is always the GTask created by g_output_stream_write_async();
863 * we called class->write_finish() from async_ready_write_callback_wrapper.
865 return g_task_propagate_int (G_TASK (result), error);
870 const guint8 *buffer;
876 free_async_write_all (gpointer data)
878 g_slice_free (AsyncWriteAll, data);
882 write_all_callback (GObject *stream,
883 GAsyncResult *result,
886 GTask *task = user_data;
887 AsyncWriteAll *data = g_task_get_task_data (task);
891 GError *error = NULL;
894 nwritten = g_output_stream_write_finish (G_OUTPUT_STREAM (stream), result, &error);
898 g_task_return_error (task, error);
899 g_object_unref (task);
903 g_assert_cmpint (nwritten, <=, data->to_write);
904 g_warn_if_fail (nwritten > 0);
906 data->to_write -= nwritten;
907 data->bytes_written += nwritten;
910 if (data->to_write == 0)
912 g_task_return_boolean (task, TRUE);
913 g_object_unref (task);
917 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
918 data->buffer + data->bytes_written,
920 g_task_get_priority (task),
921 g_task_get_cancellable (task),
922 write_all_callback, task);
926 write_all_async_thread (GTask *task,
927 gpointer source_object,
929 GCancellable *cancellable)
931 GOutputStream *stream = source_object;
932 AsyncWriteAll *data = task_data;
933 GError *error = NULL;
935 if (g_output_stream_write_all (stream, data->buffer, data->to_write, &data->bytes_written,
936 g_task_get_cancellable (task), &error))
937 g_task_return_boolean (task, TRUE);
939 g_task_return_error (task, error);
943 * g_output_stream_write_all_async:
944 * @stream: A #GOutputStream
945 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write
946 * @count: the number of bytes to write
947 * @io_priority: the io priority of the request
948 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
949 * @callback: (scope async): callback to call when the request is satisfied
950 * @user_data: (closure): the data to pass to callback function
952 * Request an asynchronous write of @count bytes from @buffer into
953 * the stream. When the operation is finished @callback will be called.
954 * You can then call g_output_stream_write_all_finish() to get the result of the
957 * This is the asynchronous version of g_output_stream_write_all().
959 * Call g_output_stream_write_all_finish() to collect the result.
961 * Any outstanding I/O request with higher priority (lower numerical
962 * value) will be executed before an outstanding request with lower
963 * priority. Default priority is %G_PRIORITY_DEFAULT.
965 * Note that no copy of @buffer will be made, so it must stay valid
966 * until @callback is called.
971 g_output_stream_write_all_async (GOutputStream *stream,
975 GCancellable *cancellable,
976 GAsyncReadyCallback callback,
982 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
983 g_return_if_fail (buffer != NULL || count == 0);
985 task = g_task_new (stream, cancellable, callback, user_data);
986 data = g_slice_new0 (AsyncWriteAll);
987 data->buffer = buffer;
988 data->to_write = count;
990 g_task_set_task_data (task, data, free_async_write_all);
991 g_task_set_priority (task, io_priority);
993 /* If async writes are going to be handled via the threadpool anyway
994 * then we may as well do it with a single dispatch instead of
995 * bouncing in and out.
997 if (g_output_stream_async_write_is_via_threads (stream))
999 g_task_run_in_thread (task, write_all_async_thread);
1000 g_object_unref (task);
1003 write_all_callback (G_OBJECT (stream), NULL, task);
1007 * g_output_stream_write_all_finish:
1008 * @stream: a #GOutputStream
1009 * @result: a #GAsyncResult
1010 * @bytes_written: (out): location to store the number of bytes that was written to the stream
1011 * @error: a #GError location to store the error occurring, or %NULL to ignore.
1013 * Finishes an asynchronous stream write operation started with
1014 * g_output_stream_write_all_async().
1016 * As a special exception to the normal conventions for functions that
1017 * use #GError, if this function returns %FALSE (and sets @error) then
1018 * @bytes_written will be set to the number of bytes that were
1019 * successfully written before the error was encountered. This
1020 * functionality is only available from C. If you need it from another
1021 * language then you must write your own loop around
1022 * g_output_stream_write_async().
1024 * Returns: %TRUE on success, %FALSE if there was an error
1029 g_output_stream_write_all_finish (GOutputStream *stream,
1030 GAsyncResult *result,
1031 gsize *bytes_written,
1036 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1037 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1039 task = G_TASK (result);
1043 AsyncWriteAll *data = (AsyncWriteAll *)g_task_get_task_data (task);
1045 *bytes_written = data->bytes_written;
1048 return g_task_propagate_boolean (task, error);
1052 write_bytes_callback (GObject *stream,
1053 GAsyncResult *result,
1056 GTask *task = user_data;
1057 GError *error = NULL;
1060 nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
1063 g_task_return_error (task, error);
1065 g_task_return_int (task, nwrote);
1066 g_object_unref (task);
1070 * g_output_stream_write_bytes_async:
1071 * @stream: A #GOutputStream.
1072 * @bytes: The bytes to write
1073 * @io_priority: the io priority of the request.
1074 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1075 * @callback: (scope async): callback to call when the request is satisfied
1076 * @user_data: (closure): the data to pass to callback function
1078 * This function is similar to g_output_stream_write_async(), but
1079 * takes a #GBytes as input. Due to the refcounted nature of #GBytes,
1080 * this allows the stream to avoid taking a copy of the data.
1082 * However, note that this function may still perform partial writes,
1083 * just like g_output_stream_write_async(). If that occurs, to continue
1084 * writing, you will need to create a new #GBytes containing just the
1085 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
1086 * #GBytes instance multiple times potentially can result in duplicated
1087 * data in the output stream.
1089 * For the synchronous, blocking version of this function, see
1090 * g_output_stream_write_bytes().
1093 g_output_stream_write_bytes_async (GOutputStream *stream,
1096 GCancellable *cancellable,
1097 GAsyncReadyCallback callback,
1104 data = g_bytes_get_data (bytes, &size);
1106 task = g_task_new (stream, cancellable, callback, user_data);
1107 g_task_set_task_data (task, g_bytes_ref (bytes),
1108 (GDestroyNotify) g_bytes_unref);
1110 g_output_stream_write_async (stream,
1114 write_bytes_callback,
1119 * g_output_stream_write_bytes_finish:
1120 * @stream: a #GOutputStream.
1121 * @result: a #GAsyncResult.
1122 * @error: a #GError location to store the error occurring, or %NULL to
1125 * Finishes a stream write-from-#GBytes operation.
1127 * Returns: a #gssize containing the number of bytes written to the stream.
1130 g_output_stream_write_bytes_finish (GOutputStream *stream,
1131 GAsyncResult *result,
1134 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
1135 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1137 return g_task_propagate_int (G_TASK (result), error);
1141 async_ready_splice_callback_wrapper (GObject *source_object,
1145 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1146 GOutputStreamClass *class;
1147 GTask *task = _data;
1149 GError *error = NULL;
1151 g_output_stream_clear_pending (stream);
1153 if (g_async_result_legacy_propagate_error (res, &error))
1157 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1158 nspliced = class->splice_finish (stream, res, &error);
1162 g_task_return_int (task, nspliced);
1164 g_task_return_error (task, error);
1165 g_object_unref (task);
1169 * g_output_stream_splice_async:
1170 * @stream: a #GOutputStream.
1171 * @source: a #GInputStream.
1172 * @flags: a set of #GOutputStreamSpliceFlags.
1173 * @io_priority: the io priority of the request.
1174 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1175 * @callback: (scope async): a #GAsyncReadyCallback.
1176 * @user_data: (closure): user data passed to @callback.
1178 * Splices a stream asynchronously.
1179 * When the operation is finished @callback will be called.
1180 * You can then call g_output_stream_splice_finish() to get the
1181 * result of the operation.
1183 * For the synchronous, blocking version of this function, see
1184 * g_output_stream_splice().
1187 g_output_stream_splice_async (GOutputStream *stream,
1188 GInputStream *source,
1189 GOutputStreamSpliceFlags flags,
1191 GCancellable *cancellable,
1192 GAsyncReadyCallback callback,
1195 GOutputStreamClass *class;
1197 GError *error = NULL;
1199 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1200 g_return_if_fail (G_IS_INPUT_STREAM (source));
1202 task = g_task_new (stream, cancellable, callback, user_data);
1203 g_task_set_source_tag (task, g_output_stream_splice_async);
1204 g_task_set_priority (task, io_priority);
1205 g_task_set_task_data (task, g_object_ref (source), g_object_unref);
1207 if (g_input_stream_is_closed (source))
1209 g_task_return_new_error (task,
1210 G_IO_ERROR, G_IO_ERROR_CLOSED,
1211 _("Source stream is already closed"));
1212 g_object_unref (task);
1216 if (!g_output_stream_set_pending (stream, &error))
1218 g_task_return_error (task, error);
1219 g_object_unref (task);
1223 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1225 class->splice_async (stream, source, flags, io_priority, cancellable,
1226 async_ready_splice_callback_wrapper, task);
1230 * g_output_stream_splice_finish:
1231 * @stream: a #GOutputStream.
1232 * @result: a #GAsyncResult.
1233 * @error: a #GError location to store the error occurring, or %NULL to
1236 * Finishes an asynchronous stream splice operation.
1238 * Returns: a #gssize of the number of bytes spliced. Note that if the
1239 * number of bytes spliced is greater than %G_MAXSSIZE, then that
1240 * will be returned, and there is no way to determine the actual
1241 * number of bytes spliced.
1244 g_output_stream_splice_finish (GOutputStream *stream,
1245 GAsyncResult *result,
1248 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1249 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1250 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
1252 /* @result is always the GTask created by g_output_stream_splice_async();
1253 * we called class->splice_finish() from async_ready_splice_callback_wrapper.
1255 return g_task_propagate_int (G_TASK (result), error);
1259 async_ready_flush_callback_wrapper (GObject *source_object,
1263 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1264 GOutputStreamClass *class;
1265 GTask *task = user_data;
1267 GError *error = NULL;
1269 g_output_stream_clear_pending (stream);
1271 if (g_async_result_legacy_propagate_error (res, &error))
1275 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1276 flushed = class->flush_finish (stream, res, &error);
1280 g_task_return_boolean (task, TRUE);
1282 g_task_return_error (task, error);
1283 g_object_unref (task);
1287 * g_output_stream_flush_async:
1288 * @stream: a #GOutputStream.
1289 * @io_priority: the io priority of the request.
1290 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1291 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1292 * @user_data: (closure): the data to pass to callback function
1294 * Forces an asynchronous write of all user-space buffered data for
1295 * the given @stream.
1296 * For behaviour details see g_output_stream_flush().
1298 * When the operation is finished @callback will be
1299 * called. You can then call g_output_stream_flush_finish() to get the
1300 * result of the operation.
1303 g_output_stream_flush_async (GOutputStream *stream,
1305 GCancellable *cancellable,
1306 GAsyncReadyCallback callback,
1309 GOutputStreamClass *class;
1311 GError *error = NULL;
1313 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1315 task = g_task_new (stream, cancellable, callback, user_data);
1316 g_task_set_source_tag (task, g_output_stream_flush_async);
1317 g_task_set_priority (task, io_priority);
1319 if (!g_output_stream_set_pending (stream, &error))
1321 g_task_return_error (task, error);
1322 g_object_unref (task);
1326 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1328 if (class->flush_async == NULL)
1330 g_task_return_boolean (task, TRUE);
1331 g_object_unref (task);
1335 class->flush_async (stream, io_priority, cancellable,
1336 async_ready_flush_callback_wrapper, task);
1340 * g_output_stream_flush_finish:
1341 * @stream: a #GOutputStream.
1342 * @result: a GAsyncResult.
1343 * @error: a #GError location to store the error occurring, or %NULL to
1346 * Finishes flushing an output stream.
1348 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1351 g_output_stream_flush_finish (GOutputStream *stream,
1352 GAsyncResult *result,
1355 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1356 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1357 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
1359 /* @result is always the GTask created by g_output_stream_flush_async();
1360 * we called class->flush_finish() from async_ready_flush_callback_wrapper.
1362 return g_task_propagate_boolean (G_TASK (result), error);
1367 async_ready_close_callback_wrapper (GObject *source_object,
1371 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1372 GOutputStreamClass *class;
1373 GTask *task = user_data;
1374 GError *error = g_task_get_task_data (task);
1376 stream->priv->closing = FALSE;
1377 stream->priv->closed = TRUE;
1379 if (!error && !g_async_result_legacy_propagate_error (res, &error))
1381 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1383 class->close_finish (stream, res,
1384 error ? NULL : &error);
1388 g_task_return_error (task, error);
1390 g_task_return_boolean (task, TRUE);
1391 g_object_unref (task);
1395 async_ready_close_flushed_callback_wrapper (GObject *source_object,
1399 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1400 GOutputStreamClass *class;
1401 GTask *task = user_data;
1402 GError *error = NULL;
1404 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1406 if (!g_async_result_legacy_propagate_error (res, &error))
1408 class->flush_finish (stream, res, &error);
1411 /* propagate the possible error */
1413 g_task_set_task_data (task, error, NULL);
1415 /* we still close, even if there was a flush error */
1416 class->close_async (stream,
1417 g_task_get_priority (task),
1418 g_task_get_cancellable (task),
1419 async_ready_close_callback_wrapper, task);
1423 real_close_async_cb (GObject *source_object,
1427 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1428 GTask *task = user_data;
1429 GError *error = NULL;
1432 g_output_stream_clear_pending (stream);
1434 ret = g_output_stream_internal_close_finish (stream, res, &error);
1437 g_task_return_error (task, error);
1439 g_task_return_boolean (task, ret);
1441 g_object_unref (task);
1445 * g_output_stream_close_async:
1446 * @stream: A #GOutputStream.
1447 * @io_priority: the io priority of the request.
1448 * @cancellable: (allow-none): optional cancellable object
1449 * @callback: (scope async): callback to call when the request is satisfied
1450 * @user_data: (closure): the data to pass to callback function
1452 * Requests an asynchronous close of the stream, releasing resources
1453 * related to it. When the operation is finished @callback will be
1454 * called. You can then call g_output_stream_close_finish() to get
1455 * the result of the operation.
1457 * For behaviour details see g_output_stream_close().
1459 * The asyncronous methods have a default fallback that uses threads
1460 * to implement asynchronicity, so they are optional for inheriting
1461 * classes. However, if you override one you must override all.
1464 g_output_stream_close_async (GOutputStream *stream,
1466 GCancellable *cancellable,
1467 GAsyncReadyCallback callback,
1471 GError *error = NULL;
1473 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1475 task = g_task_new (stream, cancellable, callback, user_data);
1476 g_task_set_source_tag (task, g_output_stream_close_async);
1477 g_task_set_priority (task, io_priority);
1479 if (!g_output_stream_set_pending (stream, &error))
1481 g_task_return_error (task, error);
1482 g_object_unref (task);
1486 g_output_stream_internal_close_async (stream, io_priority, cancellable,
1487 real_close_async_cb, task);
1490 /* Must always be called inside
1491 * g_output_stream_set_pending()/g_output_stream_clear_pending().
1494 g_output_stream_internal_close_async (GOutputStream *stream,
1496 GCancellable *cancellable,
1497 GAsyncReadyCallback callback,
1500 GOutputStreamClass *class;
1503 task = g_task_new (stream, cancellable, callback, user_data);
1504 g_task_set_source_tag (task, g_output_stream_internal_close_async);
1505 g_task_set_priority (task, io_priority);
1507 if (stream->priv->closed)
1509 g_task_return_boolean (task, TRUE);
1510 g_object_unref (task);
1514 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1515 stream->priv->closing = TRUE;
1517 /* Call close_async directly if there is no need to flush, or if the flush
1518 can be done sync (in the output stream async close thread) */
1519 if (class->flush_async == NULL ||
1520 (class->flush_async == g_output_stream_real_flush_async &&
1521 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1523 class->close_async (stream, io_priority, cancellable,
1524 async_ready_close_callback_wrapper, task);
1528 /* First do an async flush, then do the async close in the callback
1529 wrapper (see async_ready_close_flushed_callback_wrapper) */
1530 class->flush_async (stream, io_priority, cancellable,
1531 async_ready_close_flushed_callback_wrapper, task);
1536 g_output_stream_internal_close_finish (GOutputStream *stream,
1537 GAsyncResult *result,
1540 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1541 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1542 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
1544 return g_task_propagate_boolean (G_TASK (result), error);
1548 * g_output_stream_close_finish:
1549 * @stream: a #GOutputStream.
1550 * @result: a #GAsyncResult.
1551 * @error: a #GError location to store the error occurring, or %NULL to
1554 * Closes an output stream.
1556 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1559 g_output_stream_close_finish (GOutputStream *stream,
1560 GAsyncResult *result,
1563 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1564 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1565 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
1567 /* @result is always the GTask created by g_output_stream_close_async();
1568 * we called class->close_finish() from async_ready_close_callback_wrapper.
1570 return g_task_propagate_boolean (G_TASK (result), error);
1574 * g_output_stream_is_closed:
1575 * @stream: a #GOutputStream.
1577 * Checks if an output stream has already been closed.
1579 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1582 g_output_stream_is_closed (GOutputStream *stream)
1584 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1586 return stream->priv->closed;
1590 * g_output_stream_is_closing:
1591 * @stream: a #GOutputStream.
1593 * Checks if an output stream is being closed. This can be
1594 * used inside e.g. a flush implementation to see if the
1595 * flush (or other i/o operation) is called from within
1596 * the closing operation.
1598 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1603 g_output_stream_is_closing (GOutputStream *stream)
1605 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1607 return stream->priv->closing;
1611 * g_output_stream_has_pending:
1612 * @stream: a #GOutputStream.
1614 * Checks if an ouput stream has pending actions.
1616 * Returns: %TRUE if @stream has pending actions.
1619 g_output_stream_has_pending (GOutputStream *stream)
1621 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1623 return stream->priv->pending;
1627 * g_output_stream_set_pending:
1628 * @stream: a #GOutputStream.
1629 * @error: a #GError location to store the error occurring, or %NULL to
1632 * Sets @stream to have actions pending. If the pending flag is
1633 * already set or @stream is closed, it will return %FALSE and set
1636 * Returns: %TRUE if pending was previously unset and is now set.
1639 g_output_stream_set_pending (GOutputStream *stream,
1642 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1644 if (stream->priv->closed)
1646 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1647 _("Stream is already closed"));
1651 if (stream->priv->pending)
1653 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1654 /* Translators: This is an error you get if there is
1655 * already an operation running against this stream when
1656 * you try to start one */
1657 _("Stream has outstanding operation"));
1661 stream->priv->pending = TRUE;
1666 * g_output_stream_clear_pending:
1667 * @stream: output stream
1669 * Clears the pending flag on @stream.
1672 g_output_stream_clear_pending (GOutputStream *stream)
1674 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1676 stream->priv->pending = FALSE;
1680 * g_output_stream_async_write_is_via_threads:
1681 * @stream: a #GOutputStream.
1683 * Checks if an ouput stream's write_async function uses threads.
1685 * Returns: %TRUE if @stream's write_async function uses threads.
1688 g_output_stream_async_write_is_via_threads (GOutputStream *stream)
1690 GOutputStreamClass *class;
1692 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1694 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1696 return (class->write_async == g_output_stream_real_write_async &&
1697 !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
1698 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
1702 /********************************************
1703 * Default implementation of async ops *
1704 ********************************************/
1708 gsize count_requested;
1709 gssize count_written;
1713 free_write_data (WriteData *op)
1715 g_slice_free (WriteData, op);
1719 write_async_thread (GTask *task,
1720 gpointer source_object,
1722 GCancellable *cancellable)
1724 GOutputStream *stream = source_object;
1725 WriteData *op = task_data;
1726 GOutputStreamClass *class;
1727 GError *error = NULL;
1728 gssize count_written;
1730 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1731 count_written = class->write_fn (stream, op->buffer, op->count_requested,
1732 cancellable, &error);
1733 if (count_written == -1)
1734 g_task_return_error (task, error);
1736 g_task_return_int (task, count_written);
1739 static void write_async_pollable (GPollableOutputStream *stream,
1743 write_async_pollable_ready (GPollableOutputStream *stream,
1746 GTask *task = user_data;
1748 write_async_pollable (stream, task);
1753 write_async_pollable (GPollableOutputStream *stream,
1756 GError *error = NULL;
1757 WriteData *op = g_task_get_task_data (task);
1758 gssize count_written;
1760 if (g_task_return_error_if_cancelled (task))
1763 count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
1764 write_nonblocking (stream, op->buffer, op->count_requested, &error);
1766 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1770 g_error_free (error);
1772 source = g_pollable_output_stream_create_source (stream,
1773 g_task_get_cancellable (task));
1774 g_task_attach_source (task, source,
1775 (GSourceFunc) write_async_pollable_ready);
1776 g_source_unref (source);
1780 if (count_written == -1)
1781 g_task_return_error (task, error);
1783 g_task_return_int (task, count_written);
1787 g_output_stream_real_write_async (GOutputStream *stream,
1791 GCancellable *cancellable,
1792 GAsyncReadyCallback callback,
1798 op = g_slice_new0 (WriteData);
1799 task = g_task_new (stream, cancellable, callback, user_data);
1800 g_task_set_check_cancellable (task, FALSE);
1801 g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
1802 op->buffer = buffer;
1803 op->count_requested = count;
1805 if (!g_output_stream_async_write_is_via_threads (stream))
1806 write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
1808 g_task_run_in_thread (task, write_async_thread);
1809 g_object_unref (task);
1813 g_output_stream_real_write_finish (GOutputStream *stream,
1814 GAsyncResult *result,
1817 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1819 return g_task_propagate_int (G_TASK (result), error);
1823 GInputStream *source;
1824 GOutputStreamSpliceFlags flags;
1833 free_splice_data (SpliceData *op)
1835 g_clear_pointer (&op->buffer, g_free);
1836 g_object_unref (op->source);
1837 g_clear_error (&op->error);
1842 real_splice_async_complete_cb (GTask *task)
1844 SpliceData *op = g_task_get_task_data (task);
1846 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
1847 !g_input_stream_is_closed (op->source))
1850 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
1851 !g_output_stream_is_closed (g_task_get_source_object (task)))
1854 if (op->error != NULL)
1856 g_task_return_error (task, op->error);
1861 g_task_return_int (task, op->bytes_copied);
1864 g_object_unref (task);
1868 real_splice_async_close_input_cb (GObject *source,
1872 GTask *task = user_data;
1874 g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
1876 real_splice_async_complete_cb (task);
1880 real_splice_async_close_output_cb (GObject *source,
1884 GTask *task = G_TASK (user_data);
1885 SpliceData *op = g_task_get_task_data (task);
1886 GError **error = (op->error == NULL) ? &op->error : NULL;
1888 g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
1890 real_splice_async_complete_cb (task);
1894 real_splice_async_complete (GTask *task)
1896 SpliceData *op = g_task_get_task_data (task);
1897 gboolean done = TRUE;
1899 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
1902 g_input_stream_close_async (op->source, g_task_get_priority (task),
1903 g_task_get_cancellable (task),
1904 real_splice_async_close_input_cb, task);
1907 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
1910 g_output_stream_internal_close_async (g_task_get_source_object (task),
1911 g_task_get_priority (task),
1912 g_task_get_cancellable (task),
1913 real_splice_async_close_output_cb,
1918 real_splice_async_complete_cb (task);
1921 static void real_splice_async_read_cb (GObject *source,
1923 gpointer user_data);
1926 real_splice_async_write_cb (GObject *source,
1930 GOutputStreamClass *class;
1931 GTask *task = G_TASK (user_data);
1932 SpliceData *op = g_task_get_task_data (task);
1935 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1937 ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
1941 real_splice_async_complete (task);
1945 op->n_written += ret;
1946 op->bytes_copied += ret;
1947 if (op->bytes_copied > G_MAXSSIZE)
1948 op->bytes_copied = G_MAXSSIZE;
1950 if (op->n_written < op->n_read)
1952 class->write_async (g_task_get_source_object (task),
1953 op->buffer + op->n_written,
1954 op->n_read - op->n_written,
1955 g_task_get_priority (task),
1956 g_task_get_cancellable (task),
1957 real_splice_async_write_cb, task);
1961 g_input_stream_read_async (op->source, op->buffer, 8192,
1962 g_task_get_priority (task),
1963 g_task_get_cancellable (task),
1964 real_splice_async_read_cb, task);
1968 real_splice_async_read_cb (GObject *source,
1972 GOutputStreamClass *class;
1973 GTask *task = G_TASK (user_data);
1974 SpliceData *op = g_task_get_task_data (task);
1977 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1979 ret = g_input_stream_read_finish (op->source, res, &op->error);
1980 if (ret == -1 || ret == 0)
1982 real_splice_async_complete (task);
1989 class->write_async (g_task_get_source_object (task), op->buffer,
1990 op->n_read, g_task_get_priority (task),
1991 g_task_get_cancellable (task),
1992 real_splice_async_write_cb, task);
1996 splice_async_thread (GTask *task,
1997 gpointer source_object,
1999 GCancellable *cancellable)
2001 GOutputStream *stream = source_object;
2002 SpliceData *op = task_data;
2003 GOutputStreamClass *class;
2004 GError *error = NULL;
2005 gssize bytes_copied;
2007 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2009 bytes_copied = class->splice (stream,
2014 if (bytes_copied == -1)
2015 g_task_return_error (task, error);
2017 g_task_return_int (task, bytes_copied);
2021 g_output_stream_real_splice_async (GOutputStream *stream,
2022 GInputStream *source,
2023 GOutputStreamSpliceFlags flags,
2025 GCancellable *cancellable,
2026 GAsyncReadyCallback callback,
2032 op = g_new0 (SpliceData, 1);
2033 task = g_task_new (stream, cancellable, callback, user_data);
2034 g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
2036 op->source = g_object_ref (source);
2038 if (g_input_stream_async_read_is_via_threads (source) &&
2039 g_output_stream_async_write_is_via_threads (stream))
2041 g_task_run_in_thread (task, splice_async_thread);
2042 g_object_unref (task);
2046 op->buffer = g_malloc (8192);
2047 g_input_stream_read_async (op->source, op->buffer, 8192,
2048 g_task_get_priority (task),
2049 g_task_get_cancellable (task),
2050 real_splice_async_read_cb, task);
2055 g_output_stream_real_splice_finish (GOutputStream *stream,
2056 GAsyncResult *result,
2059 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2061 return g_task_propagate_int (G_TASK (result), error);
2066 flush_async_thread (GTask *task,
2067 gpointer source_object,
2069 GCancellable *cancellable)
2071 GOutputStream *stream = source_object;
2072 GOutputStreamClass *class;
2074 GError *error = NULL;
2076 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2079 result = class->flush (stream, cancellable, &error);
2082 g_task_return_boolean (task, TRUE);
2084 g_task_return_error (task, error);
2088 g_output_stream_real_flush_async (GOutputStream *stream,
2090 GCancellable *cancellable,
2091 GAsyncReadyCallback callback,
2096 task = g_task_new (stream, cancellable, callback, user_data);
2097 g_task_set_priority (task, io_priority);
2098 g_task_run_in_thread (task, flush_async_thread);
2099 g_object_unref (task);
2103 g_output_stream_real_flush_finish (GOutputStream *stream,
2104 GAsyncResult *result,
2107 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2109 return g_task_propagate_boolean (G_TASK (result), error);
2113 close_async_thread (GTask *task,
2114 gpointer source_object,
2116 GCancellable *cancellable)
2118 GOutputStream *stream = source_object;
2119 GOutputStreamClass *class;
2120 GError *error = NULL;
2121 gboolean result = TRUE;
2123 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2125 /* Do a flush here if there is a flush function, and we did not have to do
2126 * an async flush before (see g_output_stream_close_async)
2128 if (class->flush != NULL &&
2129 (class->flush_async == NULL ||
2130 class->flush_async == g_output_stream_real_flush_async))
2132 result = class->flush (stream, cancellable, &error);
2135 /* Auto handling of cancelation disabled, and ignore
2136 cancellation, since we want to close things anyway, although
2137 possibly in a quick-n-dirty way. At least we never want to leak
2140 if (class->close_fn)
2142 /* Make sure to close, even if the flush failed (see sync close) */
2144 class->close_fn (stream, cancellable, NULL);
2146 result = class->close_fn (stream, cancellable, &error);
2150 g_task_return_boolean (task, TRUE);
2152 g_task_return_error (task, error);
2156 g_output_stream_real_close_async (GOutputStream *stream,
2158 GCancellable *cancellable,
2159 GAsyncReadyCallback callback,
2164 task = g_task_new (stream, cancellable, callback, user_data);
2165 g_task_set_priority (task, io_priority);
2166 g_task_run_in_thread (task, close_async_thread);
2167 g_object_unref (task);
2171 g_output_stream_real_close_finish (GOutputStream *stream,
2172 GAsyncResult *result,
2175 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2177 return g_task_propagate_boolean (G_TASK (result), error);