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 written to the stream
239 * @cancellable: optional #GCancellable object, %NULL to ignore.
240 * @error: location to store the error occuring, or %NULL to ignore
242 * Tries to write @count bytes from @buffer into the stream. Will block
243 * during the operation.
245 * This function is similar to g_output_stream_write(), except it tries to
246 * write as many bytes as requested, only stopping on an error.
248 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
251 * If there is an error during the operation FALSE is returned and @error
252 * is set to indicate the error status, @bytes_written is updated to contain
253 * the number of bytes written into the stream before the error occured.
255 * Return value: %TRUE on success, %FALSE if there was an error
258 g_output_stream_write_all (GOutputStream *stream,
261 gsize *bytes_written,
262 GCancellable *cancellable,
265 gsize _bytes_written;
268 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
269 g_return_val_if_fail (buffer != NULL, FALSE);
272 while (_bytes_written < count)
274 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
279 *bytes_written = _bytes_written;
284 g_warning ("Write returned zero without error");
286 _bytes_written += res;
290 *bytes_written = _bytes_written;
295 * g_output_stream_flush:
296 * @stream: a #GOutputStream.
297 * @cancellable: optional cancellable object
298 * @error: location to store the error occuring, or %NULL to ignore
300 * Flushed any outstanding buffers in the stream. Will block during the operation.
301 * Closing the stream will implicitly cause a flush.
303 * This function is optional for inherited classes.
305 * If @cancellable is not %NULL, then the operation can be cancelled by
306 * triggering the cancellable object from another thread. If the operation
307 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
309 * Return value: %TRUE on success, %FALSE on error
312 g_output_stream_flush (GOutputStream *stream,
313 GCancellable *cancellable,
316 GOutputStreamClass *class;
319 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
321 if (stream->priv->closed)
323 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
324 _("Stream is already closed"));
328 if (stream->priv->pending)
330 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
331 _("Stream has outstanding operation"));
335 class = G_OUTPUT_STREAM_GET_CLASS (stream);
341 g_push_current_cancellable (cancellable);
343 stream->priv->pending = TRUE;
344 res = class->flush (stream, cancellable, error);
345 stream->priv->pending = FALSE;
348 g_pop_current_cancellable (cancellable);
355 * g_output_stream_splice:
356 * @stream: a #GOutputStream.
357 * @source: a #GInputStream.
358 * @flags: a set of #GOutputStreamSpliceFlags.
359 * @cancellable: optional #GCancellable object, %NULL to ignore.
360 * @error: a #GError location to store the error occuring, or %NULL to
363 * Splices an input stream into an output stream.
365 * Returns: a #gssize containig the size of the data spliced.
368 g_output_stream_splice (GOutputStream *stream,
369 GInputStream *source,
370 GOutputStreamSpliceFlags flags,
371 GCancellable *cancellable,
374 GOutputStreamClass *class;
377 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
378 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
380 if (stream->priv->closed)
382 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
383 _("Target stream is already closed"));
387 if (g_input_stream_is_closed (source))
389 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
390 _("Source stream is already closed"));
394 if (stream->priv->pending)
396 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
397 _("Stream has outstanding operation"));
401 class = G_OUTPUT_STREAM_GET_CLASS (stream);
405 g_push_current_cancellable (cancellable);
407 stream->priv->pending = TRUE;
408 res = class->splice (stream, source, flags, cancellable, error);
409 stream->priv->pending = FALSE;
412 g_pop_current_cancellable (cancellable);
418 g_output_stream_real_splice (GOutputStream *stream,
419 GInputStream *source,
420 GOutputStreamSpliceFlags flags,
421 GCancellable *cancellable,
424 gssize n_read, n_written;
426 char buffer[8192], *p;
433 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
446 stream->priv->pending = FALSE;
447 n_written = g_output_stream_write (stream, p, n_read, cancellable, error);
448 stream->priv->pending = TRUE;
457 bytes_copied += n_written;
463 error = NULL; /* Ignore further errors */
465 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_SOURCE)
467 /* Don't care about errors in source here */
468 g_input_stream_close (source, cancellable, NULL);
471 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_TARGET)
473 /* But write errors on close are bad! */
474 stream->priv->pending = FALSE;
475 if (!g_output_stream_close (stream, cancellable, error))
477 stream->priv->pending = TRUE;
488 * g_output_stream_close:
489 * @stream: A #GOutputStream.
490 * @cancellable: optional cancellable object
491 * @error: location to store the error occuring, or %NULL to ignore
493 * Closes the stream, releasing resources related to it.
495 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
496 * Closing a stream multiple times will not return an error.
498 * Closing a stream will automatically flush any outstanding buffers in the
501 * Streams will be automatically closed when the last reference
502 * is dropped, but you might want to call make sure resources
503 * are released as early as possible.
505 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
506 * open after the stream is closed. See the documentation for the individual
507 * stream for details.
509 * On failure the first error that happened will be reported, but the close
510 * operation will finish as much as possible. A stream that failed to
511 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
512 * is important to check and report the error to the user, otherwise
513 * there might be a loss of data as all data might not be written.
515 * If @cancellable is not NULL, then the operation can be cancelled by
516 * triggering the cancellable object from another thread. If the operation
517 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
518 * Cancelling a close will still leave the stream closed, but there some streams
519 * can use a faster close that doesn't block to e.g. check errors. On
520 * cancellation (as with any error) there is no guarantee that all written
521 * data will reach the target.
523 * Return value: %TRUE on success, %FALSE on failure
526 g_output_stream_close (GOutputStream *stream,
527 GCancellable *cancellable,
530 GOutputStreamClass *class;
533 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
535 class = G_OUTPUT_STREAM_GET_CLASS (stream);
537 if (stream->priv->closed)
540 if (stream->priv->pending)
542 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
543 _("Stream has outstanding operation"));
547 res = g_output_stream_flush (stream, cancellable, error);
549 stream->priv->pending = TRUE;
552 g_push_current_cancellable (cancellable);
556 /* flushing caused the error that we want to return,
557 * but we still want to close the underlying stream if possible
560 class->close (stream, cancellable, NULL);
566 res = class->close (stream, cancellable, error);
570 g_pop_current_cancellable (cancellable);
572 stream->priv->closed = TRUE;
573 stream->priv->pending = FALSE;
579 async_ready_callback_wrapper (GObject *source_object,
583 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
585 stream->priv->pending = FALSE;
586 if (stream->priv->outstanding_callback)
587 (*stream->priv->outstanding_callback) (source_object, res, user_data);
588 g_object_unref (stream);
592 async_ready_close_callback_wrapper (GObject *source_object,
596 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
598 stream->priv->pending = FALSE;
599 stream->priv->closed = TRUE;
600 if (stream->priv->outstanding_callback)
601 (*stream->priv->outstanding_callback) (source_object, res, user_data);
602 g_object_unref (stream);
606 * g_output_stream_write_async:
607 * @stream: A #GOutputStream.
608 * @buffer: the buffer containing the data to write.
609 * @count: the number of bytes to write
610 * @io_priority: the io priority of the request. the io priority of the request
611 * @cancellable: optional #GCancellable object, %NULL to ignore.
612 * @callback: callback to call when the request is satisfied
613 * @user_data: the data to pass to callback function
615 * Request an asynchronous write of @count bytes from @buffer into the stream.
616 * When the operation is finished @callback will be called, giving the results.
618 * During an async request no other sync and async calls are allowed, and will
619 * result in %G_IO_ERROR_PENDING errors.
621 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
623 * On success, the number of bytes written will be passed to the
624 * @callback. It is not an error if this is not the same as the requested size, as it
625 * can happen e.g. on a partial i/o error, but generally we try to write
626 * as many bytes as requested.
628 * Any outstanding i/o request with higher priority (lower numerical value) will
629 * be executed before an outstanding request with lower priority. Default
630 * priority is %G_PRIORITY_DEFAULT.
632 * The asyncronous methods have a default fallback that uses threads to implement
633 * asynchronicity, so they are optional for inheriting classes. However, if you
634 * override one you must override all.
636 * For the synchronous, blocking version of this function, see g_output_stream_write().
639 g_output_stream_write_async (GOutputStream *stream,
643 GCancellable *cancellable,
644 GAsyncReadyCallback callback,
647 GOutputStreamClass *class;
648 GSimpleAsyncResult *simple;
650 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
651 g_return_if_fail (buffer != NULL);
655 simple = g_simple_async_result_new (G_OBJECT (stream),
658 g_output_stream_write_async);
659 g_simple_async_result_complete_in_idle (simple);
660 g_object_unref (simple);
664 if (((gssize) count) < 0)
666 g_simple_async_report_error_in_idle (G_OBJECT (stream),
669 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
670 _("Too large count value passed to g_output_stream_write_async"));
674 if (stream->priv->closed)
676 g_simple_async_report_error_in_idle (G_OBJECT (stream),
679 G_IO_ERROR, G_IO_ERROR_CLOSED,
680 _("Stream is already closed"));
684 if (stream->priv->pending)
686 g_simple_async_report_error_in_idle (G_OBJECT (stream),
689 G_IO_ERROR, G_IO_ERROR_PENDING,
690 _("Stream has outstanding operation"));
694 class = G_OUTPUT_STREAM_GET_CLASS (stream);
696 stream->priv->pending = TRUE;
697 stream->priv->outstanding_callback = callback;
698 g_object_ref (stream);
699 class->write_async (stream, buffer, count, io_priority, cancellable,
700 async_ready_callback_wrapper, user_data);
704 * g_output_stream_write_finish:
705 * @stream: a #GOutputStream.
706 * @result: a #GAsyncResult.
707 * @error: a #GError location to store the error occuring, or %NULL to
710 * Finishes a stream write operation.
712 * Returns: a #gssize containing the number of bytes written to the stream.
715 g_output_stream_write_finish (GOutputStream *stream,
716 GAsyncResult *result,
719 GSimpleAsyncResult *simple;
720 GOutputStreamClass *class;
722 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
723 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
725 if (G_IS_SIMPLE_ASYNC_RESULT (result))
727 simple = G_SIMPLE_ASYNC_RESULT (result);
728 if (g_simple_async_result_propagate_error (simple, error))
731 /* Special case writes of 0 bytes */
732 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
736 class = G_OUTPUT_STREAM_GET_CLASS (stream);
737 return class->write_finish (stream, result, error);
741 GInputStream *source;
743 GAsyncReadyCallback callback;
747 async_ready_splice_callback_wrapper (GObject *source_object,
751 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
752 SpliceUserData *data = _data;
754 stream->priv->pending = FALSE;
757 (*data->callback) (source_object, res, data->user_data);
759 g_object_unref (stream);
760 g_object_unref (data->source);
765 * g_output_stream_splice_async:
766 * @stream: a #GOutputStream.
767 * @source: a #GInputStream.
768 * @flags: a set of #GOutputStreamSpliceFlags.
769 * @io_priority: the io priority of the request.
770 * @cancellable: optional #GCancellable object, %NULL to ignore.
771 * @callback: a #GAsyncReadyCallback.
772 * @user_data: user data passed to @callback.
774 * Splices a stream asynchronously.
778 g_output_stream_splice_async (GOutputStream *stream,
779 GInputStream *source,
780 GOutputStreamSpliceFlags flags,
782 GCancellable *cancellable,
783 GAsyncReadyCallback callback,
786 GOutputStreamClass *class;
787 SpliceUserData *data;
789 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
790 g_return_if_fail (G_IS_INPUT_STREAM (source));
792 if (stream->priv->closed)
794 g_simple_async_report_error_in_idle (G_OBJECT (stream),
797 G_IO_ERROR, G_IO_ERROR_CLOSED,
798 _("Target stream is already closed"));
802 if (g_input_stream_is_closed (source))
804 g_simple_async_report_error_in_idle (G_OBJECT (stream),
807 G_IO_ERROR, G_IO_ERROR_CLOSED,
808 _("Source stream is already closed"));
812 if (stream->priv->pending)
814 g_simple_async_report_error_in_idle (G_OBJECT (stream),
817 G_IO_ERROR, G_IO_ERROR_PENDING,
818 _("Stream has outstanding operation"));
822 class = G_OUTPUT_STREAM_GET_CLASS (stream);
824 stream->priv->pending = TRUE;
826 data = g_new0 (SpliceUserData, 1);
827 data->callback = callback;
828 data->user_data = user_data;
829 data->source = g_object_ref (source);
831 g_object_ref (stream);
832 class->splice_async (stream, source, flags, io_priority, cancellable,
833 async_ready_splice_callback_wrapper, data);
837 * g_output_stream_splice_finish:
838 * @stream: a #GOutputStream.
839 * @result: a #GAsyncResult.
840 * @error: a #GError location to store the error occuring, or %NULL to
843 * Finishes an asynchronous stream splice operation.
845 * Returns: a #gssize of the number of bytes spliced.
848 g_output_stream_splice_finish (GOutputStream *stream,
849 GAsyncResult *result,
852 GSimpleAsyncResult *simple;
853 GOutputStreamClass *class;
855 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
856 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
858 if (G_IS_SIMPLE_ASYNC_RESULT (result))
860 simple = G_SIMPLE_ASYNC_RESULT (result);
861 if (g_simple_async_result_propagate_error (simple, error))
865 class = G_OUTPUT_STREAM_GET_CLASS (stream);
866 return class->splice_finish (stream, result, error);
870 * g_output_stream_flush_async:
871 * @stream: a #GOutputStream.
872 * @io_priority: the io priority of the request.
873 * @cancellable: optional #GCancellable object, %NULL to ignore.
874 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
875 * @user_data: the data to pass to callback function
877 * Flushes a stream asynchronously.
881 g_output_stream_flush_async (GOutputStream *stream,
883 GCancellable *cancellable,
884 GAsyncReadyCallback callback,
887 GOutputStreamClass *class;
888 GSimpleAsyncResult *simple;
890 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
892 if (stream->priv->closed)
894 g_simple_async_report_error_in_idle (G_OBJECT (stream),
897 G_IO_ERROR, G_IO_ERROR_CLOSED,
898 _("Stream is already closed"));
902 if (stream->priv->pending)
904 g_simple_async_report_error_in_idle (G_OBJECT (stream),
907 G_IO_ERROR, G_IO_ERROR_PENDING,
908 _("Stream has outstanding operation"));
912 class = G_OUTPUT_STREAM_GET_CLASS (stream);
914 if (class->flush_async == NULL)
916 simple = g_simple_async_result_new (G_OBJECT (stream),
919 g_output_stream_flush_async);
920 g_simple_async_result_complete_in_idle (simple);
921 g_object_unref (simple);
925 stream->priv->pending = TRUE;
926 stream->priv->outstanding_callback = callback;
927 g_object_ref (stream);
928 class->flush_async (stream, io_priority, cancellable,
929 async_ready_callback_wrapper, user_data);
933 * g_output_stream_flush_finish:
934 * @stream: a #GOutputStream.
935 * @result: a GAsyncResult.
936 * @error: a #GError location to store the error occuring, or %NULL to
939 * Finishes flushing an output stream.
941 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
944 g_output_stream_flush_finish (GOutputStream *stream,
945 GAsyncResult *result,
948 GSimpleAsyncResult *simple;
949 GOutputStreamClass *klass;
951 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
952 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
954 if (G_IS_SIMPLE_ASYNC_RESULT (result))
956 simple = G_SIMPLE_ASYNC_RESULT (result);
957 if (g_simple_async_result_propagate_error (simple, error))
960 /* Special case default implementation */
961 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
965 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
966 return klass->flush_finish (stream, result, error);
971 * g_output_stream_close_async:
972 * @stream: A #GOutputStream.
973 * @io_priority: the io priority of the request.
974 * @callback: callback to call when the request is satisfied
975 * @user_data: the data to pass to callback function
976 * @cancellable: optional cancellable object
978 * Requests an asynchronous close of the stream, releasing resources related to it.
979 * When the operation is finished @callback will be called, giving the results.
981 * For behaviour details see g_output_stream_close().
983 * The asyncronous methods have a default fallback that uses threads to implement
984 * asynchronicity, so they are optional for inheriting classes. However, if you
985 * override one you must override all.
988 g_output_stream_close_async (GOutputStream *stream,
990 GCancellable *cancellable,
991 GAsyncReadyCallback callback,
994 GOutputStreamClass *class;
995 GSimpleAsyncResult *simple;
997 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
999 if (stream->priv->closed)
1001 simple = g_simple_async_result_new (G_OBJECT (stream),
1004 g_output_stream_close_async);
1005 g_simple_async_result_complete_in_idle (simple);
1006 g_object_unref (simple);
1010 if (stream->priv->pending)
1012 g_simple_async_report_error_in_idle (G_OBJECT (stream),
1015 G_IO_ERROR, G_IO_ERROR_PENDING,
1016 _("Stream has outstanding operation"));
1020 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1021 stream->priv->pending = TRUE;
1022 stream->priv->outstanding_callback = callback;
1023 g_object_ref (stream);
1024 class->close_async (stream, io_priority, cancellable,
1025 async_ready_close_callback_wrapper, user_data);
1029 * g_output_stream_close_finish:
1030 * @stream: a #GOutputStream.
1031 * @result: a #GAsyncResult.
1032 * @error: a #GError location to store the error occuring, or %NULL to
1035 * Closes an output stream.
1037 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1040 g_output_stream_close_finish (GOutputStream *stream,
1041 GAsyncResult *result,
1044 GSimpleAsyncResult *simple;
1045 GOutputStreamClass *class;
1047 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1048 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1050 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1052 simple = G_SIMPLE_ASYNC_RESULT (result);
1053 if (g_simple_async_result_propagate_error (simple, error))
1056 /* Special case already closed */
1057 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1061 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1062 return class->close_finish (stream, result, error);
1066 * g_output_stream_is_closed:
1067 * @stream: a #GOutputStream.
1069 * Checks if an output stream has already been closed.
1071 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1074 g_output_stream_is_closed (GOutputStream *stream)
1076 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1078 return stream->priv->closed;
1082 * g_output_stream_has_pending:
1083 * @stream: a #GOutputStream.
1085 * Checks if an ouput stream has pending actions.
1087 * Returns: %TRUE if @stream has pending actions.
1090 g_output_stream_has_pending (GOutputStream *stream)
1092 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1094 return stream->priv->pending;
1098 * g_output_stream_set_pending:
1099 * @stream: a #GOutputStream.
1100 * @pending: a #gboolean.
1102 * Sets the @stream as having pending actions if @pending is %TRUE.
1105 g_output_stream_set_pending (GOutputStream *stream,
1108 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1110 stream->priv->pending = pending;
1114 /********************************************
1115 * Default implementation of async ops *
1116 ********************************************/
1120 gsize count_requested;
1121 gssize count_written;
1125 write_async_thread (GSimpleAsyncResult *res,
1127 GCancellable *cancellable)
1130 GOutputStreamClass *class;
1131 GError *error = NULL;
1133 class = G_OUTPUT_STREAM_GET_CLASS (object);
1134 op = g_simple_async_result_get_op_res_gpointer (res);
1135 op->count_written = class->write (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1136 cancellable, &error);
1137 if (op->count_written == -1)
1139 g_simple_async_result_set_from_error (res, error);
1140 g_error_free (error);
1145 g_output_stream_real_write_async (GOutputStream *stream,
1149 GCancellable *cancellable,
1150 GAsyncReadyCallback callback,
1153 GSimpleAsyncResult *res;
1156 op = g_new0 (WriteData, 1);
1157 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1158 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1159 op->buffer = buffer;
1160 op->count_requested = count;
1162 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1163 g_object_unref (res);
1167 g_output_stream_real_write_finish (GOutputStream *stream,
1168 GAsyncResult *result,
1171 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1174 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1175 op = g_simple_async_result_get_op_res_gpointer (simple);
1176 return op->count_written;
1180 GInputStream *source;
1181 GOutputStreamSpliceFlags flags;
1182 gssize bytes_copied;
1186 splice_async_thread (GSimpleAsyncResult *result,
1188 GCancellable *cancellable)
1191 GOutputStreamClass *class;
1192 GError *error = NULL;
1193 GOutputStream *stream;
1195 stream = G_OUTPUT_STREAM (object);
1196 class = G_OUTPUT_STREAM_GET_CLASS (object);
1197 op = g_simple_async_result_get_op_res_gpointer (result);
1199 stream->priv->pending = FALSE;
1201 g_output_stream_splice (stream,
1206 stream->priv->pending = TRUE;
1208 if (op->bytes_copied == -1)
1210 g_simple_async_result_set_from_error (result, error);
1211 g_error_free (error);
1216 g_output_stream_real_splice_async (GOutputStream *stream,
1217 GInputStream *source,
1218 GOutputStreamSpliceFlags flags,
1220 GCancellable *cancellable,
1221 GAsyncReadyCallback callback,
1224 GSimpleAsyncResult *res;
1227 op = g_new0 (SpliceData, 1);
1228 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1229 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1231 op->source = source;
1233 /* TODO: In the case where both source and destintion have
1234 non-threadbased async calls we can use a true async copy here */
1236 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1237 g_object_unref (res);
1241 g_output_stream_real_splice_finish (GOutputStream *stream,
1242 GAsyncResult *result,
1245 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1248 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1249 op = g_simple_async_result_get_op_res_gpointer (simple);
1250 return op->bytes_copied;
1256 flush_async_thread (GSimpleAsyncResult *res,
1258 GCancellable *cancellable)
1260 GOutputStreamClass *class;
1262 GError *error = NULL;
1264 class = G_OUTPUT_STREAM_GET_CLASS (object);
1267 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1271 g_simple_async_result_set_from_error (res, error);
1272 g_error_free (error);
1277 g_output_stream_real_flush_async (GOutputStream *stream,
1279 GCancellable *cancellable,
1280 GAsyncReadyCallback callback,
1283 GSimpleAsyncResult *res;
1285 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1287 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1288 g_object_unref (res);
1292 g_output_stream_real_flush_finish (GOutputStream *stream,
1293 GAsyncResult *result,
1300 close_async_thread (GSimpleAsyncResult *res,
1302 GCancellable *cancellable)
1304 GOutputStreamClass *class;
1305 GError *error = NULL;
1308 /* Auto handling of cancelation disabled, and ignore
1309 cancellation, since we want to close things anyway, although
1310 possibly in a quick-n-dirty way. At least we never want to leak
1313 class = G_OUTPUT_STREAM_GET_CLASS (object);
1314 result = class->close (G_OUTPUT_STREAM (object), cancellable, &error);
1317 g_simple_async_result_set_from_error (res, error);
1318 g_error_free (error);
1323 g_output_stream_real_close_async (GOutputStream *stream,
1325 GCancellable *cancellable,
1326 GAsyncReadyCallback callback,
1329 GSimpleAsyncResult *res;
1331 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1333 g_simple_async_result_set_handle_cancellation (res, FALSE);
1335 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1336 g_object_unref (res);
1340 g_output_stream_real_close_finish (GOutputStream *stream,
1341 GAsyncResult *result,
1344 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1345 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1349 #define __G_OUTPUT_STREAM_C__
1350 #include "gioaliasdef.c"