1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "goutputstream.h"
25 #include "gcancellable.h"
26 #include "gasyncresult.h"
27 #include "gsimpleasyncresult.h"
28 #include "ginputstream.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 G_DEFINE_ABSTRACT_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
50 struct _GOutputStreamPrivate {
54 GAsyncReadyCallback outstanding_callback;
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_close_internal (GOutputStream *stream,
99 GCancellable *cancellable,
103 g_output_stream_finalize (GObject *object)
105 G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
109 g_output_stream_dispose (GObject *object)
111 GOutputStream *stream;
113 stream = G_OUTPUT_STREAM (object);
115 if (!stream->priv->closed)
116 g_output_stream_close (stream, NULL, NULL);
118 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
122 g_output_stream_class_init (GOutputStreamClass *klass)
124 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
128 gobject_class->finalize = g_output_stream_finalize;
129 gobject_class->dispose = g_output_stream_dispose;
131 klass->splice = g_output_stream_real_splice;
133 klass->write_async = g_output_stream_real_write_async;
134 klass->write_finish = g_output_stream_real_write_finish;
135 klass->splice_async = g_output_stream_real_splice_async;
136 klass->splice_finish = g_output_stream_real_splice_finish;
137 klass->flush_async = g_output_stream_real_flush_async;
138 klass->flush_finish = g_output_stream_real_flush_finish;
139 klass->close_async = g_output_stream_real_close_async;
140 klass->close_finish = g_output_stream_real_close_finish;
144 g_output_stream_init (GOutputStream *stream)
146 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
147 G_TYPE_OUTPUT_STREAM,
148 GOutputStreamPrivate);
152 * g_output_stream_write:
153 * @stream: a #GOutputStream.
154 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
155 * @count: the number of bytes to write
156 * @cancellable: (allow-none): optional cancellable object
157 * @error: location to store the error occurring, or %NULL to ignore
159 * Tries to write @count bytes from @buffer into the stream. Will block
160 * during the operation.
162 * If count is 0, returns 0 and does nothing. A value of @count
163 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
165 * On success, the number of bytes written to the stream is returned.
166 * It is not an error if this is not the same as the requested size, as it
167 * can happen e.g. on a partial I/O error, or if there is not enough
168 * storage in the stream. All writes block until at least one byte
169 * is written or an error occurs; 0 is never returned (unless
172 * If @cancellable is not %NULL, then the operation can be cancelled by
173 * triggering the cancellable object from another thread. If the operation
174 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
175 * operation was partially finished when the operation was cancelled the
176 * partial result will be returned, without an error.
178 * On error -1 is returned and @error is set accordingly.
182 * Return value: Number of bytes written, or -1 on error
185 g_output_stream_write (GOutputStream *stream,
188 GCancellable *cancellable,
191 GOutputStreamClass *class;
194 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
195 g_return_val_if_fail (buffer != NULL, 0);
200 if (((gssize) count) < 0)
202 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
203 _("Too large count value passed to %s"), G_STRFUNC);
207 class = G_OUTPUT_STREAM_GET_CLASS (stream);
209 if (class->write_fn == NULL)
211 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
212 _("Output stream doesn't implement write"));
216 if (!g_output_stream_set_pending (stream, error))
220 g_cancellable_push_current (cancellable);
222 res = class->write_fn (stream, buffer, count, cancellable, error);
225 g_cancellable_pop_current (cancellable);
227 g_output_stream_clear_pending (stream);
233 * g_output_stream_write_all:
234 * @stream: a #GOutputStream.
235 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
236 * @count: the number of bytes to write
237 * @bytes_written: (out): location to store the number of bytes that was
238 * written to the stream
239 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
240 * @error: location to store the error occurring, or %NULL to ignore
242 * Tries to write @count bytes from @buffer into the stream. Will block
243 * during the operation.
245 * This function is similar to g_output_stream_write(), except it tries to
246 * write as many bytes as requested, only stopping on an error.
248 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
251 * If there is an error during the operation %FALSE is returned and @error
252 * is set to indicate the error status, @bytes_written is updated to contain
253 * the number of bytes written into the stream before the error occurred.
255 * Return value: %TRUE on success, %FALSE if there was an error
258 g_output_stream_write_all (GOutputStream *stream,
261 gsize *bytes_written,
262 GCancellable *cancellable,
265 gsize _bytes_written;
268 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
269 g_return_val_if_fail (buffer != NULL, FALSE);
272 while (_bytes_written < count)
274 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
279 *bytes_written = _bytes_written;
284 g_warning ("Write returned zero without error");
286 _bytes_written += res;
290 *bytes_written = _bytes_written;
296 * g_output_stream_write_bytes:
297 * @stream: a #GOutputStream.
298 * @bytes: the #GBytes to write
299 * @cancellable: (allow-none): optional cancellable object
300 * @error: location to store the error occurring, or %NULL to ignore
302 * Tries to write the data from @bytes into the stream. Will block
303 * during the operation.
305 * If @bytes is 0-length, returns 0 and does nothing. A #GBytes larger
306 * than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
308 * On success, the number of bytes written to the stream is returned.
309 * It is not an error if this is not the same as the requested size, as it
310 * can happen e.g. on a partial I/O error, or if there is not enough
311 * storage in the stream. All writes block until at least one byte
312 * is written or an error occurs; 0 is never returned (unless
313 * the size of @bytes is 0).
315 * If @cancellable is not %NULL, then the operation can be cancelled by
316 * triggering the cancellable object from another thread. If the operation
317 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
318 * operation was partially finished when the operation was cancelled the
319 * partial result will be returned, without an error.
321 * On error -1 is returned and @error is set accordingly.
323 * Return value: Number of bytes written, or -1 on error
326 g_output_stream_write_bytes (GOutputStream *stream,
328 GCancellable *cancellable,
334 data = g_bytes_get_data (bytes, &size);
336 return g_output_stream_write (stream,
343 * g_output_stream_flush:
344 * @stream: a #GOutputStream.
345 * @cancellable: (allow-none): optional cancellable object
346 * @error: location to store the error occurring, or %NULL to ignore
348 * Forces a write of all user-space buffered data for the given
349 * @stream. Will block during the operation. Closing the stream will
350 * implicitly cause a flush.
352 * This function is optional for inherited classes.
354 * If @cancellable is not %NULL, then the operation can be cancelled by
355 * triggering the cancellable object from another thread. If the operation
356 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
358 * Return value: %TRUE on success, %FALSE on error
361 g_output_stream_flush (GOutputStream *stream,
362 GCancellable *cancellable,
365 GOutputStreamClass *class;
368 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
370 if (!g_output_stream_set_pending (stream, error))
373 class = G_OUTPUT_STREAM_GET_CLASS (stream);
379 g_cancellable_push_current (cancellable);
381 res = class->flush (stream, cancellable, error);
384 g_cancellable_pop_current (cancellable);
387 g_output_stream_clear_pending (stream);
393 * g_output_stream_splice:
394 * @stream: a #GOutputStream.
395 * @source: a #GInputStream.
396 * @flags: a set of #GOutputStreamSpliceFlags.
397 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
398 * @error: a #GError location to store the error occurring, or %NULL to
401 * Splices an input stream into an output stream.
403 * Returns: a #gssize containing the size of the data spliced, or
404 * -1 if an error occurred. Note that if the number of bytes
405 * spliced is greater than %G_MAXSSIZE, then that will be
406 * returned, and there is no way to determine the actual number
410 g_output_stream_splice (GOutputStream *stream,
411 GInputStream *source,
412 GOutputStreamSpliceFlags flags,
413 GCancellable *cancellable,
416 GOutputStreamClass *class;
419 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
420 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
422 if (g_input_stream_is_closed (source))
424 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
425 _("Source stream is already closed"));
429 if (!g_output_stream_set_pending (stream, error))
432 class = G_OUTPUT_STREAM_GET_CLASS (stream);
435 g_cancellable_push_current (cancellable);
437 bytes_copied = class->splice (stream, source, flags, cancellable, error);
440 g_cancellable_pop_current (cancellable);
442 g_output_stream_clear_pending (stream);
448 g_output_stream_real_splice (GOutputStream *stream,
449 GInputStream *source,
450 GOutputStreamSpliceFlags flags,
451 GCancellable *cancellable,
454 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
455 gssize n_read, n_written;
457 char buffer[8192], *p;
461 if (class->write_fn == NULL)
463 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
464 _("Output stream doesn't implement write"));
472 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
485 n_written = class->write_fn (stream, p, n_read, cancellable, error);
494 bytes_copied += n_written;
497 if (bytes_copied > G_MAXSSIZE)
498 bytes_copied = G_MAXSSIZE;
504 error = NULL; /* Ignore further errors */
506 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
508 /* Don't care about errors in source here */
509 g_input_stream_close (source, cancellable, NULL);
512 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
514 /* But write errors on close are bad! */
515 res = _g_output_stream_close_internal (stream, cancellable, error);
524 /* Must always be called inside
525 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
527 _g_output_stream_close_internal (GOutputStream *stream,
528 GCancellable *cancellable,
531 GOutputStreamClass *class;
534 if (stream->priv->closed)
537 class = G_OUTPUT_STREAM_GET_CLASS (stream);
539 stream->priv->closing = TRUE;
542 g_cancellable_push_current (cancellable);
545 res = class->flush (stream, cancellable, error);
551 /* flushing caused the error that we want to return,
552 * but we still want to close the underlying stream if possible
555 class->close_fn (stream, cancellable, NULL);
561 res = class->close_fn (stream, cancellable, error);
565 g_cancellable_pop_current (cancellable);
567 stream->priv->closing = FALSE;
568 stream->priv->closed = TRUE;
574 * g_output_stream_close:
575 * @stream: A #GOutputStream.
576 * @cancellable: (allow-none): optional cancellable object
577 * @error: location to store the error occurring, or %NULL to ignore
579 * Closes the stream, releasing resources related to it.
581 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
582 * Closing a stream multiple times will not return an error.
584 * Closing a stream will automatically flush any outstanding buffers in the
587 * Streams will be automatically closed when the last reference
588 * is dropped, but you might want to call this function to make sure
589 * resources are released as early as possible.
591 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
592 * open after the stream is closed. See the documentation for the individual
593 * stream for details.
595 * On failure the first error that happened will be reported, but the close
596 * operation will finish as much as possible. A stream that failed to
597 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
598 * is important to check and report the error to the user, otherwise
599 * there might be a loss of data as all data might not be written.
601 * If @cancellable is not %NULL, then the operation can be cancelled by
602 * triggering the cancellable object from another thread. If the operation
603 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
604 * Cancelling a close will still leave the stream closed, but there some streams
605 * can use a faster close that doesn't block to e.g. check errors. On
606 * cancellation (as with any error) there is no guarantee that all written
607 * data will reach the target.
609 * Return value: %TRUE on success, %FALSE on failure
612 g_output_stream_close (GOutputStream *stream,
613 GCancellable *cancellable,
618 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
620 if (stream->priv->closed)
623 if (!g_output_stream_set_pending (stream, error))
626 res = _g_output_stream_close_internal (stream, cancellable, error);
628 g_output_stream_clear_pending (stream);
634 async_ready_callback_wrapper (GObject *source_object,
638 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
640 g_output_stream_clear_pending (stream);
641 if (stream->priv->outstanding_callback)
642 (*stream->priv->outstanding_callback) (source_object, res, user_data);
643 g_object_unref (stream);
648 GCancellable *cancellable;
654 async_ready_close_callback_wrapper (GObject *source_object,
658 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
659 CloseUserData *data = user_data;
661 stream->priv->closing = FALSE;
662 stream->priv->closed = TRUE;
664 g_output_stream_clear_pending (stream);
666 if (stream->priv->outstanding_callback)
668 if (data->flush_error != NULL)
670 GSimpleAsyncResult *err;
672 err = g_simple_async_result_new_take_error (source_object,
673 stream->priv->outstanding_callback,
676 data->flush_error = NULL;
678 (*stream->priv->outstanding_callback) (source_object,
679 G_ASYNC_RESULT (err),
681 g_object_unref (err);
685 (*stream->priv->outstanding_callback) (source_object,
691 g_object_unref (stream);
693 if (data->cancellable)
694 g_object_unref (data->cancellable);
696 if (data->flush_error)
697 g_error_free (data->flush_error);
699 g_slice_free (CloseUserData, data);
703 async_ready_close_flushed_callback_wrapper (GObject *source_object,
707 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
708 GOutputStreamClass *class;
709 CloseUserData *data = user_data;
710 GSimpleAsyncResult *simple;
712 /* propagate the possible error */
713 if (G_IS_SIMPLE_ASYNC_RESULT (res))
715 simple = G_SIMPLE_ASYNC_RESULT (res);
716 g_simple_async_result_propagate_error (simple, &data->flush_error);
719 class = G_OUTPUT_STREAM_GET_CLASS (stream);
721 /* we still close, even if there was a flush error */
722 class->close_async (stream, data->io_priority, data->cancellable,
723 async_ready_close_callback_wrapper, user_data);
727 * g_output_stream_write_async:
728 * @stream: A #GOutputStream.
729 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
730 * @count: the number of bytes to write
731 * @io_priority: the io priority of the request.
732 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
733 * @callback: (scope async): callback to call when the request is satisfied
734 * @user_data: (closure): the data to pass to callback function
736 * Request an asynchronous write of @count bytes from @buffer into
737 * the stream. When the operation is finished @callback will be called.
738 * You can then call g_output_stream_write_finish() to get the result of the
741 * During an async request no other sync and async calls are allowed,
742 * and will result in %G_IO_ERROR_PENDING errors.
744 * A value of @count larger than %G_MAXSSIZE will cause a
745 * %G_IO_ERROR_INVALID_ARGUMENT error.
747 * On success, the number of bytes written will be passed to the
748 * @callback. It is not an error if this is not the same as the
749 * requested size, as it can happen e.g. on a partial I/O error,
750 * but generally we try to write as many bytes as requested.
752 * You are guaranteed that this method will never fail with
753 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
754 * method will just wait until this changes.
756 * Any outstanding I/O request with higher priority (lower numerical
757 * value) will be executed before an outstanding request with lower
758 * priority. Default priority is %G_PRIORITY_DEFAULT.
760 * The asyncronous methods have a default fallback that uses threads
761 * to implement asynchronicity, so they are optional for inheriting
762 * classes. However, if you override one you must override all.
764 * For the synchronous, blocking version of this function, see
765 * g_output_stream_write().
768 g_output_stream_write_async (GOutputStream *stream,
772 GCancellable *cancellable,
773 GAsyncReadyCallback callback,
776 GOutputStreamClass *class;
777 GSimpleAsyncResult *simple;
778 GError *error = NULL;
780 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
781 g_return_if_fail (buffer != NULL);
785 simple = g_simple_async_result_new (G_OBJECT (stream),
788 g_output_stream_write_async);
789 g_simple_async_result_complete_in_idle (simple);
790 g_object_unref (simple);
794 if (((gssize) count) < 0)
796 g_simple_async_report_error_in_idle (G_OBJECT (stream),
799 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
800 _("Too large count value passed to %s"),
805 if (!g_output_stream_set_pending (stream, &error))
807 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
814 class = G_OUTPUT_STREAM_GET_CLASS (stream);
816 stream->priv->outstanding_callback = callback;
817 g_object_ref (stream);
818 class->write_async (stream, buffer, count, io_priority, cancellable,
819 async_ready_callback_wrapper, user_data);
823 * g_output_stream_write_finish:
824 * @stream: a #GOutputStream.
825 * @result: a #GAsyncResult.
826 * @error: a #GError location to store the error occurring, or %NULL to
829 * Finishes a stream write operation.
831 * Returns: a #gssize containing the number of bytes written to the stream.
834 g_output_stream_write_finish (GOutputStream *stream,
835 GAsyncResult *result,
838 GSimpleAsyncResult *simple;
839 GOutputStreamClass *class;
841 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
842 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
844 if (g_async_result_legacy_propagate_error (result, error))
847 if (G_IS_SIMPLE_ASYNC_RESULT (result))
849 simple = G_SIMPLE_ASYNC_RESULT (result);
851 /* Special case writes of 0 bytes */
852 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
856 class = G_OUTPUT_STREAM_GET_CLASS (stream);
857 return class->write_finish (stream, result, error);
861 write_bytes_callback (GObject *stream,
862 GAsyncResult *result,
865 GSimpleAsyncResult *simple = user_data;
866 GError *error = NULL;
869 nwrote = g_output_stream_write_finish (G_OUTPUT_STREAM (stream),
872 g_simple_async_result_take_error (simple, error);
874 g_simple_async_result_set_op_res_gssize (simple, nwrote);
875 g_simple_async_result_complete (simple);
876 g_object_unref (simple);
880 * g_output_stream_write_bytes_async:
881 * @stream: A #GOutputStream.
882 * @bytes: The bytes to write
883 * @io_priority: the io priority of the request.
884 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
885 * @callback: (scope async): callback to call when the request is satisfied
886 * @user_data: (closure): the data to pass to callback function
888 * Request an asynchronous write of the data in @bytes to the stream.
889 * When the operation is finished @callback will be called. You can
890 * then call g_output_stream_write_bytes_finish() to get the result of
893 * During an async request no other sync and async calls are allowed,
894 * and will result in %G_IO_ERROR_PENDING errors.
896 * A #GBytes larger than %G_MAXSSIZE will cause a
897 * %G_IO_ERROR_INVALID_ARGUMENT error.
899 * On success, the number of bytes written will be passed to the
900 * @callback. It is not an error if this is not the same as the
901 * requested size, as it can happen e.g. on a partial I/O error,
902 * but generally we try to write as many bytes as requested.
904 * You are guaranteed that this method will never fail with
905 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
906 * method will just wait until this changes.
908 * Any outstanding I/O request with higher priority (lower numerical
909 * value) will be executed before an outstanding request with lower
910 * priority. Default priority is %G_PRIORITY_DEFAULT.
912 * For the synchronous, blocking version of this function, see
913 * g_output_stream_write_bytes().
916 g_output_stream_write_bytes_async (GOutputStream *stream,
919 GCancellable *cancellable,
920 GAsyncReadyCallback callback,
923 GSimpleAsyncResult *simple;
927 data = g_bytes_get_data (bytes, &size);
929 simple = g_simple_async_result_new (G_OBJECT (stream),
931 g_output_stream_write_bytes_async);
932 g_simple_async_result_set_op_res_gpointer (simple, g_bytes_ref (bytes),
933 (GDestroyNotify) g_bytes_unref);
935 g_output_stream_write_async (stream,
939 write_bytes_callback,
944 * g_output_stream_write_bytes_finish:
945 * @stream: a #GOutputStream.
946 * @result: a #GAsyncResult.
947 * @error: a #GError location to store the error occurring, or %NULL to
950 * Finishes a stream write-from-#GBytes operation.
952 * Returns: a #gssize containing the number of bytes written to the stream.
955 g_output_stream_write_bytes_finish (GOutputStream *stream,
956 GAsyncResult *result,
959 GSimpleAsyncResult *simple;
961 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
962 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (stream), g_output_stream_write_bytes_async), -1);
964 simple = G_SIMPLE_ASYNC_RESULT (result);
965 if (g_simple_async_result_propagate_error (simple, error))
967 return g_simple_async_result_get_op_res_gssize (simple);
971 GInputStream *source;
973 GAsyncReadyCallback callback;
977 async_ready_splice_callback_wrapper (GObject *source_object,
981 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
982 SpliceUserData *data = _data;
984 g_output_stream_clear_pending (stream);
987 (*data->callback) (source_object, res, data->user_data);
989 g_object_unref (stream);
990 g_object_unref (data->source);
995 * g_output_stream_splice_async:
996 * @stream: a #GOutputStream.
997 * @source: a #GInputStream.
998 * @flags: a set of #GOutputStreamSpliceFlags.
999 * @io_priority: the io priority of the request.
1000 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1001 * @callback: (scope async): a #GAsyncReadyCallback.
1002 * @user_data: (closure): user data passed to @callback.
1004 * Splices a stream asynchronously.
1005 * When the operation is finished @callback will be called.
1006 * You can then call g_output_stream_splice_finish() to get the
1007 * result of the operation.
1009 * For the synchronous, blocking version of this function, see
1010 * g_output_stream_splice().
1013 g_output_stream_splice_async (GOutputStream *stream,
1014 GInputStream *source,
1015 GOutputStreamSpliceFlags flags,
1017 GCancellable *cancellable,
1018 GAsyncReadyCallback callback,
1021 GOutputStreamClass *class;
1022 SpliceUserData *data;
1023 GError *error = NULL;
1025 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1026 g_return_if_fail (G_IS_INPUT_STREAM (source));
1028 if (g_input_stream_is_closed (source))
1030 g_simple_async_report_error_in_idle (G_OBJECT (stream),
1033 G_IO_ERROR, G_IO_ERROR_CLOSED,
1034 _("Source stream is already closed"));
1038 if (!g_output_stream_set_pending (stream, &error))
1040 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
1047 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1049 data = g_new0 (SpliceUserData, 1);
1050 data->callback = callback;
1051 data->user_data = user_data;
1052 data->source = g_object_ref (source);
1054 g_object_ref (stream);
1055 class->splice_async (stream, source, flags, io_priority, cancellable,
1056 async_ready_splice_callback_wrapper, data);
1060 * g_output_stream_splice_finish:
1061 * @stream: a #GOutputStream.
1062 * @result: a #GAsyncResult.
1063 * @error: a #GError location to store the error occurring, or %NULL to
1066 * Finishes an asynchronous stream splice operation.
1068 * Returns: a #gssize of the number of bytes spliced. Note that if the
1069 * number of bytes spliced is greater than %G_MAXSSIZE, then that
1070 * will be returned, and there is no way to determine the actual
1071 * number of bytes spliced.
1074 g_output_stream_splice_finish (GOutputStream *stream,
1075 GAsyncResult *result,
1078 GOutputStreamClass *class;
1080 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
1081 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
1083 if (g_async_result_legacy_propagate_error (result, error))
1086 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1087 return class->splice_finish (stream, result, error);
1091 * g_output_stream_flush_async:
1092 * @stream: a #GOutputStream.
1093 * @io_priority: the io priority of the request.
1094 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
1095 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
1096 * @user_data: (closure): the data to pass to callback function
1098 * Forces an asynchronous write of all user-space buffered data for
1099 * the given @stream.
1100 * For behaviour details see g_output_stream_flush().
1102 * When the operation is finished @callback will be
1103 * called. You can then call g_output_stream_flush_finish() to get the
1104 * result of the operation.
1107 g_output_stream_flush_async (GOutputStream *stream,
1109 GCancellable *cancellable,
1110 GAsyncReadyCallback callback,
1113 GOutputStreamClass *class;
1114 GSimpleAsyncResult *simple;
1115 GError *error = NULL;
1117 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1119 if (!g_output_stream_set_pending (stream, &error))
1121 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
1128 stream->priv->outstanding_callback = callback;
1129 g_object_ref (stream);
1131 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1133 if (class->flush_async == NULL)
1135 simple = g_simple_async_result_new (G_OBJECT (stream),
1136 async_ready_callback_wrapper,
1138 g_output_stream_flush_async);
1139 g_simple_async_result_complete_in_idle (simple);
1140 g_object_unref (simple);
1144 class->flush_async (stream, io_priority, cancellable,
1145 async_ready_callback_wrapper, user_data);
1149 * g_output_stream_flush_finish:
1150 * @stream: a #GOutputStream.
1151 * @result: a GAsyncResult.
1152 * @error: a #GError location to store the error occurring, or %NULL to
1155 * Finishes flushing an output stream.
1157 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1160 g_output_stream_flush_finish (GOutputStream *stream,
1161 GAsyncResult *result,
1164 GSimpleAsyncResult *simple;
1165 GOutputStreamClass *klass;
1167 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1168 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1170 if (g_async_result_legacy_propagate_error (result, error))
1173 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1175 simple = G_SIMPLE_ASYNC_RESULT (result);
1177 /* Special case default implementation */
1178 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
1182 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
1183 return klass->flush_finish (stream, result, error);
1188 * g_output_stream_close_async:
1189 * @stream: A #GOutputStream.
1190 * @io_priority: the io priority of the request.
1191 * @cancellable: (allow-none): optional cancellable object
1192 * @callback: (scope async): callback to call when the request is satisfied
1193 * @user_data: (closure): the data to pass to callback function
1195 * Requests an asynchronous close of the stream, releasing resources
1196 * related to it. When the operation is finished @callback will be
1197 * called. You can then call g_output_stream_close_finish() to get
1198 * the result of the operation.
1200 * For behaviour details see g_output_stream_close().
1202 * The asyncronous methods have a default fallback that uses threads
1203 * to implement asynchronicity, so they are optional for inheriting
1204 * classes. However, if you override one you must override all.
1207 g_output_stream_close_async (GOutputStream *stream,
1209 GCancellable *cancellable,
1210 GAsyncReadyCallback callback,
1213 GOutputStreamClass *class;
1214 GSimpleAsyncResult *simple;
1215 GError *error = NULL;
1216 CloseUserData *data;
1218 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1220 if (stream->priv->closed)
1222 simple = g_simple_async_result_new (G_OBJECT (stream),
1225 g_output_stream_close_async);
1226 g_simple_async_result_complete_in_idle (simple);
1227 g_object_unref (simple);
1231 if (!g_output_stream_set_pending (stream, &error))
1233 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
1240 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1241 stream->priv->closing = TRUE;
1242 stream->priv->outstanding_callback = callback;
1243 g_object_ref (stream);
1245 data = g_slice_new0 (CloseUserData);
1247 if (cancellable != NULL)
1248 data->cancellable = g_object_ref (cancellable);
1250 data->io_priority = io_priority;
1251 data->user_data = user_data;
1253 /* Call close_async directly if there is no need to flush, or if the flush
1254 can be done sync (in the output stream async close thread) */
1255 if (class->flush_async == NULL ||
1256 (class->flush_async == g_output_stream_real_flush_async &&
1257 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1259 class->close_async (stream, io_priority, cancellable,
1260 async_ready_close_callback_wrapper, data);
1264 /* First do an async flush, then do the async close in the callback
1265 wrapper (see async_ready_close_flushed_callback_wrapper) */
1266 class->flush_async (stream, io_priority, cancellable,
1267 async_ready_close_flushed_callback_wrapper, data);
1272 * g_output_stream_close_finish:
1273 * @stream: a #GOutputStream.
1274 * @result: a #GAsyncResult.
1275 * @error: a #GError location to store the error occurring, or %NULL to
1278 * Closes an output stream.
1280 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1283 g_output_stream_close_finish (GOutputStream *stream,
1284 GAsyncResult *result,
1287 GSimpleAsyncResult *simple;
1288 GOutputStreamClass *class;
1290 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1291 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1293 if (g_async_result_legacy_propagate_error (result, error))
1296 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1298 simple = G_SIMPLE_ASYNC_RESULT (result);
1300 /* Special case already closed */
1301 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1305 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1306 return class->close_finish (stream, result, error);
1310 * g_output_stream_is_closed:
1311 * @stream: a #GOutputStream.
1313 * Checks if an output stream has already been closed.
1315 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1318 g_output_stream_is_closed (GOutputStream *stream)
1320 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1322 return stream->priv->closed;
1326 * g_output_stream_is_closing:
1327 * @stream: a #GOutputStream.
1329 * Checks if an output stream is being closed. This can be
1330 * used inside e.g. a flush implementation to see if the
1331 * flush (or other i/o operation) is called from within
1332 * the closing operation.
1334 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1339 g_output_stream_is_closing (GOutputStream *stream)
1341 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1343 return stream->priv->closing;
1347 * g_output_stream_has_pending:
1348 * @stream: a #GOutputStream.
1350 * Checks if an ouput stream has pending actions.
1352 * Returns: %TRUE if @stream has pending actions.
1355 g_output_stream_has_pending (GOutputStream *stream)
1357 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1359 return stream->priv->pending;
1363 * g_output_stream_set_pending:
1364 * @stream: a #GOutputStream.
1365 * @error: a #GError location to store the error occurring, or %NULL to
1368 * Sets @stream to have actions pending. If the pending flag is
1369 * already set or @stream is closed, it will return %FALSE and set
1372 * Return value: %TRUE if pending was previously unset and is now set.
1375 g_output_stream_set_pending (GOutputStream *stream,
1378 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1380 if (stream->priv->closed)
1382 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1383 _("Stream is already closed"));
1387 if (stream->priv->pending)
1389 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1390 /* Translators: This is an error you get if there is
1391 * already an operation running against this stream when
1392 * you try to start one */
1393 _("Stream has outstanding operation"));
1397 stream->priv->pending = TRUE;
1402 * g_output_stream_clear_pending:
1403 * @stream: output stream
1405 * Clears the pending flag on @stream.
1408 g_output_stream_clear_pending (GOutputStream *stream)
1410 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1412 stream->priv->pending = FALSE;
1416 /********************************************
1417 * Default implementation of async ops *
1418 ********************************************/
1422 gsize count_requested;
1423 gssize count_written;
1425 GCancellable *cancellable;
1431 free_write_data (WriteData *op)
1433 if (op->cancellable)
1434 g_object_unref (op->cancellable);
1435 g_slice_free (WriteData, op);
1439 write_async_thread (GSimpleAsyncResult *res,
1441 GCancellable *cancellable)
1444 GOutputStreamClass *class;
1445 GError *error = NULL;
1447 class = G_OUTPUT_STREAM_GET_CLASS (object);
1448 op = g_simple_async_result_get_op_res_gpointer (res);
1449 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1450 cancellable, &error);
1451 if (op->count_written == -1)
1452 g_simple_async_result_take_error (res, error);
1455 static void write_async_pollable (GPollableOutputStream *stream,
1456 GSimpleAsyncResult *result);
1459 write_async_pollable_ready (GPollableOutputStream *stream,
1462 GSimpleAsyncResult *result = user_data;
1464 write_async_pollable (stream, result);
1469 write_async_pollable (GPollableOutputStream *stream,
1470 GSimpleAsyncResult *result)
1472 GError *error = NULL;
1473 WriteData *op = g_simple_async_result_get_op_res_gpointer (result);
1475 if (g_cancellable_set_error_if_cancelled (op->cancellable, &error))
1476 op->count_written = -1;
1479 op->count_written = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
1480 write_nonblocking (stream, op->buffer, op->count_requested, &error);
1483 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1487 g_error_free (error);
1488 op->need_idle = FALSE;
1490 source = g_pollable_output_stream_create_source (stream, op->cancellable);
1491 g_source_set_callback (source,
1492 (GSourceFunc) write_async_pollable_ready,
1493 g_object_ref (result), g_object_unref);
1494 g_source_set_priority (source, op->io_priority);
1495 g_source_attach (source, g_main_context_get_thread_default ());
1496 g_source_unref (source);
1500 if (op->count_written == -1)
1501 g_simple_async_result_take_error (result, error);
1504 g_simple_async_result_complete_in_idle (result);
1506 g_simple_async_result_complete (result);
1510 g_output_stream_real_write_async (GOutputStream *stream,
1514 GCancellable *cancellable,
1515 GAsyncReadyCallback callback,
1518 GSimpleAsyncResult *res;
1521 op = g_slice_new0 (WriteData);
1522 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1523 g_simple_async_result_set_op_res_gpointer (res, op, (GDestroyNotify) free_write_data);
1524 op->buffer = buffer;
1525 op->count_requested = count;
1526 op->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
1527 op->io_priority = io_priority;
1528 op->need_idle = TRUE;
1530 if (G_IS_POLLABLE_OUTPUT_STREAM (stream) &&
1531 g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (stream)))
1532 write_async_pollable (G_POLLABLE_OUTPUT_STREAM (stream), res);
1534 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1535 g_object_unref (res);
1539 g_output_stream_real_write_finish (GOutputStream *stream,
1540 GAsyncResult *result,
1543 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1546 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1548 if (g_simple_async_result_propagate_error (simple, error))
1551 op = g_simple_async_result_get_op_res_gpointer (simple);
1552 return op->count_written;
1556 GInputStream *source;
1557 GOutputStreamSpliceFlags flags;
1558 gssize bytes_copied;
1562 splice_async_thread (GSimpleAsyncResult *result,
1564 GCancellable *cancellable)
1567 GOutputStreamClass *class;
1568 GError *error = NULL;
1569 GOutputStream *stream;
1571 stream = G_OUTPUT_STREAM (object);
1572 class = G_OUTPUT_STREAM_GET_CLASS (object);
1573 op = g_simple_async_result_get_op_res_gpointer (result);
1575 op->bytes_copied = class->splice (stream,
1580 if (op->bytes_copied == -1)
1581 g_simple_async_result_take_error (result, error);
1585 g_output_stream_real_splice_async (GOutputStream *stream,
1586 GInputStream *source,
1587 GOutputStreamSpliceFlags flags,
1589 GCancellable *cancellable,
1590 GAsyncReadyCallback callback,
1593 GSimpleAsyncResult *res;
1596 op = g_new0 (SpliceData, 1);
1597 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1598 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1600 op->source = source;
1602 /* TODO: In the case where both source and destintion have
1603 non-threadbased async calls we can use a true async copy here */
1605 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1606 g_object_unref (res);
1610 g_output_stream_real_splice_finish (GOutputStream *stream,
1611 GAsyncResult *result,
1614 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1617 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1619 if (g_simple_async_result_propagate_error (simple, error))
1622 op = g_simple_async_result_get_op_res_gpointer (simple);
1623 return op->bytes_copied;
1628 flush_async_thread (GSimpleAsyncResult *res,
1630 GCancellable *cancellable)
1632 GOutputStreamClass *class;
1634 GError *error = NULL;
1636 class = G_OUTPUT_STREAM_GET_CLASS (object);
1639 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1642 g_simple_async_result_take_error (res, error);
1646 g_output_stream_real_flush_async (GOutputStream *stream,
1648 GCancellable *cancellable,
1649 GAsyncReadyCallback callback,
1652 GSimpleAsyncResult *res;
1654 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1656 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1657 g_object_unref (res);
1661 g_output_stream_real_flush_finish (GOutputStream *stream,
1662 GAsyncResult *result,
1665 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1667 if (g_simple_async_result_propagate_error (simple, error))
1673 close_async_thread (GSimpleAsyncResult *res,
1675 GCancellable *cancellable)
1677 GOutputStreamClass *class;
1678 GError *error = NULL;
1679 gboolean result = TRUE;
1681 class = G_OUTPUT_STREAM_GET_CLASS (object);
1683 /* Do a flush here if there is a flush function, and we did not have to do
1684 an async flush before (see g_output_stream_close_async) */
1685 if (class->flush != NULL &&
1686 (class->flush_async == NULL ||
1687 class->flush_async == g_output_stream_real_flush_async))
1689 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1692 /* Auto handling of cancelation disabled, and ignore
1693 cancellation, since we want to close things anyway, although
1694 possibly in a quick-n-dirty way. At least we never want to leak
1697 if (class->close_fn)
1699 /* Make sure to close, even if the flush failed (see sync close) */
1701 class->close_fn (G_OUTPUT_STREAM (object), cancellable, NULL);
1703 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1706 g_simple_async_result_take_error (res, error);
1711 g_output_stream_real_close_async (GOutputStream *stream,
1713 GCancellable *cancellable,
1714 GAsyncReadyCallback callback,
1717 GSimpleAsyncResult *res;
1719 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1721 g_simple_async_result_set_handle_cancellation (res, FALSE);
1723 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1724 g_object_unref (res);
1728 g_output_stream_real_close_finish (GOutputStream *stream,
1729 GAsyncResult *result,
1732 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1734 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1736 if (g_simple_async_result_propagate_error (simple, error))