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: optional cancellable object
154 * @error: location to store the error occuring, 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: location to store the number of bytes that was
233 * written to the stream
234 * @cancellable: optional #GCancellable object, %NULL to ignore.
235 * @error: location to store the error occuring, 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: optional cancellable object
294 * @error: location to store the error occuring, 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: optional #GCancellable object, %NULL to ignore.
345 * @error: a #GError location to store the error occuring, 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.
354 g_output_stream_splice (GOutputStream *stream,
355 GInputStream *source,
356 GOutputStreamSpliceFlags flags,
357 GCancellable *cancellable,
360 GOutputStreamClass *class;
363 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
364 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
366 if (g_input_stream_is_closed (source))
368 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
369 _("Source stream is already closed"));
373 if (!g_output_stream_set_pending (stream, error))
376 class = G_OUTPUT_STREAM_GET_CLASS (stream);
379 g_cancellable_push_current (cancellable);
381 bytes_copied = class->splice (stream, source, flags, cancellable, error);
384 g_cancellable_pop_current (cancellable);
386 g_output_stream_clear_pending (stream);
392 g_output_stream_real_splice (GOutputStream *stream,
393 GInputStream *source,
394 GOutputStreamSpliceFlags flags,
395 GCancellable *cancellable,
398 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
399 gssize n_read, n_written;
401 char buffer[8192], *p;
405 if (class->write_fn == NULL)
407 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
408 _("Output stream doesn't implement write"));
416 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
429 n_written = class->write_fn (stream, p, n_read, cancellable, error);
438 bytes_copied += n_written;
445 error = NULL; /* Ignore further errors */
447 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
449 /* Don't care about errors in source here */
450 g_input_stream_close (source, cancellable, NULL);
453 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
455 /* But write errors on close are bad! */
456 if (class->close_fn &&
457 !class->close_fn (stream, cancellable, error))
469 * g_output_stream_close:
470 * @stream: A #GOutputStream.
471 * @cancellable: optional cancellable object
472 * @error: location to store the error occuring, or %NULL to ignore
474 * Closes the stream, releasing resources related to it.
476 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
477 * Closing a stream multiple times will not return an error.
479 * Closing a stream will automatically flush any outstanding buffers in the
482 * Streams will be automatically closed when the last reference
483 * is dropped, but you might want to call this function to make sure
484 * resources are released as early as possible.
486 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
487 * open after the stream is closed. See the documentation for the individual
488 * stream for details.
490 * On failure the first error that happened will be reported, but the close
491 * operation will finish as much as possible. A stream that failed to
492 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
493 * is important to check and report the error to the user, otherwise
494 * there might be a loss of data as all data might not be written.
496 * If @cancellable is not NULL, then the operation can be cancelled by
497 * triggering the cancellable object from another thread. If the operation
498 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
499 * Cancelling a close will still leave the stream closed, but there some streams
500 * can use a faster close that doesn't block to e.g. check errors. On
501 * cancellation (as with any error) there is no guarantee that all written
502 * data will reach the target.
504 * Return value: %TRUE on success, %FALSE on failure
507 g_output_stream_close (GOutputStream *stream,
508 GCancellable *cancellable,
511 GOutputStreamClass *class;
514 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
516 class = G_OUTPUT_STREAM_GET_CLASS (stream);
518 if (stream->priv->closed)
521 if (!g_output_stream_set_pending (stream, error))
524 stream->priv->closing = TRUE;
527 g_cancellable_push_current (cancellable);
530 res = class->flush (stream, cancellable, error);
536 /* flushing caused the error that we want to return,
537 * but we still want to close the underlying stream if possible
540 class->close_fn (stream, cancellable, NULL);
546 res = class->close_fn (stream, cancellable, error);
550 g_cancellable_pop_current (cancellable);
552 stream->priv->closing = FALSE;
553 stream->priv->closed = TRUE;
554 g_output_stream_clear_pending (stream);
560 async_ready_callback_wrapper (GObject *source_object,
564 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
566 g_output_stream_clear_pending (stream);
567 if (stream->priv->outstanding_callback)
568 (*stream->priv->outstanding_callback) (source_object, res, user_data);
569 g_object_unref (stream);
574 GCancellable *cancellable;
580 async_ready_close_callback_wrapper (GObject *source_object,
584 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
585 CloseUserData *data = user_data;
587 stream->priv->closing = FALSE;
588 stream->priv->closed = TRUE;
590 g_output_stream_clear_pending (stream);
592 if (stream->priv->outstanding_callback)
594 if (data->flush_error != NULL)
596 GSimpleAsyncResult *err;
598 err = g_simple_async_result_new_take_error (source_object,
599 stream->priv->outstanding_callback,
602 data->flush_error = NULL;
604 (*stream->priv->outstanding_callback) (source_object,
605 G_ASYNC_RESULT (err),
607 g_object_unref (err);
611 (*stream->priv->outstanding_callback) (source_object,
617 g_object_unref (stream);
619 if (data->cancellable)
620 g_object_unref (data->cancellable);
622 if (data->flush_error)
623 g_error_free (data->flush_error);
625 g_slice_free (CloseUserData, data);
629 async_ready_close_flushed_callback_wrapper (GObject *source_object,
633 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
634 GOutputStreamClass *class;
635 CloseUserData *data = user_data;
636 GSimpleAsyncResult *simple;
638 /* propagate the possible error */
639 if (G_IS_SIMPLE_ASYNC_RESULT (res))
641 simple = G_SIMPLE_ASYNC_RESULT (res);
642 g_simple_async_result_propagate_error (simple, &data->flush_error);
645 class = G_OUTPUT_STREAM_GET_CLASS (stream);
647 /* we still close, even if there was a flush error */
648 class->close_async (stream, data->io_priority, data->cancellable,
649 async_ready_close_callback_wrapper, user_data);
653 * g_output_stream_write_async:
654 * @stream: A #GOutputStream.
655 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
656 * @count: the number of bytes to write
657 * @io_priority: the io priority of the request.
658 * @cancellable: optional #GCancellable object, %NULL to ignore.
659 * @callback: callback to call when the request is satisfied
660 * @user_data: the data to pass to callback function
662 * Request an asynchronous write of @count bytes from @buffer into
663 * the stream. When the operation is finished @callback will be called.
664 * You can then call g_output_stream_write_finish() to get the result of the
667 * During an async request no other sync and async calls are allowed,
668 * and will result in %G_IO_ERROR_PENDING errors.
670 * A value of @count larger than %G_MAXSSIZE will cause a
671 * %G_IO_ERROR_INVALID_ARGUMENT error.
673 * On success, the number of bytes written will be passed to the
674 * @callback. It is not an error if this is not the same as the
675 * requested size, as it can happen e.g. on a partial I/O error,
676 * but generally we try to write as many bytes as requested.
678 * You are guaranteed that this method will never fail with
679 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
680 * method will just wait until this changes.
682 * Any outstanding I/O request with higher priority (lower numerical
683 * value) will be executed before an outstanding request with lower
684 * priority. Default priority is %G_PRIORITY_DEFAULT.
686 * The asyncronous methods have a default fallback that uses threads
687 * to implement asynchronicity, so they are optional for inheriting
688 * classes. However, if you override one you must override all.
690 * For the synchronous, blocking version of this function, see
691 * g_output_stream_write().
694 g_output_stream_write_async (GOutputStream *stream,
698 GCancellable *cancellable,
699 GAsyncReadyCallback callback,
702 GOutputStreamClass *class;
703 GSimpleAsyncResult *simple;
704 GError *error = NULL;
706 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
707 g_return_if_fail (buffer != NULL);
711 simple = g_simple_async_result_new (G_OBJECT (stream),
714 g_output_stream_write_async);
715 g_simple_async_result_complete_in_idle (simple);
716 g_object_unref (simple);
720 if (((gssize) count) < 0)
722 g_simple_async_report_error_in_idle (G_OBJECT (stream),
725 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
726 _("Too large count value passed to %s"),
731 if (!g_output_stream_set_pending (stream, &error))
733 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
737 g_error_free (error);
741 class = G_OUTPUT_STREAM_GET_CLASS (stream);
743 stream->priv->outstanding_callback = callback;
744 g_object_ref (stream);
745 class->write_async (stream, buffer, count, io_priority, cancellable,
746 async_ready_callback_wrapper, user_data);
750 * g_output_stream_write_finish:
751 * @stream: a #GOutputStream.
752 * @result: a #GAsyncResult.
753 * @error: a #GError location to store the error occuring, or %NULL to
756 * Finishes a stream write operation.
758 * Returns: a #gssize containing the number of bytes written to the stream.
761 g_output_stream_write_finish (GOutputStream *stream,
762 GAsyncResult *result,
765 GSimpleAsyncResult *simple;
766 GOutputStreamClass *class;
768 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
769 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
771 if (G_IS_SIMPLE_ASYNC_RESULT (result))
773 simple = G_SIMPLE_ASYNC_RESULT (result);
774 if (g_simple_async_result_propagate_error (simple, error))
777 /* Special case writes of 0 bytes */
778 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
782 class = G_OUTPUT_STREAM_GET_CLASS (stream);
783 return class->write_finish (stream, result, error);
787 GInputStream *source;
789 GAsyncReadyCallback callback;
793 async_ready_splice_callback_wrapper (GObject *source_object,
797 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
798 SpliceUserData *data = _data;
800 g_output_stream_clear_pending (stream);
803 (*data->callback) (source_object, res, data->user_data);
805 g_object_unref (stream);
806 g_object_unref (data->source);
811 * g_output_stream_splice_async:
812 * @stream: a #GOutputStream.
813 * @source: a #GInputStream.
814 * @flags: a set of #GOutputStreamSpliceFlags.
815 * @io_priority: the io priority of the request.
816 * @cancellable: optional #GCancellable object, %NULL to ignore.
817 * @callback: a #GAsyncReadyCallback.
818 * @user_data: user data passed to @callback.
820 * Splices a stream asynchronously.
821 * When the operation is finished @callback will be called.
822 * You can then call g_output_stream_splice_finish() to get the
823 * result of the operation.
825 * For the synchronous, blocking version of this function, see
826 * g_output_stream_splice().
829 g_output_stream_splice_async (GOutputStream *stream,
830 GInputStream *source,
831 GOutputStreamSpliceFlags flags,
833 GCancellable *cancellable,
834 GAsyncReadyCallback callback,
837 GOutputStreamClass *class;
838 SpliceUserData *data;
839 GError *error = NULL;
841 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
842 g_return_if_fail (G_IS_INPUT_STREAM (source));
844 if (g_input_stream_is_closed (source))
846 g_simple_async_report_error_in_idle (G_OBJECT (stream),
849 G_IO_ERROR, G_IO_ERROR_CLOSED,
850 _("Source stream is already closed"));
854 if (!g_output_stream_set_pending (stream, &error))
856 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
860 g_error_free (error);
864 class = G_OUTPUT_STREAM_GET_CLASS (stream);
866 data = g_new0 (SpliceUserData, 1);
867 data->callback = callback;
868 data->user_data = user_data;
869 data->source = g_object_ref (source);
871 g_object_ref (stream);
872 class->splice_async (stream, source, flags, io_priority, cancellable,
873 async_ready_splice_callback_wrapper, data);
877 * g_output_stream_splice_finish:
878 * @stream: a #GOutputStream.
879 * @result: a #GAsyncResult.
880 * @error: a #GError location to store the error occuring, or %NULL to
883 * Finishes an asynchronous stream splice operation.
885 * Returns: a #gssize of the number of bytes spliced.
888 g_output_stream_splice_finish (GOutputStream *stream,
889 GAsyncResult *result,
892 GSimpleAsyncResult *simple;
893 GOutputStreamClass *class;
895 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
896 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
898 if (G_IS_SIMPLE_ASYNC_RESULT (result))
900 simple = G_SIMPLE_ASYNC_RESULT (result);
901 if (g_simple_async_result_propagate_error (simple, error))
905 class = G_OUTPUT_STREAM_GET_CLASS (stream);
906 return class->splice_finish (stream, result, error);
910 * g_output_stream_flush_async:
911 * @stream: a #GOutputStream.
912 * @io_priority: the io priority of the request.
913 * @cancellable: optional #GCancellable object, %NULL to ignore.
914 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
915 * @user_data: the data to pass to callback function
917 * Flushes a stream asynchronously.
918 * For behaviour details see g_output_stream_flush().
920 * When the operation is finished @callback will be
921 * called. You can then call g_output_stream_flush_finish() to get the
922 * result of the operation.
925 g_output_stream_flush_async (GOutputStream *stream,
927 GCancellable *cancellable,
928 GAsyncReadyCallback callback,
931 GOutputStreamClass *class;
932 GSimpleAsyncResult *simple;
933 GError *error = NULL;
935 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
937 if (!g_output_stream_set_pending (stream, &error))
939 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
943 g_error_free (error);
947 stream->priv->outstanding_callback = callback;
948 g_object_ref (stream);
950 class = G_OUTPUT_STREAM_GET_CLASS (stream);
952 if (class->flush_async == NULL)
954 simple = g_simple_async_result_new (G_OBJECT (stream),
955 async_ready_callback_wrapper,
957 g_output_stream_flush_async);
958 g_simple_async_result_complete_in_idle (simple);
959 g_object_unref (simple);
963 class->flush_async (stream, io_priority, cancellable,
964 async_ready_callback_wrapper, user_data);
968 * g_output_stream_flush_finish:
969 * @stream: a #GOutputStream.
970 * @result: a GAsyncResult.
971 * @error: a #GError location to store the error occuring, or %NULL to
974 * Finishes flushing an output stream.
976 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
979 g_output_stream_flush_finish (GOutputStream *stream,
980 GAsyncResult *result,
983 GSimpleAsyncResult *simple;
984 GOutputStreamClass *klass;
986 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
987 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
989 if (G_IS_SIMPLE_ASYNC_RESULT (result))
991 simple = G_SIMPLE_ASYNC_RESULT (result);
992 if (g_simple_async_result_propagate_error (simple, error))
995 /* Special case default implementation */
996 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
1000 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
1001 return klass->flush_finish (stream, result, error);
1006 * g_output_stream_close_async:
1007 * @stream: A #GOutputStream.
1008 * @io_priority: the io priority of the request.
1009 * @callback: callback to call when the request is satisfied
1010 * @user_data: the data to pass to callback function
1011 * @cancellable: optional cancellable object
1013 * Requests an asynchronous close of the stream, releasing resources
1014 * related to it. When the operation is finished @callback will be
1015 * called. You can then call g_output_stream_close_finish() to get
1016 * the result of the operation.
1018 * For behaviour details see g_output_stream_close().
1020 * The asyncronous methods have a default fallback that uses threads
1021 * to implement asynchronicity, so they are optional for inheriting
1022 * classes. However, if you override one you must override all.
1025 g_output_stream_close_async (GOutputStream *stream,
1027 GCancellable *cancellable,
1028 GAsyncReadyCallback callback,
1031 GOutputStreamClass *class;
1032 GSimpleAsyncResult *simple;
1033 GError *error = NULL;
1034 CloseUserData *data;
1036 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1038 if (stream->priv->closed)
1040 simple = g_simple_async_result_new (G_OBJECT (stream),
1043 g_output_stream_close_async);
1044 g_simple_async_result_complete_in_idle (simple);
1045 g_object_unref (simple);
1049 if (!g_output_stream_set_pending (stream, &error))
1051 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
1055 g_error_free (error);
1059 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1060 stream->priv->closing = TRUE;
1061 stream->priv->outstanding_callback = callback;
1062 g_object_ref (stream);
1064 data = g_slice_new0 (CloseUserData);
1066 if (cancellable != NULL)
1067 data->cancellable = g_object_ref (cancellable);
1069 data->io_priority = io_priority;
1070 data->user_data = user_data;
1072 /* Call close_async directly if there is no need to flush, or if the flush
1073 can be done sync (in the output stream async close thread) */
1074 if (class->flush_async == NULL ||
1075 (class->flush_async == g_output_stream_real_flush_async &&
1076 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1078 class->close_async (stream, io_priority, cancellable,
1079 async_ready_close_callback_wrapper, data);
1083 /* First do an async flush, then do the async close in the callback
1084 wrapper (see async_ready_close_flushed_callback_wrapper) */
1085 class->flush_async (stream, io_priority, cancellable,
1086 async_ready_close_flushed_callback_wrapper, data);
1091 * g_output_stream_close_finish:
1092 * @stream: a #GOutputStream.
1093 * @result: a #GAsyncResult.
1094 * @error: a #GError location to store the error occuring, or %NULL to
1097 * Closes an output stream.
1099 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1102 g_output_stream_close_finish (GOutputStream *stream,
1103 GAsyncResult *result,
1106 GSimpleAsyncResult *simple;
1107 GOutputStreamClass *class;
1109 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1110 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1112 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1114 simple = G_SIMPLE_ASYNC_RESULT (result);
1115 if (g_simple_async_result_propagate_error (simple, error))
1118 /* Special case already closed */
1119 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1123 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1124 return class->close_finish (stream, result, error);
1128 * g_output_stream_is_closed:
1129 * @stream: a #GOutputStream.
1131 * Checks if an output stream has already been closed.
1133 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1136 g_output_stream_is_closed (GOutputStream *stream)
1138 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1140 return stream->priv->closed;
1144 * g_output_stream_is_closing:
1145 * @stream: a #GOutputStream.
1147 * Checks if an output stream is being closed. This can be
1148 * used inside e.g. a flush implementation to see if the
1149 * flush (or other i/o operation) is called from within
1150 * the closing operation.
1152 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1157 g_output_stream_is_closing (GOutputStream *stream)
1159 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1161 return stream->priv->closing;
1165 * g_output_stream_has_pending:
1166 * @stream: a #GOutputStream.
1168 * Checks if an ouput stream has pending actions.
1170 * Returns: %TRUE if @stream has pending actions.
1173 g_output_stream_has_pending (GOutputStream *stream)
1175 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1177 return stream->priv->pending;
1181 * g_output_stream_set_pending:
1182 * @stream: a #GOutputStream.
1183 * @error: a #GError location to store the error occuring, or %NULL to
1186 * Sets @stream to have actions pending. If the pending flag is
1187 * already set or @stream is closed, it will return %FALSE and set
1190 * Return value: %TRUE if pending was previously unset and is now set.
1193 g_output_stream_set_pending (GOutputStream *stream,
1196 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1198 if (stream->priv->closed)
1200 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1201 _("Stream is already closed"));
1205 if (stream->priv->pending)
1207 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1208 /* Translators: This is an error you get if there is
1209 * already an operation running against this stream when
1210 * you try to start one */
1211 _("Stream has outstanding operation"));
1215 stream->priv->pending = TRUE;
1220 * g_output_stream_clear_pending:
1221 * @stream: output stream
1223 * Clears the pending flag on @stream.
1226 g_output_stream_clear_pending (GOutputStream *stream)
1228 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1230 stream->priv->pending = FALSE;
1234 /********************************************
1235 * Default implementation of async ops *
1236 ********************************************/
1240 gsize count_requested;
1241 gssize count_written;
1245 write_async_thread (GSimpleAsyncResult *res,
1247 GCancellable *cancellable)
1250 GOutputStreamClass *class;
1251 GError *error = NULL;
1253 class = G_OUTPUT_STREAM_GET_CLASS (object);
1254 op = g_simple_async_result_get_op_res_gpointer (res);
1255 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1256 cancellable, &error);
1257 if (op->count_written == -1)
1258 g_simple_async_result_take_error (res, error);
1262 g_output_stream_real_write_async (GOutputStream *stream,
1266 GCancellable *cancellable,
1267 GAsyncReadyCallback callback,
1270 GSimpleAsyncResult *res;
1273 op = g_new0 (WriteData, 1);
1274 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1275 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1276 op->buffer = buffer;
1277 op->count_requested = count;
1279 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1280 g_object_unref (res);
1284 g_output_stream_real_write_finish (GOutputStream *stream,
1285 GAsyncResult *result,
1288 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1291 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1292 op = g_simple_async_result_get_op_res_gpointer (simple);
1293 return op->count_written;
1297 GInputStream *source;
1298 GOutputStreamSpliceFlags flags;
1299 gssize bytes_copied;
1303 splice_async_thread (GSimpleAsyncResult *result,
1305 GCancellable *cancellable)
1308 GOutputStreamClass *class;
1309 GError *error = NULL;
1310 GOutputStream *stream;
1312 stream = G_OUTPUT_STREAM (object);
1313 class = G_OUTPUT_STREAM_GET_CLASS (object);
1314 op = g_simple_async_result_get_op_res_gpointer (result);
1316 op->bytes_copied = class->splice (stream,
1321 if (op->bytes_copied == -1)
1322 g_simple_async_result_take_error (result, error);
1326 g_output_stream_real_splice_async (GOutputStream *stream,
1327 GInputStream *source,
1328 GOutputStreamSpliceFlags flags,
1330 GCancellable *cancellable,
1331 GAsyncReadyCallback callback,
1334 GSimpleAsyncResult *res;
1337 op = g_new0 (SpliceData, 1);
1338 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1339 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1341 op->source = source;
1343 /* TODO: In the case where both source and destintion have
1344 non-threadbased async calls we can use a true async copy here */
1346 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1347 g_object_unref (res);
1351 g_output_stream_real_splice_finish (GOutputStream *stream,
1352 GAsyncResult *result,
1355 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1358 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1359 op = g_simple_async_result_get_op_res_gpointer (simple);
1360 return op->bytes_copied;
1365 flush_async_thread (GSimpleAsyncResult *res,
1367 GCancellable *cancellable)
1369 GOutputStreamClass *class;
1371 GError *error = NULL;
1373 class = G_OUTPUT_STREAM_GET_CLASS (object);
1376 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1379 g_simple_async_result_take_error (res, error);
1383 g_output_stream_real_flush_async (GOutputStream *stream,
1385 GCancellable *cancellable,
1386 GAsyncReadyCallback callback,
1389 GSimpleAsyncResult *res;
1391 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1393 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1394 g_object_unref (res);
1398 g_output_stream_real_flush_finish (GOutputStream *stream,
1399 GAsyncResult *result,
1406 close_async_thread (GSimpleAsyncResult *res,
1408 GCancellable *cancellable)
1410 GOutputStreamClass *class;
1411 GError *error = NULL;
1412 gboolean result = TRUE;
1414 class = G_OUTPUT_STREAM_GET_CLASS (object);
1416 /* Do a flush here if there is a flush function, and we did not have to do
1417 an async flush before (see g_output_stream_close_async) */
1418 if (class->flush != NULL &&
1419 (class->flush_async == NULL ||
1420 class->flush_async == g_output_stream_real_flush_async))
1422 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1425 /* Auto handling of cancelation disabled, and ignore
1426 cancellation, since we want to close things anyway, although
1427 possibly in a quick-n-dirty way. At least we never want to leak
1430 if (class->close_fn)
1432 /* Make sure to close, even if the flush failed (see sync close) */
1434 class->close_fn (G_OUTPUT_STREAM (object), cancellable, NULL);
1436 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1439 g_simple_async_result_take_error (res, error);
1444 g_output_stream_real_close_async (GOutputStream *stream,
1446 GCancellable *cancellable,
1447 GAsyncReadyCallback callback,
1450 GSimpleAsyncResult *res;
1452 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1454 g_simple_async_result_set_handle_cancellation (res, FALSE);
1456 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1457 g_object_unref (res);
1461 g_output_stream_real_close_finish (GOutputStream *stream,
1462 GAsyncResult *result,
1465 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1466 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);