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 G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
100 g_output_stream_dispose (GObject *object)
102 GOutputStream *stream;
104 stream = G_OUTPUT_STREAM (object);
106 if (!stream->priv->closed)
107 g_output_stream_close (stream, NULL, NULL);
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 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 %s"), G_STRFUNC);
195 class = G_OUTPUT_STREAM_GET_CLASS (stream);
197 if (class->write_fn == NULL)
199 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
200 _("Output stream doesn't implement write"));
204 if (!g_output_stream_set_pending (stream, error))
208 g_cancellable_push_current (cancellable);
210 res = class->write_fn (stream, buffer, count, cancellable, error);
213 g_cancellable_pop_current (cancellable);
215 g_output_stream_clear_pending (stream);
221 * g_output_stream_write_all:
222 * @stream: a #GOutputStream.
223 * @buffer: the buffer containing the data to write.
224 * @count: the number of bytes to write
225 * @bytes_written: location to store the number of bytes that was
226 * written to the stream
227 * @cancellable: optional #GCancellable object, %NULL to ignore.
228 * @error: location to store the error occuring, or %NULL to ignore
230 * Tries to write @count bytes from @buffer into the stream. Will block
231 * during the operation.
233 * This function is similar to g_output_stream_write(), except it tries to
234 * write as many bytes as requested, only stopping on an error.
236 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
239 * If there is an error during the operation FALSE is returned and @error
240 * is set to indicate the error status, @bytes_written is updated to contain
241 * the number of bytes written into the stream before the error occurred.
243 * Return value: %TRUE on success, %FALSE if there was an error
246 g_output_stream_write_all (GOutputStream *stream,
249 gsize *bytes_written,
250 GCancellable *cancellable,
253 gsize _bytes_written;
256 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
257 g_return_val_if_fail (buffer != NULL, FALSE);
260 while (_bytes_written < count)
262 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
267 *bytes_written = _bytes_written;
272 g_warning ("Write returned zero without error");
274 _bytes_written += res;
278 *bytes_written = _bytes_written;
284 * g_output_stream_flush:
285 * @stream: a #GOutputStream.
286 * @cancellable: optional cancellable object
287 * @error: location to store the error occuring, or %NULL to ignore
289 * Flushed any outstanding buffers in the stream. Will block during
290 * the operation. Closing the stream will implicitly cause a flush.
292 * This function is optional for inherited classes.
294 * If @cancellable is not %NULL, then the operation can be cancelled by
295 * triggering the cancellable object from another thread. If the operation
296 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
298 * Return value: %TRUE on success, %FALSE on error
301 g_output_stream_flush (GOutputStream *stream,
302 GCancellable *cancellable,
305 GOutputStreamClass *class;
308 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
310 if (!g_output_stream_set_pending (stream, error))
313 class = G_OUTPUT_STREAM_GET_CLASS (stream);
319 g_cancellable_push_current (cancellable);
321 res = class->flush (stream, cancellable, error);
324 g_cancellable_pop_current (cancellable);
327 g_output_stream_clear_pending (stream);
333 * g_output_stream_splice:
334 * @stream: a #GOutputStream.
335 * @source: a #GInputStream.
336 * @flags: a set of #GOutputStreamSpliceFlags.
337 * @cancellable: optional #GCancellable object, %NULL to ignore.
338 * @error: a #GError location to store the error occuring, or %NULL to
341 * Splices an input stream into an output stream.
343 * Returns: a #gssize containing the size of the data spliced.
346 g_output_stream_splice (GOutputStream *stream,
347 GInputStream *source,
348 GOutputStreamSpliceFlags flags,
349 GCancellable *cancellable,
352 GOutputStreamClass *class;
355 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
356 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
358 if (g_input_stream_is_closed (source))
360 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
361 _("Source stream is already closed"));
365 if (!g_output_stream_set_pending (stream, error))
368 class = G_OUTPUT_STREAM_GET_CLASS (stream);
372 g_cancellable_push_current (cancellable);
374 res = class->splice (stream, source, flags, cancellable, error);
377 g_cancellable_pop_current (cancellable);
379 g_output_stream_clear_pending (stream);
385 g_output_stream_real_splice (GOutputStream *stream,
386 GInputStream *source,
387 GOutputStreamSpliceFlags flags,
388 GCancellable *cancellable,
391 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
392 gssize n_read, n_written;
394 char buffer[8192], *p;
398 if (class->write_fn == NULL)
400 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
401 _("Output stream doesn't implement write"));
409 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
422 n_written = class->write_fn (stream, p, n_read, cancellable, error);
431 bytes_copied += n_written;
438 error = NULL; /* Ignore further errors */
440 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
442 /* Don't care about errors in source here */
443 g_input_stream_close (source, cancellable, NULL);
446 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
448 /* But write errors on close are bad! */
449 if (!class->close_fn (stream, cancellable, error))
461 * g_output_stream_close:
462 * @stream: A #GOutputStream.
463 * @cancellable: optional cancellable object
464 * @error: location to store the error occuring, or %NULL to ignore
466 * Closes the stream, releasing resources related to it.
468 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
469 * Closing a stream multiple times will not return an error.
471 * Closing a stream will automatically flush any outstanding buffers in the
474 * Streams will be automatically closed when the last reference
475 * is dropped, but you might want to call this function to make sure
476 * resources are released as early as possible.
478 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
479 * open after the stream is closed. See the documentation for the individual
480 * stream for details.
482 * On failure the first error that happened will be reported, but the close
483 * operation will finish as much as possible. A stream that failed to
484 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
485 * is important to check and report the error to the user, otherwise
486 * there might be a loss of data as all data might not be written.
488 * If @cancellable is not NULL, then the operation can be cancelled by
489 * triggering the cancellable object from another thread. If the operation
490 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
491 * Cancelling a close will still leave the stream closed, but there some streams
492 * can use a faster close that doesn't block to e.g. check errors. On
493 * cancellation (as with any error) there is no guarantee that all written
494 * data will reach the target.
496 * Return value: %TRUE on success, %FALSE on failure
499 g_output_stream_close (GOutputStream *stream,
500 GCancellable *cancellable,
503 GOutputStreamClass *class;
506 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
508 class = G_OUTPUT_STREAM_GET_CLASS (stream);
510 if (stream->priv->closed)
513 if (!g_output_stream_set_pending (stream, error))
517 g_cancellable_push_current (cancellable);
520 res = class->flush (stream, cancellable, error);
526 /* flushing caused the error that we want to return,
527 * but we still want to close the underlying stream if possible
530 class->close_fn (stream, cancellable, NULL);
536 res = class->close_fn (stream, cancellable, error);
540 g_cancellable_pop_current (cancellable);
542 stream->priv->closed = TRUE;
543 g_output_stream_clear_pending (stream);
549 async_ready_callback_wrapper (GObject *source_object,
553 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
555 g_output_stream_clear_pending (stream);
556 if (stream->priv->outstanding_callback)
557 (*stream->priv->outstanding_callback) (source_object, res, user_data);
558 g_object_unref (stream);
562 async_ready_close_callback_wrapper (GObject *source_object,
566 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
568 stream->priv->closed = TRUE;
569 g_output_stream_clear_pending (stream);
570 if (stream->priv->outstanding_callback)
571 (*stream->priv->outstanding_callback) (source_object, res, user_data);
572 g_object_unref (stream);
576 * g_output_stream_write_async:
577 * @stream: A #GOutputStream.
578 * @buffer: the buffer containing the data to write.
579 * @count: the number of bytes to write
580 * @io_priority: the io priority of the request.
581 * @cancellable: optional #GCancellable object, %NULL to ignore.
582 * @callback: callback to call when the request is satisfied
583 * @user_data: the data to pass to callback function
585 * Request an asynchronous write of @count bytes from @buffer into
586 * the stream. When the operation is finished @callback will be called.
587 * You can then call g_output_stream_write_finish() to get the result of the
590 * During an async request no other sync and async calls are allowed,
591 * and will result in %G_IO_ERROR_PENDING errors.
593 * A value of @count larger than %G_MAXSSIZE will cause a
594 * %G_IO_ERROR_INVALID_ARGUMENT error.
596 * On success, the number of bytes written will be passed to the
597 * @callback. It is not an error if this is not the same as the
598 * requested size, as it can happen e.g. on a partial I/O error,
599 * but generally we try to write as many bytes as requested.
601 * Any outstanding I/O request with higher priority (lower numerical
602 * value) will be executed before an outstanding request with lower
603 * priority. Default priority is %G_PRIORITY_DEFAULT.
605 * The asyncronous methods have a default fallback that uses threads
606 * to implement asynchronicity, so they are optional for inheriting
607 * classes. However, if you override one you must override all.
609 * For the synchronous, blocking version of this function, see
610 * g_output_stream_write().
613 g_output_stream_write_async (GOutputStream *stream,
617 GCancellable *cancellable,
618 GAsyncReadyCallback callback,
621 GOutputStreamClass *class;
622 GSimpleAsyncResult *simple;
623 GError *error = NULL;
625 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
626 g_return_if_fail (buffer != NULL);
630 simple = g_simple_async_result_new (G_OBJECT (stream),
633 g_output_stream_write_async);
634 g_simple_async_result_complete_in_idle (simple);
635 g_object_unref (simple);
639 if (((gssize) count) < 0)
641 g_simple_async_report_error_in_idle (G_OBJECT (stream),
644 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
645 _("Too large count value passed to %s"),
650 if (!g_output_stream_set_pending (stream, &error))
652 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
656 g_error_free (error);
660 class = G_OUTPUT_STREAM_GET_CLASS (stream);
662 stream->priv->outstanding_callback = callback;
663 g_object_ref (stream);
664 class->write_async (stream, buffer, count, io_priority, cancellable,
665 async_ready_callback_wrapper, user_data);
669 * g_output_stream_write_finish:
670 * @stream: a #GOutputStream.
671 * @result: a #GAsyncResult.
672 * @error: a #GError location to store the error occuring, or %NULL to
675 * Finishes a stream write operation.
677 * Returns: a #gssize containing the number of bytes written to the stream.
680 g_output_stream_write_finish (GOutputStream *stream,
681 GAsyncResult *result,
684 GSimpleAsyncResult *simple;
685 GOutputStreamClass *class;
687 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
688 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
690 if (G_IS_SIMPLE_ASYNC_RESULT (result))
692 simple = G_SIMPLE_ASYNC_RESULT (result);
693 if (g_simple_async_result_propagate_error (simple, error))
696 /* Special case writes of 0 bytes */
697 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
701 class = G_OUTPUT_STREAM_GET_CLASS (stream);
702 return class->write_finish (stream, result, error);
706 GInputStream *source;
708 GAsyncReadyCallback callback;
712 async_ready_splice_callback_wrapper (GObject *source_object,
716 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
717 SpliceUserData *data = _data;
719 g_output_stream_clear_pending (stream);
722 (*data->callback) (source_object, res, data->user_data);
724 g_object_unref (stream);
725 g_object_unref (data->source);
730 * g_output_stream_splice_async:
731 * @stream: a #GOutputStream.
732 * @source: a #GInputStream.
733 * @flags: a set of #GOutputStreamSpliceFlags.
734 * @io_priority: the io priority of the request.
735 * @cancellable: optional #GCancellable object, %NULL to ignore.
736 * @callback: a #GAsyncReadyCallback.
737 * @user_data: user data passed to @callback.
739 * Splices a stream asynchronously.
740 * When the operation is finished @callback will be called.
741 * You can then call g_output_stream_splice_finish() to get the
742 * result of the operation.
744 * For the synchronous, blocking version of this function, see
745 * g_output_stream_splice().
748 g_output_stream_splice_async (GOutputStream *stream,
749 GInputStream *source,
750 GOutputStreamSpliceFlags flags,
752 GCancellable *cancellable,
753 GAsyncReadyCallback callback,
756 GOutputStreamClass *class;
757 SpliceUserData *data;
758 GError *error = NULL;
760 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
761 g_return_if_fail (G_IS_INPUT_STREAM (source));
763 if (g_input_stream_is_closed (source))
765 g_simple_async_report_error_in_idle (G_OBJECT (stream),
768 G_IO_ERROR, G_IO_ERROR_CLOSED,
769 _("Source stream is already closed"));
773 if (!g_output_stream_set_pending (stream, &error))
775 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
779 g_error_free (error);
783 class = G_OUTPUT_STREAM_GET_CLASS (stream);
785 data = g_new0 (SpliceUserData, 1);
786 data->callback = callback;
787 data->user_data = user_data;
788 data->source = g_object_ref (source);
790 g_object_ref (stream);
791 class->splice_async (stream, source, flags, io_priority, cancellable,
792 async_ready_splice_callback_wrapper, data);
796 * g_output_stream_splice_finish:
797 * @stream: a #GOutputStream.
798 * @result: a #GAsyncResult.
799 * @error: a #GError location to store the error occuring, or %NULL to
802 * Finishes an asynchronous stream splice operation.
804 * Returns: a #gssize of the number of bytes spliced.
807 g_output_stream_splice_finish (GOutputStream *stream,
808 GAsyncResult *result,
811 GSimpleAsyncResult *simple;
812 GOutputStreamClass *class;
814 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
815 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
817 if (G_IS_SIMPLE_ASYNC_RESULT (result))
819 simple = G_SIMPLE_ASYNC_RESULT (result);
820 if (g_simple_async_result_propagate_error (simple, error))
824 class = G_OUTPUT_STREAM_GET_CLASS (stream);
825 return class->splice_finish (stream, result, error);
829 * g_output_stream_flush_async:
830 * @stream: a #GOutputStream.
831 * @io_priority: the io priority of the request.
832 * @cancellable: optional #GCancellable object, %NULL to ignore.
833 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
834 * @user_data: the data to pass to callback function
836 * Flushes a stream asynchronously.
837 * For behaviour details see g_output_stream_flush().
839 * When the operation is finished @callback will be
840 * called. You can then call g_output_stream_flush_finish() to get the
841 * result of the operation.
844 g_output_stream_flush_async (GOutputStream *stream,
846 GCancellable *cancellable,
847 GAsyncReadyCallback callback,
850 GOutputStreamClass *class;
851 GSimpleAsyncResult *simple;
852 GError *error = NULL;
854 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
856 if (!g_output_stream_set_pending (stream, &error))
858 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
862 g_error_free (error);
866 stream->priv->outstanding_callback = callback;
867 g_object_ref (stream);
869 class = G_OUTPUT_STREAM_GET_CLASS (stream);
871 if (class->flush_async == NULL)
873 simple = g_simple_async_result_new (G_OBJECT (stream),
874 async_ready_callback_wrapper,
876 g_output_stream_flush_async);
877 g_simple_async_result_complete_in_idle (simple);
878 g_object_unref (simple);
882 class->flush_async (stream, io_priority, cancellable,
883 async_ready_callback_wrapper, user_data);
887 * g_output_stream_flush_finish:
888 * @stream: a #GOutputStream.
889 * @result: a GAsyncResult.
890 * @error: a #GError location to store the error occuring, or %NULL to
893 * Finishes flushing an output stream.
895 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
898 g_output_stream_flush_finish (GOutputStream *stream,
899 GAsyncResult *result,
902 GSimpleAsyncResult *simple;
903 GOutputStreamClass *klass;
905 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
906 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
908 if (G_IS_SIMPLE_ASYNC_RESULT (result))
910 simple = G_SIMPLE_ASYNC_RESULT (result);
911 if (g_simple_async_result_propagate_error (simple, error))
914 /* Special case default implementation */
915 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
919 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
920 return klass->flush_finish (stream, result, error);
925 * g_output_stream_close_async:
926 * @stream: A #GOutputStream.
927 * @io_priority: the io priority of the request.
928 * @callback: callback to call when the request is satisfied
929 * @user_data: the data to pass to callback function
930 * @cancellable: optional cancellable object
932 * Requests an asynchronous close of the stream, releasing resources
933 * related to it. When the operation is finished @callback will be
934 * called. You can then call g_output_stream_close_finish() to get
935 * the result of the operation.
937 * For behaviour details see g_output_stream_close().
939 * The asyncronous methods have a default fallback that uses threads
940 * to implement asynchronicity, so they are optional for inheriting
941 * classes. However, if you override one you must override all.
944 g_output_stream_close_async (GOutputStream *stream,
946 GCancellable *cancellable,
947 GAsyncReadyCallback callback,
950 GOutputStreamClass *class;
951 GSimpleAsyncResult *simple;
952 GError *error = NULL;
954 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
956 if (stream->priv->closed)
958 simple = g_simple_async_result_new (G_OBJECT (stream),
961 g_output_stream_close_async);
962 g_simple_async_result_complete_in_idle (simple);
963 g_object_unref (simple);
967 if (!g_output_stream_set_pending (stream, &error))
969 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
973 g_error_free (error);
977 class = G_OUTPUT_STREAM_GET_CLASS (stream);
978 stream->priv->outstanding_callback = callback;
979 g_object_ref (stream);
980 class->close_async (stream, io_priority, cancellable,
981 async_ready_close_callback_wrapper, user_data);
985 * g_output_stream_close_finish:
986 * @stream: a #GOutputStream.
987 * @result: a #GAsyncResult.
988 * @error: a #GError location to store the error occuring, or %NULL to
991 * Closes an output stream.
993 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
996 g_output_stream_close_finish (GOutputStream *stream,
997 GAsyncResult *result,
1000 GSimpleAsyncResult *simple;
1001 GOutputStreamClass *class;
1003 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1004 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1006 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1008 simple = G_SIMPLE_ASYNC_RESULT (result);
1009 if (g_simple_async_result_propagate_error (simple, error))
1012 /* Special case already closed */
1013 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1017 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1018 return class->close_finish (stream, result, error);
1022 * g_output_stream_is_closed:
1023 * @stream: a #GOutputStream.
1025 * Checks if an output stream has already been closed.
1027 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1030 g_output_stream_is_closed (GOutputStream *stream)
1032 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1034 return stream->priv->closed;
1038 * g_output_stream_has_pending:
1039 * @stream: a #GOutputStream.
1041 * Checks if an ouput stream has pending actions.
1043 * Returns: %TRUE if @stream has pending actions.
1046 g_output_stream_has_pending (GOutputStream *stream)
1048 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1050 return stream->priv->pending;
1054 * g_output_stream_set_pending:
1055 * @stream: a #GOutputStream.
1056 * @error: a #GError location to store the error occuring, or %NULL to
1059 * Sets @stream to have actions pending. If the pending flag is
1060 * already set or @stream is closed, it will return %FALSE and set
1063 * Return value: %TRUE if pending was previously unset and is now set.
1066 g_output_stream_set_pending (GOutputStream *stream,
1069 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1071 if (stream->priv->closed)
1073 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1074 _("Stream is already closed"));
1078 if (stream->priv->pending)
1080 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1081 /* Translators: This is an error you get if there is
1082 * already an operation running against this stream when
1083 * you try to start one */
1084 _("Stream has outstanding operation"));
1088 stream->priv->pending = TRUE;
1093 * g_output_stream_clear_pending:
1094 * @stream: output stream
1096 * Clears the pending flag on @stream.
1099 g_output_stream_clear_pending (GOutputStream *stream)
1101 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1103 stream->priv->pending = FALSE;
1107 /********************************************
1108 * Default implementation of async ops *
1109 ********************************************/
1113 gsize count_requested;
1114 gssize count_written;
1118 write_async_thread (GSimpleAsyncResult *res,
1120 GCancellable *cancellable)
1123 GOutputStreamClass *class;
1124 GError *error = NULL;
1126 class = G_OUTPUT_STREAM_GET_CLASS (object);
1127 op = g_simple_async_result_get_op_res_gpointer (res);
1128 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1129 cancellable, &error);
1130 if (op->count_written == -1)
1132 g_simple_async_result_set_from_error (res, error);
1133 g_error_free (error);
1138 g_output_stream_real_write_async (GOutputStream *stream,
1142 GCancellable *cancellable,
1143 GAsyncReadyCallback callback,
1146 GSimpleAsyncResult *res;
1149 op = g_new0 (WriteData, 1);
1150 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1151 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1152 op->buffer = buffer;
1153 op->count_requested = count;
1155 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1156 g_object_unref (res);
1160 g_output_stream_real_write_finish (GOutputStream *stream,
1161 GAsyncResult *result,
1164 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1167 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1168 op = g_simple_async_result_get_op_res_gpointer (simple);
1169 return op->count_written;
1173 GInputStream *source;
1174 GOutputStreamSpliceFlags flags;
1175 gssize bytes_copied;
1179 splice_async_thread (GSimpleAsyncResult *result,
1181 GCancellable *cancellable)
1184 GOutputStreamClass *class;
1185 GError *error = NULL;
1186 GOutputStream *stream;
1188 stream = G_OUTPUT_STREAM (object);
1189 class = G_OUTPUT_STREAM_GET_CLASS (object);
1190 op = g_simple_async_result_get_op_res_gpointer (result);
1192 op->bytes_copied = class->splice (stream,
1197 if (op->bytes_copied == -1)
1199 g_simple_async_result_set_from_error (result, error);
1200 g_error_free (error);
1205 g_output_stream_real_splice_async (GOutputStream *stream,
1206 GInputStream *source,
1207 GOutputStreamSpliceFlags flags,
1209 GCancellable *cancellable,
1210 GAsyncReadyCallback callback,
1213 GSimpleAsyncResult *res;
1216 op = g_new0 (SpliceData, 1);
1217 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1218 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1220 op->source = source;
1222 /* TODO: In the case where both source and destintion have
1223 non-threadbased async calls we can use a true async copy here */
1225 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1226 g_object_unref (res);
1230 g_output_stream_real_splice_finish (GOutputStream *stream,
1231 GAsyncResult *result,
1234 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1237 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1238 op = g_simple_async_result_get_op_res_gpointer (simple);
1239 return op->bytes_copied;
1244 flush_async_thread (GSimpleAsyncResult *res,
1246 GCancellable *cancellable)
1248 GOutputStreamClass *class;
1250 GError *error = NULL;
1252 class = G_OUTPUT_STREAM_GET_CLASS (object);
1255 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1259 g_simple_async_result_set_from_error (res, error);
1260 g_error_free (error);
1265 g_output_stream_real_flush_async (GOutputStream *stream,
1267 GCancellable *cancellable,
1268 GAsyncReadyCallback callback,
1271 GSimpleAsyncResult *res;
1273 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1275 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1276 g_object_unref (res);
1280 g_output_stream_real_flush_finish (GOutputStream *stream,
1281 GAsyncResult *result,
1288 close_async_thread (GSimpleAsyncResult *res,
1290 GCancellable *cancellable)
1292 GOutputStreamClass *class;
1293 GError *error = NULL;
1296 /* Auto handling of cancelation disabled, and ignore
1297 cancellation, since we want to close things anyway, although
1298 possibly in a quick-n-dirty way. At least we never want to leak
1301 class = G_OUTPUT_STREAM_GET_CLASS (object);
1302 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1305 g_simple_async_result_set_from_error (res, error);
1306 g_error_free (error);
1311 g_output_stream_real_close_async (GOutputStream *stream,
1313 GCancellable *cancellable,
1314 GAsyncReadyCallback callback,
1317 GSimpleAsyncResult *res;
1319 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1321 g_simple_async_result_set_handle_cancellation (res, FALSE);
1323 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1324 g_object_unref (res);
1328 g_output_stream_real_close_finish (GOutputStream *stream,
1329 GAsyncResult *result,
1332 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1333 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1337 #define __G_OUTPUT_STREAM_C__
1338 #include "gioaliasdef.c"