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"
29 * SECTION:goutputstream
30 * @short_description: base class for implementing streaming output
36 G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
38 struct _GOutputStreamPrivate {
42 GAsyncReadyCallback outstanding_callback;
45 static gssize g_output_stream_real_splice (GOutputStream *stream,
47 GOutputStreamSpliceFlags flags,
48 GCancellable *cancellable,
50 static void g_output_stream_real_write_async (GOutputStream *stream,
54 GCancellable *cancellable,
55 GAsyncReadyCallback callback,
57 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
60 static void g_output_stream_real_splice_async (GOutputStream *stream,
62 GOutputStreamSpliceFlags flags,
64 GCancellable *cancellable,
65 GAsyncReadyCallback callback,
67 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
70 static void g_output_stream_real_flush_async (GOutputStream *stream,
72 GCancellable *cancellable,
73 GAsyncReadyCallback callback,
75 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
78 static void g_output_stream_real_close_async (GOutputStream *stream,
80 GCancellable *cancellable,
81 GAsyncReadyCallback callback,
83 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
88 g_output_stream_finalize (GObject *object)
90 GOutputStream *stream;
92 stream = G_OUTPUT_STREAM (object);
94 if (G_OBJECT_CLASS (g_output_stream_parent_class)->finalize)
95 (*G_OBJECT_CLASS (g_output_stream_parent_class)->finalize) (object);
99 g_output_stream_dispose (GObject *object)
101 GOutputStream *stream;
103 stream = G_OUTPUT_STREAM (object);
105 if (!stream->priv->closed)
106 g_output_stream_close (stream, NULL, NULL);
108 if (G_OBJECT_CLASS (g_output_stream_parent_class)->dispose)
109 (*G_OBJECT_CLASS (g_output_stream_parent_class)->dispose) (object);
113 g_output_stream_class_init (GOutputStreamClass *klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
117 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
119 gobject_class->finalize = g_output_stream_finalize;
120 gobject_class->dispose = g_output_stream_dispose;
122 klass->splice = g_output_stream_real_splice;
124 klass->write_async = g_output_stream_real_write_async;
125 klass->write_finish = g_output_stream_real_write_finish;
126 klass->splice_async = g_output_stream_real_splice_async;
127 klass->splice_finish = g_output_stream_real_splice_finish;
128 klass->flush_async = g_output_stream_real_flush_async;
129 klass->flush_finish = g_output_stream_real_flush_finish;
130 klass->close_async = g_output_stream_real_close_async;
131 klass->close_finish = g_output_stream_real_close_finish;
135 g_output_stream_init (GOutputStream *stream)
137 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
138 G_TYPE_OUTPUT_STREAM,
139 GOutputStreamPrivate);
143 * g_output_stream_write:
144 * @stream: a #GOutputStream.
145 * @buffer: the buffer containing the data to write.
146 * @count: the number of bytes to write
147 * @cancellable: optional cancellable object
148 * @error: location to store the error occuring, or %NULL to ignore
150 * Tries to write @count bytes from @buffer into the stream. Will block
151 * during the operation.
153 * If count is zero returns zero and does nothing. A value of @count
154 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
156 * On success, the number of bytes written to the stream is returned.
157 * It is not an error if this is not the same as the requested size, as it
158 * can happen e.g. on a partial i/o error, or if the there is not enough
159 * storage in the stream. All writes either block until at least one byte
160 * is written, so zero is never returned (unless @count is zero).
162 * If @cancellable is not NULL, then the operation can be cancelled by
163 * triggering the cancellable object from another thread. If the operation
164 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
165 * operation was partially finished when the operation was cancelled the
166 * partial result will be returned, without an error.
168 * On error -1 is returned and @error is set accordingly.
170 * Return value: Number of bytes written, or -1 on error
173 g_output_stream_write (GOutputStream *stream,
176 GCancellable *cancellable,
179 GOutputStreamClass *class;
182 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
183 g_return_val_if_fail (buffer != NULL, 0);
188 if (((gssize) count) < 0)
190 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
191 _("Too large count value passed to g_output_stream_write"));
195 if (stream->priv->closed)
197 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
198 _("Stream is already closed"));
202 if (stream->priv->pending)
204 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
205 _("Stream has outstanding operation"));
209 class = G_OUTPUT_STREAM_GET_CLASS (stream);
211 if (class->write == NULL)
213 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
214 _("Output stream doesn't implement write"));
219 g_push_current_cancellable (cancellable);
221 stream->priv->pending = TRUE;
222 res = class->write (stream, buffer, count, cancellable, error);
223 stream->priv->pending = FALSE;
226 g_pop_current_cancellable (cancellable);
232 * g_output_stream_write_all:
233 * @stream: a #GOutputStream.
234 * @buffer: the buffer containing the data to write.
235 * @count: the number of bytes to write
236 * @bytes_written: location to store the number of bytes that was written to the stream
237 * @cancellable: optional #GCancellable object, %NULL to ignore.
238 * @error: location to store the error occuring, or %NULL to ignore
240 * Tries to write @count bytes from @buffer into the stream. Will block
241 * during the operation.
243 * This function is similar to g_output_stream_write(), except it tries to
244 * write as many bytes as requested, only stopping on an error.
246 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
249 * If there is an error during the operation FALSE is returned and @error
250 * is set to indicate the error status, @bytes_written is updated to contain
251 * the number of bytes written into the stream before the error occured.
253 * Return value: %TRUE on success, %FALSE if there was an error
256 g_output_stream_write_all (GOutputStream *stream,
259 gsize *bytes_written,
260 GCancellable *cancellable,
263 gsize _bytes_written;
266 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
267 g_return_val_if_fail (buffer != NULL, FALSE);
270 while (_bytes_written < count)
272 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
277 *bytes_written = _bytes_written;
282 g_warning ("Write returned zero without error");
284 _bytes_written += res;
288 *bytes_written = _bytes_written;
293 * g_output_stream_flush:
294 * @stream: a #GOutputStream.
295 * @cancellable: optional cancellable object
296 * @error: location to store the error occuring, or %NULL to ignore
298 * Flushed any outstanding buffers in the stream. Will block during the operation.
299 * Closing the stream will implicitly cause a flush.
301 * This function is optional for inherited classes.
303 * If @cancellable is not %NULL, then the operation can be cancelled by
304 * triggering the cancellable object from another thread. If the operation
305 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
307 * Return value: %TRUE on success, %FALSE on error
310 g_output_stream_flush (GOutputStream *stream,
311 GCancellable *cancellable,
314 GOutputStreamClass *class;
317 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
319 if (stream->priv->closed)
321 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
322 _("Stream is already closed"));
326 if (stream->priv->pending)
328 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
329 _("Stream has outstanding operation"));
333 class = G_OUTPUT_STREAM_GET_CLASS (stream);
339 g_push_current_cancellable (cancellable);
341 stream->priv->pending = TRUE;
342 res = class->flush (stream, cancellable, error);
343 stream->priv->pending = FALSE;
346 g_pop_current_cancellable (cancellable);
353 * g_output_stream_splice:
354 * @stream: a #GOutputStream.
355 * @source: a #GInputStream.
356 * @flags: a set of #GOutputStreamSpliceFlags.
357 * @cancellable: optional #GCancellable object, %NULL to ignore.
358 * @error: a #GError location to store the error occuring, or %NULL to
361 * Splices an input stream into an output stream.
363 * Returns: a #gssize containig the size of the data spliced.
366 g_output_stream_splice (GOutputStream *stream,
367 GInputStream *source,
368 GOutputStreamSpliceFlags flags,
369 GCancellable *cancellable,
372 GOutputStreamClass *class;
375 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
376 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
378 if (stream->priv->closed)
380 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
381 _("Target stream is already closed"));
385 if (g_input_stream_is_closed (source))
387 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
388 _("Source stream is already closed"));
392 if (stream->priv->pending)
394 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
395 _("Stream has outstanding operation"));
399 class = G_OUTPUT_STREAM_GET_CLASS (stream);
403 g_push_current_cancellable (cancellable);
405 stream->priv->pending = TRUE;
406 res = class->splice (stream, source, flags, cancellable, error);
407 stream->priv->pending = FALSE;
410 g_pop_current_cancellable (cancellable);
416 g_output_stream_real_splice (GOutputStream *stream,
417 GInputStream *source,
418 GOutputStreamSpliceFlags flags,
419 GCancellable *cancellable,
422 gssize n_read, n_written;
424 char buffer[8192], *p;
431 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
444 stream->priv->pending = FALSE;
445 n_written = g_output_stream_write (stream, p, n_read, cancellable, error);
446 stream->priv->pending = TRUE;
455 bytes_copied += n_written;
461 error = NULL; /* Ignore further errors */
463 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_SOURCE)
465 /* Don't care about errors in source here */
466 g_input_stream_close (source, cancellable, NULL);
469 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_TARGET)
471 /* But write errors on close are bad! */
472 stream->priv->pending = FALSE;
473 if (!g_output_stream_close (stream, cancellable, error))
475 stream->priv->pending = TRUE;
486 * g_output_stream_close:
487 * @stream: A #GOutputStream.
488 * @cancellable: optional cancellable object
489 * @error: location to store the error occuring, or %NULL to ignore
491 * Closes the stream, releasing resources related to it.
493 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
494 * Closing a stream multiple times will not return an error.
496 * Closing a stream will automatically flush any outstanding buffers in the
499 * Streams will be automatically closed when the last reference
500 * is dropped, but you might want to call make sure resources
501 * are released as early as possible.
503 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
504 * open after the stream is closed. See the documentation for the individual
505 * stream for details.
507 * On failure the first error that happened will be reported, but the close
508 * operation will finish as much as possible. A stream that failed to
509 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
510 * is important to check and report the error to the user, otherwise
511 * there might be a loss of data as all data might not be written.
513 * If @cancellable is not NULL, then the operation can be cancelled by
514 * triggering the cancellable object from another thread. If the operation
515 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
516 * Cancelling a close will still leave the stream closed, but there some streams
517 * can use a faster close that doesn't block to e.g. check errors. On
518 * cancellation (as with any error) there is no guarantee that all written
519 * data will reach the target.
521 * Return value: %TRUE on success, %FALSE on failure
524 g_output_stream_close (GOutputStream *stream,
525 GCancellable *cancellable,
528 GOutputStreamClass *class;
531 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
533 class = G_OUTPUT_STREAM_GET_CLASS (stream);
535 if (stream->priv->closed)
538 if (stream->priv->pending)
540 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
541 _("Stream has outstanding operation"));
545 res = g_output_stream_flush (stream, cancellable, error);
547 stream->priv->pending = TRUE;
550 g_push_current_cancellable (cancellable);
554 /* flushing caused the error that we want to return,
555 * but we still want to close the underlying stream if possible
558 class->close (stream, cancellable, NULL);
564 res = class->close (stream, cancellable, error);
568 g_pop_current_cancellable (cancellable);
570 stream->priv->closed = TRUE;
571 stream->priv->pending = FALSE;
577 async_ready_callback_wrapper (GObject *source_object,
581 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
583 stream->priv->pending = FALSE;
584 if (stream->priv->outstanding_callback)
585 (*stream->priv->outstanding_callback) (source_object, res, user_data);
586 g_object_unref (stream);
590 async_ready_close_callback_wrapper (GObject *source_object,
594 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
596 stream->priv->pending = FALSE;
597 stream->priv->closed = TRUE;
598 if (stream->priv->outstanding_callback)
599 (*stream->priv->outstanding_callback) (source_object, res, user_data);
600 g_object_unref (stream);
604 * g_output_stream_write_async:
605 * @stream: A #GOutputStream.
606 * @buffer: the buffer containing the data to write.
607 * @count: the number of bytes to write
608 * @io_priority: the io priority of the request. the io priority of the request
609 * @cancellable: optional #GCancellable object, %NULL to ignore.
610 * @callback: callback to call when the request is satisfied
611 * @user_data: the data to pass to callback function
613 * Request an asynchronous write of @count bytes from @buffer into the stream.
614 * When the operation is finished @callback will be called, giving the results.
616 * During an async request no other sync and async calls are allowed, and will
617 * result in %G_IO_ERROR_PENDING errors.
619 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
621 * On success, the number of bytes written will be passed to the
622 * @callback. It is not an error if this is not the same as the requested size, as it
623 * can happen e.g. on a partial i/o error, but generally we try to write
624 * as many bytes as requested.
626 * Any outstanding i/o request with higher priority (lower numerical value) will
627 * be executed before an outstanding request with lower priority. Default
628 * priority is %G_PRIORITY_DEFAULT.
630 * The asyncronous methods have a default fallback that uses threads to implement
631 * asynchronicity, so they are optional for inheriting classes. However, if you
632 * override one you must override all.
634 * For the synchronous, blocking version of this function, see g_output_stream_write().
637 g_output_stream_write_async (GOutputStream *stream,
641 GCancellable *cancellable,
642 GAsyncReadyCallback callback,
645 GOutputStreamClass *class;
646 GSimpleAsyncResult *simple;
648 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
649 g_return_if_fail (buffer != NULL);
653 simple = g_simple_async_result_new (G_OBJECT (stream),
656 g_output_stream_write_async);
657 g_simple_async_result_complete_in_idle (simple);
658 g_object_unref (simple);
662 if (((gssize) count) < 0)
664 g_simple_async_report_error_in_idle (G_OBJECT (stream),
667 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
668 _("Too large count value passed to g_output_stream_write_async"));
672 if (stream->priv->closed)
674 g_simple_async_report_error_in_idle (G_OBJECT (stream),
677 G_IO_ERROR, G_IO_ERROR_CLOSED,
678 _("Stream is already closed"));
682 if (stream->priv->pending)
684 g_simple_async_report_error_in_idle (G_OBJECT (stream),
687 G_IO_ERROR, G_IO_ERROR_PENDING,
688 _("Stream has outstanding operation"));
692 class = G_OUTPUT_STREAM_GET_CLASS (stream);
694 stream->priv->pending = TRUE;
695 stream->priv->outstanding_callback = callback;
696 g_object_ref (stream);
697 class->write_async (stream, buffer, count, io_priority, cancellable,
698 async_ready_callback_wrapper, user_data);
702 * g_output_stream_write_finish:
703 * @stream: a #GOutputStream.
704 * @result: a #GAsyncResult.
705 * @error: a #GError location to store the error occuring, or %NULL to
708 * Finishes a stream write operation.
710 * Returns: a #gssize containing the number of bytes written to the stream.
713 g_output_stream_write_finish (GOutputStream *stream,
714 GAsyncResult *result,
717 GSimpleAsyncResult *simple;
718 GOutputStreamClass *class;
720 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
721 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
723 if (G_IS_SIMPLE_ASYNC_RESULT (result))
725 simple = G_SIMPLE_ASYNC_RESULT (result);
726 if (g_simple_async_result_propagate_error (simple, error))
729 /* Special case writes of 0 bytes */
730 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
734 class = G_OUTPUT_STREAM_GET_CLASS (stream);
735 return class->write_finish (stream, result, error);
739 GInputStream *source;
741 GAsyncReadyCallback callback;
745 async_ready_splice_callback_wrapper (GObject *source_object,
749 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
750 SpliceUserData *data = _data;
752 stream->priv->pending = FALSE;
755 (*data->callback) (source_object, res, data->user_data);
757 g_object_unref (stream);
758 g_object_unref (data->source);
763 * g_output_stream_splice_async:
764 * @stream: a #GOutputStream.
765 * @source: a #GInputStream.
766 * @flags: a set of #GOutputStreamSpliceFlags.
767 * @io_priority: the io priority of the request.
768 * @cancellable: optional #GCancellable object, %NULL to ignore.
769 * @callback: a #GAsyncReadyCallback.
770 * @user_data: user data passed to @callback.
772 * Splices a stream asynchronously.
776 g_output_stream_splice_async (GOutputStream *stream,
777 GInputStream *source,
778 GOutputStreamSpliceFlags flags,
780 GCancellable *cancellable,
781 GAsyncReadyCallback callback,
784 GOutputStreamClass *class;
785 SpliceUserData *data;
787 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
788 g_return_if_fail (G_IS_INPUT_STREAM (source));
790 if (stream->priv->closed)
792 g_simple_async_report_error_in_idle (G_OBJECT (stream),
795 G_IO_ERROR, G_IO_ERROR_CLOSED,
796 _("Target stream is already closed"));
800 if (g_input_stream_is_closed (source))
802 g_simple_async_report_error_in_idle (G_OBJECT (stream),
805 G_IO_ERROR, G_IO_ERROR_CLOSED,
806 _("Source stream is already closed"));
810 if (stream->priv->pending)
812 g_simple_async_report_error_in_idle (G_OBJECT (stream),
815 G_IO_ERROR, G_IO_ERROR_PENDING,
816 _("Stream has outstanding operation"));
820 class = G_OUTPUT_STREAM_GET_CLASS (stream);
822 stream->priv->pending = TRUE;
824 data = g_new0 (SpliceUserData, 1);
825 data->callback = callback;
826 data->user_data = user_data;
827 data->source = g_object_ref (source);
829 g_object_ref (stream);
830 class->splice_async (stream, source, flags, io_priority, cancellable,
831 async_ready_splice_callback_wrapper, data);
835 * g_output_stream_splice_finish:
836 * @stream: a #GOutputStream.
837 * @result: a #GAsyncResult.
838 * @error: a #GError location to store the error occuring, or %NULL to
841 * Finishes an asynchronous stream splice operation.
843 * Returns: a #gssize of the number of bytes spliced.
846 g_output_stream_splice_finish (GOutputStream *stream,
847 GAsyncResult *result,
850 GSimpleAsyncResult *simple;
851 GOutputStreamClass *class;
853 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
854 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
856 if (G_IS_SIMPLE_ASYNC_RESULT (result))
858 simple = G_SIMPLE_ASYNC_RESULT (result);
859 if (g_simple_async_result_propagate_error (simple, error))
863 class = G_OUTPUT_STREAM_GET_CLASS (stream);
864 return class->splice_finish (stream, result, error);
868 * g_output_stream_flush_async:
869 * @stream: a #GOutputStream.
870 * @io_priority: the io priority of the request.
871 * @cancellable: optional #GCancellable object, %NULL to ignore.
872 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
873 * @user_data: the data to pass to callback function
875 * Flushes a stream asynchronously.
879 g_output_stream_flush_async (GOutputStream *stream,
881 GCancellable *cancellable,
882 GAsyncReadyCallback callback,
885 GOutputStreamClass *class;
886 GSimpleAsyncResult *simple;
888 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
890 if (stream->priv->closed)
892 g_simple_async_report_error_in_idle (G_OBJECT (stream),
895 G_IO_ERROR, G_IO_ERROR_CLOSED,
896 _("Stream is already closed"));
900 if (stream->priv->pending)
902 g_simple_async_report_error_in_idle (G_OBJECT (stream),
905 G_IO_ERROR, G_IO_ERROR_PENDING,
906 _("Stream has outstanding operation"));
910 class = G_OUTPUT_STREAM_GET_CLASS (stream);
912 if (class->flush_async == NULL)
914 simple = g_simple_async_result_new (G_OBJECT (stream),
917 g_output_stream_flush_async);
918 g_simple_async_result_complete_in_idle (simple);
919 g_object_unref (simple);
923 stream->priv->pending = TRUE;
924 stream->priv->outstanding_callback = callback;
925 g_object_ref (stream);
926 class->flush_async (stream, io_priority, cancellable,
927 async_ready_callback_wrapper, user_data);
931 * g_output_stream_flush_finish:
932 * @stream: a #GOutputStream.
933 * @result: a GAsyncResult.
934 * @error: a #GError location to store the error occuring, or %NULL to
937 * Finishes flushing an output stream.
939 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
942 g_output_stream_flush_finish (GOutputStream *stream,
943 GAsyncResult *result,
946 GSimpleAsyncResult *simple;
947 GOutputStreamClass *klass;
949 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
950 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
952 if (G_IS_SIMPLE_ASYNC_RESULT (result))
954 simple = G_SIMPLE_ASYNC_RESULT (result);
955 if (g_simple_async_result_propagate_error (simple, error))
958 /* Special case default implementation */
959 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
963 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
964 return klass->flush_finish (stream, result, error);
969 * g_output_stream_close_async:
970 * @stream: A #GOutputStream.
971 * @callback: callback to call when the request is satisfied
972 * @user_data: the data to pass to callback function
973 * @cancellable: optional cancellable object
975 * Requests an asynchronous closes of the stream, releasing resources related to it.
976 * When the operation is finished @callback will be called, giving the results.
978 * For behaviour details see g_output_stream_close().
980 * The asyncronous methods have a default fallback that uses threads to implement
981 * asynchronicity, so they are optional for inheriting classes. However, if you
982 * override one you must override all.
985 g_output_stream_close_async (GOutputStream *stream,
987 GCancellable *cancellable,
988 GAsyncReadyCallback callback,
991 GOutputStreamClass *class;
992 GSimpleAsyncResult *simple;
994 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
996 if (stream->priv->closed)
998 simple = g_simple_async_result_new (G_OBJECT (stream),
1001 g_output_stream_close_async);
1002 g_simple_async_result_complete_in_idle (simple);
1003 g_object_unref (simple);
1007 if (stream->priv->pending)
1009 g_simple_async_report_error_in_idle (G_OBJECT (stream),
1012 G_IO_ERROR, G_IO_ERROR_PENDING,
1013 _("Stream has outstanding operation"));
1017 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1018 stream->priv->pending = TRUE;
1019 stream->priv->outstanding_callback = callback;
1020 g_object_ref (stream);
1021 class->close_async (stream, io_priority, cancellable,
1022 async_ready_close_callback_wrapper, user_data);
1026 * g_output_stream_close_finish:
1027 * @stream: a #GOutputStream.
1028 * @result: a #GAsyncResult.
1029 * @error: a #GError location to store the error occuring, or %NULL to
1032 * Closes an output stream.
1034 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1037 g_output_stream_close_finish (GOutputStream *stream,
1038 GAsyncResult *result,
1041 GSimpleAsyncResult *simple;
1042 GOutputStreamClass *class;
1044 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1045 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1047 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1049 simple = G_SIMPLE_ASYNC_RESULT (result);
1050 if (g_simple_async_result_propagate_error (simple, error))
1053 /* Special case already closed */
1054 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1058 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1059 return class->close_finish (stream, result, error);
1063 * g_output_stream_is_closed:
1064 * @stream: a #GOutputStream.
1066 * Checks if an output stream has already been closed.
1068 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1071 g_output_stream_is_closed (GOutputStream *stream)
1073 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1075 return stream->priv->closed;
1079 * g_output_stream_has_pending:
1080 * @stream: a #GOutputStream.
1082 * Checks if an ouput stream has pending actions.
1084 * Returns: %TRUE if @stream has pending actions.
1087 g_output_stream_has_pending (GOutputStream *stream)
1089 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1091 return stream->priv->pending;
1095 * g_output_stream_set_pending:
1096 * @stream: a #GOutputStream.
1097 * @pending: a #gboolean.
1099 * Sets the @stream as having pending actions if @pending is %TRUE.
1102 g_output_stream_set_pending (GOutputStream *stream,
1105 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1107 stream->priv->pending = pending;
1111 /********************************************
1112 * Default implementation of async ops *
1113 ********************************************/
1117 gsize count_requested;
1118 gssize count_written;
1122 write_async_thread (GSimpleAsyncResult *res,
1124 GCancellable *cancellable)
1127 GOutputStreamClass *class;
1128 GError *error = NULL;
1130 class = G_OUTPUT_STREAM_GET_CLASS (object);
1131 op = g_simple_async_result_get_op_res_gpointer (res);
1132 op->count_written = class->write (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1133 cancellable, &error);
1134 if (op->count_written == -1)
1136 g_simple_async_result_set_from_error (res, error);
1137 g_error_free (error);
1142 g_output_stream_real_write_async (GOutputStream *stream,
1146 GCancellable *cancellable,
1147 GAsyncReadyCallback callback,
1150 GSimpleAsyncResult *res;
1153 op = g_new0 (WriteData, 1);
1154 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1155 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1156 op->buffer = buffer;
1157 op->count_requested = count;
1159 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1160 g_object_unref (res);
1164 g_output_stream_real_write_finish (GOutputStream *stream,
1165 GAsyncResult *result,
1168 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1171 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1172 op = g_simple_async_result_get_op_res_gpointer (simple);
1173 return op->count_written;
1177 GInputStream *source;
1178 GOutputStreamSpliceFlags flags;
1179 gssize bytes_copied;
1183 splice_async_thread (GSimpleAsyncResult *result,
1185 GCancellable *cancellable)
1188 GOutputStreamClass *class;
1189 GError *error = NULL;
1190 GOutputStream *stream;
1192 stream = G_OUTPUT_STREAM (object);
1193 class = G_OUTPUT_STREAM_GET_CLASS (object);
1194 op = g_simple_async_result_get_op_res_gpointer (result);
1196 stream->priv->pending = FALSE;
1198 g_output_stream_splice (stream,
1203 stream->priv->pending = TRUE;
1205 if (op->bytes_copied == -1)
1207 g_simple_async_result_set_from_error (result, error);
1208 g_error_free (error);
1213 g_output_stream_real_splice_async (GOutputStream *stream,
1214 GInputStream *source,
1215 GOutputStreamSpliceFlags flags,
1217 GCancellable *cancellable,
1218 GAsyncReadyCallback callback,
1221 GSimpleAsyncResult *res;
1224 op = g_new0 (SpliceData, 1);
1225 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1226 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1228 op->source = source;
1230 /* TODO: In the case where both source and destintion have
1231 non-threadbased async calls we can use a true async copy here */
1233 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1234 g_object_unref (res);
1238 g_output_stream_real_splice_finish (GOutputStream *stream,
1239 GAsyncResult *result,
1242 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1245 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1246 op = g_simple_async_result_get_op_res_gpointer (simple);
1247 return op->bytes_copied;
1253 flush_async_thread (GSimpleAsyncResult *res,
1255 GCancellable *cancellable)
1257 GOutputStreamClass *class;
1259 GError *error = NULL;
1261 class = G_OUTPUT_STREAM_GET_CLASS (object);
1264 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1268 g_simple_async_result_set_from_error (res, error);
1269 g_error_free (error);
1274 g_output_stream_real_flush_async (GOutputStream *stream,
1276 GCancellable *cancellable,
1277 GAsyncReadyCallback callback,
1280 GSimpleAsyncResult *res;
1282 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1284 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1285 g_object_unref (res);
1289 g_output_stream_real_flush_finish (GOutputStream *stream,
1290 GAsyncResult *result,
1297 close_async_thread (GSimpleAsyncResult *res,
1299 GCancellable *cancellable)
1301 GOutputStreamClass *class;
1302 GError *error = NULL;
1305 /* Auto handling of cancelation disabled, and ignore
1306 cancellation, since we want to close things anyway, although
1307 possibly in a quick-n-dirty way. At least we never want to leak
1310 class = G_OUTPUT_STREAM_GET_CLASS (object);
1311 result = class->close (G_OUTPUT_STREAM (object), cancellable, &error);
1314 g_simple_async_result_set_from_error (res, error);
1315 g_error_free (error);
1320 g_output_stream_real_close_async (GOutputStream *stream,
1322 GCancellable *cancellable,
1323 GAsyncReadyCallback callback,
1326 GSimpleAsyncResult *res;
1328 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1330 g_simple_async_result_set_handle_cancellation (res, FALSE);
1332 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1333 g_object_unref (res);
1337 g_output_stream_real_close_finish (GOutputStream *stream,
1338 GAsyncResult *result,
1341 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1342 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);