1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "goutputstream.h"
25 #include "gcancellable.h"
26 #include "gasyncresult.h"
27 #include "gsimpleasyncresult.h"
28 #include "ginputstream.h"
35 * SECTION:goutputstream
36 * @short_description: Base class for implementing streaming output
39 * GOutputStream has functions to write to a stream (g_output_stream_write()),
40 * to close a stream (g_output_stream_close()) and to flush pending writes
41 * (g_output_stream_flush()).
43 * To copy the content of an input stream to an output stream without
44 * manually handling the reads and writes, use g_output_stream_splice().
46 * All of these functions have async variants too.
49 G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
51 struct _GOutputStreamPrivate {
55 GAsyncReadyCallback outstanding_callback;
58 static gssize g_output_stream_real_splice (GOutputStream *stream,
60 GOutputStreamSpliceFlags flags,
61 GCancellable *cancellable,
63 static void g_output_stream_real_write_async (GOutputStream *stream,
67 GCancellable *cancellable,
68 GAsyncReadyCallback callback,
70 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
73 static void g_output_stream_real_splice_async (GOutputStream *stream,
75 GOutputStreamSpliceFlags flags,
77 GCancellable *cancellable,
78 GAsyncReadyCallback callback,
80 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
83 static void g_output_stream_real_flush_async (GOutputStream *stream,
85 GCancellable *cancellable,
86 GAsyncReadyCallback callback,
88 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
91 static void g_output_stream_real_close_async (GOutputStream *stream,
93 GCancellable *cancellable,
94 GAsyncReadyCallback callback,
96 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
101 g_output_stream_finalize (GObject *object)
103 G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
107 g_output_stream_dispose (GObject *object)
109 GOutputStream *stream;
111 stream = G_OUTPUT_STREAM (object);
113 if (!stream->priv->closed)
114 g_output_stream_close (stream, NULL, NULL);
116 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
120 g_output_stream_class_init (GOutputStreamClass *klass)
122 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
124 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
126 gobject_class->finalize = g_output_stream_finalize;
127 gobject_class->dispose = g_output_stream_dispose;
129 klass->splice = g_output_stream_real_splice;
131 klass->write_async = g_output_stream_real_write_async;
132 klass->write_finish = g_output_stream_real_write_finish;
133 klass->splice_async = g_output_stream_real_splice_async;
134 klass->splice_finish = g_output_stream_real_splice_finish;
135 klass->flush_async = g_output_stream_real_flush_async;
136 klass->flush_finish = g_output_stream_real_flush_finish;
137 klass->close_async = g_output_stream_real_close_async;
138 klass->close_finish = g_output_stream_real_close_finish;
142 g_output_stream_init (GOutputStream *stream)
144 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
145 G_TYPE_OUTPUT_STREAM,
146 GOutputStreamPrivate);
150 * g_output_stream_write:
151 * @stream: a #GOutputStream.
152 * @buffer: the buffer containing the data to write.
153 * @count: the number of bytes to write
154 * @cancellable: optional cancellable object
155 * @error: location to store the error occuring, or %NULL to ignore
157 * Tries to write @count bytes from @buffer into the stream. Will block
158 * during the operation.
160 * If count is zero returns zero and does nothing. A value of @count
161 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
163 * On success, the number of bytes written to the stream is returned.
164 * It is not an error if this is not the same as the requested size, as it
165 * can happen e.g. on a partial i/o error, or if there is not enough
166 * storage in the stream. All writes either block until at least one byte
167 * is written, so zero is never returned (unless @count is zero).
169 * If @cancellable is not NULL, then the operation can be cancelled by
170 * triggering the cancellable object from another thread. If the operation
171 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
172 * operation was partially finished when the operation was cancelled the
173 * partial result will be returned, without an error.
175 * On error -1 is returned and @error is set accordingly.
177 * Return value: Number of bytes written, or -1 on error
180 g_output_stream_write (GOutputStream *stream,
183 GCancellable *cancellable,
186 GOutputStreamClass *class;
189 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
190 g_return_val_if_fail (buffer != NULL, 0);
195 if (((gssize) count) < 0)
197 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
198 _("Too large count value passed to %s"), G_STRFUNC);
202 class = G_OUTPUT_STREAM_GET_CLASS (stream);
204 if (class->write_fn == NULL)
206 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
207 _("Output stream doesn't implement write"));
211 if (!g_output_stream_set_pending (stream, error))
215 g_cancellable_push_current (cancellable);
217 res = class->write_fn (stream, buffer, count, cancellable, error);
220 g_cancellable_pop_current (cancellable);
222 g_output_stream_clear_pending (stream);
228 * g_output_stream_write_all:
229 * @stream: a #GOutputStream.
230 * @buffer: the buffer containing the data to write.
231 * @count: the number of bytes to write
232 * @bytes_written: location to store the number of bytes that was
233 * written to the stream
234 * @cancellable: optional #GCancellable object, %NULL to ignore.
235 * @error: location to store the error occuring, or %NULL to ignore
237 * Tries to write @count bytes from @buffer into the stream. Will block
238 * during the operation.
240 * This function is similar to g_output_stream_write(), except it tries to
241 * write as many bytes as requested, only stopping on an error.
243 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
246 * If there is an error during the operation FALSE is returned and @error
247 * is set to indicate the error status, @bytes_written is updated to contain
248 * the number of bytes written into the stream before the error occurred.
250 * Return value: %TRUE on success, %FALSE if there was an error
253 g_output_stream_write_all (GOutputStream *stream,
256 gsize *bytes_written,
257 GCancellable *cancellable,
260 gsize _bytes_written;
263 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
264 g_return_val_if_fail (buffer != NULL, FALSE);
267 while (_bytes_written < count)
269 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
274 *bytes_written = _bytes_written;
279 g_warning ("Write returned zero without error");
281 _bytes_written += res;
285 *bytes_written = _bytes_written;
291 * g_output_stream_flush:
292 * @stream: a #GOutputStream.
293 * @cancellable: optional cancellable object
294 * @error: location to store the error occuring, or %NULL to ignore
296 * Flushed any outstanding buffers in the stream. Will block during
297 * the operation. Closing the stream will implicitly cause a flush.
299 * This function is optional for inherited classes.
301 * If @cancellable is not %NULL, then the operation can be cancelled by
302 * triggering the cancellable object from another thread. If the operation
303 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
305 * Return value: %TRUE on success, %FALSE on error
308 g_output_stream_flush (GOutputStream *stream,
309 GCancellable *cancellable,
312 GOutputStreamClass *class;
315 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
317 if (!g_output_stream_set_pending (stream, error))
320 class = G_OUTPUT_STREAM_GET_CLASS (stream);
326 g_cancellable_push_current (cancellable);
328 res = class->flush (stream, cancellable, error);
331 g_cancellable_pop_current (cancellable);
334 g_output_stream_clear_pending (stream);
340 * g_output_stream_splice:
341 * @stream: a #GOutputStream.
342 * @source: a #GInputStream.
343 * @flags: a set of #GOutputStreamSpliceFlags.
344 * @cancellable: optional #GCancellable object, %NULL to ignore.
345 * @error: a #GError location to store the error occuring, or %NULL to
348 * Splices an input stream into an output stream.
350 * Returns: a #gssize containing the size of the data spliced, or
351 * -1 if an error occurred.
354 g_output_stream_splice (GOutputStream *stream,
355 GInputStream *source,
356 GOutputStreamSpliceFlags flags,
357 GCancellable *cancellable,
360 GOutputStreamClass *class;
363 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
364 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
366 if (g_input_stream_is_closed (source))
368 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
369 _("Source stream is already closed"));
373 if (!g_output_stream_set_pending (stream, error))
376 class = G_OUTPUT_STREAM_GET_CLASS (stream);
379 g_cancellable_push_current (cancellable);
381 bytes_copied = class->splice (stream, source, flags, cancellable, error);
384 g_cancellable_pop_current (cancellable);
386 g_output_stream_clear_pending (stream);
392 g_output_stream_real_splice (GOutputStream *stream,
393 GInputStream *source,
394 GOutputStreamSpliceFlags flags,
395 GCancellable *cancellable,
398 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
399 gssize n_read, n_written;
401 char buffer[8192], *p;
405 if (class->write_fn == NULL)
407 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
408 _("Output stream doesn't implement write"));
416 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
429 n_written = class->write_fn (stream, p, n_read, cancellable, error);
438 bytes_copied += n_written;
445 error = NULL; /* Ignore further errors */
447 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
449 /* Don't care about errors in source here */
450 g_input_stream_close (source, cancellable, NULL);
453 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
455 /* But write errors on close are bad! */
456 if (class->close_fn &&
457 !class->close_fn (stream, cancellable, error))
469 * g_output_stream_close:
470 * @stream: A #GOutputStream.
471 * @cancellable: optional cancellable object
472 * @error: location to store the error occuring, or %NULL to ignore
474 * Closes the stream, releasing resources related to it.
476 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
477 * Closing a stream multiple times will not return an error.
479 * Closing a stream will automatically flush any outstanding buffers in the
482 * Streams will be automatically closed when the last reference
483 * is dropped, but you might want to call this function to make sure
484 * resources are released as early as possible.
486 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
487 * open after the stream is closed. See the documentation for the individual
488 * stream for details.
490 * On failure the first error that happened will be reported, but the close
491 * operation will finish as much as possible. A stream that failed to
492 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
493 * is important to check and report the error to the user, otherwise
494 * there might be a loss of data as all data might not be written.
496 * If @cancellable is not NULL, then the operation can be cancelled by
497 * triggering the cancellable object from another thread. If the operation
498 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
499 * Cancelling a close will still leave the stream closed, but there some streams
500 * can use a faster close that doesn't block to e.g. check errors. On
501 * cancellation (as with any error) there is no guarantee that all written
502 * data will reach the target.
504 * Return value: %TRUE on success, %FALSE on failure
507 g_output_stream_close (GOutputStream *stream,
508 GCancellable *cancellable,
511 GOutputStreamClass *class;
514 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
516 class = G_OUTPUT_STREAM_GET_CLASS (stream);
518 if (stream->priv->closed)
521 if (!g_output_stream_set_pending (stream, error))
524 stream->priv->closing = TRUE;
527 g_cancellable_push_current (cancellable);
530 res = class->flush (stream, cancellable, error);
536 /* flushing caused the error that we want to return,
537 * but we still want to close the underlying stream if possible
540 class->close_fn (stream, cancellable, NULL);
546 res = class->close_fn (stream, cancellable, error);
550 g_cancellable_pop_current (cancellable);
552 stream->priv->closing = FALSE;
553 stream->priv->closed = TRUE;
554 g_output_stream_clear_pending (stream);
560 async_ready_callback_wrapper (GObject *source_object,
564 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
566 g_output_stream_clear_pending (stream);
567 if (stream->priv->outstanding_callback)
568 (*stream->priv->outstanding_callback) (source_object, res, user_data);
569 g_object_unref (stream);
573 async_ready_close_callback_wrapper (GObject *source_object,
577 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
579 stream->priv->closing = FALSE;
580 stream->priv->closed = TRUE;
581 g_output_stream_clear_pending (stream);
582 if (stream->priv->outstanding_callback)
583 (*stream->priv->outstanding_callback) (source_object, res, user_data);
584 g_object_unref (stream);
588 * g_output_stream_write_async:
589 * @stream: A #GOutputStream.
590 * @buffer: the buffer containing the data to write.
591 * @count: the number of bytes to write
592 * @io_priority: the io priority of the request.
593 * @cancellable: optional #GCancellable object, %NULL to ignore.
594 * @callback: callback to call when the request is satisfied
595 * @user_data: the data to pass to callback function
597 * Request an asynchronous write of @count bytes from @buffer into
598 * the stream. When the operation is finished @callback will be called.
599 * You can then call g_output_stream_write_finish() to get the result of the
602 * During an async request no other sync and async calls are allowed,
603 * and will result in %G_IO_ERROR_PENDING errors.
605 * A value of @count larger than %G_MAXSSIZE will cause a
606 * %G_IO_ERROR_INVALID_ARGUMENT error.
608 * On success, the number of bytes written will be passed to the
609 * @callback. It is not an error if this is not the same as the
610 * requested size, as it can happen e.g. on a partial I/O error,
611 * but generally we try to write as many bytes as requested.
613 * Any outstanding I/O request with higher priority (lower numerical
614 * value) will be executed before an outstanding request with lower
615 * priority. Default priority is %G_PRIORITY_DEFAULT.
617 * The asyncronous methods have a default fallback that uses threads
618 * to implement asynchronicity, so they are optional for inheriting
619 * classes. However, if you override one you must override all.
621 * For the synchronous, blocking version of this function, see
622 * g_output_stream_write().
625 g_output_stream_write_async (GOutputStream *stream,
629 GCancellable *cancellable,
630 GAsyncReadyCallback callback,
633 GOutputStreamClass *class;
634 GSimpleAsyncResult *simple;
635 GError *error = NULL;
637 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
638 g_return_if_fail (buffer != NULL);
642 simple = g_simple_async_result_new (G_OBJECT (stream),
645 g_output_stream_write_async);
646 g_simple_async_result_complete_in_idle (simple);
647 g_object_unref (simple);
651 if (((gssize) count) < 0)
653 g_simple_async_report_error_in_idle (G_OBJECT (stream),
656 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
657 _("Too large count value passed to %s"),
662 if (!g_output_stream_set_pending (stream, &error))
664 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
668 g_error_free (error);
672 class = G_OUTPUT_STREAM_GET_CLASS (stream);
674 stream->priv->outstanding_callback = callback;
675 g_object_ref (stream);
676 class->write_async (stream, buffer, count, io_priority, cancellable,
677 async_ready_callback_wrapper, user_data);
681 * g_output_stream_write_finish:
682 * @stream: a #GOutputStream.
683 * @result: a #GAsyncResult.
684 * @error: a #GError location to store the error occuring, or %NULL to
687 * Finishes a stream write operation.
689 * Returns: a #gssize containing the number of bytes written to the stream.
692 g_output_stream_write_finish (GOutputStream *stream,
693 GAsyncResult *result,
696 GSimpleAsyncResult *simple;
697 GOutputStreamClass *class;
699 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
700 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
702 if (G_IS_SIMPLE_ASYNC_RESULT (result))
704 simple = G_SIMPLE_ASYNC_RESULT (result);
705 if (g_simple_async_result_propagate_error (simple, error))
708 /* Special case writes of 0 bytes */
709 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
713 class = G_OUTPUT_STREAM_GET_CLASS (stream);
714 return class->write_finish (stream, result, error);
718 GInputStream *source;
720 GAsyncReadyCallback callback;
724 async_ready_splice_callback_wrapper (GObject *source_object,
728 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
729 SpliceUserData *data = _data;
731 g_output_stream_clear_pending (stream);
734 (*data->callback) (source_object, res, data->user_data);
736 g_object_unref (stream);
737 g_object_unref (data->source);
742 * g_output_stream_splice_async:
743 * @stream: a #GOutputStream.
744 * @source: a #GInputStream.
745 * @flags: a set of #GOutputStreamSpliceFlags.
746 * @io_priority: the io priority of the request.
747 * @cancellable: optional #GCancellable object, %NULL to ignore.
748 * @callback: a #GAsyncReadyCallback.
749 * @user_data: user data passed to @callback.
751 * Splices a stream asynchronously.
752 * When the operation is finished @callback will be called.
753 * You can then call g_output_stream_splice_finish() to get the
754 * result of the operation.
756 * For the synchronous, blocking version of this function, see
757 * g_output_stream_splice().
760 g_output_stream_splice_async (GOutputStream *stream,
761 GInputStream *source,
762 GOutputStreamSpliceFlags flags,
764 GCancellable *cancellable,
765 GAsyncReadyCallback callback,
768 GOutputStreamClass *class;
769 SpliceUserData *data;
770 GError *error = NULL;
772 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
773 g_return_if_fail (G_IS_INPUT_STREAM (source));
775 if (g_input_stream_is_closed (source))
777 g_simple_async_report_error_in_idle (G_OBJECT (stream),
780 G_IO_ERROR, G_IO_ERROR_CLOSED,
781 _("Source stream is already closed"));
785 if (!g_output_stream_set_pending (stream, &error))
787 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
791 g_error_free (error);
795 class = G_OUTPUT_STREAM_GET_CLASS (stream);
797 data = g_new0 (SpliceUserData, 1);
798 data->callback = callback;
799 data->user_data = user_data;
800 data->source = g_object_ref (source);
802 g_object_ref (stream);
803 class->splice_async (stream, source, flags, io_priority, cancellable,
804 async_ready_splice_callback_wrapper, data);
808 * g_output_stream_splice_finish:
809 * @stream: a #GOutputStream.
810 * @result: a #GAsyncResult.
811 * @error: a #GError location to store the error occuring, or %NULL to
814 * Finishes an asynchronous stream splice operation.
816 * Returns: a #gssize of the number of bytes spliced.
819 g_output_stream_splice_finish (GOutputStream *stream,
820 GAsyncResult *result,
823 GSimpleAsyncResult *simple;
824 GOutputStreamClass *class;
826 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
827 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
829 if (G_IS_SIMPLE_ASYNC_RESULT (result))
831 simple = G_SIMPLE_ASYNC_RESULT (result);
832 if (g_simple_async_result_propagate_error (simple, error))
836 class = G_OUTPUT_STREAM_GET_CLASS (stream);
837 return class->splice_finish (stream, result, error);
841 * g_output_stream_flush_async:
842 * @stream: a #GOutputStream.
843 * @io_priority: the io priority of the request.
844 * @cancellable: optional #GCancellable object, %NULL to ignore.
845 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
846 * @user_data: the data to pass to callback function
848 * Flushes a stream asynchronously.
849 * For behaviour details see g_output_stream_flush().
851 * When the operation is finished @callback will be
852 * called. You can then call g_output_stream_flush_finish() to get the
853 * result of the operation.
856 g_output_stream_flush_async (GOutputStream *stream,
858 GCancellable *cancellable,
859 GAsyncReadyCallback callback,
862 GOutputStreamClass *class;
863 GSimpleAsyncResult *simple;
864 GError *error = NULL;
866 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
868 if (!g_output_stream_set_pending (stream, &error))
870 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
874 g_error_free (error);
878 stream->priv->outstanding_callback = callback;
879 g_object_ref (stream);
881 class = G_OUTPUT_STREAM_GET_CLASS (stream);
883 if (class->flush_async == NULL)
885 simple = g_simple_async_result_new (G_OBJECT (stream),
886 async_ready_callback_wrapper,
888 g_output_stream_flush_async);
889 g_simple_async_result_complete_in_idle (simple);
890 g_object_unref (simple);
894 class->flush_async (stream, io_priority, cancellable,
895 async_ready_callback_wrapper, user_data);
899 * g_output_stream_flush_finish:
900 * @stream: a #GOutputStream.
901 * @result: a GAsyncResult.
902 * @error: a #GError location to store the error occuring, or %NULL to
905 * Finishes flushing an output stream.
907 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
910 g_output_stream_flush_finish (GOutputStream *stream,
911 GAsyncResult *result,
914 GSimpleAsyncResult *simple;
915 GOutputStreamClass *klass;
917 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
918 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
920 if (G_IS_SIMPLE_ASYNC_RESULT (result))
922 simple = G_SIMPLE_ASYNC_RESULT (result);
923 if (g_simple_async_result_propagate_error (simple, error))
926 /* Special case default implementation */
927 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
931 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
932 return klass->flush_finish (stream, result, error);
937 * g_output_stream_close_async:
938 * @stream: A #GOutputStream.
939 * @io_priority: the io priority of the request.
940 * @callback: callback to call when the request is satisfied
941 * @user_data: the data to pass to callback function
942 * @cancellable: optional cancellable object
944 * Requests an asynchronous close of the stream, releasing resources
945 * related to it. When the operation is finished @callback will be
946 * called. You can then call g_output_stream_close_finish() to get
947 * the result of the operation.
949 * For behaviour details see g_output_stream_close().
951 * The asyncronous methods have a default fallback that uses threads
952 * to implement asynchronicity, so they are optional for inheriting
953 * classes. However, if you override one you must override all.
956 g_output_stream_close_async (GOutputStream *stream,
958 GCancellable *cancellable,
959 GAsyncReadyCallback callback,
962 GOutputStreamClass *class;
963 GSimpleAsyncResult *simple;
964 GError *error = NULL;
966 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
968 if (stream->priv->closed)
970 simple = g_simple_async_result_new (G_OBJECT (stream),
973 g_output_stream_close_async);
974 g_simple_async_result_complete_in_idle (simple);
975 g_object_unref (simple);
979 if (!g_output_stream_set_pending (stream, &error))
981 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
985 g_error_free (error);
989 class = G_OUTPUT_STREAM_GET_CLASS (stream);
990 stream->priv->closing = TRUE;
991 stream->priv->outstanding_callback = callback;
992 g_object_ref (stream);
993 class->close_async (stream, io_priority, cancellable,
994 async_ready_close_callback_wrapper, user_data);
998 * g_output_stream_close_finish:
999 * @stream: a #GOutputStream.
1000 * @result: a #GAsyncResult.
1001 * @error: a #GError location to store the error occuring, or %NULL to
1004 * Closes an output stream.
1006 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1009 g_output_stream_close_finish (GOutputStream *stream,
1010 GAsyncResult *result,
1013 GSimpleAsyncResult *simple;
1014 GOutputStreamClass *class;
1016 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1017 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1019 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1021 simple = G_SIMPLE_ASYNC_RESULT (result);
1022 if (g_simple_async_result_propagate_error (simple, error))
1025 /* Special case already closed */
1026 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1030 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1031 return class->close_finish (stream, result, error);
1035 * g_output_stream_is_closed:
1036 * @stream: a #GOutputStream.
1038 * Checks if an output stream has already been closed.
1040 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1043 g_output_stream_is_closed (GOutputStream *stream)
1045 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1047 return stream->priv->closed;
1051 * g_output_stream_is_closing:
1052 * @stream: a #GOutputStream.
1054 * Checks if an output stream is being closed. This can be
1055 * used inside e.g. a flush implementation to see if the
1056 * flush (or other i/o operation) is called from within
1057 * the closing operation.
1059 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1064 g_output_stream_is_closing (GOutputStream *stream)
1066 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1068 return stream->priv->closing;
1072 * g_output_stream_has_pending:
1073 * @stream: a #GOutputStream.
1075 * Checks if an ouput stream has pending actions.
1077 * Returns: %TRUE if @stream has pending actions.
1080 g_output_stream_has_pending (GOutputStream *stream)
1082 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1084 return stream->priv->pending;
1088 * g_output_stream_set_pending:
1089 * @stream: a #GOutputStream.
1090 * @error: a #GError location to store the error occuring, or %NULL to
1093 * Sets @stream to have actions pending. If the pending flag is
1094 * already set or @stream is closed, it will return %FALSE and set
1097 * Return value: %TRUE if pending was previously unset and is now set.
1100 g_output_stream_set_pending (GOutputStream *stream,
1103 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1105 if (stream->priv->closed)
1107 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1108 _("Stream is already closed"));
1112 if (stream->priv->pending)
1114 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1115 /* Translators: This is an error you get if there is
1116 * already an operation running against this stream when
1117 * you try to start one */
1118 _("Stream has outstanding operation"));
1122 stream->priv->pending = TRUE;
1127 * g_output_stream_clear_pending:
1128 * @stream: output stream
1130 * Clears the pending flag on @stream.
1133 g_output_stream_clear_pending (GOutputStream *stream)
1135 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1137 stream->priv->pending = FALSE;
1141 /********************************************
1142 * Default implementation of async ops *
1143 ********************************************/
1147 gsize count_requested;
1148 gssize count_written;
1152 write_async_thread (GSimpleAsyncResult *res,
1154 GCancellable *cancellable)
1157 GOutputStreamClass *class;
1158 GError *error = NULL;
1160 class = G_OUTPUT_STREAM_GET_CLASS (object);
1161 op = g_simple_async_result_get_op_res_gpointer (res);
1162 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1163 cancellable, &error);
1164 if (op->count_written == -1)
1166 g_simple_async_result_set_from_error (res, error);
1167 g_error_free (error);
1172 g_output_stream_real_write_async (GOutputStream *stream,
1176 GCancellable *cancellable,
1177 GAsyncReadyCallback callback,
1180 GSimpleAsyncResult *res;
1183 op = g_new0 (WriteData, 1);
1184 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1185 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1186 op->buffer = buffer;
1187 op->count_requested = count;
1189 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1190 g_object_unref (res);
1194 g_output_stream_real_write_finish (GOutputStream *stream,
1195 GAsyncResult *result,
1198 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1201 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1202 op = g_simple_async_result_get_op_res_gpointer (simple);
1203 return op->count_written;
1207 GInputStream *source;
1208 GOutputStreamSpliceFlags flags;
1209 gssize bytes_copied;
1213 splice_async_thread (GSimpleAsyncResult *result,
1215 GCancellable *cancellable)
1218 GOutputStreamClass *class;
1219 GError *error = NULL;
1220 GOutputStream *stream;
1222 stream = G_OUTPUT_STREAM (object);
1223 class = G_OUTPUT_STREAM_GET_CLASS (object);
1224 op = g_simple_async_result_get_op_res_gpointer (result);
1226 op->bytes_copied = class->splice (stream,
1231 if (op->bytes_copied == -1)
1233 g_simple_async_result_set_from_error (result, error);
1234 g_error_free (error);
1239 g_output_stream_real_splice_async (GOutputStream *stream,
1240 GInputStream *source,
1241 GOutputStreamSpliceFlags flags,
1243 GCancellable *cancellable,
1244 GAsyncReadyCallback callback,
1247 GSimpleAsyncResult *res;
1250 op = g_new0 (SpliceData, 1);
1251 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1252 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1254 op->source = source;
1256 /* TODO: In the case where both source and destintion have
1257 non-threadbased async calls we can use a true async copy here */
1259 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1260 g_object_unref (res);
1264 g_output_stream_real_splice_finish (GOutputStream *stream,
1265 GAsyncResult *result,
1268 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1271 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1272 op = g_simple_async_result_get_op_res_gpointer (simple);
1273 return op->bytes_copied;
1278 flush_async_thread (GSimpleAsyncResult *res,
1280 GCancellable *cancellable)
1282 GOutputStreamClass *class;
1284 GError *error = NULL;
1286 class = G_OUTPUT_STREAM_GET_CLASS (object);
1289 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1293 g_simple_async_result_set_from_error (res, error);
1294 g_error_free (error);
1299 g_output_stream_real_flush_async (GOutputStream *stream,
1301 GCancellable *cancellable,
1302 GAsyncReadyCallback callback,
1305 GSimpleAsyncResult *res;
1307 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1309 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1310 g_object_unref (res);
1314 g_output_stream_real_flush_finish (GOutputStream *stream,
1315 GAsyncResult *result,
1322 close_async_thread (GSimpleAsyncResult *res,
1324 GCancellable *cancellable)
1326 GOutputStreamClass *class;
1327 GError *error = NULL;
1330 /* Auto handling of cancelation disabled, and ignore
1331 cancellation, since we want to close things anyway, although
1332 possibly in a quick-n-dirty way. At least we never want to leak
1335 class = G_OUTPUT_STREAM_GET_CLASS (object);
1336 if (class->close_fn)
1338 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1341 g_simple_async_result_set_from_error (res, error);
1342 g_error_free (error);
1348 g_output_stream_real_close_async (GOutputStream *stream,
1350 GCancellable *cancellable,
1351 GAsyncReadyCallback callback,
1354 GSimpleAsyncResult *res;
1356 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1358 g_simple_async_result_set_handle_cancellation (res, FALSE);
1360 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1361 g_object_unref (res);
1365 g_output_stream_real_close_finish (GOutputStream *stream,
1366 GAsyncResult *result,
1369 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1370 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1374 #define __G_OUTPUT_STREAM_C__
1375 #include "gioaliasdef.c"