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_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 uint8): 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 zero returns zero 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 either block until at least one byte
166 * is written, so zero is never returned (unless @count is zero).
168 * If @cancellable is not NULL, then the operation can be cancelled by
169 * triggering the cancellable object from another thread. If the operation
170 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
171 * operation was partially finished when the operation was cancelled the
172 * partial result will be returned, without an error.
174 * On error -1 is returned and @error is set accordingly.
176 * Return value: Number of bytes written, or -1 on error
179 g_output_stream_write (GOutputStream *stream,
182 GCancellable *cancellable,
185 GOutputStreamClass *class;
188 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
189 g_return_val_if_fail (buffer != NULL, 0);
194 if (((gssize) count) < 0)
196 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
197 _("Too large count value passed to %s"), G_STRFUNC);
201 class = G_OUTPUT_STREAM_GET_CLASS (stream);
203 if (class->write_fn == NULL)
205 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
206 _("Output stream doesn't implement write"));
210 if (!g_output_stream_set_pending (stream, error))
214 g_cancellable_push_current (cancellable);
216 res = class->write_fn (stream, buffer, count, cancellable, error);
219 g_cancellable_pop_current (cancellable);
221 g_output_stream_clear_pending (stream);
227 * g_output_stream_write_all:
228 * @stream: a #GOutputStream.
229 * @buffer: (array length=count) (element-type uint8): the buffer containing the data to write.
230 * @count: the number of bytes to write
231 * @bytes_written: location to store the number of bytes that was
232 * written to the stream
233 * @cancellable: optional #GCancellable object, %NULL to ignore.
234 * @error: location to store the error occuring, or %NULL to ignore
236 * Tries to write @count bytes from @buffer into the stream. Will block
237 * during the operation.
239 * This function is similar to g_output_stream_write(), except it tries to
240 * write as many bytes as requested, only stopping on an error.
242 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
245 * If there is an error during the operation FALSE is returned and @error
246 * is set to indicate the error status, @bytes_written is updated to contain
247 * the number of bytes written into the stream before the error occurred.
249 * Return value: %TRUE on success, %FALSE if there was an error
252 g_output_stream_write_all (GOutputStream *stream,
255 gsize *bytes_written,
256 GCancellable *cancellable,
259 gsize _bytes_written;
262 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
263 g_return_val_if_fail (buffer != NULL, FALSE);
266 while (_bytes_written < count)
268 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
273 *bytes_written = _bytes_written;
278 g_warning ("Write returned zero without error");
280 _bytes_written += res;
284 *bytes_written = _bytes_written;
290 * g_output_stream_flush:
291 * @stream: a #GOutputStream.
292 * @cancellable: optional cancellable object
293 * @error: location to store the error occuring, or %NULL to ignore
295 * Flushed any outstanding buffers in the stream. Will block during
296 * the operation. Closing the stream will implicitly cause a flush.
298 * This function is optional for inherited classes.
300 * If @cancellable is not %NULL, then the operation can be cancelled by
301 * triggering the cancellable object from another thread. If the operation
302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
304 * Return value: %TRUE on success, %FALSE on error
307 g_output_stream_flush (GOutputStream *stream,
308 GCancellable *cancellable,
311 GOutputStreamClass *class;
314 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
316 if (!g_output_stream_set_pending (stream, error))
319 class = G_OUTPUT_STREAM_GET_CLASS (stream);
325 g_cancellable_push_current (cancellable);
327 res = class->flush (stream, cancellable, error);
330 g_cancellable_pop_current (cancellable);
333 g_output_stream_clear_pending (stream);
339 * g_output_stream_splice:
340 * @stream: a #GOutputStream.
341 * @source: a #GInputStream.
342 * @flags: a set of #GOutputStreamSpliceFlags.
343 * @cancellable: optional #GCancellable object, %NULL to ignore.
344 * @error: a #GError location to store the error occuring, or %NULL to
347 * Splices an input stream into an output stream.
349 * Returns: a #gssize containing the size of the data spliced, or
350 * -1 if an error occurred.
353 g_output_stream_splice (GOutputStream *stream,
354 GInputStream *source,
355 GOutputStreamSpliceFlags flags,
356 GCancellable *cancellable,
359 GOutputStreamClass *class;
362 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
363 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
365 if (g_input_stream_is_closed (source))
367 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
368 _("Source stream is already closed"));
372 if (!g_output_stream_set_pending (stream, error))
375 class = G_OUTPUT_STREAM_GET_CLASS (stream);
378 g_cancellable_push_current (cancellable);
380 bytes_copied = class->splice (stream, source, flags, cancellable, error);
383 g_cancellable_pop_current (cancellable);
385 g_output_stream_clear_pending (stream);
391 g_output_stream_real_splice (GOutputStream *stream,
392 GInputStream *source,
393 GOutputStreamSpliceFlags flags,
394 GCancellable *cancellable,
397 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
398 gssize n_read, n_written;
400 char buffer[8192], *p;
404 if (class->write_fn == NULL)
406 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
407 _("Output stream doesn't implement write"));
415 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
428 n_written = class->write_fn (stream, p, n_read, cancellable, error);
437 bytes_copied += n_written;
444 error = NULL; /* Ignore further errors */
446 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
448 /* Don't care about errors in source here */
449 g_input_stream_close (source, cancellable, NULL);
452 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
454 /* But write errors on close are bad! */
455 if (class->close_fn &&
456 !class->close_fn (stream, cancellable, error))
468 * g_output_stream_close:
469 * @stream: A #GOutputStream.
470 * @cancellable: optional cancellable object
471 * @error: location to store the error occuring, or %NULL to ignore
473 * Closes the stream, releasing resources related to it.
475 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
476 * Closing a stream multiple times will not return an error.
478 * Closing a stream will automatically flush any outstanding buffers in the
481 * Streams will be automatically closed when the last reference
482 * is dropped, but you might want to call this function to make sure
483 * resources are released as early as possible.
485 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
486 * open after the stream is closed. See the documentation for the individual
487 * stream for details.
489 * On failure the first error that happened will be reported, but the close
490 * operation will finish as much as possible. A stream that failed to
491 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
492 * is important to check and report the error to the user, otherwise
493 * there might be a loss of data as all data might not be written.
495 * If @cancellable is not NULL, then the operation can be cancelled by
496 * triggering the cancellable object from another thread. If the operation
497 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
498 * Cancelling a close will still leave the stream closed, but there some streams
499 * can use a faster close that doesn't block to e.g. check errors. On
500 * cancellation (as with any error) there is no guarantee that all written
501 * data will reach the target.
503 * Return value: %TRUE on success, %FALSE on failure
506 g_output_stream_close (GOutputStream *stream,
507 GCancellable *cancellable,
510 GOutputStreamClass *class;
513 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
515 class = G_OUTPUT_STREAM_GET_CLASS (stream);
517 if (stream->priv->closed)
520 if (!g_output_stream_set_pending (stream, error))
523 stream->priv->closing = TRUE;
526 g_cancellable_push_current (cancellable);
529 res = class->flush (stream, cancellable, error);
535 /* flushing caused the error that we want to return,
536 * but we still want to close the underlying stream if possible
539 class->close_fn (stream, cancellable, NULL);
545 res = class->close_fn (stream, cancellable, error);
549 g_cancellable_pop_current (cancellable);
551 stream->priv->closing = FALSE;
552 stream->priv->closed = TRUE;
553 g_output_stream_clear_pending (stream);
559 async_ready_callback_wrapper (GObject *source_object,
563 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
565 g_output_stream_clear_pending (stream);
566 if (stream->priv->outstanding_callback)
567 (*stream->priv->outstanding_callback) (source_object, res, user_data);
568 g_object_unref (stream);
573 GCancellable *cancellable;
579 async_ready_close_callback_wrapper (GObject *source_object,
583 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
584 CloseUserData *data = user_data;
586 stream->priv->closing = FALSE;
587 stream->priv->closed = TRUE;
589 g_output_stream_clear_pending (stream);
591 if (stream->priv->outstanding_callback)
593 if (data->flush_error != NULL)
595 GSimpleAsyncResult *err;
597 err = g_simple_async_result_new_from_error (source_object,
598 stream->priv->outstanding_callback,
602 (*stream->priv->outstanding_callback) (source_object,
603 G_ASYNC_RESULT (err),
605 g_object_unref (err);
609 (*stream->priv->outstanding_callback) (source_object,
615 g_object_unref (stream);
617 if (data->cancellable)
618 g_object_unref (data->cancellable);
620 if (data->flush_error)
621 g_error_free (data->flush_error);
623 g_slice_free (CloseUserData, data);
627 async_ready_close_flushed_callback_wrapper (GObject *source_object,
631 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
632 GOutputStreamClass *class;
633 CloseUserData *data = user_data;
634 GSimpleAsyncResult *simple;
636 /* propagate the possible error */
637 if (G_IS_SIMPLE_ASYNC_RESULT (res))
639 simple = G_SIMPLE_ASYNC_RESULT (res);
640 g_simple_async_result_propagate_error (simple, &data->flush_error);
643 class = G_OUTPUT_STREAM_GET_CLASS (stream);
645 /* we still close, even if there was a flush error */
646 class->close_async (stream, data->io_priority, data->cancellable,
647 async_ready_close_callback_wrapper, user_data);
651 * g_output_stream_write_async:
652 * @stream: A #GOutputStream.
653 * @buffer: (array length=count) (element-type uint8): the buffer containing the data to write.
654 * @count: the number of bytes to write
655 * @io_priority: the io priority of the request.
656 * @cancellable: optional #GCancellable object, %NULL to ignore.
657 * @callback: callback to call when the request is satisfied
658 * @user_data: the data to pass to callback function
660 * Request an asynchronous write of @count bytes from @buffer into
661 * the stream. When the operation is finished @callback will be called.
662 * You can then call g_output_stream_write_finish() to get the result of the
665 * During an async request no other sync and async calls are allowed,
666 * and will result in %G_IO_ERROR_PENDING errors.
668 * A value of @count larger than %G_MAXSSIZE will cause a
669 * %G_IO_ERROR_INVALID_ARGUMENT error.
671 * On success, the number of bytes written will be passed to the
672 * @callback. It is not an error if this is not the same as the
673 * requested size, as it can happen e.g. on a partial I/O error,
674 * but generally we try to write as many bytes as requested.
676 * Any outstanding I/O request with higher priority (lower numerical
677 * value) will be executed before an outstanding request with lower
678 * priority. Default priority is %G_PRIORITY_DEFAULT.
680 * The asyncronous methods have a default fallback that uses threads
681 * to implement asynchronicity, so they are optional for inheriting
682 * classes. However, if you override one you must override all.
684 * For the synchronous, blocking version of this function, see
685 * g_output_stream_write().
688 g_output_stream_write_async (GOutputStream *stream,
692 GCancellable *cancellable,
693 GAsyncReadyCallback callback,
696 GOutputStreamClass *class;
697 GSimpleAsyncResult *simple;
698 GError *error = NULL;
700 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
701 g_return_if_fail (buffer != NULL);
705 simple = g_simple_async_result_new (G_OBJECT (stream),
708 g_output_stream_write_async);
709 g_simple_async_result_complete_in_idle (simple);
710 g_object_unref (simple);
714 if (((gssize) count) < 0)
716 g_simple_async_report_error_in_idle (G_OBJECT (stream),
719 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
720 _("Too large count value passed to %s"),
725 if (!g_output_stream_set_pending (stream, &error))
727 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
731 g_error_free (error);
735 class = G_OUTPUT_STREAM_GET_CLASS (stream);
737 stream->priv->outstanding_callback = callback;
738 g_object_ref (stream);
739 class->write_async (stream, buffer, count, io_priority, cancellable,
740 async_ready_callback_wrapper, user_data);
744 * g_output_stream_write_finish:
745 * @stream: a #GOutputStream.
746 * @result: a #GAsyncResult.
747 * @error: a #GError location to store the error occuring, or %NULL to
750 * Finishes a stream write operation.
752 * Returns: a #gssize containing the number of bytes written to the stream.
755 g_output_stream_write_finish (GOutputStream *stream,
756 GAsyncResult *result,
759 GSimpleAsyncResult *simple;
760 GOutputStreamClass *class;
762 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
763 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
765 if (G_IS_SIMPLE_ASYNC_RESULT (result))
767 simple = G_SIMPLE_ASYNC_RESULT (result);
768 if (g_simple_async_result_propagate_error (simple, error))
771 /* Special case writes of 0 bytes */
772 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
776 class = G_OUTPUT_STREAM_GET_CLASS (stream);
777 return class->write_finish (stream, result, error);
781 GInputStream *source;
783 GAsyncReadyCallback callback;
787 async_ready_splice_callback_wrapper (GObject *source_object,
791 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
792 SpliceUserData *data = _data;
794 g_output_stream_clear_pending (stream);
797 (*data->callback) (source_object, res, data->user_data);
799 g_object_unref (stream);
800 g_object_unref (data->source);
805 * g_output_stream_splice_async:
806 * @stream: a #GOutputStream.
807 * @source: a #GInputStream.
808 * @flags: a set of #GOutputStreamSpliceFlags.
809 * @io_priority: the io priority of the request.
810 * @cancellable: optional #GCancellable object, %NULL to ignore.
811 * @callback: a #GAsyncReadyCallback.
812 * @user_data: user data passed to @callback.
814 * Splices a stream asynchronously.
815 * When the operation is finished @callback will be called.
816 * You can then call g_output_stream_splice_finish() to get the
817 * result of the operation.
819 * For the synchronous, blocking version of this function, see
820 * g_output_stream_splice().
823 g_output_stream_splice_async (GOutputStream *stream,
824 GInputStream *source,
825 GOutputStreamSpliceFlags flags,
827 GCancellable *cancellable,
828 GAsyncReadyCallback callback,
831 GOutputStreamClass *class;
832 SpliceUserData *data;
833 GError *error = NULL;
835 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
836 g_return_if_fail (G_IS_INPUT_STREAM (source));
838 if (g_input_stream_is_closed (source))
840 g_simple_async_report_error_in_idle (G_OBJECT (stream),
843 G_IO_ERROR, G_IO_ERROR_CLOSED,
844 _("Source stream is already closed"));
848 if (!g_output_stream_set_pending (stream, &error))
850 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
854 g_error_free (error);
858 class = G_OUTPUT_STREAM_GET_CLASS (stream);
860 data = g_new0 (SpliceUserData, 1);
861 data->callback = callback;
862 data->user_data = user_data;
863 data->source = g_object_ref (source);
865 g_object_ref (stream);
866 class->splice_async (stream, source, flags, io_priority, cancellable,
867 async_ready_splice_callback_wrapper, data);
871 * g_output_stream_splice_finish:
872 * @stream: a #GOutputStream.
873 * @result: a #GAsyncResult.
874 * @error: a #GError location to store the error occuring, or %NULL to
877 * Finishes an asynchronous stream splice operation.
879 * Returns: a #gssize of the number of bytes spliced.
882 g_output_stream_splice_finish (GOutputStream *stream,
883 GAsyncResult *result,
886 GSimpleAsyncResult *simple;
887 GOutputStreamClass *class;
889 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
890 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
892 if (G_IS_SIMPLE_ASYNC_RESULT (result))
894 simple = G_SIMPLE_ASYNC_RESULT (result);
895 if (g_simple_async_result_propagate_error (simple, error))
899 class = G_OUTPUT_STREAM_GET_CLASS (stream);
900 return class->splice_finish (stream, result, error);
904 * g_output_stream_flush_async:
905 * @stream: a #GOutputStream.
906 * @io_priority: the io priority of the request.
907 * @cancellable: optional #GCancellable object, %NULL to ignore.
908 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
909 * @user_data: the data to pass to callback function
911 * Flushes a stream asynchronously.
912 * For behaviour details see g_output_stream_flush().
914 * When the operation is finished @callback will be
915 * called. You can then call g_output_stream_flush_finish() to get the
916 * result of the operation.
919 g_output_stream_flush_async (GOutputStream *stream,
921 GCancellable *cancellable,
922 GAsyncReadyCallback callback,
925 GOutputStreamClass *class;
926 GSimpleAsyncResult *simple;
927 GError *error = NULL;
929 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
931 if (!g_output_stream_set_pending (stream, &error))
933 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
937 g_error_free (error);
941 stream->priv->outstanding_callback = callback;
942 g_object_ref (stream);
944 class = G_OUTPUT_STREAM_GET_CLASS (stream);
946 if (class->flush_async == NULL)
948 simple = g_simple_async_result_new (G_OBJECT (stream),
949 async_ready_callback_wrapper,
951 g_output_stream_flush_async);
952 g_simple_async_result_complete_in_idle (simple);
953 g_object_unref (simple);
957 class->flush_async (stream, io_priority, cancellable,
958 async_ready_callback_wrapper, user_data);
962 * g_output_stream_flush_finish:
963 * @stream: a #GOutputStream.
964 * @result: a GAsyncResult.
965 * @error: a #GError location to store the error occuring, or %NULL to
968 * Finishes flushing an output stream.
970 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
973 g_output_stream_flush_finish (GOutputStream *stream,
974 GAsyncResult *result,
977 GSimpleAsyncResult *simple;
978 GOutputStreamClass *klass;
980 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
981 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
983 if (G_IS_SIMPLE_ASYNC_RESULT (result))
985 simple = G_SIMPLE_ASYNC_RESULT (result);
986 if (g_simple_async_result_propagate_error (simple, error))
989 /* Special case default implementation */
990 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
994 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
995 return klass->flush_finish (stream, result, error);
1000 * g_output_stream_close_async:
1001 * @stream: A #GOutputStream.
1002 * @io_priority: the io priority of the request.
1003 * @callback: callback to call when the request is satisfied
1004 * @user_data: the data to pass to callback function
1005 * @cancellable: optional cancellable object
1007 * Requests an asynchronous close of the stream, releasing resources
1008 * related to it. When the operation is finished @callback will be
1009 * called. You can then call g_output_stream_close_finish() to get
1010 * the result of the operation.
1012 * For behaviour details see g_output_stream_close().
1014 * The asyncronous methods have a default fallback that uses threads
1015 * to implement asynchronicity, so they are optional for inheriting
1016 * classes. However, if you override one you must override all.
1019 g_output_stream_close_async (GOutputStream *stream,
1021 GCancellable *cancellable,
1022 GAsyncReadyCallback callback,
1025 GOutputStreamClass *class;
1026 GSimpleAsyncResult *simple;
1027 GError *error = NULL;
1028 CloseUserData *data;
1030 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1032 if (stream->priv->closed)
1034 simple = g_simple_async_result_new (G_OBJECT (stream),
1037 g_output_stream_close_async);
1038 g_simple_async_result_complete_in_idle (simple);
1039 g_object_unref (simple);
1043 if (!g_output_stream_set_pending (stream, &error))
1045 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
1049 g_error_free (error);
1053 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1054 stream->priv->closing = TRUE;
1055 stream->priv->outstanding_callback = callback;
1056 g_object_ref (stream);
1058 data = g_slice_new0 (CloseUserData);
1060 if (cancellable != NULL)
1061 data->cancellable = g_object_ref (cancellable);
1063 data->io_priority = io_priority;
1064 data->user_data = user_data;
1066 /* Call close_async directly if there is no need to flush, or if the flush
1067 can be done sync (in the output stream async close thread) */
1068 if (class->flush_async == NULL ||
1069 (class->flush_async == g_output_stream_real_flush_async &&
1070 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1072 class->close_async (stream, io_priority, cancellable,
1073 async_ready_close_callback_wrapper, data);
1077 /* First do an async flush, then do the async close in the callback
1078 wrapper (see async_ready_close_flushed_callback_wrapper) */
1079 class->flush_async (stream, io_priority, cancellable,
1080 async_ready_close_flushed_callback_wrapper, data);
1085 * g_output_stream_close_finish:
1086 * @stream: a #GOutputStream.
1087 * @result: a #GAsyncResult.
1088 * @error: a #GError location to store the error occuring, or %NULL to
1091 * Closes an output stream.
1093 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1096 g_output_stream_close_finish (GOutputStream *stream,
1097 GAsyncResult *result,
1100 GSimpleAsyncResult *simple;
1101 GOutputStreamClass *class;
1103 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1104 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1106 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1108 simple = G_SIMPLE_ASYNC_RESULT (result);
1109 if (g_simple_async_result_propagate_error (simple, error))
1112 /* Special case already closed */
1113 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1117 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1118 return class->close_finish (stream, result, error);
1122 * g_output_stream_is_closed:
1123 * @stream: a #GOutputStream.
1125 * Checks if an output stream has already been closed.
1127 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1130 g_output_stream_is_closed (GOutputStream *stream)
1132 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1134 return stream->priv->closed;
1138 * g_output_stream_is_closing:
1139 * @stream: a #GOutputStream.
1141 * Checks if an output stream is being closed. This can be
1142 * used inside e.g. a flush implementation to see if the
1143 * flush (or other i/o operation) is called from within
1144 * the closing operation.
1146 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1151 g_output_stream_is_closing (GOutputStream *stream)
1153 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1155 return stream->priv->closing;
1159 * g_output_stream_has_pending:
1160 * @stream: a #GOutputStream.
1162 * Checks if an ouput stream has pending actions.
1164 * Returns: %TRUE if @stream has pending actions.
1167 g_output_stream_has_pending (GOutputStream *stream)
1169 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1171 return stream->priv->pending;
1175 * g_output_stream_set_pending:
1176 * @stream: a #GOutputStream.
1177 * @error: a #GError location to store the error occuring, or %NULL to
1180 * Sets @stream to have actions pending. If the pending flag is
1181 * already set or @stream is closed, it will return %FALSE and set
1184 * Return value: %TRUE if pending was previously unset and is now set.
1187 g_output_stream_set_pending (GOutputStream *stream,
1190 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1192 if (stream->priv->closed)
1194 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1195 _("Stream is already closed"));
1199 if (stream->priv->pending)
1201 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1202 /* Translators: This is an error you get if there is
1203 * already an operation running against this stream when
1204 * you try to start one */
1205 _("Stream has outstanding operation"));
1209 stream->priv->pending = TRUE;
1214 * g_output_stream_clear_pending:
1215 * @stream: output stream
1217 * Clears the pending flag on @stream.
1220 g_output_stream_clear_pending (GOutputStream *stream)
1222 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1224 stream->priv->pending = FALSE;
1228 /********************************************
1229 * Default implementation of async ops *
1230 ********************************************/
1234 gsize count_requested;
1235 gssize count_written;
1239 write_async_thread (GSimpleAsyncResult *res,
1241 GCancellable *cancellable)
1244 GOutputStreamClass *class;
1245 GError *error = NULL;
1247 class = G_OUTPUT_STREAM_GET_CLASS (object);
1248 op = g_simple_async_result_get_op_res_gpointer (res);
1249 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1250 cancellable, &error);
1251 if (op->count_written == -1)
1253 g_simple_async_result_set_from_error (res, error);
1254 g_error_free (error);
1259 g_output_stream_real_write_async (GOutputStream *stream,
1263 GCancellable *cancellable,
1264 GAsyncReadyCallback callback,
1267 GSimpleAsyncResult *res;
1270 op = g_new0 (WriteData, 1);
1271 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1272 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1273 op->buffer = buffer;
1274 op->count_requested = count;
1276 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1277 g_object_unref (res);
1281 g_output_stream_real_write_finish (GOutputStream *stream,
1282 GAsyncResult *result,
1285 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1288 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1289 op = g_simple_async_result_get_op_res_gpointer (simple);
1290 return op->count_written;
1294 GInputStream *source;
1295 GOutputStreamSpliceFlags flags;
1296 gssize bytes_copied;
1300 splice_async_thread (GSimpleAsyncResult *result,
1302 GCancellable *cancellable)
1305 GOutputStreamClass *class;
1306 GError *error = NULL;
1307 GOutputStream *stream;
1309 stream = G_OUTPUT_STREAM (object);
1310 class = G_OUTPUT_STREAM_GET_CLASS (object);
1311 op = g_simple_async_result_get_op_res_gpointer (result);
1313 op->bytes_copied = class->splice (stream,
1318 if (op->bytes_copied == -1)
1320 g_simple_async_result_set_from_error (result, error);
1321 g_error_free (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);
1380 g_simple_async_result_set_from_error (res, error);
1381 g_error_free (error);
1386 g_output_stream_real_flush_async (GOutputStream *stream,
1388 GCancellable *cancellable,
1389 GAsyncReadyCallback callback,
1392 GSimpleAsyncResult *res;
1394 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1396 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1397 g_object_unref (res);
1401 g_output_stream_real_flush_finish (GOutputStream *stream,
1402 GAsyncResult *result,
1409 close_async_thread (GSimpleAsyncResult *res,
1411 GCancellable *cancellable)
1413 GOutputStreamClass *class;
1414 GError *error = NULL;
1415 gboolean result = TRUE;
1417 class = G_OUTPUT_STREAM_GET_CLASS (object);
1419 /* Do a flush here if there is a flush function, and we did not have to do
1420 an async flush before (see g_output_stream_close_async) */
1421 if (class->flush != NULL &&
1422 (class->flush_async == NULL ||
1423 class->flush_async == g_output_stream_real_flush_async))
1425 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1428 /* Auto handling of cancelation disabled, and ignore
1429 cancellation, since we want to close things anyway, although
1430 possibly in a quick-n-dirty way. At least we never want to leak
1433 if (class->close_fn)
1435 /* Make sure to close, even if the flush failed (see sync close) */
1437 class->close_fn (G_OUTPUT_STREAM (object), cancellable, NULL);
1439 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1443 g_simple_async_result_set_from_error (res, error);
1444 g_error_free (error);
1450 g_output_stream_real_close_async (GOutputStream *stream,
1452 GCancellable *cancellable,
1453 GAsyncReadyCallback callback,
1456 GSimpleAsyncResult *res;
1458 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1460 g_simple_async_result_set_handle_cancellation (res, FALSE);
1462 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1463 g_object_unref (res);
1467 g_output_stream_real_close_finish (GOutputStream *stream,
1468 GAsyncResult *result,
1471 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1472 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);