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 "gsimpleasyncresult.h"
31 * SECTION:goutputstream
32 * @short_description: base class for implementing streaming output
38 G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
40 struct _GOutputStreamPrivate {
44 GAsyncReadyCallback outstanding_callback;
47 static gssize g_output_stream_real_splice (GOutputStream *stream,
49 GOutputStreamSpliceFlags flags,
50 GCancellable *cancellable,
52 static void g_output_stream_real_write_async (GOutputStream *stream,
56 GCancellable *cancellable,
57 GAsyncReadyCallback callback,
59 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
62 static void g_output_stream_real_splice_async (GOutputStream *stream,
64 GOutputStreamSpliceFlags flags,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
72 static void g_output_stream_real_flush_async (GOutputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
80 static void g_output_stream_real_close_async (GOutputStream *stream,
82 GCancellable *cancellable,
83 GAsyncReadyCallback callback,
85 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
90 g_output_stream_finalize (GObject *object)
92 GOutputStream *stream;
94 stream = G_OUTPUT_STREAM (object);
96 if (G_OBJECT_CLASS (g_output_stream_parent_class)->finalize)
97 (*G_OBJECT_CLASS (g_output_stream_parent_class)->finalize) (object);
101 g_output_stream_dispose (GObject *object)
103 GOutputStream *stream;
105 stream = G_OUTPUT_STREAM (object);
107 if (!stream->priv->closed)
108 g_output_stream_close (stream, NULL, NULL);
110 if (G_OBJECT_CLASS (g_output_stream_parent_class)->dispose)
111 (*G_OBJECT_CLASS (g_output_stream_parent_class)->dispose) (object);
115 g_output_stream_class_init (GOutputStreamClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
121 gobject_class->finalize = g_output_stream_finalize;
122 gobject_class->dispose = g_output_stream_dispose;
124 klass->splice = g_output_stream_real_splice;
126 klass->write_async = g_output_stream_real_write_async;
127 klass->write_finish = g_output_stream_real_write_finish;
128 klass->splice_async = g_output_stream_real_splice_async;
129 klass->splice_finish = g_output_stream_real_splice_finish;
130 klass->flush_async = g_output_stream_real_flush_async;
131 klass->flush_finish = g_output_stream_real_flush_finish;
132 klass->close_async = g_output_stream_real_close_async;
133 klass->close_finish = g_output_stream_real_close_finish;
137 g_output_stream_init (GOutputStream *stream)
139 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
140 G_TYPE_OUTPUT_STREAM,
141 GOutputStreamPrivate);
145 * g_output_stream_write:
146 * @stream: a #GOutputStream.
147 * @buffer: the buffer containing the data to write.
148 * @count: the number of bytes to write
149 * @cancellable: optional cancellable object
150 * @error: location to store the error occuring, or %NULL to ignore
152 * Tries to write @count bytes from @buffer into the stream. Will block
153 * during the operation.
155 * If count is zero returns zero and does nothing. A value of @count
156 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
158 * On success, the number of bytes written to the stream is returned.
159 * It is not an error if this is not the same as the requested size, as it
160 * can happen e.g. on a partial i/o error, or if the there is not enough
161 * storage in the stream. All writes either block until at least one byte
162 * is written, so zero is never returned (unless @count is zero).
164 * If @cancellable is not NULL, then the operation can be cancelled by
165 * triggering the cancellable object from another thread. If the operation
166 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
167 * operation was partially finished when the operation was cancelled the
168 * partial result will be returned, without an error.
170 * On error -1 is returned and @error is set accordingly.
172 * Return value: Number of bytes written, or -1 on error
175 g_output_stream_write (GOutputStream *stream,
178 GCancellable *cancellable,
181 GOutputStreamClass *class;
184 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
185 g_return_val_if_fail (buffer != NULL, 0);
190 if (((gssize) count) < 0)
192 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
193 _("Too large count value passed to g_output_stream_write"));
197 if (stream->priv->closed)
199 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
200 _("Stream is already closed"));
204 if (stream->priv->pending)
206 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
207 _("Stream has outstanding operation"));
211 class = G_OUTPUT_STREAM_GET_CLASS (stream);
213 if (class->write == NULL)
215 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
216 _("Output stream doesn't implement write"));
221 g_push_current_cancellable (cancellable);
223 stream->priv->pending = TRUE;
224 res = class->write (stream, buffer, count, cancellable, error);
225 stream->priv->pending = FALSE;
228 g_pop_current_cancellable (cancellable);
234 * g_output_stream_write_all:
235 * @stream: a #GOutputStream.
236 * @buffer: the buffer containing the data to write.
237 * @count: the number of bytes to write
238 * @bytes_written: location to store the number of bytes that was
239 * written to the stream
240 * @cancellable: optional #GCancellable object, %NULL to ignore.
241 * @error: location to store the error occuring, or %NULL to ignore
243 * Tries to write @count bytes from @buffer into the stream. Will block
244 * during the operation.
246 * This function is similar to g_output_stream_write(), except it tries to
247 * write as many bytes as requested, only stopping on an error.
249 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
252 * If there is an error during the operation FALSE is returned and @error
253 * is set to indicate the error status, @bytes_written is updated to contain
254 * the number of bytes written into the stream before the error occured.
256 * Return value: %TRUE on success, %FALSE if there was an error
259 g_output_stream_write_all (GOutputStream *stream,
262 gsize *bytes_written,
263 GCancellable *cancellable,
266 gsize _bytes_written;
269 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
270 g_return_val_if_fail (buffer != NULL, FALSE);
273 while (_bytes_written < count)
275 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
280 *bytes_written = _bytes_written;
285 g_warning ("Write returned zero without error");
287 _bytes_written += res;
291 *bytes_written = _bytes_written;
297 * g_output_stream_flush:
298 * @stream: a #GOutputStream.
299 * @cancellable: optional cancellable object
300 * @error: location to store the error occuring, or %NULL to ignore
302 * Flushed any outstanding buffers in the stream. Will block during
303 * the operation. Closing the stream will implicitly cause a flush.
305 * This function is optional for inherited classes.
307 * If @cancellable is not %NULL, then the operation can be cancelled by
308 * triggering the cancellable object from another thread. If the operation
309 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
311 * Return value: %TRUE on success, %FALSE on error
314 g_output_stream_flush (GOutputStream *stream,
315 GCancellable *cancellable,
318 GOutputStreamClass *class;
321 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
323 if (stream->priv->closed)
325 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
326 _("Stream is already closed"));
330 if (stream->priv->pending)
332 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
333 _("Stream has outstanding operation"));
337 class = G_OUTPUT_STREAM_GET_CLASS (stream);
343 g_push_current_cancellable (cancellable);
345 stream->priv->pending = TRUE;
346 res = class->flush (stream, cancellable, error);
347 stream->priv->pending = FALSE;
350 g_pop_current_cancellable (cancellable);
357 * g_output_stream_splice:
358 * @stream: a #GOutputStream.
359 * @source: a #GInputStream.
360 * @flags: a set of #GOutputStreamSpliceFlags.
361 * @cancellable: optional #GCancellable object, %NULL to ignore.
362 * @error: a #GError location to store the error occuring, or %NULL to
365 * Splices an input stream into an output stream.
367 * Returns: a #gssize containig the size of the data spliced.
370 g_output_stream_splice (GOutputStream *stream,
371 GInputStream *source,
372 GOutputStreamSpliceFlags flags,
373 GCancellable *cancellable,
376 GOutputStreamClass *class;
379 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
380 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
382 if (stream->priv->closed)
384 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
385 _("Target stream is already closed"));
389 if (g_input_stream_is_closed (source))
391 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
392 _("Source stream is already closed"));
396 if (stream->priv->pending)
398 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
399 _("Stream has outstanding operation"));
403 class = G_OUTPUT_STREAM_GET_CLASS (stream);
407 g_push_current_cancellable (cancellable);
409 stream->priv->pending = TRUE;
410 res = class->splice (stream, source, flags, cancellable, error);
411 stream->priv->pending = FALSE;
414 g_pop_current_cancellable (cancellable);
420 g_output_stream_real_splice (GOutputStream *stream,
421 GInputStream *source,
422 GOutputStreamSpliceFlags flags,
423 GCancellable *cancellable,
426 gssize n_read, n_written;
428 char buffer[8192], *p;
435 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
448 stream->priv->pending = FALSE;
449 n_written = g_output_stream_write (stream, p, n_read, cancellable, error);
450 stream->priv->pending = TRUE;
459 bytes_copied += n_written;
465 error = NULL; /* Ignore further errors */
467 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_SOURCE)
469 /* Don't care about errors in source here */
470 g_input_stream_close (source, cancellable, NULL);
473 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_TARGET)
475 /* But write errors on close are bad! */
476 stream->priv->pending = FALSE;
477 if (!g_output_stream_close (stream, cancellable, error))
479 stream->priv->pending = TRUE;
490 * g_output_stream_close:
491 * @stream: A #GOutputStream.
492 * @cancellable: optional cancellable object
493 * @error: location to store the error occuring, or %NULL to ignore
495 * Closes the stream, releasing resources related to it.
497 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
498 * Closing a stream multiple times will not return an error.
500 * Closing a stream will automatically flush any outstanding buffers in the
503 * Streams will be automatically closed when the last reference
504 * is dropped, but you might want to call make sure resources
505 * are released as early as possible.
507 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
508 * open after the stream is closed. See the documentation for the individual
509 * stream for details.
511 * On failure the first error that happened will be reported, but the close
512 * operation will finish as much as possible. A stream that failed to
513 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
514 * is important to check and report the error to the user, otherwise
515 * there might be a loss of data as all data might not be written.
517 * If @cancellable is not NULL, then the operation can be cancelled by
518 * triggering the cancellable object from another thread. If the operation
519 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
520 * Cancelling a close will still leave the stream closed, but there some streams
521 * can use a faster close that doesn't block to e.g. check errors. On
522 * cancellation (as with any error) there is no guarantee that all written
523 * data will reach the target.
525 * Return value: %TRUE on success, %FALSE on failure
528 g_output_stream_close (GOutputStream *stream,
529 GCancellable *cancellable,
532 GOutputStreamClass *class;
535 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
537 class = G_OUTPUT_STREAM_GET_CLASS (stream);
539 if (stream->priv->closed)
542 if (stream->priv->pending)
544 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
545 _("Stream has outstanding operation"));
549 res = g_output_stream_flush (stream, cancellable, error);
551 stream->priv->pending = TRUE;
554 g_push_current_cancellable (cancellable);
558 /* flushing caused the error that we want to return,
559 * but we still want to close the underlying stream if possible
562 class->close (stream, cancellable, NULL);
568 res = class->close (stream, cancellable, error);
572 g_pop_current_cancellable (cancellable);
574 stream->priv->closed = TRUE;
575 stream->priv->pending = FALSE;
581 async_ready_callback_wrapper (GObject *source_object,
585 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
587 stream->priv->pending = FALSE;
588 if (stream->priv->outstanding_callback)
589 (*stream->priv->outstanding_callback) (source_object, res, user_data);
590 g_object_unref (stream);
594 async_ready_close_callback_wrapper (GObject *source_object,
598 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
600 stream->priv->pending = FALSE;
601 stream->priv->closed = TRUE;
602 if (stream->priv->outstanding_callback)
603 (*stream->priv->outstanding_callback) (source_object, res, user_data);
604 g_object_unref (stream);
608 * g_output_stream_write_async:
609 * @stream: A #GOutputStream.
610 * @buffer: the buffer containing the data to write.
611 * @count: the number of bytes to write
612 * @io_priority: the io priority of the request.
613 * @cancellable: optional #GCancellable object, %NULL to ignore.
614 * @callback: callback to call when the request is satisfied
615 * @user_data: the data to pass to callback function
617 * Request an asynchronous write of @count bytes from @buffer into
618 * the stream. When the operation is finished @callback will be called,
619 * giving the results.
621 * During an async request no other sync and async calls are allowed,
622 * and will result in %G_IO_ERROR_PENDING errors.
624 * A value of @count larger than %G_MAXSSIZE will cause a
625 * %G_IO_ERROR_INVALID_ARGUMENT error.
627 * On success, the number of bytes written will be passed to the
628 * @callback. It is not an error if this is not the same as the
629 * requested size, as it can happen e.g. on a partial I/O error,
630 * but generally we try to write as many bytes as requested.
632 * Any outstanding I/O request with higher priority (lower numerical
633 * value) will be executed before an outstanding request with lower
634 * priority. Default priority is %G_PRIORITY_DEFAULT.
636 * The asyncronous methods have a default fallback that uses threads
637 * to implement asynchronicity, so they are optional for inheriting
638 * classes. However, if you override one you must override all.
640 * For the synchronous, blocking version of this function, see
641 * g_output_stream_write().
644 g_output_stream_write_async (GOutputStream *stream,
648 GCancellable *cancellable,
649 GAsyncReadyCallback callback,
652 GOutputStreamClass *class;
653 GSimpleAsyncResult *simple;
655 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
656 g_return_if_fail (buffer != NULL);
660 simple = g_simple_async_result_new (G_OBJECT (stream),
663 g_output_stream_write_async);
664 g_simple_async_result_complete_in_idle (simple);
665 g_object_unref (simple);
669 if (((gssize) count) < 0)
671 g_simple_async_report_error_in_idle (G_OBJECT (stream),
674 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
675 _("Too large count value passed to g_output_stream_write_async"));
679 if (stream->priv->closed)
681 g_simple_async_report_error_in_idle (G_OBJECT (stream),
684 G_IO_ERROR, G_IO_ERROR_CLOSED,
685 _("Stream is already closed"));
689 if (stream->priv->pending)
691 g_simple_async_report_error_in_idle (G_OBJECT (stream),
694 G_IO_ERROR, G_IO_ERROR_PENDING,
695 _("Stream has outstanding operation"));
699 class = G_OUTPUT_STREAM_GET_CLASS (stream);
701 stream->priv->pending = TRUE;
702 stream->priv->outstanding_callback = callback;
703 g_object_ref (stream);
704 class->write_async (stream, buffer, count, io_priority, cancellable,
705 async_ready_callback_wrapper, user_data);
709 * g_output_stream_write_finish:
710 * @stream: a #GOutputStream.
711 * @result: a #GAsyncResult.
712 * @error: a #GError location to store the error occuring, or %NULL to
715 * Finishes a stream write operation.
717 * Returns: a #gssize containing the number of bytes written to the stream.
720 g_output_stream_write_finish (GOutputStream *stream,
721 GAsyncResult *result,
724 GSimpleAsyncResult *simple;
725 GOutputStreamClass *class;
727 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
728 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
730 if (G_IS_SIMPLE_ASYNC_RESULT (result))
732 simple = G_SIMPLE_ASYNC_RESULT (result);
733 if (g_simple_async_result_propagate_error (simple, error))
736 /* Special case writes of 0 bytes */
737 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
741 class = G_OUTPUT_STREAM_GET_CLASS (stream);
742 return class->write_finish (stream, result, error);
746 GInputStream *source;
748 GAsyncReadyCallback callback;
752 async_ready_splice_callback_wrapper (GObject *source_object,
756 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
757 SpliceUserData *data = _data;
759 stream->priv->pending = FALSE;
762 (*data->callback) (source_object, res, data->user_data);
764 g_object_unref (stream);
765 g_object_unref (data->source);
770 * g_output_stream_splice_async:
771 * @stream: a #GOutputStream.
772 * @source: a #GInputStream.
773 * @flags: a set of #GOutputStreamSpliceFlags.
774 * @io_priority: the io priority of the request.
775 * @cancellable: optional #GCancellable object, %NULL to ignore.
776 * @callback: a #GAsyncReadyCallback.
777 * @user_data: user data passed to @callback.
779 * Splices a stream asynchronously.
783 g_output_stream_splice_async (GOutputStream *stream,
784 GInputStream *source,
785 GOutputStreamSpliceFlags flags,
787 GCancellable *cancellable,
788 GAsyncReadyCallback callback,
791 GOutputStreamClass *class;
792 SpliceUserData *data;
794 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
795 g_return_if_fail (G_IS_INPUT_STREAM (source));
797 if (stream->priv->closed)
799 g_simple_async_report_error_in_idle (G_OBJECT (stream),
802 G_IO_ERROR, G_IO_ERROR_CLOSED,
803 _("Target stream is already closed"));
807 if (g_input_stream_is_closed (source))
809 g_simple_async_report_error_in_idle (G_OBJECT (stream),
812 G_IO_ERROR, G_IO_ERROR_CLOSED,
813 _("Source stream is already closed"));
817 if (stream->priv->pending)
819 g_simple_async_report_error_in_idle (G_OBJECT (stream),
822 G_IO_ERROR, G_IO_ERROR_PENDING,
823 _("Stream has outstanding operation"));
827 class = G_OUTPUT_STREAM_GET_CLASS (stream);
829 stream->priv->pending = TRUE;
831 data = g_new0 (SpliceUserData, 1);
832 data->callback = callback;
833 data->user_data = user_data;
834 data->source = g_object_ref (source);
836 g_object_ref (stream);
837 class->splice_async (stream, source, flags, io_priority, cancellable,
838 async_ready_splice_callback_wrapper, data);
842 * g_output_stream_splice_finish:
843 * @stream: a #GOutputStream.
844 * @result: a #GAsyncResult.
845 * @error: a #GError location to store the error occuring, or %NULL to
848 * Finishes an asynchronous stream splice operation.
850 * Returns: a #gssize of the number of bytes spliced.
853 g_output_stream_splice_finish (GOutputStream *stream,
854 GAsyncResult *result,
857 GSimpleAsyncResult *simple;
858 GOutputStreamClass *class;
860 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
861 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
863 if (G_IS_SIMPLE_ASYNC_RESULT (result))
865 simple = G_SIMPLE_ASYNC_RESULT (result);
866 if (g_simple_async_result_propagate_error (simple, error))
870 class = G_OUTPUT_STREAM_GET_CLASS (stream);
871 return class->splice_finish (stream, result, error);
875 * g_output_stream_flush_async:
876 * @stream: a #GOutputStream.
877 * @io_priority: the io priority of the request.
878 * @cancellable: optional #GCancellable object, %NULL to ignore.
879 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
880 * @user_data: the data to pass to callback function
882 * Flushes a stream asynchronously.
886 g_output_stream_flush_async (GOutputStream *stream,
888 GCancellable *cancellable,
889 GAsyncReadyCallback callback,
892 GOutputStreamClass *class;
893 GSimpleAsyncResult *simple;
895 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
897 if (stream->priv->closed)
899 g_simple_async_report_error_in_idle (G_OBJECT (stream),
902 G_IO_ERROR, G_IO_ERROR_CLOSED,
903 _("Stream is already closed"));
907 if (stream->priv->pending)
909 g_simple_async_report_error_in_idle (G_OBJECT (stream),
912 G_IO_ERROR, G_IO_ERROR_PENDING,
913 _("Stream has outstanding operation"));
917 class = G_OUTPUT_STREAM_GET_CLASS (stream);
919 if (class->flush_async == NULL)
921 simple = g_simple_async_result_new (G_OBJECT (stream),
924 g_output_stream_flush_async);
925 g_simple_async_result_complete_in_idle (simple);
926 g_object_unref (simple);
930 stream->priv->pending = TRUE;
931 stream->priv->outstanding_callback = callback;
932 g_object_ref (stream);
933 class->flush_async (stream, io_priority, cancellable,
934 async_ready_callback_wrapper, user_data);
938 * g_output_stream_flush_finish:
939 * @stream: a #GOutputStream.
940 * @result: a GAsyncResult.
941 * @error: a #GError location to store the error occuring, or %NULL to
944 * Finishes flushing an output stream.
946 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
949 g_output_stream_flush_finish (GOutputStream *stream,
950 GAsyncResult *result,
953 GSimpleAsyncResult *simple;
954 GOutputStreamClass *klass;
956 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
957 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
959 if (G_IS_SIMPLE_ASYNC_RESULT (result))
961 simple = G_SIMPLE_ASYNC_RESULT (result);
962 if (g_simple_async_result_propagate_error (simple, error))
965 /* Special case default implementation */
966 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
970 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
971 return klass->flush_finish (stream, result, error);
976 * g_output_stream_close_async:
977 * @stream: A #GOutputStream.
978 * @io_priority: the io priority of the request.
979 * @callback: callback to call when the request is satisfied
980 * @user_data: the data to pass to callback function
981 * @cancellable: optional cancellable object
983 * Requests an asynchronous close of the stream, releasing resources
984 * related to it. When the operation is finished @callback will be
985 * called, giving the results.
987 * For behaviour details see g_output_stream_close().
989 * The asyncronous methods have a default fallback that uses threads
990 * to implement asynchronicity, so they are optional for inheriting
991 * classes. However, if you override one you must override all.
994 g_output_stream_close_async (GOutputStream *stream,
996 GCancellable *cancellable,
997 GAsyncReadyCallback callback,
1000 GOutputStreamClass *class;
1001 GSimpleAsyncResult *simple;
1003 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1005 if (stream->priv->closed)
1007 simple = g_simple_async_result_new (G_OBJECT (stream),
1010 g_output_stream_close_async);
1011 g_simple_async_result_complete_in_idle (simple);
1012 g_object_unref (simple);
1016 if (stream->priv->pending)
1018 g_simple_async_report_error_in_idle (G_OBJECT (stream),
1021 G_IO_ERROR, G_IO_ERROR_PENDING,
1022 _("Stream has outstanding operation"));
1026 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1027 stream->priv->pending = TRUE;
1028 stream->priv->outstanding_callback = callback;
1029 g_object_ref (stream);
1030 class->close_async (stream, io_priority, cancellable,
1031 async_ready_close_callback_wrapper, user_data);
1035 * g_output_stream_close_finish:
1036 * @stream: a #GOutputStream.
1037 * @result: a #GAsyncResult.
1038 * @error: a #GError location to store the error occuring, or %NULL to
1041 * Closes an output stream.
1043 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1046 g_output_stream_close_finish (GOutputStream *stream,
1047 GAsyncResult *result,
1050 GSimpleAsyncResult *simple;
1051 GOutputStreamClass *class;
1053 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1054 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1056 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1058 simple = G_SIMPLE_ASYNC_RESULT (result);
1059 if (g_simple_async_result_propagate_error (simple, error))
1062 /* Special case already closed */
1063 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1067 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1068 return class->close_finish (stream, result, error);
1072 * g_output_stream_is_closed:
1073 * @stream: a #GOutputStream.
1075 * Checks if an output stream has already been closed.
1077 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1080 g_output_stream_is_closed (GOutputStream *stream)
1082 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1084 return stream->priv->closed;
1088 * g_output_stream_has_pending:
1089 * @stream: a #GOutputStream.
1091 * Checks if an ouput stream has pending actions.
1093 * Returns: %TRUE if @stream has pending actions.
1096 g_output_stream_has_pending (GOutputStream *stream)
1098 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1100 return stream->priv->pending;
1104 * g_output_stream_set_pending:
1105 * @stream: a #GOutputStream.
1106 * @pending: a #gboolean.
1108 * Sets the @stream as having pending actions if @pending is %TRUE.
1111 g_output_stream_set_pending (GOutputStream *stream,
1114 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1116 stream->priv->pending = pending;
1120 /********************************************
1121 * Default implementation of async ops *
1122 ********************************************/
1126 gsize count_requested;
1127 gssize count_written;
1131 write_async_thread (GSimpleAsyncResult *res,
1133 GCancellable *cancellable)
1136 GOutputStreamClass *class;
1137 GError *error = NULL;
1139 class = G_OUTPUT_STREAM_GET_CLASS (object);
1140 op = g_simple_async_result_get_op_res_gpointer (res);
1141 op->count_written = class->write (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1142 cancellable, &error);
1143 if (op->count_written == -1)
1145 g_simple_async_result_set_from_error (res, error);
1146 g_error_free (error);
1151 g_output_stream_real_write_async (GOutputStream *stream,
1155 GCancellable *cancellable,
1156 GAsyncReadyCallback callback,
1159 GSimpleAsyncResult *res;
1162 op = g_new0 (WriteData, 1);
1163 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1164 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1165 op->buffer = buffer;
1166 op->count_requested = count;
1168 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1169 g_object_unref (res);
1173 g_output_stream_real_write_finish (GOutputStream *stream,
1174 GAsyncResult *result,
1177 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1180 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1181 op = g_simple_async_result_get_op_res_gpointer (simple);
1182 return op->count_written;
1186 GInputStream *source;
1187 GOutputStreamSpliceFlags flags;
1188 gssize bytes_copied;
1192 splice_async_thread (GSimpleAsyncResult *result,
1194 GCancellable *cancellable)
1197 GOutputStreamClass *class;
1198 GError *error = NULL;
1199 GOutputStream *stream;
1201 stream = G_OUTPUT_STREAM (object);
1202 class = G_OUTPUT_STREAM_GET_CLASS (object);
1203 op = g_simple_async_result_get_op_res_gpointer (result);
1205 stream->priv->pending = FALSE;
1207 g_output_stream_splice (stream,
1212 stream->priv->pending = TRUE;
1214 if (op->bytes_copied == -1)
1216 g_simple_async_result_set_from_error (result, error);
1217 g_error_free (error);
1222 g_output_stream_real_splice_async (GOutputStream *stream,
1223 GInputStream *source,
1224 GOutputStreamSpliceFlags flags,
1226 GCancellable *cancellable,
1227 GAsyncReadyCallback callback,
1230 GSimpleAsyncResult *res;
1233 op = g_new0 (SpliceData, 1);
1234 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1235 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1237 op->source = source;
1239 /* TODO: In the case where both source and destintion have
1240 non-threadbased async calls we can use a true async copy here */
1242 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1243 g_object_unref (res);
1247 g_output_stream_real_splice_finish (GOutputStream *stream,
1248 GAsyncResult *result,
1251 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1254 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1255 op = g_simple_async_result_get_op_res_gpointer (simple);
1256 return op->bytes_copied;
1261 flush_async_thread (GSimpleAsyncResult *res,
1263 GCancellable *cancellable)
1265 GOutputStreamClass *class;
1267 GError *error = NULL;
1269 class = G_OUTPUT_STREAM_GET_CLASS (object);
1272 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1276 g_simple_async_result_set_from_error (res, error);
1277 g_error_free (error);
1282 g_output_stream_real_flush_async (GOutputStream *stream,
1284 GCancellable *cancellable,
1285 GAsyncReadyCallback callback,
1288 GSimpleAsyncResult *res;
1290 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1292 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1293 g_object_unref (res);
1297 g_output_stream_real_flush_finish (GOutputStream *stream,
1298 GAsyncResult *result,
1305 close_async_thread (GSimpleAsyncResult *res,
1307 GCancellable *cancellable)
1309 GOutputStreamClass *class;
1310 GError *error = NULL;
1313 /* Auto handling of cancelation disabled, and ignore
1314 cancellation, since we want to close things anyway, although
1315 possibly in a quick-n-dirty way. At least we never want to leak
1318 class = G_OUTPUT_STREAM_GET_CLASS (object);
1319 result = class->close (G_OUTPUT_STREAM (object), cancellable, &error);
1322 g_simple_async_result_set_from_error (res, error);
1323 g_error_free (error);
1328 g_output_stream_real_close_async (GOutputStream *stream,
1330 GCancellable *cancellable,
1331 GAsyncReadyCallback callback,
1334 GSimpleAsyncResult *res;
1336 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1338 g_simple_async_result_set_handle_cancellation (res, FALSE);
1340 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1341 g_object_unref (res);
1345 g_output_stream_real_close_finish (GOutputStream *stream,
1346 GAsyncResult *result,
1349 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1350 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1354 #define __G_OUTPUT_STREAM_C__
1355 #include "gioaliasdef.c"