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 {
54 GAsyncReadyCallback outstanding_callback;
57 static gssize g_output_stream_real_splice (GOutputStream *stream,
59 GOutputStreamSpliceFlags flags,
60 GCancellable *cancellable,
62 static void g_output_stream_real_write_async (GOutputStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
72 static void g_output_stream_real_splice_async (GOutputStream *stream,
74 GOutputStreamSpliceFlags flags,
76 GCancellable *cancellable,
77 GAsyncReadyCallback callback,
79 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
82 static void g_output_stream_real_flush_async (GOutputStream *stream,
84 GCancellable *cancellable,
85 GAsyncReadyCallback callback,
87 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
90 static void g_output_stream_real_close_async (GOutputStream *stream,
92 GCancellable *cancellable,
93 GAsyncReadyCallback callback,
95 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
100 g_output_stream_finalize (GObject *object)
102 GOutputStream *stream;
104 stream = G_OUTPUT_STREAM (object);
106 G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
110 g_output_stream_dispose (GObject *object)
112 GOutputStream *stream;
114 stream = G_OUTPUT_STREAM (object);
116 if (!stream->priv->closed)
117 g_output_stream_close (stream, NULL, NULL);
119 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
123 g_output_stream_class_init (GOutputStreamClass *klass)
125 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
127 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
129 gobject_class->finalize = g_output_stream_finalize;
130 gobject_class->dispose = g_output_stream_dispose;
132 klass->splice = g_output_stream_real_splice;
134 klass->write_async = g_output_stream_real_write_async;
135 klass->write_finish = g_output_stream_real_write_finish;
136 klass->splice_async = g_output_stream_real_splice_async;
137 klass->splice_finish = g_output_stream_real_splice_finish;
138 klass->flush_async = g_output_stream_real_flush_async;
139 klass->flush_finish = g_output_stream_real_flush_finish;
140 klass->close_async = g_output_stream_real_close_async;
141 klass->close_finish = g_output_stream_real_close_finish;
145 g_output_stream_init (GOutputStream *stream)
147 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
148 G_TYPE_OUTPUT_STREAM,
149 GOutputStreamPrivate);
153 * g_output_stream_write:
154 * @stream: a #GOutputStream.
155 * @buffer: the buffer containing the data to write.
156 * @count: the number of bytes to write
157 * @cancellable: optional cancellable object
158 * @error: location to store the error occuring, or %NULL to ignore
160 * Tries to write @count bytes from @buffer into the stream. Will block
161 * during the operation.
163 * If count is zero returns zero and does nothing. A value of @count
164 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
166 * On success, the number of bytes written to the stream is returned.
167 * It is not an error if this is not the same as the requested size, as it
168 * can happen e.g. on a partial i/o error, or if there is not enough
169 * storage in the stream. All writes either block until at least one byte
170 * is written, so zero is never returned (unless @count is zero).
172 * If @cancellable is not NULL, then the operation can be cancelled by
173 * triggering the cancellable object from another thread. If the operation
174 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
175 * operation was partially finished when the operation was cancelled the
176 * partial result will be returned, without an error.
178 * On error -1 is returned and @error is set accordingly.
180 * Return value: Number of bytes written, or -1 on error
183 g_output_stream_write (GOutputStream *stream,
186 GCancellable *cancellable,
189 GOutputStreamClass *class;
192 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
193 g_return_val_if_fail (buffer != NULL, 0);
198 if (((gssize) count) < 0)
200 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
201 _("Too large count value passed to %s"), G_STRFUNC);
205 class = G_OUTPUT_STREAM_GET_CLASS (stream);
207 if (class->write_fn == NULL)
209 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
210 _("Output stream doesn't implement write"));
214 if (!g_output_stream_set_pending (stream, error))
218 g_cancellable_push_current (cancellable);
220 res = class->write_fn (stream, buffer, count, cancellable, error);
223 g_cancellable_pop_current (cancellable);
225 g_output_stream_clear_pending (stream);
231 * g_output_stream_write_all:
232 * @stream: a #GOutputStream.
233 * @buffer: the buffer containing the data to write.
234 * @count: the number of bytes to write
235 * @bytes_written: location to store the number of bytes that was
236 * 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 occurred.
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;
294 * g_output_stream_flush:
295 * @stream: a #GOutputStream.
296 * @cancellable: optional cancellable object
297 * @error: location to store the error occuring, or %NULL to ignore
299 * Flushed any outstanding buffers in the stream. Will block during
300 * the operation. Closing the stream will implicitly cause a flush.
302 * This function is optional for inherited classes.
304 * If @cancellable is not %NULL, then the operation can be cancelled by
305 * triggering the cancellable object from another thread. If the operation
306 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
308 * Return value: %TRUE on success, %FALSE on error
311 g_output_stream_flush (GOutputStream *stream,
312 GCancellable *cancellable,
315 GOutputStreamClass *class;
318 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
320 if (!g_output_stream_set_pending (stream, error))
323 class = G_OUTPUT_STREAM_GET_CLASS (stream);
329 g_cancellable_push_current (cancellable);
331 res = class->flush (stream, cancellable, error);
334 g_cancellable_pop_current (cancellable);
337 g_output_stream_clear_pending (stream);
343 * g_output_stream_splice:
344 * @stream: a #GOutputStream.
345 * @source: a #GInputStream.
346 * @flags: a set of #GOutputStreamSpliceFlags.
347 * @cancellable: optional #GCancellable object, %NULL to ignore.
348 * @error: a #GError location to store the error occuring, or %NULL to
351 * Splices an input stream into an output stream.
353 * Returns: a #gssize containing the size of the data spliced.
356 g_output_stream_splice (GOutputStream *stream,
357 GInputStream *source,
358 GOutputStreamSpliceFlags flags,
359 GCancellable *cancellable,
362 GOutputStreamClass *class;
365 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
366 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
368 if (g_input_stream_is_closed (source))
370 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
371 _("Source stream is already closed"));
375 if (!g_output_stream_set_pending (stream, error))
378 class = G_OUTPUT_STREAM_GET_CLASS (stream);
382 g_cancellable_push_current (cancellable);
384 res = class->splice (stream, source, flags, cancellable, error);
387 g_cancellable_pop_current (cancellable);
389 g_output_stream_clear_pending (stream);
395 g_output_stream_real_splice (GOutputStream *stream,
396 GInputStream *source,
397 GOutputStreamSpliceFlags flags,
398 GCancellable *cancellable,
401 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
402 gssize n_read, n_written;
404 char buffer[8192], *p;
408 if (class->write_fn == NULL)
410 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
411 _("Output stream doesn't implement write"));
419 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
432 n_written = class->write_fn (stream, p, n_read, cancellable, error);
441 bytes_copied += n_written;
448 error = NULL; /* Ignore further errors */
450 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
452 /* Don't care about errors in source here */
453 g_input_stream_close (source, cancellable, NULL);
456 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
458 /* But write errors on close are bad! */
459 if (!class->close_fn (stream, cancellable, error))
471 * g_output_stream_close:
472 * @stream: A #GOutputStream.
473 * @cancellable: optional cancellable object
474 * @error: location to store the error occuring, or %NULL to ignore
476 * Closes the stream, releasing resources related to it.
478 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
479 * Closing a stream multiple times will not return an error.
481 * Closing a stream will automatically flush any outstanding buffers in the
484 * Streams will be automatically closed when the last reference
485 * is dropped, but you might want to call this function to make sure
486 * resources are released as early as possible.
488 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
489 * open after the stream is closed. See the documentation for the individual
490 * stream for details.
492 * On failure the first error that happened will be reported, but the close
493 * operation will finish as much as possible. A stream that failed to
494 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
495 * is important to check and report the error to the user, otherwise
496 * there might be a loss of data as all data might not be written.
498 * If @cancellable is not NULL, then the operation can be cancelled by
499 * triggering the cancellable object from another thread. If the operation
500 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
501 * Cancelling a close will still leave the stream closed, but there some streams
502 * can use a faster close that doesn't block to e.g. check errors. On
503 * cancellation (as with any error) there is no guarantee that all written
504 * data will reach the target.
506 * Return value: %TRUE on success, %FALSE on failure
509 g_output_stream_close (GOutputStream *stream,
510 GCancellable *cancellable,
513 GOutputStreamClass *class;
516 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
518 class = G_OUTPUT_STREAM_GET_CLASS (stream);
520 if (stream->priv->closed)
523 if (!g_output_stream_set_pending (stream, error))
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->closed = TRUE;
553 g_output_stream_clear_pending (stream);
559 async_ready_callback_wrapper (GObject *source_object,
563 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
565 g_output_stream_clear_pending (stream);
566 if (stream->priv->outstanding_callback)
567 (*stream->priv->outstanding_callback) (source_object, res, user_data);
568 g_object_unref (stream);
572 async_ready_close_callback_wrapper (GObject *source_object,
576 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
578 stream->priv->closed = TRUE;
579 g_output_stream_clear_pending (stream);
580 if (stream->priv->outstanding_callback)
581 (*stream->priv->outstanding_callback) (source_object, res, user_data);
582 g_object_unref (stream);
586 * g_output_stream_write_async:
587 * @stream: A #GOutputStream.
588 * @buffer: the buffer containing the data to write.
589 * @count: the number of bytes to write
590 * @io_priority: the io priority of the request.
591 * @cancellable: optional #GCancellable object, %NULL to ignore.
592 * @callback: callback to call when the request is satisfied
593 * @user_data: the data to pass to callback function
595 * Request an asynchronous write of @count bytes from @buffer into
596 * the stream. When the operation is finished @callback will be called.
597 * You can then call g_output_stream_write_finish() to get the result of the
600 * During an async request no other sync and async calls are allowed,
601 * and will result in %G_IO_ERROR_PENDING errors.
603 * A value of @count larger than %G_MAXSSIZE will cause a
604 * %G_IO_ERROR_INVALID_ARGUMENT error.
606 * On success, the number of bytes written will be passed to the
607 * @callback. It is not an error if this is not the same as the
608 * requested size, as it can happen e.g. on a partial I/O error,
609 * but generally we try to write as many bytes as requested.
611 * Any outstanding I/O request with higher priority (lower numerical
612 * value) will be executed before an outstanding request with lower
613 * priority. Default priority is %G_PRIORITY_DEFAULT.
615 * The asyncronous methods have a default fallback that uses threads
616 * to implement asynchronicity, so they are optional for inheriting
617 * classes. However, if you override one you must override all.
619 * For the synchronous, blocking version of this function, see
620 * g_output_stream_write().
623 g_output_stream_write_async (GOutputStream *stream,
627 GCancellable *cancellable,
628 GAsyncReadyCallback callback,
631 GOutputStreamClass *class;
632 GSimpleAsyncResult *simple;
633 GError *error = NULL;
635 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
636 g_return_if_fail (buffer != NULL);
640 simple = g_simple_async_result_new (G_OBJECT (stream),
643 g_output_stream_write_async);
644 g_simple_async_result_complete_in_idle (simple);
645 g_object_unref (simple);
649 if (((gssize) count) < 0)
651 g_simple_async_report_error_in_idle (G_OBJECT (stream),
654 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
655 _("Too large count value passed to %s"),
660 if (!g_output_stream_set_pending (stream, &error))
662 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
666 g_error_free (error);
670 class = G_OUTPUT_STREAM_GET_CLASS (stream);
672 stream->priv->outstanding_callback = callback;
673 g_object_ref (stream);
674 class->write_async (stream, buffer, count, io_priority, cancellable,
675 async_ready_callback_wrapper, user_data);
679 * g_output_stream_write_finish:
680 * @stream: a #GOutputStream.
681 * @result: a #GAsyncResult.
682 * @error: a #GError location to store the error occuring, or %NULL to
685 * Finishes a stream write operation.
687 * Returns: a #gssize containing the number of bytes written to the stream.
690 g_output_stream_write_finish (GOutputStream *stream,
691 GAsyncResult *result,
694 GSimpleAsyncResult *simple;
695 GOutputStreamClass *class;
697 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
698 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
700 if (G_IS_SIMPLE_ASYNC_RESULT (result))
702 simple = G_SIMPLE_ASYNC_RESULT (result);
703 if (g_simple_async_result_propagate_error (simple, error))
706 /* Special case writes of 0 bytes */
707 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
711 class = G_OUTPUT_STREAM_GET_CLASS (stream);
712 return class->write_finish (stream, result, error);
716 GInputStream *source;
718 GAsyncReadyCallback callback;
722 async_ready_splice_callback_wrapper (GObject *source_object,
726 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
727 SpliceUserData *data = _data;
729 g_output_stream_clear_pending (stream);
732 (*data->callback) (source_object, res, data->user_data);
734 g_object_unref (stream);
735 g_object_unref (data->source);
740 * g_output_stream_splice_async:
741 * @stream: a #GOutputStream.
742 * @source: a #GInputStream.
743 * @flags: a set of #GOutputStreamSpliceFlags.
744 * @io_priority: the io priority of the request.
745 * @cancellable: optional #GCancellable object, %NULL to ignore.
746 * @callback: a #GAsyncReadyCallback.
747 * @user_data: user data passed to @callback.
749 * Splices a stream asynchronously.
750 * When the operation is finished @callback will be called.
751 * You can then call g_output_stream_splice_finish() to get the
752 * result of the operation.
754 * For the synchronous, blocking version of this function, see
755 * g_output_stream_splice().
758 g_output_stream_splice_async (GOutputStream *stream,
759 GInputStream *source,
760 GOutputStreamSpliceFlags flags,
762 GCancellable *cancellable,
763 GAsyncReadyCallback callback,
766 GOutputStreamClass *class;
767 SpliceUserData *data;
768 GError *error = NULL;
770 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
771 g_return_if_fail (G_IS_INPUT_STREAM (source));
773 if (g_input_stream_is_closed (source))
775 g_simple_async_report_error_in_idle (G_OBJECT (stream),
778 G_IO_ERROR, G_IO_ERROR_CLOSED,
779 _("Source stream is already closed"));
783 if (!g_output_stream_set_pending (stream, &error))
785 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
789 g_error_free (error);
793 class = G_OUTPUT_STREAM_GET_CLASS (stream);
795 data = g_new0 (SpliceUserData, 1);
796 data->callback = callback;
797 data->user_data = user_data;
798 data->source = g_object_ref (source);
800 g_object_ref (stream);
801 class->splice_async (stream, source, flags, io_priority, cancellable,
802 async_ready_splice_callback_wrapper, data);
806 * g_output_stream_splice_finish:
807 * @stream: a #GOutputStream.
808 * @result: a #GAsyncResult.
809 * @error: a #GError location to store the error occuring, or %NULL to
812 * Finishes an asynchronous stream splice operation.
814 * Returns: a #gssize of the number of bytes spliced.
817 g_output_stream_splice_finish (GOutputStream *stream,
818 GAsyncResult *result,
821 GSimpleAsyncResult *simple;
822 GOutputStreamClass *class;
824 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
825 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
827 if (G_IS_SIMPLE_ASYNC_RESULT (result))
829 simple = G_SIMPLE_ASYNC_RESULT (result);
830 if (g_simple_async_result_propagate_error (simple, error))
834 class = G_OUTPUT_STREAM_GET_CLASS (stream);
835 return class->splice_finish (stream, result, error);
839 * g_output_stream_flush_async:
840 * @stream: a #GOutputStream.
841 * @io_priority: the io priority of the request.
842 * @cancellable: optional #GCancellable object, %NULL to ignore.
843 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
844 * @user_data: the data to pass to callback function
846 * Flushes a stream asynchronously.
847 * For behaviour details see g_output_stream_flush().
849 * When the operation is finished @callback will be
850 * called. You can then call g_output_stream_flush_finish() to get the
851 * result of the operation.
854 g_output_stream_flush_async (GOutputStream *stream,
856 GCancellable *cancellable,
857 GAsyncReadyCallback callback,
860 GOutputStreamClass *class;
861 GSimpleAsyncResult *simple;
862 GError *error = NULL;
864 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
866 if (!g_output_stream_set_pending (stream, &error))
868 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
872 g_error_free (error);
876 stream->priv->outstanding_callback = callback;
877 g_object_ref (stream);
879 class = G_OUTPUT_STREAM_GET_CLASS (stream);
881 if (class->flush_async == NULL)
883 simple = g_simple_async_result_new (G_OBJECT (stream),
884 async_ready_callback_wrapper,
886 g_output_stream_flush_async);
887 g_simple_async_result_complete_in_idle (simple);
888 g_object_unref (simple);
892 class->flush_async (stream, io_priority, cancellable,
893 async_ready_callback_wrapper, user_data);
897 * g_output_stream_flush_finish:
898 * @stream: a #GOutputStream.
899 * @result: a GAsyncResult.
900 * @error: a #GError location to store the error occuring, or %NULL to
903 * Finishes flushing an output stream.
905 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
908 g_output_stream_flush_finish (GOutputStream *stream,
909 GAsyncResult *result,
912 GSimpleAsyncResult *simple;
913 GOutputStreamClass *klass;
915 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
916 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
918 if (G_IS_SIMPLE_ASYNC_RESULT (result))
920 simple = G_SIMPLE_ASYNC_RESULT (result);
921 if (g_simple_async_result_propagate_error (simple, error))
924 /* Special case default implementation */
925 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
929 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
930 return klass->flush_finish (stream, result, error);
935 * g_output_stream_close_async:
936 * @stream: A #GOutputStream.
937 * @io_priority: the io priority of the request.
938 * @callback: callback to call when the request is satisfied
939 * @user_data: the data to pass to callback function
940 * @cancellable: optional cancellable object
942 * Requests an asynchronous close of the stream, releasing resources
943 * related to it. When the operation is finished @callback will be
944 * called. You can then call g_output_stream_close_finish() to get
945 * the result of the operation.
947 * For behaviour details see g_output_stream_close().
949 * The asyncronous methods have a default fallback that uses threads
950 * to implement asynchronicity, so they are optional for inheriting
951 * classes. However, if you override one you must override all.
954 g_output_stream_close_async (GOutputStream *stream,
956 GCancellable *cancellable,
957 GAsyncReadyCallback callback,
960 GOutputStreamClass *class;
961 GSimpleAsyncResult *simple;
962 GError *error = NULL;
964 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
966 if (stream->priv->closed)
968 simple = g_simple_async_result_new (G_OBJECT (stream),
971 g_output_stream_close_async);
972 g_simple_async_result_complete_in_idle (simple);
973 g_object_unref (simple);
977 if (!g_output_stream_set_pending (stream, &error))
979 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
983 g_error_free (error);
987 class = G_OUTPUT_STREAM_GET_CLASS (stream);
988 stream->priv->outstanding_callback = callback;
989 g_object_ref (stream);
990 class->close_async (stream, io_priority, cancellable,
991 async_ready_close_callback_wrapper, user_data);
995 * g_output_stream_close_finish:
996 * @stream: a #GOutputStream.
997 * @result: a #GAsyncResult.
998 * @error: a #GError location to store the error occuring, or %NULL to
1001 * Closes an output stream.
1003 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1006 g_output_stream_close_finish (GOutputStream *stream,
1007 GAsyncResult *result,
1010 GSimpleAsyncResult *simple;
1011 GOutputStreamClass *class;
1013 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1014 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1016 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1018 simple = G_SIMPLE_ASYNC_RESULT (result);
1019 if (g_simple_async_result_propagate_error (simple, error))
1022 /* Special case already closed */
1023 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1027 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1028 return class->close_finish (stream, result, error);
1032 * g_output_stream_is_closed:
1033 * @stream: a #GOutputStream.
1035 * Checks if an output stream has already been closed.
1037 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1040 g_output_stream_is_closed (GOutputStream *stream)
1042 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1044 return stream->priv->closed;
1048 * g_output_stream_has_pending:
1049 * @stream: a #GOutputStream.
1051 * Checks if an ouput stream has pending actions.
1053 * Returns: %TRUE if @stream has pending actions.
1056 g_output_stream_has_pending (GOutputStream *stream)
1058 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1060 return stream->priv->pending;
1064 * g_output_stream_set_pending:
1065 * @stream: a #GOutputStream.
1066 * @error: a #GError location to store the error occuring, or %NULL to
1069 * Sets @stream to have actions pending. If the pending flag is
1070 * already set or @stream is closed, it will return %FALSE and set
1073 * Return value: %TRUE if pending was previously unset and is now set.
1076 g_output_stream_set_pending (GOutputStream *stream,
1079 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1081 if (stream->priv->closed)
1083 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1084 _("Stream is already closed"));
1088 if (stream->priv->pending)
1090 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1091 /* Translators: This is an error you get if there is
1092 * already an operation running against this stream when
1093 * you try to start one */
1094 _("Stream has outstanding operation"));
1098 stream->priv->pending = TRUE;
1103 * g_output_stream_clear_pending:
1104 * @stream: output stream
1106 * Clears the pending flag on @stream.
1109 g_output_stream_clear_pending (GOutputStream *stream)
1111 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1113 stream->priv->pending = FALSE;
1117 /********************************************
1118 * Default implementation of async ops *
1119 ********************************************/
1123 gsize count_requested;
1124 gssize count_written;
1128 write_async_thread (GSimpleAsyncResult *res,
1130 GCancellable *cancellable)
1133 GOutputStreamClass *class;
1134 GError *error = NULL;
1136 class = G_OUTPUT_STREAM_GET_CLASS (object);
1137 op = g_simple_async_result_get_op_res_gpointer (res);
1138 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1139 cancellable, &error);
1140 if (op->count_written == -1)
1142 g_simple_async_result_set_from_error (res, error);
1143 g_error_free (error);
1148 g_output_stream_real_write_async (GOutputStream *stream,
1152 GCancellable *cancellable,
1153 GAsyncReadyCallback callback,
1156 GSimpleAsyncResult *res;
1159 op = g_new0 (WriteData, 1);
1160 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1161 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1162 op->buffer = buffer;
1163 op->count_requested = count;
1165 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1166 g_object_unref (res);
1170 g_output_stream_real_write_finish (GOutputStream *stream,
1171 GAsyncResult *result,
1174 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1177 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1178 op = g_simple_async_result_get_op_res_gpointer (simple);
1179 return op->count_written;
1183 GInputStream *source;
1184 GOutputStreamSpliceFlags flags;
1185 gssize bytes_copied;
1189 splice_async_thread (GSimpleAsyncResult *result,
1191 GCancellable *cancellable)
1194 GOutputStreamClass *class;
1195 GError *error = NULL;
1196 GOutputStream *stream;
1198 stream = G_OUTPUT_STREAM (object);
1199 class = G_OUTPUT_STREAM_GET_CLASS (object);
1200 op = g_simple_async_result_get_op_res_gpointer (result);
1202 op->bytes_copied = class->splice (stream,
1207 if (op->bytes_copied == -1)
1209 g_simple_async_result_set_from_error (result, error);
1210 g_error_free (error);
1215 g_output_stream_real_splice_async (GOutputStream *stream,
1216 GInputStream *source,
1217 GOutputStreamSpliceFlags flags,
1219 GCancellable *cancellable,
1220 GAsyncReadyCallback callback,
1223 GSimpleAsyncResult *res;
1226 op = g_new0 (SpliceData, 1);
1227 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1228 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1230 op->source = source;
1232 /* TODO: In the case where both source and destintion have
1233 non-threadbased async calls we can use a true async copy here */
1235 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1236 g_object_unref (res);
1240 g_output_stream_real_splice_finish (GOutputStream *stream,
1241 GAsyncResult *result,
1244 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1247 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1248 op = g_simple_async_result_get_op_res_gpointer (simple);
1249 return op->bytes_copied;
1254 flush_async_thread (GSimpleAsyncResult *res,
1256 GCancellable *cancellable)
1258 GOutputStreamClass *class;
1260 GError *error = NULL;
1262 class = G_OUTPUT_STREAM_GET_CLASS (object);
1265 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1269 g_simple_async_result_set_from_error (res, error);
1270 g_error_free (error);
1275 g_output_stream_real_flush_async (GOutputStream *stream,
1277 GCancellable *cancellable,
1278 GAsyncReadyCallback callback,
1281 GSimpleAsyncResult *res;
1283 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1285 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1286 g_object_unref (res);
1290 g_output_stream_real_flush_finish (GOutputStream *stream,
1291 GAsyncResult *result,
1298 close_async_thread (GSimpleAsyncResult *res,
1300 GCancellable *cancellable)
1302 GOutputStreamClass *class;
1303 GError *error = NULL;
1306 /* Auto handling of cancelation disabled, and ignore
1307 cancellation, since we want to close things anyway, although
1308 possibly in a quick-n-dirty way. At least we never want to leak
1311 class = G_OUTPUT_STREAM_GET_CLASS (object);
1312 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1315 g_simple_async_result_set_from_error (res, error);
1316 g_error_free (error);
1321 g_output_stream_real_close_async (GOutputStream *stream,
1323 GCancellable *cancellable,
1324 GAsyncReadyCallback callback,
1327 GSimpleAsyncResult *res;
1329 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1331 g_simple_async_result_set_handle_cancellation (res, FALSE);
1333 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1334 g_object_unref (res);
1338 g_output_stream_real_close_finish (GOutputStream *stream,
1339 GAsyncResult *result,
1342 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1343 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1347 #define __G_OUTPUT_STREAM_C__
1348 #include "gioaliasdef.c"