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"
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,
100 g_output_stream_finalize (GObject *object)
102 G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
106 g_output_stream_dispose (GObject *object)
108 GOutputStream *stream;
110 stream = G_OUTPUT_STREAM (object);
112 if (!stream->priv->closed)
113 g_output_stream_close (stream, NULL, NULL);
115 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
119 g_output_stream_class_init (GOutputStreamClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
123 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
125 gobject_class->finalize = g_output_stream_finalize;
126 gobject_class->dispose = g_output_stream_dispose;
128 klass->splice = g_output_stream_real_splice;
130 klass->write_async = g_output_stream_real_write_async;
131 klass->write_finish = g_output_stream_real_write_finish;
132 klass->splice_async = g_output_stream_real_splice_async;
133 klass->splice_finish = g_output_stream_real_splice_finish;
134 klass->flush_async = g_output_stream_real_flush_async;
135 klass->flush_finish = g_output_stream_real_flush_finish;
136 klass->close_async = g_output_stream_real_close_async;
137 klass->close_finish = g_output_stream_real_close_finish;
141 g_output_stream_init (GOutputStream *stream)
143 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
144 G_TYPE_OUTPUT_STREAM,
145 GOutputStreamPrivate);
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.
177 * Return value: Number of bytes written, or -1 on error
180 g_output_stream_write (GOutputStream *stream,
183 GCancellable *cancellable,
186 GOutputStreamClass *class;
189 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
190 g_return_val_if_fail (buffer != NULL, 0);
195 if (((gssize) count) < 0)
197 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
198 _("Too large count value passed to %s"), G_STRFUNC);
202 class = G_OUTPUT_STREAM_GET_CLASS (stream);
204 if (class->write_fn == NULL)
206 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
207 _("Output stream doesn't implement write"));
211 if (!g_output_stream_set_pending (stream, error))
215 g_cancellable_push_current (cancellable);
217 res = class->write_fn (stream, buffer, count, cancellable, error);
220 g_cancellable_pop_current (cancellable);
222 g_output_stream_clear_pending (stream);
228 * g_output_stream_write_all:
229 * @stream: a #GOutputStream.
230 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
231 * @count: the number of bytes to write
232 * @bytes_written: (out): location to store the number of bytes that was
233 * written to the stream
234 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
235 * @error: location to store the error occurring, or %NULL to ignore
237 * Tries to write @count bytes from @buffer into the stream. Will block
238 * during the operation.
240 * This function is similar to g_output_stream_write(), except it tries to
241 * write as many bytes as requested, only stopping on an error.
243 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
246 * If there is an error during the operation FALSE is returned and @error
247 * is set to indicate the error status, @bytes_written is updated to contain
248 * the number of bytes written into the stream before the error occurred.
250 * Return value: %TRUE on success, %FALSE if there was an error
253 g_output_stream_write_all (GOutputStream *stream,
256 gsize *bytes_written,
257 GCancellable *cancellable,
260 gsize _bytes_written;
263 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
264 g_return_val_if_fail (buffer != NULL, FALSE);
267 while (_bytes_written < count)
269 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
274 *bytes_written = _bytes_written;
279 g_warning ("Write returned zero without error");
281 _bytes_written += res;
285 *bytes_written = _bytes_written;
291 * g_output_stream_flush:
292 * @stream: a #GOutputStream.
293 * @cancellable: (allow-none): optional cancellable object
294 * @error: location to store the error occurring, or %NULL to ignore
296 * Flushed any outstanding buffers in the stream. Will block during
297 * the operation. Closing the stream will implicitly cause a flush.
299 * This function is optional for inherited classes.
301 * If @cancellable is not %NULL, then the operation can be cancelled by
302 * triggering the cancellable object from another thread. If the operation
303 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
305 * Return value: %TRUE on success, %FALSE on error
308 g_output_stream_flush (GOutputStream *stream,
309 GCancellable *cancellable,
312 GOutputStreamClass *class;
315 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
317 if (!g_output_stream_set_pending (stream, error))
320 class = G_OUTPUT_STREAM_GET_CLASS (stream);
326 g_cancellable_push_current (cancellable);
328 res = class->flush (stream, cancellable, error);
331 g_cancellable_pop_current (cancellable);
334 g_output_stream_clear_pending (stream);
340 * g_output_stream_splice:
341 * @stream: a #GOutputStream.
342 * @source: a #GInputStream.
343 * @flags: a set of #GOutputStreamSpliceFlags.
344 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
345 * @error: a #GError location to store the error occurring, or %NULL to
348 * Splices an input stream into an output stream.
350 * Returns: a #gssize containing the size of the data spliced, or
351 * -1 if an error occurred. Note that if the number of bytes
352 * spliced is greater than %G_MAXSSIZE, then that will be
353 * returned, and there is no way to determine the actual number
357 g_output_stream_splice (GOutputStream *stream,
358 GInputStream *source,
359 GOutputStreamSpliceFlags flags,
360 GCancellable *cancellable,
363 GOutputStreamClass *class;
366 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
367 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
369 if (g_input_stream_is_closed (source))
371 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
372 _("Source stream is already closed"));
376 if (!g_output_stream_set_pending (stream, error))
379 class = G_OUTPUT_STREAM_GET_CLASS (stream);
382 g_cancellable_push_current (cancellable);
384 bytes_copied = class->splice (stream, source, flags, cancellable, error);
387 g_cancellable_pop_current (cancellable);
389 g_output_stream_clear_pending (stream);
395 g_output_stream_real_splice (GOutputStream *stream,
396 GInputStream *source,
397 GOutputStreamSpliceFlags flags,
398 GCancellable *cancellable,
401 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
402 gssize n_read, n_written;
404 char buffer[8192], *p;
408 if (class->write_fn == NULL)
410 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
411 _("Output stream doesn't implement write"));
419 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
432 n_written = class->write_fn (stream, p, n_read, cancellable, error);
441 bytes_copied += n_written;
444 if (bytes_copied > G_MAXSSIZE)
445 bytes_copied = G_MAXSSIZE;
451 error = NULL; /* Ignore further errors */
453 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
455 /* Don't care about errors in source here */
456 g_input_stream_close (source, cancellable, NULL);
459 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
461 /* But write errors on close are bad! */
462 if (class->close_fn &&
463 !class->close_fn (stream, cancellable, error))
475 * g_output_stream_close:
476 * @stream: A #GOutputStream.
477 * @cancellable: (allow-none): optional cancellable object
478 * @error: location to store the error occurring, or %NULL to ignore
480 * Closes the stream, releasing resources related to it.
482 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
483 * Closing a stream multiple times will not return an error.
485 * Closing a stream will automatically flush any outstanding buffers in the
488 * Streams will be automatically closed when the last reference
489 * is dropped, but you might want to call this function to make sure
490 * resources are released as early as possible.
492 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
493 * open after the stream is closed. See the documentation for the individual
494 * stream for details.
496 * On failure the first error that happened will be reported, but the close
497 * operation will finish as much as possible. A stream that failed to
498 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
499 * is important to check and report the error to the user, otherwise
500 * there might be a loss of data as all data might not be written.
502 * If @cancellable is not NULL, then the operation can be cancelled by
503 * triggering the cancellable object from another thread. If the operation
504 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
505 * Cancelling a close will still leave the stream closed, but there some streams
506 * can use a faster close that doesn't block to e.g. check errors. On
507 * cancellation (as with any error) there is no guarantee that all written
508 * data will reach the target.
510 * Return value: %TRUE on success, %FALSE on failure
513 g_output_stream_close (GOutputStream *stream,
514 GCancellable *cancellable,
517 GOutputStreamClass *class;
520 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
522 class = G_OUTPUT_STREAM_GET_CLASS (stream);
524 if (stream->priv->closed)
527 if (!g_output_stream_set_pending (stream, error))
530 stream->priv->closing = TRUE;
533 g_cancellable_push_current (cancellable);
536 res = class->flush (stream, cancellable, error);
542 /* flushing caused the error that we want to return,
543 * but we still want to close the underlying stream if possible
546 class->close_fn (stream, cancellable, NULL);
552 res = class->close_fn (stream, cancellable, error);
556 g_cancellable_pop_current (cancellable);
558 stream->priv->closing = FALSE;
559 stream->priv->closed = TRUE;
560 g_output_stream_clear_pending (stream);
566 async_ready_callback_wrapper (GObject *source_object,
570 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
572 g_output_stream_clear_pending (stream);
573 if (stream->priv->outstanding_callback)
574 (*stream->priv->outstanding_callback) (source_object, res, user_data);
575 g_object_unref (stream);
580 GCancellable *cancellable;
586 async_ready_close_callback_wrapper (GObject *source_object,
590 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
591 CloseUserData *data = user_data;
593 stream->priv->closing = FALSE;
594 stream->priv->closed = TRUE;
596 g_output_stream_clear_pending (stream);
598 if (stream->priv->outstanding_callback)
600 if (data->flush_error != NULL)
602 GSimpleAsyncResult *err;
604 err = g_simple_async_result_new_take_error (source_object,
605 stream->priv->outstanding_callback,
608 data->flush_error = NULL;
610 (*stream->priv->outstanding_callback) (source_object,
611 G_ASYNC_RESULT (err),
613 g_object_unref (err);
617 (*stream->priv->outstanding_callback) (source_object,
623 g_object_unref (stream);
625 if (data->cancellable)
626 g_object_unref (data->cancellable);
628 if (data->flush_error)
629 g_error_free (data->flush_error);
631 g_slice_free (CloseUserData, data);
635 async_ready_close_flushed_callback_wrapper (GObject *source_object,
639 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
640 GOutputStreamClass *class;
641 CloseUserData *data = user_data;
642 GSimpleAsyncResult *simple;
644 /* propagate the possible error */
645 if (G_IS_SIMPLE_ASYNC_RESULT (res))
647 simple = G_SIMPLE_ASYNC_RESULT (res);
648 g_simple_async_result_propagate_error (simple, &data->flush_error);
651 class = G_OUTPUT_STREAM_GET_CLASS (stream);
653 /* we still close, even if there was a flush error */
654 class->close_async (stream, data->io_priority, data->cancellable,
655 async_ready_close_callback_wrapper, user_data);
659 * g_output_stream_write_async:
660 * @stream: A #GOutputStream.
661 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
662 * @count: the number of bytes to write
663 * @io_priority: the io priority of the request.
664 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
665 * @callback: (scope async): callback to call when the request is satisfied
666 * @user_data: (closure): the data to pass to callback function
668 * Request an asynchronous write of @count bytes from @buffer into
669 * the stream. When the operation is finished @callback will be called.
670 * You can then call g_output_stream_write_finish() to get the result of the
673 * During an async request no other sync and async calls are allowed,
674 * and will result in %G_IO_ERROR_PENDING errors.
676 * A value of @count larger than %G_MAXSSIZE will cause a
677 * %G_IO_ERROR_INVALID_ARGUMENT error.
679 * On success, the number of bytes written will be passed to the
680 * @callback. It is not an error if this is not the same as the
681 * requested size, as it can happen e.g. on a partial I/O error,
682 * but generally we try to write as many bytes as requested.
684 * You are guaranteed that this method will never fail with
685 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
686 * method will just wait until this changes.
688 * Any outstanding I/O request with higher priority (lower numerical
689 * value) will be executed before an outstanding request with lower
690 * priority. Default priority is %G_PRIORITY_DEFAULT.
692 * The asyncronous methods have a default fallback that uses threads
693 * to implement asynchronicity, so they are optional for inheriting
694 * classes. However, if you override one you must override all.
696 * For the synchronous, blocking version of this function, see
697 * g_output_stream_write().
700 g_output_stream_write_async (GOutputStream *stream,
704 GCancellable *cancellable,
705 GAsyncReadyCallback callback,
708 GOutputStreamClass *class;
709 GSimpleAsyncResult *simple;
710 GError *error = NULL;
712 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
713 g_return_if_fail (buffer != NULL);
717 simple = g_simple_async_result_new (G_OBJECT (stream),
720 g_output_stream_write_async);
721 g_simple_async_result_complete_in_idle (simple);
722 g_object_unref (simple);
726 if (((gssize) count) < 0)
728 g_simple_async_report_error_in_idle (G_OBJECT (stream),
731 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
732 _("Too large count value passed to %s"),
737 if (!g_output_stream_set_pending (stream, &error))
739 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
746 class = G_OUTPUT_STREAM_GET_CLASS (stream);
748 stream->priv->outstanding_callback = callback;
749 g_object_ref (stream);
750 class->write_async (stream, buffer, count, io_priority, cancellable,
751 async_ready_callback_wrapper, user_data);
755 * g_output_stream_write_finish:
756 * @stream: a #GOutputStream.
757 * @result: a #GAsyncResult.
758 * @error: a #GError location to store the error occurring, or %NULL to
761 * Finishes a stream write operation.
763 * Returns: a #gssize containing the number of bytes written to the stream.
766 g_output_stream_write_finish (GOutputStream *stream,
767 GAsyncResult *result,
770 GSimpleAsyncResult *simple;
771 GOutputStreamClass *class;
773 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
774 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
776 if (G_IS_SIMPLE_ASYNC_RESULT (result))
778 simple = G_SIMPLE_ASYNC_RESULT (result);
779 if (g_simple_async_result_propagate_error (simple, error))
782 /* Special case writes of 0 bytes */
783 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
787 class = G_OUTPUT_STREAM_GET_CLASS (stream);
788 return class->write_finish (stream, result, error);
792 GInputStream *source;
794 GAsyncReadyCallback callback;
798 async_ready_splice_callback_wrapper (GObject *source_object,
802 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
803 SpliceUserData *data = _data;
805 g_output_stream_clear_pending (stream);
808 (*data->callback) (source_object, res, data->user_data);
810 g_object_unref (stream);
811 g_object_unref (data->source);
816 * g_output_stream_splice_async:
817 * @stream: a #GOutputStream.
818 * @source: a #GInputStream.
819 * @flags: a set of #GOutputStreamSpliceFlags.
820 * @io_priority: the io priority of the request.
821 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
822 * @callback: (scope async): a #GAsyncReadyCallback.
823 * @user_data: (closure): user data passed to @callback.
825 * Splices a stream asynchronously.
826 * When the operation is finished @callback will be called.
827 * You can then call g_output_stream_splice_finish() to get the
828 * result of the operation.
830 * For the synchronous, blocking version of this function, see
831 * g_output_stream_splice().
834 g_output_stream_splice_async (GOutputStream *stream,
835 GInputStream *source,
836 GOutputStreamSpliceFlags flags,
838 GCancellable *cancellable,
839 GAsyncReadyCallback callback,
842 GOutputStreamClass *class;
843 SpliceUserData *data;
844 GError *error = NULL;
846 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
847 g_return_if_fail (G_IS_INPUT_STREAM (source));
849 if (g_input_stream_is_closed (source))
851 g_simple_async_report_error_in_idle (G_OBJECT (stream),
854 G_IO_ERROR, G_IO_ERROR_CLOSED,
855 _("Source stream is already closed"));
859 if (!g_output_stream_set_pending (stream, &error))
861 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
868 class = G_OUTPUT_STREAM_GET_CLASS (stream);
870 data = g_new0 (SpliceUserData, 1);
871 data->callback = callback;
872 data->user_data = user_data;
873 data->source = g_object_ref (source);
875 g_object_ref (stream);
876 class->splice_async (stream, source, flags, io_priority, cancellable,
877 async_ready_splice_callback_wrapper, data);
881 * g_output_stream_splice_finish:
882 * @stream: a #GOutputStream.
883 * @result: a #GAsyncResult.
884 * @error: a #GError location to store the error occurring, or %NULL to
887 * Finishes an asynchronous stream splice operation.
889 * Returns: a #gssize of the number of bytes spliced. Note that if the
890 * number of bytes spliced is greater than %G_MAXSSIZE, then that
891 * will be returned, and there is no way to determine the actual
892 * number of bytes spliced.
895 g_output_stream_splice_finish (GOutputStream *stream,
896 GAsyncResult *result,
899 GSimpleAsyncResult *simple;
900 GOutputStreamClass *class;
902 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
903 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
905 if (G_IS_SIMPLE_ASYNC_RESULT (result))
907 simple = G_SIMPLE_ASYNC_RESULT (result);
908 if (g_simple_async_result_propagate_error (simple, error))
912 class = G_OUTPUT_STREAM_GET_CLASS (stream);
913 return class->splice_finish (stream, result, error);
917 * g_output_stream_flush_async:
918 * @stream: a #GOutputStream.
919 * @io_priority: the io priority of the request.
920 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
921 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
922 * @user_data: (closure): the data to pass to callback function
924 * Flushes a stream asynchronously.
925 * For behaviour details see g_output_stream_flush().
927 * When the operation is finished @callback will be
928 * called. You can then call g_output_stream_flush_finish() to get the
929 * result of the operation.
932 g_output_stream_flush_async (GOutputStream *stream,
934 GCancellable *cancellable,
935 GAsyncReadyCallback callback,
938 GOutputStreamClass *class;
939 GSimpleAsyncResult *simple;
940 GError *error = NULL;
942 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
944 if (!g_output_stream_set_pending (stream, &error))
946 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
953 stream->priv->outstanding_callback = callback;
954 g_object_ref (stream);
956 class = G_OUTPUT_STREAM_GET_CLASS (stream);
958 if (class->flush_async == NULL)
960 simple = g_simple_async_result_new (G_OBJECT (stream),
961 async_ready_callback_wrapper,
963 g_output_stream_flush_async);
964 g_simple_async_result_complete_in_idle (simple);
965 g_object_unref (simple);
969 class->flush_async (stream, io_priority, cancellable,
970 async_ready_callback_wrapper, user_data);
974 * g_output_stream_flush_finish:
975 * @stream: a #GOutputStream.
976 * @result: a GAsyncResult.
977 * @error: a #GError location to store the error occurring, or %NULL to
980 * Finishes flushing an output stream.
982 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
985 g_output_stream_flush_finish (GOutputStream *stream,
986 GAsyncResult *result,
989 GSimpleAsyncResult *simple;
990 GOutputStreamClass *klass;
992 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
993 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
995 if (G_IS_SIMPLE_ASYNC_RESULT (result))
997 simple = G_SIMPLE_ASYNC_RESULT (result);
998 if (g_simple_async_result_propagate_error (simple, error))
1001 /* Special case default implementation */
1002 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
1006 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
1007 return klass->flush_finish (stream, result, error);
1012 * g_output_stream_close_async:
1013 * @stream: A #GOutputStream.
1014 * @io_priority: the io priority of the request.
1015 * @cancellable: (allow-none): optional cancellable object
1016 * @callback: (scope async): callback to call when the request is satisfied
1017 * @user_data: (closure): the data to pass to callback function
1019 * Requests an asynchronous close of the stream, releasing resources
1020 * related to it. When the operation is finished @callback will be
1021 * called. You can then call g_output_stream_close_finish() to get
1022 * the result of the operation.
1024 * For behaviour details see g_output_stream_close().
1026 * The asyncronous methods have a default fallback that uses threads
1027 * to implement asynchronicity, so they are optional for inheriting
1028 * classes. However, if you override one you must override all.
1031 g_output_stream_close_async (GOutputStream *stream,
1033 GCancellable *cancellable,
1034 GAsyncReadyCallback callback,
1037 GOutputStreamClass *class;
1038 GSimpleAsyncResult *simple;
1039 GError *error = NULL;
1040 CloseUserData *data;
1042 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1044 if (stream->priv->closed)
1046 simple = g_simple_async_result_new (G_OBJECT (stream),
1049 g_output_stream_close_async);
1050 g_simple_async_result_complete_in_idle (simple);
1051 g_object_unref (simple);
1055 if (!g_output_stream_set_pending (stream, &error))
1057 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
1064 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1065 stream->priv->closing = TRUE;
1066 stream->priv->outstanding_callback = callback;
1067 g_object_ref (stream);
1069 data = g_slice_new0 (CloseUserData);
1071 if (cancellable != NULL)
1072 data->cancellable = g_object_ref (cancellable);
1074 data->io_priority = io_priority;
1075 data->user_data = user_data;
1077 /* Call close_async directly if there is no need to flush, or if the flush
1078 can be done sync (in the output stream async close thread) */
1079 if (class->flush_async == NULL ||
1080 (class->flush_async == g_output_stream_real_flush_async &&
1081 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1083 class->close_async (stream, io_priority, cancellable,
1084 async_ready_close_callback_wrapper, data);
1088 /* First do an async flush, then do the async close in the callback
1089 wrapper (see async_ready_close_flushed_callback_wrapper) */
1090 class->flush_async (stream, io_priority, cancellable,
1091 async_ready_close_flushed_callback_wrapper, data);
1096 * g_output_stream_close_finish:
1097 * @stream: a #GOutputStream.
1098 * @result: a #GAsyncResult.
1099 * @error: a #GError location to store the error occurring, or %NULL to
1102 * Closes an output stream.
1104 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1107 g_output_stream_close_finish (GOutputStream *stream,
1108 GAsyncResult *result,
1111 GSimpleAsyncResult *simple;
1112 GOutputStreamClass *class;
1114 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1115 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1117 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1119 simple = G_SIMPLE_ASYNC_RESULT (result);
1120 if (g_simple_async_result_propagate_error (simple, error))
1123 /* Special case already closed */
1124 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1128 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1129 return class->close_finish (stream, result, error);
1133 * g_output_stream_is_closed:
1134 * @stream: a #GOutputStream.
1136 * Checks if an output stream has already been closed.
1138 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1141 g_output_stream_is_closed (GOutputStream *stream)
1143 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1145 return stream->priv->closed;
1149 * g_output_stream_is_closing:
1150 * @stream: a #GOutputStream.
1152 * Checks if an output stream is being closed. This can be
1153 * used inside e.g. a flush implementation to see if the
1154 * flush (or other i/o operation) is called from within
1155 * the closing operation.
1157 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1162 g_output_stream_is_closing (GOutputStream *stream)
1164 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1166 return stream->priv->closing;
1170 * g_output_stream_has_pending:
1171 * @stream: a #GOutputStream.
1173 * Checks if an ouput stream has pending actions.
1175 * Returns: %TRUE if @stream has pending actions.
1178 g_output_stream_has_pending (GOutputStream *stream)
1180 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1182 return stream->priv->pending;
1186 * g_output_stream_set_pending:
1187 * @stream: a #GOutputStream.
1188 * @error: a #GError location to store the error occurring, or %NULL to
1191 * Sets @stream to have actions pending. If the pending flag is
1192 * already set or @stream is closed, it will return %FALSE and set
1195 * Return value: %TRUE if pending was previously unset and is now set.
1198 g_output_stream_set_pending (GOutputStream *stream,
1201 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1203 if (stream->priv->closed)
1205 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1206 _("Stream is already closed"));
1210 if (stream->priv->pending)
1212 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1213 /* Translators: This is an error you get if there is
1214 * already an operation running against this stream when
1215 * you try to start one */
1216 _("Stream has outstanding operation"));
1220 stream->priv->pending = TRUE;
1225 * g_output_stream_clear_pending:
1226 * @stream: output stream
1228 * Clears the pending flag on @stream.
1231 g_output_stream_clear_pending (GOutputStream *stream)
1233 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1235 stream->priv->pending = FALSE;
1239 /********************************************
1240 * Default implementation of async ops *
1241 ********************************************/
1245 gsize count_requested;
1246 gssize count_written;
1250 write_async_thread (GSimpleAsyncResult *res,
1252 GCancellable *cancellable)
1255 GOutputStreamClass *class;
1256 GError *error = NULL;
1258 class = G_OUTPUT_STREAM_GET_CLASS (object);
1259 op = g_simple_async_result_get_op_res_gpointer (res);
1260 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1261 cancellable, &error);
1262 if (op->count_written == -1)
1263 g_simple_async_result_take_error (res, error);
1267 g_output_stream_real_write_async (GOutputStream *stream,
1271 GCancellable *cancellable,
1272 GAsyncReadyCallback callback,
1275 GSimpleAsyncResult *res;
1278 op = g_new0 (WriteData, 1);
1279 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1280 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1281 op->buffer = buffer;
1282 op->count_requested = count;
1284 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1285 g_object_unref (res);
1289 g_output_stream_real_write_finish (GOutputStream *stream,
1290 GAsyncResult *result,
1293 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1296 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1297 op = g_simple_async_result_get_op_res_gpointer (simple);
1298 return op->count_written;
1302 GInputStream *source;
1303 GOutputStreamSpliceFlags flags;
1304 gssize bytes_copied;
1308 splice_async_thread (GSimpleAsyncResult *result,
1310 GCancellable *cancellable)
1313 GOutputStreamClass *class;
1314 GError *error = NULL;
1315 GOutputStream *stream;
1317 stream = G_OUTPUT_STREAM (object);
1318 class = G_OUTPUT_STREAM_GET_CLASS (object);
1319 op = g_simple_async_result_get_op_res_gpointer (result);
1321 op->bytes_copied = class->splice (stream,
1326 if (op->bytes_copied == -1)
1327 g_simple_async_result_take_error (result, error);
1331 g_output_stream_real_splice_async (GOutputStream *stream,
1332 GInputStream *source,
1333 GOutputStreamSpliceFlags flags,
1335 GCancellable *cancellable,
1336 GAsyncReadyCallback callback,
1339 GSimpleAsyncResult *res;
1342 op = g_new0 (SpliceData, 1);
1343 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1344 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1346 op->source = source;
1348 /* TODO: In the case where both source and destintion have
1349 non-threadbased async calls we can use a true async copy here */
1351 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1352 g_object_unref (res);
1356 g_output_stream_real_splice_finish (GOutputStream *stream,
1357 GAsyncResult *result,
1360 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1363 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1364 op = g_simple_async_result_get_op_res_gpointer (simple);
1365 return op->bytes_copied;
1370 flush_async_thread (GSimpleAsyncResult *res,
1372 GCancellable *cancellable)
1374 GOutputStreamClass *class;
1376 GError *error = NULL;
1378 class = G_OUTPUT_STREAM_GET_CLASS (object);
1381 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1384 g_simple_async_result_take_error (res, error);
1388 g_output_stream_real_flush_async (GOutputStream *stream,
1390 GCancellable *cancellable,
1391 GAsyncReadyCallback callback,
1394 GSimpleAsyncResult *res;
1396 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1398 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1399 g_object_unref (res);
1403 g_output_stream_real_flush_finish (GOutputStream *stream,
1404 GAsyncResult *result,
1411 close_async_thread (GSimpleAsyncResult *res,
1413 GCancellable *cancellable)
1415 GOutputStreamClass *class;
1416 GError *error = NULL;
1417 gboolean result = TRUE;
1419 class = G_OUTPUT_STREAM_GET_CLASS (object);
1421 /* Do a flush here if there is a flush function, and we did not have to do
1422 an async flush before (see g_output_stream_close_async) */
1423 if (class->flush != NULL &&
1424 (class->flush_async == NULL ||
1425 class->flush_async == g_output_stream_real_flush_async))
1427 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1430 /* Auto handling of cancelation disabled, and ignore
1431 cancellation, since we want to close things anyway, although
1432 possibly in a quick-n-dirty way. At least we never want to leak
1435 if (class->close_fn)
1437 /* Make sure to close, even if the flush failed (see sync close) */
1439 class->close_fn (G_OUTPUT_STREAM (object), cancellable, NULL);
1441 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1444 g_simple_async_result_take_error (res, error);
1449 g_output_stream_real_close_async (GOutputStream *stream,
1451 GCancellable *cancellable,
1452 GAsyncReadyCallback callback,
1455 GSimpleAsyncResult *res;
1457 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1459 g_simple_async_result_set_handle_cancellation (res, FALSE);
1461 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1462 g_object_unref (res);
1466 g_output_stream_real_close_finish (GOutputStream *stream,
1467 GAsyncResult *result,
1470 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1471 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);