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.
251 * As a special exception to the normal conventions for functions that
252 * use #GError, if this function returns %FALSE (and sets @error) then
253 * @bytes_written will be set to the number of bytes that were
254 * successfully written before the error was encountered. This
255 * functionality is only available from C. If you need it from another
256 * language then you must write your own loop around
257 * g_output_stream_write().
259 * Returns: %TRUE on success, %FALSE if there was an error
262 g_output_stream_write_all (GOutputStream *stream,
265 gsize *bytes_written,
266 GCancellable *cancellable,
269 gsize _bytes_written;
272 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
273 g_return_val_if_fail (buffer != NULL, FALSE);
276 while (_bytes_written < count)
278 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
283 *bytes_written = _bytes_written;
288 g_warning ("Write returned zero without error");
290 _bytes_written += res;
294 *bytes_written = _bytes_written;
300 * g_output_stream_printf:
301 * @stream: a #GOutputStream.
302 * @bytes_written: (out): location to store the number of bytes that was
303 * written to the stream
304 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
305 * @error: location to store the error occurring, or %NULL to ignore
306 * @format: the format string. See the printf() documentation
307 * @...: the parameters to insert into the format string
309 * This is a utility function around g_output_stream_write_all(). It
310 * uses g_strdup_vprintf() to turn @format and @... into a string that
311 * is then written to @stream.
313 * See the documentation of g_output_stream_write_all() about the
314 * behavior of the actual write operation.
316 * Note that partial writes cannot be properly checked with this
317 * function due to the variable length of the written string, if you
318 * need precise control over partial write failures, you need to
319 * create you own printf()-like wrapper around g_output_stream_write()
320 * or g_output_stream_write_all().
324 * Returns: %TRUE on success, %FALSE if there was an error
327 g_output_stream_printf (GOutputStream *stream,
328 gsize *bytes_written,
329 GCancellable *cancellable,
337 va_start (args, format);
338 success = g_output_stream_vprintf (stream, bytes_written, cancellable,
339 error, format, args);
346 * g_output_stream_vprintf:
347 * @stream: a #GOutputStream.
348 * @bytes_written: (out): location to store the number of bytes that was
349 * written to the stream
350 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
351 * @error: location to store the error occurring, or %NULL to ignore
352 * @format: the format string. See the printf() documentation
353 * @args: the parameters to insert into the format string
355 * This is a utility function around g_output_stream_write_all(). It
356 * uses g_strdup_vprintf() to turn @format and @args into a string that
357 * is then written to @stream.
359 * See the documentation of g_output_stream_write_all() about the
360 * behavior of the actual write operation.
362 * Note that partial writes cannot be properly checked with this
363 * function due to the variable length of the written string, if you
364 * need precise control over partial write failures, you need to
365 * create you own printf()-like wrapper around g_output_stream_write()
366 * or g_output_stream_write_all().
370 * Returns: %TRUE on success, %FALSE if there was an error
373 g_output_stream_vprintf (GOutputStream *stream,
374 gsize *bytes_written,
375 GCancellable *cancellable,
383 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
384 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (stream), FALSE);
385 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
386 g_return_val_if_fail (format != NULL, FALSE);
388 text = g_strdup_vprintf (format, args);
389 success = g_output_stream_write_all (stream,
391 bytes_written, cancellable, error);
398 * g_output_stream_write_bytes:
399 * @stream: a #GOutputStream.
400 * @bytes: the #GBytes to write
401 * @cancellable: (allow-none): optional cancellable object
402 * @error: location to store the error occurring, or %NULL to ignore
404 * A wrapper function for g_output_stream_write() which takes a
405 * #GBytes as input. This can be more convenient for use by language
406 * bindings or in other cases where the refcounted nature of #GBytes
407 * is helpful over a bare pointer interface.
409 * However, note that this function may still perform partial writes,
410 * just like g_output_stream_write(). If that occurs, to continue
411 * writing, you will need to create a new #GBytes containing just the
412 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
413 * #GBytes instance multiple times potentially can result in duplicated
414 * data in the output stream.
416 * Returns: Number of bytes written, or -1 on error
419 g_output_stream_write_bytes (GOutputStream *stream,
421 GCancellable *cancellable,
427 data = g_bytes_get_data (bytes, &size);
429 return g_output_stream_write (stream,
436 * g_output_stream_flush:
437 * @stream: a #GOutputStream.
438 * @cancellable: (allow-none): optional cancellable object
439 * @error: location to store the error occurring, or %NULL to ignore
441 * Forces a write of all user-space buffered data for the given
442 * @stream. Will block during the operation. Closing the stream will
443 * implicitly cause a flush.
445 * This function is optional for inherited classes.
447 * If @cancellable is not %NULL, then the operation can be cancelled by
448 * triggering the cancellable object from another thread. If the operation
449 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
451 * Returns: %TRUE on success, %FALSE on error
454 g_output_stream_flush (GOutputStream *stream,
455 GCancellable *cancellable,
458 GOutputStreamClass *class;
461 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
463 if (!g_output_stream_set_pending (stream, error))
466 class = G_OUTPUT_STREAM_GET_CLASS (stream);
472 g_cancellable_push_current (cancellable);
474 res = class->flush (stream, cancellable, error);
477 g_cancellable_pop_current (cancellable);
480 g_output_stream_clear_pending (stream);
486 * g_output_stream_splice:
487 * @stream: a #GOutputStream.
488 * @source: a #GInputStream.
489 * @flags: a set of #GOutputStreamSpliceFlags.
490 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
491 * @error: a #GError location to store the error occurring, or %NULL to
494 * Splices an input stream into an output stream.
496 * Returns: a #gssize containing the size of the data spliced, or
497 * -1 if an error occurred. Note that if the number of bytes
498 * spliced is greater than %G_MAXSSIZE, then that will be
499 * returned, and there is no way to determine the actual number
503 g_output_stream_splice (GOutputStream *stream,
504 GInputStream *source,
505 GOutputStreamSpliceFlags flags,
506 GCancellable *cancellable,
509 GOutputStreamClass *class;
512 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
513 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
515 if (g_input_stream_is_closed (source))
517 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
518 _("Source stream is already closed"));
522 if (!g_output_stream_set_pending (stream, error))
525 class = G_OUTPUT_STREAM_GET_CLASS (stream);
528 g_cancellable_push_current (cancellable);
530 bytes_copied = class->splice (stream, source, flags, cancellable, error);
533 g_cancellable_pop_current (cancellable);
535 g_output_stream_clear_pending (stream);
541 g_output_stream_real_splice (GOutputStream *stream,
542 GInputStream *source,
543 GOutputStreamSpliceFlags flags,
544 GCancellable *cancellable,
547 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
548 gssize n_read, n_written;
550 char buffer[8192], *p;
554 if (class->write_fn == NULL)
556 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
557 _("Output stream doesn't implement write"));
565 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
578 n_written = class->write_fn (stream, p, n_read, cancellable, error);
587 bytes_copied += n_written;
590 if (bytes_copied > G_MAXSSIZE)
591 bytes_copied = G_MAXSSIZE;
597 error = NULL; /* Ignore further errors */
599 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
601 /* Don't care about errors in source here */
602 g_input_stream_close (source, cancellable, NULL);
605 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
607 /* But write errors on close are bad! */
608 res = g_output_stream_internal_close (stream, cancellable, error);
617 /* Must always be called inside
618 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
620 g_output_stream_internal_close (GOutputStream *stream,
621 GCancellable *cancellable,
624 GOutputStreamClass *class;
627 if (stream->priv->closed)
630 class = G_OUTPUT_STREAM_GET_CLASS (stream);
632 stream->priv->closing = TRUE;
635 g_cancellable_push_current (cancellable);
638 res = class->flush (stream, cancellable, error);
644 /* flushing caused the error that we want to return,
645 * but we still want to close the underlying stream if possible
648 class->close_fn (stream, cancellable, NULL);
654 res = class->close_fn (stream, cancellable, error);
658 g_cancellable_pop_current (cancellable);
660 stream->priv->closing = FALSE;
661 stream->priv->closed = TRUE;
667 * g_output_stream_close:
668 * @stream: A #GOutputStream.
669 * @cancellable: (allow-none): optional cancellable object
670 * @error: location to store the error occurring, or %NULL to ignore
672 * Closes the stream, releasing resources related to it.
674 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
675 * Closing a stream multiple times will not return an error.
677 * Closing a stream will automatically flush any outstanding buffers in the
680 * Streams will be automatically closed when the last reference
681 * is dropped, but you might want to call this function to make sure
682 * resources are released as early as possible.
684 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
685 * open after the stream is closed. See the documentation for the individual
686 * stream for details.
688 * On failure the first error that happened will be reported, but the close
689 * operation will finish as much as possible. A stream that failed to
690 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
691 * is important to check and report the error to the user, otherwise
692 * there might be a loss of data as all data might not be written.
694 * If @cancellable is not %NULL, then the operation can be cancelled by
695 * triggering the cancellable object from another thread. If the operation
696 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
697 * Cancelling a close will still leave the stream closed, but there some streams
698 * can use a faster close that doesn't block to e.g. check errors. On
699 * cancellation (as with any error) there is no guarantee that all written
700 * data will reach the target.
702 * Returns: %TRUE on success, %FALSE on failure
705 g_output_stream_close (GOutputStream *stream,
706 GCancellable *cancellable,
711 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
713 if (stream->priv->closed)
716 if (!g_output_stream_set_pending (stream, error))
719 res = g_output_stream_internal_close (stream, cancellable, error);
721 g_output_stream_clear_pending (stream);
727 async_ready_write_callback_wrapper (GObject *source_object,
731 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
732 GOutputStreamClass *class;
733 GTask *task = user_data;
735 GError *error = NULL;
737 g_output_stream_clear_pending (stream);
739 if (g_async_result_legacy_propagate_error (res, &error))
743 class = G_OUTPUT_STREAM_GET_CLASS (stream);
744 nwrote = class->write_finish (stream, res, &error);
748 g_task_return_int (task, nwrote);
750 g_task_return_error (task, error);
751 g_object_unref (task);
755 * g_output_stream_write_async:
756 * @stream: A #GOutputStream.
757 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
758 * @count: the number of bytes to write
759 * @io_priority: the io priority of the request.
760 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
761 * @callback: (scope async): callback to call when the request is satisfied
762 * @user_data: (closure): the data to pass to callback function
764 * Request an asynchronous write of @count bytes from @buffer into
765 * the stream. When the operation is finished @callback will be called.
766 * You can then call g_output_stream_write_finish() to get the result of the
769 * During an async request no other sync and async calls are allowed,
770 * and will result in %G_IO_ERROR_PENDING errors.
772 * A value of @count larger than %G_MAXSSIZE will cause a
773 * %G_IO_ERROR_INVALID_ARGUMENT error.
775 * On success, the number of bytes written will be passed to the
776 * @callback. It is not an error if this is not the same as the
777 * requested size, as it can happen e.g. on a partial I/O error,
778 * but generally we try to write as many bytes as requested.
780 * You are guaranteed that this method will never fail with
781 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
782 * method will just wait until this changes.
784 * Any outstanding I/O request with higher priority (lower numerical
785 * value) will be executed before an outstanding request with lower
786 * priority. Default priority is %G_PRIORITY_DEFAULT.
788 * The asyncronous methods have a default fallback that uses threads
789 * to implement asynchronicity, so they are optional for inheriting
790 * classes. However, if you override one you must override all.
792 * For the synchronous, blocking version of this function, see
793 * g_output_stream_write().
795 * Note that no copy of @buffer will be made, so it must stay valid
796 * until @callback is called. See g_output_stream_write_bytes_async()
797 * for a #GBytes version that will automatically hold a reference to
798 * the contents (without copying) for the duration of the call.
801 g_output_stream_write_async (GOutputStream *stream,
805 GCancellable *cancellable,
806 GAsyncReadyCallback callback,
809 GOutputStreamClass *class;
810 GError *error = NULL;
813 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
814 g_return_if_fail (buffer != NULL);
816 task = g_task_new (stream, cancellable, callback, user_data);
817 g_task_set_source_tag (task, g_output_stream_write_async);
818 g_task_set_priority (task, io_priority);
822 g_task_return_int (task, 0);
823 g_object_unref (task);
827 if (((gssize) count) < 0)
829 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
830 _("Too large count value passed to %s"),
832 g_object_unref (task);
836 if (!g_output_stream_set_pending (stream, &error))
838 g_task_return_error (task, error);
839 g_object_unref (task);
843 class = G_OUTPUT_STREAM_GET_CLASS (stream);
845 class->write_async (stream, buffer, count, io_priority, cancellable,
846 async_ready_write_callback_wrapper, task);
850 * g_output_stream_write_finish:
851 * @stream: a #GOutputStream.
852 * @result: a #GAsyncResult.
853 * @error: a #GError location to store the error occurring, or %NULL to
856 * Finishes a stream write operation.
858 * Returns: a #gssize containing the number of bytes written to the stream.
861 g_output_stream_write_finish (GOutputStream *stream,
862 GAsyncResult *result,
865 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
866 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
867 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_write_async), FALSE);
869 /* @result is always the GTask created by g_output_stream_write_async();
870 * we called class->write_finish() from async_ready_write_callback_wrapper.
872 return g_task_propagate_int (G_TASK (result), error);
877 const guint8 *buffer;
883 free_async_write_all (gpointer data)
885 g_slice_free (AsyncWriteAll, data);
889 write_all_callback (GObject *stream,
890 GAsyncResult *result,
893 GTask *task = user_data;
894 AsyncWriteAll *data = g_task_get_task_data (task);
898 GError *error = NULL;
901 nwritten = g_output_stream_write_finish (G_OUTPUT_STREAM (stream), result, &error);
905 g_task_return_error (task, error);
906 g_object_unref (task);
910 g_assert_cmpint (nwritten, <=, data->to_write);
911 g_warn_if_fail (nwritten > 0);
913 data->to_write -= nwritten;
914 data->bytes_written += nwritten;
917 if (data->to_write == 0)
919 g_task_return_boolean (task, TRUE);
920 g_object_unref (task);
924 g_output_stream_write_async (G_OUTPUT_STREAM (stream),
925 data->buffer + data->bytes_written,
927 g_task_get_priority (task),
928 g_task_get_cancellable (task),
929 write_all_callback, task);
933 write_all_async_thread (GTask *task,
934 gpointer source_object,
936 GCancellable *cancellable)
938 GOutputStream *stream = source_object;
939 AsyncWriteAll *data = task_data;
940 GError *error = NULL;
942 if (g_output_stream_write_all (stream, data->buffer, data->to_write, &data->bytes_written,
943 g_task_get_cancellable (task), &error))
944 g_task_return_boolean (task, TRUE);
946 g_task_return_error (task, error);
950 * g_output_stream_write_all_async:
951 * @stream: A #GOutputStream
952 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write
953 * @count: the number of bytes to write
954 * @io_priority: the io priority of the request
955 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
956 * @callback: (scope async): callback to call when the request is satisfied
957 * @user_data: (closure): the data to pass to callback function
959 * Request an asynchronous write of @count bytes from @buffer into
960 * the stream. When the operation is finished @callback will be called.
961 * You can then call g_output_stream_write_all_finish() to get the result of the
964 * This is the asynchronous version of g_output_stream_write_all().
966 * Call g_output_stream_write_all_finish() to collect the result.
968 * Any outstanding I/O request with higher priority (lower numerical
969 * value) will be executed before an outstanding request with lower
970 * priority. Default priority is %G_PRIORITY_DEFAULT.
972 * Note that no copy of @buffer will be made, so it must stay valid
973 * until @callback is called.
978 g_output_stream_write_all_async (GOutputStream *stream,
982 GCancellable *cancellable,
983 GAsyncReadyCallback callback,
989 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
990 g_return_if_fail (buffer != NULL || count == 0);
992 task = g_task_new (stream, cancellable, callback, user_data);
993 data = g_slice_new0 (AsyncWriteAll);
994 data->buffer = buffer;
995 data->to_write = count;
997 g_task_set_task_data (task, data, free_async_write_all);
998 g_task_set_priority (task, io_priority);
1000 /* If async writes are going to be handled via the threadpool anyway
1001 * then we may as well do it with a single dispatch instead of
1002 * bouncing in and out.
1004 if (g_output_stream_async_write_is_via_threads (stream))
1006 g_task_run_in_thread (task, write_all_async_thread);
1007 g_object_unref (task);
1010 write_all_callback (G_OBJECT (stream), NULL, task);
1014 * g_output_stream_write_all_finish:
1015 * @stream: a #GOutputStream
1016 * @result: a #GAsyncResult
1017 * @bytes_written: (out): location to store the number of bytes that was written to the stream
1018 * @error: a #GError location to store the error occurring, or %NULL to ignore.
1020 * Finishes an asynchronous stream write operation started with
1021 * g_output_stream_write_all_async().
1023 * As a special exception to the normal conventions for functions that
1024 * use #GError, if this function returns %FALSE (and sets @error) then
1025 * @bytes_written will be set to the number of bytes that were
1026 * successfully written before the error was encountered. This
1027 * functionality is only available from C. If you need it from another
1028 * language then you must write your own loop around
1029 * g_output_stream_write_async().
1031 * Returns: %TRUE on success, %FALSE if there was an error
1036 g_output_stream_write_all_finish (GOutputStream *stream,
1037 GAsyncResult *result,
1038 gsize *bytes_written,
1043 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1044 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1046 task = G_TASK (result);
1050 AsyncWriteAll *data = (AsyncWriteAll *)g_task_get_task_data (task);
1052 *bytes_written = data->bytes_written;
1055 return g_task_propagate_boolean (task, error);
1059 write_bytes_callback (GObject *stream,
1060 GAsyncResult *result,
1063 GTask *task = user_data;
1064 GError *error = NULL;
1067 nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
1070 g_task_return_error (task, error);
1072 g_task_return_int (task, nwrote);
1073 g_object_unref (task);
1077 * g_output_stream_write_bytes_async:
1078 * @stream: A #GOutputStream.
1079 * @bytes: The bytes to write
1080 * @io_priority: the io priority of the request.
1081 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1082 * @callback: (scope async): callback to call when the request is satisfied
1083 * @user_data: (closure): the data to pass to callback function
1085 * This function is similar to g_output_stream_write_async(), but
1086 * takes a #GBytes as input. Due to the refcounted nature of #GBytes,
1087 * this allows the stream to avoid taking a copy of the data.
1089 * However, note that this function may still perform partial writes,
1090 * just like g_output_stream_write_async(). If that occurs, to continue
1091 * writing, you will need to create a new #GBytes containing just the
1092 * remaining bytes, using g_bytes_new_from_bytes(). Passing the same
1093 * #GBytes instance multiple times potentially can result in duplicated
1094 * data in the output stream.
1096 * For the synchronous, blocking version of this function, see
1097 * g_output_stream_write_bytes().
1100 g_output_stream_write_bytes_async (GOutputStream *stream,
1103 GCancellable *cancellable,
1104 GAsyncReadyCallback callback,
1111 data = g_bytes_get_data (bytes, &size);
1113 task = g_task_new (stream, cancellable, callback, user_data);
1114 g_task_set_task_data (task, g_bytes_ref (bytes),
1115 (GDestroyNotify) g_bytes_unref);
1117 g_output_stream_write_async (stream,
1121 write_bytes_callback,
1126 * g_output_stream_write_bytes_finish:
1127 * @stream: a #GOutputStream.
1128 * @result: a #GAsyncResult.
1129 * @error: a #GError location to store the error occurring, or %NULL to
1132 * Finishes a stream write-from-#GBytes operation.
1134 * Returns: a #gssize containing the number of bytes written to the stream.
1137 g_output_stream_write_bytes_finish (GOutputStream *stream,
1138 GAsyncResult *result,
1141 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
1142 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1144 return g_task_propagate_int (G_TASK (result), error);
1148 async_ready_splice_callback_wrapper (GObject *source_object,
1152 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1153 GOutputStreamClass *class;
1154 GTask *task = _data;
1156 GError *error = NULL;
1158 g_output_stream_clear_pending (stream);
1160 if (g_async_result_legacy_propagate_error (res, &error))
1164 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1165 nspliced = class->splice_finish (stream, res, &error);
1169 g_task_return_int (task, nspliced);
1171 g_task_return_error (task, error);
1172 g_object_unref (task);
1176 * g_output_stream_splice_async:
1177 * @stream: a #GOutputStream.
1178 * @source: a #GInputStream.
1179 * @flags: a set of #GOutputStreamSpliceFlags.
1180 * @io_priority: the io priority of the request.
1181 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1182 * @callback: (scope async): a #GAsyncReadyCallback.
1183 * @user_data: (closure): user data passed to @callback.
1185 * Splices a stream asynchronously.
1186 * When the operation is finished @callback will be called.
1187 * You can then call g_output_stream_splice_finish() to get the
1188 * result of the operation.
1190 * For the synchronous, blocking version of this function, see
1191 * g_output_stream_splice().
1194 g_output_stream_splice_async (GOutputStream *stream,
1195 GInputStream *source,
1196 GOutputStreamSpliceFlags flags,
1198 GCancellable *cancellable,
1199 GAsyncReadyCallback callback,
1202 GOutputStreamClass *class;
1204 GError *error = NULL;
1206 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1207 g_return_if_fail (G_IS_INPUT_STREAM (source));
1209 task = g_task_new (stream, cancellable, callback, user_data);
1210 g_task_set_source_tag (task, g_output_stream_splice_async);
1211 g_task_set_priority (task, io_priority);
1212 g_task_set_task_data (task, g_object_ref (source), g_object_unref);
1214 if (g_input_stream_is_closed (source))
1216 g_task_return_new_error (task,
1217 G_IO_ERROR, G_IO_ERROR_CLOSED,
1218 _("Source stream is already closed"));
1219 g_object_unref (task);
1223 if (!g_output_stream_set_pending (stream, &error))
1225 g_task_return_error (task, error);
1226 g_object_unref (task);
1230 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1232 class->splice_async (stream, source, flags, io_priority, cancellable,
1233 async_ready_splice_callback_wrapper, task);
1237 * g_output_stream_splice_finish:
1238 * @stream: a #GOutputStream.
1239 * @result: a #GAsyncResult.
1240 * @error: a #GError location to store the error occurring, or %NULL to
1243 * Finishes an asynchronous stream splice operation.
1245 * Returns: a #gssize of the number of bytes spliced. Note that if the
1246 * number of bytes spliced is greater than %G_MAXSSIZE, then that
1247 * will be returned, and there is no way to determine the actual
1248 * number of bytes spliced.
1251 g_output_stream_splice_finish (GOutputStream *stream,
1252 GAsyncResult *result,
1255 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1256 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1257 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_splice_async), FALSE);
1259 /* @result is always the GTask created by g_output_stream_splice_async();
1260 * we called class->splice_finish() from async_ready_splice_callback_wrapper.
1262 return g_task_propagate_int (G_TASK (result), error);
1266 async_ready_flush_callback_wrapper (GObject *source_object,
1270 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1271 GOutputStreamClass *class;
1272 GTask *task = user_data;
1274 GError *error = NULL;
1276 g_output_stream_clear_pending (stream);
1278 if (g_async_result_legacy_propagate_error (res, &error))
1282 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1283 flushed = class->flush_finish (stream, res, &error);
1287 g_task_return_boolean (task, TRUE);
1289 g_task_return_error (task, error);
1290 g_object_unref (task);
1294 * g_output_stream_flush_async:
1295 * @stream: a #GOutputStream.
1296 * @io_priority: the io priority of the request.
1297 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1298 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1299 * @user_data: (closure): the data to pass to callback function
1301 * Forces an asynchronous write of all user-space buffered data for
1302 * the given @stream.
1303 * For behaviour details see g_output_stream_flush().
1305 * When the operation is finished @callback will be
1306 * called. You can then call g_output_stream_flush_finish() to get the
1307 * result of the operation.
1310 g_output_stream_flush_async (GOutputStream *stream,
1312 GCancellable *cancellable,
1313 GAsyncReadyCallback callback,
1316 GOutputStreamClass *class;
1318 GError *error = NULL;
1320 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1322 task = g_task_new (stream, cancellable, callback, user_data);
1323 g_task_set_source_tag (task, g_output_stream_flush_async);
1324 g_task_set_priority (task, io_priority);
1326 if (!g_output_stream_set_pending (stream, &error))
1328 g_task_return_error (task, error);
1329 g_object_unref (task);
1333 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1335 if (class->flush_async == NULL)
1337 g_task_return_boolean (task, TRUE);
1338 g_object_unref (task);
1342 class->flush_async (stream, io_priority, cancellable,
1343 async_ready_flush_callback_wrapper, task);
1347 * g_output_stream_flush_finish:
1348 * @stream: a #GOutputStream.
1349 * @result: a GAsyncResult.
1350 * @error: a #GError location to store the error occurring, or %NULL to
1353 * Finishes flushing an output stream.
1355 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1358 g_output_stream_flush_finish (GOutputStream *stream,
1359 GAsyncResult *result,
1362 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1363 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1364 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_flush_async), FALSE);
1366 /* @result is always the GTask created by g_output_stream_flush_async();
1367 * we called class->flush_finish() from async_ready_flush_callback_wrapper.
1369 return g_task_propagate_boolean (G_TASK (result), error);
1374 async_ready_close_callback_wrapper (GObject *source_object,
1378 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1379 GOutputStreamClass *class;
1380 GTask *task = user_data;
1381 GError *error = g_task_get_task_data (task);
1383 stream->priv->closing = FALSE;
1384 stream->priv->closed = TRUE;
1386 if (!error && !g_async_result_legacy_propagate_error (res, &error))
1388 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1390 class->close_finish (stream, res,
1391 error ? NULL : &error);
1395 g_task_return_error (task, error);
1397 g_task_return_boolean (task, TRUE);
1398 g_object_unref (task);
1402 async_ready_close_flushed_callback_wrapper (GObject *source_object,
1406 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1407 GOutputStreamClass *class;
1408 GTask *task = user_data;
1409 GError *error = NULL;
1411 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1413 if (!g_async_result_legacy_propagate_error (res, &error))
1415 class->flush_finish (stream, res, &error);
1418 /* propagate the possible error */
1420 g_task_set_task_data (task, error, NULL);
1422 /* we still close, even if there was a flush error */
1423 class->close_async (stream,
1424 g_task_get_priority (task),
1425 g_task_get_cancellable (task),
1426 async_ready_close_callback_wrapper, task);
1430 real_close_async_cb (GObject *source_object,
1434 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
1435 GTask *task = user_data;
1436 GError *error = NULL;
1439 g_output_stream_clear_pending (stream);
1441 ret = g_output_stream_internal_close_finish (stream, res, &error);
1444 g_task_return_error (task, error);
1446 g_task_return_boolean (task, ret);
1448 g_object_unref (task);
1452 * g_output_stream_close_async:
1453 * @stream: A #GOutputStream.
1454 * @io_priority: the io priority of the request.
1455 * @cancellable: (allow-none): optional cancellable object
1456 * @callback: (scope async): callback to call when the request is satisfied
1457 * @user_data: (closure): the data to pass to callback function
1459 * Requests an asynchronous close of the stream, releasing resources
1460 * related to it. When the operation is finished @callback will be
1461 * called. You can then call g_output_stream_close_finish() to get
1462 * the result of the operation.
1464 * For behaviour details see g_output_stream_close().
1466 * The asyncronous methods have a default fallback that uses threads
1467 * to implement asynchronicity, so they are optional for inheriting
1468 * classes. However, if you override one you must override all.
1471 g_output_stream_close_async (GOutputStream *stream,
1473 GCancellable *cancellable,
1474 GAsyncReadyCallback callback,
1478 GError *error = NULL;
1480 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1482 task = g_task_new (stream, cancellable, callback, user_data);
1483 g_task_set_source_tag (task, g_output_stream_close_async);
1484 g_task_set_priority (task, io_priority);
1486 if (!g_output_stream_set_pending (stream, &error))
1488 g_task_return_error (task, error);
1489 g_object_unref (task);
1493 g_output_stream_internal_close_async (stream, io_priority, cancellable,
1494 real_close_async_cb, task);
1497 /* Must always be called inside
1498 * g_output_stream_set_pending()/g_output_stream_clear_pending().
1501 g_output_stream_internal_close_async (GOutputStream *stream,
1503 GCancellable *cancellable,
1504 GAsyncReadyCallback callback,
1507 GOutputStreamClass *class;
1510 task = g_task_new (stream, cancellable, callback, user_data);
1511 g_task_set_source_tag (task, g_output_stream_internal_close_async);
1512 g_task_set_priority (task, io_priority);
1514 if (stream->priv->closed)
1516 g_task_return_boolean (task, TRUE);
1517 g_object_unref (task);
1521 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1522 stream->priv->closing = TRUE;
1524 /* Call close_async directly if there is no need to flush, or if the flush
1525 can be done sync (in the output stream async close thread) */
1526 if (class->flush_async == NULL ||
1527 (class->flush_async == g_output_stream_real_flush_async &&
1528 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1530 class->close_async (stream, io_priority, cancellable,
1531 async_ready_close_callback_wrapper, task);
1535 /* First do an async flush, then do the async close in the callback
1536 wrapper (see async_ready_close_flushed_callback_wrapper) */
1537 class->flush_async (stream, io_priority, cancellable,
1538 async_ready_close_flushed_callback_wrapper, task);
1543 g_output_stream_internal_close_finish (GOutputStream *stream,
1544 GAsyncResult *result,
1547 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1548 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1549 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_internal_close_async), FALSE);
1551 return g_task_propagate_boolean (G_TASK (result), error);
1555 * g_output_stream_close_finish:
1556 * @stream: a #GOutputStream.
1557 * @result: a #GAsyncResult.
1558 * @error: a #GError location to store the error occurring, or %NULL to
1561 * Closes an output stream.
1563 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1566 g_output_stream_close_finish (GOutputStream *stream,
1567 GAsyncResult *result,
1570 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1571 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1572 g_return_val_if_fail (g_async_result_is_tagged (result, g_output_stream_close_async), FALSE);
1574 /* @result is always the GTask created by g_output_stream_close_async();
1575 * we called class->close_finish() from async_ready_close_callback_wrapper.
1577 return g_task_propagate_boolean (G_TASK (result), error);
1581 * g_output_stream_is_closed:
1582 * @stream: a #GOutputStream.
1584 * Checks if an output stream has already been closed.
1586 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1589 g_output_stream_is_closed (GOutputStream *stream)
1591 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1593 return stream->priv->closed;
1597 * g_output_stream_is_closing:
1598 * @stream: a #GOutputStream.
1600 * Checks if an output stream is being closed. This can be
1601 * used inside e.g. a flush implementation to see if the
1602 * flush (or other i/o operation) is called from within
1603 * the closing operation.
1605 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1610 g_output_stream_is_closing (GOutputStream *stream)
1612 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1614 return stream->priv->closing;
1618 * g_output_stream_has_pending:
1619 * @stream: a #GOutputStream.
1621 * Checks if an ouput stream has pending actions.
1623 * Returns: %TRUE if @stream has pending actions.
1626 g_output_stream_has_pending (GOutputStream *stream)
1628 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1630 return stream->priv->pending;
1634 * g_output_stream_set_pending:
1635 * @stream: a #GOutputStream.
1636 * @error: a #GError location to store the error occurring, or %NULL to
1639 * Sets @stream to have actions pending. If the pending flag is
1640 * already set or @stream is closed, it will return %FALSE and set
1643 * Returns: %TRUE if pending was previously unset and is now set.
1646 g_output_stream_set_pending (GOutputStream *stream,
1649 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1651 if (stream->priv->closed)
1653 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1654 _("Stream is already closed"));
1658 if (stream->priv->pending)
1660 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1661 /* Translators: This is an error you get if there is
1662 * already an operation running against this stream when
1663 * you try to start one */
1664 _("Stream has outstanding operation"));
1668 stream->priv->pending = TRUE;
1673 * g_output_stream_clear_pending:
1674 * @stream: output stream
1676 * Clears the pending flag on @stream.
1679 g_output_stream_clear_pending (GOutputStream *stream)
1681 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1683 stream->priv->pending = FALSE;
1687 * g_output_stream_async_write_is_via_threads:
1688 * @stream: a #GOutputStream.
1690 * Checks if an ouput stream's write_async function uses threads.
1692 * Returns: %TRUE if @stream's write_async function uses threads.
1695 g_output_stream_async_write_is_via_threads (GOutputStream *stream)
1697 GOutputStreamClass *class;
1699 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1701 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1703 return (class->write_async == g_output_stream_real_write_async &&
1704 !(G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
1705 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream))));
1709 /********************************************
1710 * Default implementation of async ops *
1711 ********************************************/
1715 gsize count_requested;
1716 gssize count_written;
1720 free_write_data (WriteData *op)
1722 g_slice_free (WriteData, op);
1726 write_async_thread (GTask *task,
1727 gpointer source_object,
1729 GCancellable *cancellable)
1731 GOutputStream *stream = source_object;
1732 WriteData *op = task_data;
1733 GOutputStreamClass *class;
1734 GError *error = NULL;
1735 gssize count_written;
1737 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1738 count_written = class->write_fn (stream, op->buffer, op->count_requested,
1739 cancellable, &error);
1740 if (count_written == -1)
1741 g_task_return_error (task, error);
1743 g_task_return_int (task, count_written);
1746 static void write_async_pollable (GPollableOutputStream *stream,
1750 write_async_pollable_ready (GPollableOutputStream *stream,
1753 GTask *task = user_data;
1755 write_async_pollable (stream, task);
1760 write_async_pollable (GPollableOutputStream *stream,
1763 GError *error = NULL;
1764 WriteData *op = g_task_get_task_data (task);
1765 gssize count_written;
1767 if (g_task_return_error_if_cancelled (task))
1770 count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
1771 write_nonblocking (stream, op->buffer, op->count_requested, &error);
1773 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1777 g_error_free (error);
1779 source = g_pollable_output_stream_create_source (stream,
1780 g_task_get_cancellable (task));
1781 g_task_attach_source (task, source,
1782 (GSourceFunc) write_async_pollable_ready);
1783 g_source_unref (source);
1787 if (count_written == -1)
1788 g_task_return_error (task, error);
1790 g_task_return_int (task, count_written);
1794 g_output_stream_real_write_async (GOutputStream *stream,
1798 GCancellable *cancellable,
1799 GAsyncReadyCallback callback,
1805 op = g_slice_new0 (WriteData);
1806 task = g_task_new (stream, cancellable, callback, user_data);
1807 g_task_set_check_cancellable (task, FALSE);
1808 g_task_set_task_data (task, op, (GDestroyNotify) free_write_data);
1809 op->buffer = buffer;
1810 op->count_requested = count;
1812 if (!g_output_stream_async_write_is_via_threads (stream))
1813 write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), task);
1815 g_task_run_in_thread (task, write_async_thread);
1816 g_object_unref (task);
1820 g_output_stream_real_write_finish (GOutputStream *stream,
1821 GAsyncResult *result,
1824 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1826 return g_task_propagate_int (G_TASK (result), error);
1830 GInputStream *source;
1831 GOutputStreamSpliceFlags flags;
1840 free_splice_data (SpliceData *op)
1842 g_clear_pointer (&op->buffer, g_free);
1843 g_object_unref (op->source);
1844 g_clear_error (&op->error);
1849 real_splice_async_complete_cb (GTask *task)
1851 SpliceData *op = g_task_get_task_data (task);
1853 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE &&
1854 !g_input_stream_is_closed (op->source))
1857 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET &&
1858 !g_output_stream_is_closed (g_task_get_source_object (task)))
1861 if (op->error != NULL)
1863 g_task_return_error (task, op->error);
1868 g_task_return_int (task, op->bytes_copied);
1871 g_object_unref (task);
1875 real_splice_async_close_input_cb (GObject *source,
1879 GTask *task = user_data;
1881 g_input_stream_close_finish (G_INPUT_STREAM (source), res, NULL);
1883 real_splice_async_complete_cb (task);
1887 real_splice_async_close_output_cb (GObject *source,
1891 GTask *task = G_TASK (user_data);
1892 SpliceData *op = g_task_get_task_data (task);
1893 GError **error = (op->error == NULL) ? &op->error : NULL;
1895 g_output_stream_internal_close_finish (G_OUTPUT_STREAM (source), res, error);
1897 real_splice_async_complete_cb (task);
1901 real_splice_async_complete (GTask *task)
1903 SpliceData *op = g_task_get_task_data (task);
1904 gboolean done = TRUE;
1906 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
1909 g_input_stream_close_async (op->source, g_task_get_priority (task),
1910 g_task_get_cancellable (task),
1911 real_splice_async_close_input_cb, task);
1914 if (op->flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
1917 g_output_stream_internal_close_async (g_task_get_source_object (task),
1918 g_task_get_priority (task),
1919 g_task_get_cancellable (task),
1920 real_splice_async_close_output_cb,
1925 real_splice_async_complete_cb (task);
1928 static void real_splice_async_read_cb (GObject *source,
1930 gpointer user_data);
1933 real_splice_async_write_cb (GObject *source,
1937 GOutputStreamClass *class;
1938 GTask *task = G_TASK (user_data);
1939 SpliceData *op = g_task_get_task_data (task);
1942 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1944 ret = class->write_finish (G_OUTPUT_STREAM (source), res, &op->error);
1948 real_splice_async_complete (task);
1952 op->n_written += ret;
1953 op->bytes_copied += ret;
1954 if (op->bytes_copied > G_MAXSSIZE)
1955 op->bytes_copied = G_MAXSSIZE;
1957 if (op->n_written < op->n_read)
1959 class->write_async (g_task_get_source_object (task),
1960 op->buffer + op->n_written,
1961 op->n_read - op->n_written,
1962 g_task_get_priority (task),
1963 g_task_get_cancellable (task),
1964 real_splice_async_write_cb, task);
1968 g_input_stream_read_async (op->source, op->buffer, 8192,
1969 g_task_get_priority (task),
1970 g_task_get_cancellable (task),
1971 real_splice_async_read_cb, task);
1975 real_splice_async_read_cb (GObject *source,
1979 GOutputStreamClass *class;
1980 GTask *task = G_TASK (user_data);
1981 SpliceData *op = g_task_get_task_data (task);
1984 class = G_OUTPUT_STREAM_GET_CLASS (g_task_get_source_object (task));
1986 ret = g_input_stream_read_finish (op->source, res, &op->error);
1987 if (ret == -1 || ret == 0)
1989 real_splice_async_complete (task);
1996 class->write_async (g_task_get_source_object (task), op->buffer,
1997 op->n_read, g_task_get_priority (task),
1998 g_task_get_cancellable (task),
1999 real_splice_async_write_cb, task);
2003 splice_async_thread (GTask *task,
2004 gpointer source_object,
2006 GCancellable *cancellable)
2008 GOutputStream *stream = source_object;
2009 SpliceData *op = task_data;
2010 GOutputStreamClass *class;
2011 GError *error = NULL;
2012 gssize bytes_copied;
2014 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2016 bytes_copied = class->splice (stream,
2021 if (bytes_copied == -1)
2022 g_task_return_error (task, error);
2024 g_task_return_int (task, bytes_copied);
2028 g_output_stream_real_splice_async (GOutputStream *stream,
2029 GInputStream *source,
2030 GOutputStreamSpliceFlags flags,
2032 GCancellable *cancellable,
2033 GAsyncReadyCallback callback,
2039 op = g_new0 (SpliceData, 1);
2040 task = g_task_new (stream, cancellable, callback, user_data);
2041 g_task_set_task_data (task, op, (GDestroyNotify)free_splice_data);
2043 op->source = g_object_ref (source);
2045 if (g_input_stream_async_read_is_via_threads (source) &&
2046 g_output_stream_async_write_is_via_threads (stream))
2048 g_task_run_in_thread (task, splice_async_thread);
2049 g_object_unref (task);
2053 op->buffer = g_malloc (8192);
2054 g_input_stream_read_async (op->source, op->buffer, 8192,
2055 g_task_get_priority (task),
2056 g_task_get_cancellable (task),
2057 real_splice_async_read_cb, task);
2062 g_output_stream_real_splice_finish (GOutputStream *stream,
2063 GAsyncResult *result,
2066 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2068 return g_task_propagate_int (G_TASK (result), error);
2073 flush_async_thread (GTask *task,
2074 gpointer source_object,
2076 GCancellable *cancellable)
2078 GOutputStream *stream = source_object;
2079 GOutputStreamClass *class;
2081 GError *error = NULL;
2083 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2086 result = class->flush (stream, cancellable, &error);
2089 g_task_return_boolean (task, TRUE);
2091 g_task_return_error (task, error);
2095 g_output_stream_real_flush_async (GOutputStream *stream,
2097 GCancellable *cancellable,
2098 GAsyncReadyCallback callback,
2103 task = g_task_new (stream, cancellable, callback, user_data);
2104 g_task_set_priority (task, io_priority);
2105 g_task_run_in_thread (task, flush_async_thread);
2106 g_object_unref (task);
2110 g_output_stream_real_flush_finish (GOutputStream *stream,
2111 GAsyncResult *result,
2114 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2116 return g_task_propagate_boolean (G_TASK (result), error);
2120 close_async_thread (GTask *task,
2121 gpointer source_object,
2123 GCancellable *cancellable)
2125 GOutputStream *stream = source_object;
2126 GOutputStreamClass *class;
2127 GError *error = NULL;
2128 gboolean result = TRUE;
2130 class = G_OUTPUT_STREAM_GET_CLASS (stream);
2132 /* Do a flush here if there is a flush function, and we did not have to do
2133 * an async flush before (see g_output_stream_close_async)
2135 if (class->flush != NULL &&
2136 (class->flush_async == NULL ||
2137 class->flush_async == g_output_stream_real_flush_async))
2139 result = class->flush (stream, cancellable, &error);
2142 /* Auto handling of cancelation disabled, and ignore
2143 cancellation, since we want to close things anyway, although
2144 possibly in a quick-n-dirty way. At least we never want to leak
2147 if (class->close_fn)
2149 /* Make sure to close, even if the flush failed (see sync close) */
2151 class->close_fn (stream, cancellable, NULL);
2153 result = class->close_fn (stream, cancellable, &error);
2157 g_task_return_boolean (task, TRUE);
2159 g_task_return_error (task, error);
2163 g_output_stream_real_close_async (GOutputStream *stream,
2165 GCancellable *cancellable,
2166 GAsyncReadyCallback callback,
2171 task = g_task_new (stream, cancellable, callback, user_data);
2172 g_task_set_priority (task, io_priority);
2173 g_task_run_in_thread (task, close_async_thread);
2174 g_object_unref (task);
2178 g_output_stream_real_close_finish (GOutputStream *stream,
2179 GAsyncResult *result,
2182 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
2184 return g_task_propagate_boolean (G_TASK (result), error);