1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
24 #include "goutputstream.h"
25 #include "gsimpleasyncresult.h"
31 * SECTION:goutputstream
32 * @short_description: Base class for implementing streaming output
38 G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
40 struct _GOutputStreamPrivate {
44 GAsyncReadyCallback outstanding_callback;
47 static gssize g_output_stream_real_splice (GOutputStream *stream,
49 GOutputStreamSpliceFlags flags,
50 GCancellable *cancellable,
52 static void g_output_stream_real_write_async (GOutputStream *stream,
56 GCancellable *cancellable,
57 GAsyncReadyCallback callback,
59 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
62 static void g_output_stream_real_splice_async (GOutputStream *stream,
64 GOutputStreamSpliceFlags flags,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
72 static void g_output_stream_real_flush_async (GOutputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
80 static void g_output_stream_real_close_async (GOutputStream *stream,
82 GCancellable *cancellable,
83 GAsyncReadyCallback callback,
85 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
90 g_output_stream_finalize (GObject *object)
92 GOutputStream *stream;
94 stream = G_OUTPUT_STREAM (object);
96 if (G_OBJECT_CLASS (g_output_stream_parent_class)->finalize)
97 (*G_OBJECT_CLASS (g_output_stream_parent_class)->finalize) (object);
101 g_output_stream_dispose (GObject *object)
103 GOutputStream *stream;
105 stream = G_OUTPUT_STREAM (object);
107 if (!stream->priv->closed)
108 g_output_stream_close (stream, NULL, NULL);
110 if (G_OBJECT_CLASS (g_output_stream_parent_class)->dispose)
111 (*G_OBJECT_CLASS (g_output_stream_parent_class)->dispose) (object);
115 g_output_stream_class_init (GOutputStreamClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
121 gobject_class->finalize = g_output_stream_finalize;
122 gobject_class->dispose = g_output_stream_dispose;
124 klass->splice = g_output_stream_real_splice;
126 klass->write_async = g_output_stream_real_write_async;
127 klass->write_finish = g_output_stream_real_write_finish;
128 klass->splice_async = g_output_stream_real_splice_async;
129 klass->splice_finish = g_output_stream_real_splice_finish;
130 klass->flush_async = g_output_stream_real_flush_async;
131 klass->flush_finish = g_output_stream_real_flush_finish;
132 klass->close_async = g_output_stream_real_close_async;
133 klass->close_finish = g_output_stream_real_close_finish;
137 g_output_stream_init (GOutputStream *stream)
139 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
140 G_TYPE_OUTPUT_STREAM,
141 GOutputStreamPrivate);
145 * g_output_stream_write:
146 * @stream: a #GOutputStream.
147 * @buffer: the buffer containing the data to write.
148 * @count: the number of bytes to write
149 * @cancellable: optional cancellable object
150 * @error: location to store the error occuring, or %NULL to ignore
152 * Tries to write @count bytes from @buffer into the stream. Will block
153 * during the operation.
155 * If count is zero returns zero and does nothing. A value of @count
156 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
158 * On success, the number of bytes written to the stream is returned.
159 * It is not an error if this is not the same as the requested size, as it
160 * can happen e.g. on a partial i/o error, or if the there is not enough
161 * storage in the stream. All writes either block until at least one byte
162 * is written, so zero is never returned (unless @count is zero).
164 * If @cancellable is not NULL, then the operation can be cancelled by
165 * triggering the cancellable object from another thread. If the operation
166 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
167 * operation was partially finished when the operation was cancelled the
168 * partial result will be returned, without an error.
170 * On error -1 is returned and @error is set accordingly.
172 * Return value: Number of bytes written, or -1 on error
175 g_output_stream_write (GOutputStream *stream,
178 GCancellable *cancellable,
181 GOutputStreamClass *class;
184 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
185 g_return_val_if_fail (buffer != NULL, 0);
190 if (((gssize) count) < 0)
192 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
193 _("Too large count value passed to g_output_stream_write"));
197 class = G_OUTPUT_STREAM_GET_CLASS (stream);
199 if (class->write_fn == NULL)
201 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
202 _("Output stream doesn't implement write"));
206 if (!g_output_stream_set_pending (stream, error))
210 g_cancellable_push_current (cancellable);
212 res = class->write_fn (stream, buffer, count, cancellable, error);
215 g_cancellable_pop_current (cancellable);
217 g_output_stream_clear_pending (stream);
223 * g_output_stream_write_all:
224 * @stream: a #GOutputStream.
225 * @buffer: the buffer containing the data to write.
226 * @count: the number of bytes to write
227 * @bytes_written: location to store the number of bytes that was
228 * written to the stream
229 * @cancellable: optional #GCancellable object, %NULL to ignore.
230 * @error: location to store the error occuring, or %NULL to ignore
232 * Tries to write @count bytes from @buffer into the stream. Will block
233 * during the operation.
235 * This function is similar to g_output_stream_write(), except it tries to
236 * write as many bytes as requested, only stopping on an error.
238 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
241 * If there is an error during the operation FALSE is returned and @error
242 * is set to indicate the error status, @bytes_written is updated to contain
243 * the number of bytes written into the stream before the error occurred.
245 * Return value: %TRUE on success, %FALSE if there was an error
248 g_output_stream_write_all (GOutputStream *stream,
251 gsize *bytes_written,
252 GCancellable *cancellable,
255 gsize _bytes_written;
258 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
259 g_return_val_if_fail (buffer != NULL, FALSE);
262 while (_bytes_written < count)
264 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
269 *bytes_written = _bytes_written;
274 g_warning ("Write returned zero without error");
276 _bytes_written += res;
280 *bytes_written = _bytes_written;
286 * g_output_stream_flush:
287 * @stream: a #GOutputStream.
288 * @cancellable: optional cancellable object
289 * @error: location to store the error occuring, or %NULL to ignore
291 * Flushed any outstanding buffers in the stream. Will block during
292 * the operation. Closing the stream will implicitly cause a flush.
294 * This function is optional for inherited classes.
296 * If @cancellable is not %NULL, then the operation can be cancelled by
297 * triggering the cancellable object from another thread. If the operation
298 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
300 * Return value: %TRUE on success, %FALSE on error
303 g_output_stream_flush (GOutputStream *stream,
304 GCancellable *cancellable,
307 GOutputStreamClass *class;
310 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
312 if (!g_output_stream_set_pending (stream, error))
315 class = G_OUTPUT_STREAM_GET_CLASS (stream);
321 g_cancellable_push_current (cancellable);
323 res = class->flush (stream, cancellable, error);
326 g_cancellable_pop_current (cancellable);
329 g_output_stream_clear_pending (stream);
335 * g_output_stream_splice:
336 * @stream: a #GOutputStream.
337 * @source: a #GInputStream.
338 * @flags: a set of #GOutputStreamSpliceFlags.
339 * @cancellable: optional #GCancellable object, %NULL to ignore.
340 * @error: a #GError location to store the error occuring, or %NULL to
343 * Splices an input stream into an output stream.
345 * Returns: a #gssize containing the size of the data spliced.
348 g_output_stream_splice (GOutputStream *stream,
349 GInputStream *source,
350 GOutputStreamSpliceFlags flags,
351 GCancellable *cancellable,
354 GOutputStreamClass *class;
357 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
358 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
360 if (g_input_stream_is_closed (source))
362 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
363 _("Source stream is already closed"));
367 if (!g_output_stream_set_pending (stream, error))
370 class = G_OUTPUT_STREAM_GET_CLASS (stream);
374 g_cancellable_push_current (cancellable);
376 res = class->splice (stream, source, flags, cancellable, error);
379 g_cancellable_pop_current (cancellable);
381 g_output_stream_clear_pending (stream);
387 g_output_stream_real_splice (GOutputStream *stream,
388 GInputStream *source,
389 GOutputStreamSpliceFlags flags,
390 GCancellable *cancellable,
393 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
394 gssize n_read, n_written;
396 char buffer[8192], *p;
400 if (class->write_fn == NULL)
402 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
403 _("Output stream doesn't implement write"));
411 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
424 n_written = class->write_fn (stream, p, n_read, cancellable, error);
433 bytes_copied += n_written;
440 error = NULL; /* Ignore further errors */
442 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
444 /* Don't care about errors in source here */
445 g_input_stream_close (source, cancellable, NULL);
448 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
450 /* But write errors on close are bad! */
451 if (!class->close_fn (stream, cancellable, error))
463 * g_output_stream_close:
464 * @stream: A #GOutputStream.
465 * @cancellable: optional cancellable object
466 * @error: location to store the error occuring, or %NULL to ignore
468 * Closes the stream, releasing resources related to it.
470 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
471 * Closing a stream multiple times will not return an error.
473 * Closing a stream will automatically flush any outstanding buffers in the
476 * Streams will be automatically closed when the last reference
477 * is dropped, but you might want to call this function to make sure
478 * resources are released as early as possible.
480 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
481 * open after the stream is closed. See the documentation for the individual
482 * stream for details.
484 * On failure the first error that happened will be reported, but the close
485 * operation will finish as much as possible. A stream that failed to
486 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
487 * is important to check and report the error to the user, otherwise
488 * there might be a loss of data as all data might not be written.
490 * If @cancellable is not NULL, then the operation can be cancelled by
491 * triggering the cancellable object from another thread. If the operation
492 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
493 * Cancelling a close will still leave the stream closed, but there some streams
494 * can use a faster close that doesn't block to e.g. check errors. On
495 * cancellation (as with any error) there is no guarantee that all written
496 * data will reach the target.
498 * Return value: %TRUE on success, %FALSE on failure
501 g_output_stream_close (GOutputStream *stream,
502 GCancellable *cancellable,
505 GOutputStreamClass *class;
508 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
510 class = G_OUTPUT_STREAM_GET_CLASS (stream);
512 if (stream->priv->closed)
515 if (!g_output_stream_set_pending (stream, error))
519 g_cancellable_push_current (cancellable);
522 res = class->flush (stream, cancellable, error);
528 /* flushing caused the error that we want to return,
529 * but we still want to close the underlying stream if possible
532 class->close_fn (stream, cancellable, NULL);
538 res = class->close_fn (stream, cancellable, error);
542 g_cancellable_pop_current (cancellable);
544 stream->priv->closed = TRUE;
545 g_output_stream_clear_pending (stream);
551 async_ready_callback_wrapper (GObject *source_object,
555 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
557 g_output_stream_clear_pending (stream);
558 if (stream->priv->outstanding_callback)
559 (*stream->priv->outstanding_callback) (source_object, res, user_data);
560 g_object_unref (stream);
564 async_ready_close_callback_wrapper (GObject *source_object,
568 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
570 stream->priv->closed = TRUE;
571 g_output_stream_clear_pending (stream);
572 if (stream->priv->outstanding_callback)
573 (*stream->priv->outstanding_callback) (source_object, res, user_data);
574 g_object_unref (stream);
578 * g_output_stream_write_async:
579 * @stream: A #GOutputStream.
580 * @buffer: the buffer containing the data to write.
581 * @count: the number of bytes to write
582 * @io_priority: the io priority of the request.
583 * @cancellable: optional #GCancellable object, %NULL to ignore.
584 * @callback: callback to call when the request is satisfied
585 * @user_data: the data to pass to callback function
587 * Request an asynchronous write of @count bytes from @buffer into
588 * the stream. When the operation is finished @callback will be called.
589 * You can then call g_output_stream_write_finish() to get the result of the
592 * During an async request no other sync and async calls are allowed,
593 * and will result in %G_IO_ERROR_PENDING errors.
595 * A value of @count larger than %G_MAXSSIZE will cause a
596 * %G_IO_ERROR_INVALID_ARGUMENT error.
598 * On success, the number of bytes written will be passed to the
599 * @callback. It is not an error if this is not the same as the
600 * requested size, as it can happen e.g. on a partial I/O error,
601 * but generally we try to write as many bytes as requested.
603 * Any outstanding I/O request with higher priority (lower numerical
604 * value) will be executed before an outstanding request with lower
605 * priority. Default priority is %G_PRIORITY_DEFAULT.
607 * The asyncronous methods have a default fallback that uses threads
608 * to implement asynchronicity, so they are optional for inheriting
609 * classes. However, if you override one you must override all.
611 * For the synchronous, blocking version of this function, see
612 * g_output_stream_write().
615 g_output_stream_write_async (GOutputStream *stream,
619 GCancellable *cancellable,
620 GAsyncReadyCallback callback,
623 GOutputStreamClass *class;
624 GSimpleAsyncResult *simple;
625 GError *error = NULL;
627 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
628 g_return_if_fail (buffer != NULL);
632 simple = g_simple_async_result_new (G_OBJECT (stream),
635 g_output_stream_write_async);
636 g_simple_async_result_complete_in_idle (simple);
637 g_object_unref (simple);
641 if (((gssize) count) < 0)
643 g_simple_async_report_error_in_idle (G_OBJECT (stream),
646 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
647 _("Too large count value passed to g_output_stream_write_async"));
651 if (!g_output_stream_set_pending (stream, &error))
653 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
657 g_error_free (error);
661 class = G_OUTPUT_STREAM_GET_CLASS (stream);
663 stream->priv->outstanding_callback = callback;
664 g_object_ref (stream);
665 class->write_async (stream, buffer, count, io_priority, cancellable,
666 async_ready_callback_wrapper, user_data);
670 * g_output_stream_write_finish:
671 * @stream: a #GOutputStream.
672 * @result: a #GAsyncResult.
673 * @error: a #GError location to store the error occuring, or %NULL to
676 * Finishes a stream write operation.
678 * Returns: a #gssize containing the number of bytes written to the stream.
681 g_output_stream_write_finish (GOutputStream *stream,
682 GAsyncResult *result,
685 GSimpleAsyncResult *simple;
686 GOutputStreamClass *class;
688 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
689 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
691 if (G_IS_SIMPLE_ASYNC_RESULT (result))
693 simple = G_SIMPLE_ASYNC_RESULT (result);
694 if (g_simple_async_result_propagate_error (simple, error))
697 /* Special case writes of 0 bytes */
698 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
702 class = G_OUTPUT_STREAM_GET_CLASS (stream);
703 return class->write_finish (stream, result, error);
707 GInputStream *source;
709 GAsyncReadyCallback callback;
713 async_ready_splice_callback_wrapper (GObject *source_object,
717 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
718 SpliceUserData *data = _data;
720 g_output_stream_clear_pending (stream);
723 (*data->callback) (source_object, res, data->user_data);
725 g_object_unref (stream);
726 g_object_unref (data->source);
731 * g_output_stream_splice_async:
732 * @stream: a #GOutputStream.
733 * @source: a #GInputStream.
734 * @flags: a set of #GOutputStreamSpliceFlags.
735 * @io_priority: the io priority of the request.
736 * @cancellable: optional #GCancellable object, %NULL to ignore.
737 * @callback: a #GAsyncReadyCallback.
738 * @user_data: user data passed to @callback.
740 * Splices a stream asynchronously.
741 * When the operation is finished @callback will be called.
742 * You can then call g_output_stream_splice_finish() to get the
743 * result of the operation.
745 * For the synchronous, blocking version of this function, see
746 * g_output_stream_splice().
749 g_output_stream_splice_async (GOutputStream *stream,
750 GInputStream *source,
751 GOutputStreamSpliceFlags flags,
753 GCancellable *cancellable,
754 GAsyncReadyCallback callback,
757 GOutputStreamClass *class;
758 SpliceUserData *data;
759 GError *error = NULL;
761 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
762 g_return_if_fail (G_IS_INPUT_STREAM (source));
764 if (g_input_stream_is_closed (source))
766 g_simple_async_report_error_in_idle (G_OBJECT (stream),
769 G_IO_ERROR, G_IO_ERROR_CLOSED,
770 _("Source stream is already closed"));
774 if (!g_output_stream_set_pending (stream, &error))
776 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
780 g_error_free (error);
784 class = G_OUTPUT_STREAM_GET_CLASS (stream);
786 data = g_new0 (SpliceUserData, 1);
787 data->callback = callback;
788 data->user_data = user_data;
789 data->source = g_object_ref (source);
791 g_object_ref (stream);
792 class->splice_async (stream, source, flags, io_priority, cancellable,
793 async_ready_splice_callback_wrapper, data);
797 * g_output_stream_splice_finish:
798 * @stream: a #GOutputStream.
799 * @result: a #GAsyncResult.
800 * @error: a #GError location to store the error occuring, or %NULL to
803 * Finishes an asynchronous stream splice operation.
805 * Returns: a #gssize of the number of bytes spliced.
808 g_output_stream_splice_finish (GOutputStream *stream,
809 GAsyncResult *result,
812 GSimpleAsyncResult *simple;
813 GOutputStreamClass *class;
815 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
816 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
818 if (G_IS_SIMPLE_ASYNC_RESULT (result))
820 simple = G_SIMPLE_ASYNC_RESULT (result);
821 if (g_simple_async_result_propagate_error (simple, error))
825 class = G_OUTPUT_STREAM_GET_CLASS (stream);
826 return class->splice_finish (stream, result, error);
830 * g_output_stream_flush_async:
831 * @stream: a #GOutputStream.
832 * @io_priority: the io priority of the request.
833 * @cancellable: optional #GCancellable object, %NULL to ignore.
834 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
835 * @user_data: the data to pass to callback function
837 * Flushes a stream asynchronously.
838 * For behaviour details see g_output_stream_flush().
840 * When the operation is finished @callback will be
841 * called. You can then call g_output_stream_flush_finish() to get the
842 * result of the operation.
845 g_output_stream_flush_async (GOutputStream *stream,
847 GCancellable *cancellable,
848 GAsyncReadyCallback callback,
851 GOutputStreamClass *class;
852 GSimpleAsyncResult *simple;
853 GError *error = NULL;
855 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
857 if (!g_output_stream_set_pending (stream, &error))
859 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
863 g_error_free (error);
867 stream->priv->outstanding_callback = callback;
868 g_object_ref (stream);
870 class = G_OUTPUT_STREAM_GET_CLASS (stream);
872 if (class->flush_async == NULL)
874 simple = g_simple_async_result_new (G_OBJECT (stream),
875 async_ready_callback_wrapper,
877 g_output_stream_flush_async);
878 g_simple_async_result_complete_in_idle (simple);
879 g_object_unref (simple);
883 class->flush_async (stream, io_priority, cancellable,
884 async_ready_callback_wrapper, user_data);
888 * g_output_stream_flush_finish:
889 * @stream: a #GOutputStream.
890 * @result: a GAsyncResult.
891 * @error: a #GError location to store the error occuring, or %NULL to
894 * Finishes flushing an output stream.
896 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
899 g_output_stream_flush_finish (GOutputStream *stream,
900 GAsyncResult *result,
903 GSimpleAsyncResult *simple;
904 GOutputStreamClass *klass;
906 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
907 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
909 if (G_IS_SIMPLE_ASYNC_RESULT (result))
911 simple = G_SIMPLE_ASYNC_RESULT (result);
912 if (g_simple_async_result_propagate_error (simple, error))
915 /* Special case default implementation */
916 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
920 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
921 return klass->flush_finish (stream, result, error);
926 * g_output_stream_close_async:
927 * @stream: A #GOutputStream.
928 * @io_priority: the io priority of the request.
929 * @callback: callback to call when the request is satisfied
930 * @user_data: the data to pass to callback function
931 * @cancellable: optional cancellable object
933 * Requests an asynchronous close of the stream, releasing resources
934 * related to it. When the operation is finished @callback will be
935 * called. You can then call g_output_stream_close_finish() to get
936 * the result of the operation.
938 * For behaviour details see g_output_stream_close().
940 * The asyncronous methods have a default fallback that uses threads
941 * to implement asynchronicity, so they are optional for inheriting
942 * classes. However, if you override one you must override all.
945 g_output_stream_close_async (GOutputStream *stream,
947 GCancellable *cancellable,
948 GAsyncReadyCallback callback,
951 GOutputStreamClass *class;
952 GSimpleAsyncResult *simple;
953 GError *error = NULL;
955 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
957 if (stream->priv->closed)
959 simple = g_simple_async_result_new (G_OBJECT (stream),
962 g_output_stream_close_async);
963 g_simple_async_result_complete_in_idle (simple);
964 g_object_unref (simple);
968 if (!g_output_stream_set_pending (stream, &error))
970 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
974 g_error_free (error);
978 class = G_OUTPUT_STREAM_GET_CLASS (stream);
979 stream->priv->outstanding_callback = callback;
980 g_object_ref (stream);
981 class->close_async (stream, io_priority, cancellable,
982 async_ready_close_callback_wrapper, user_data);
986 * g_output_stream_close_finish:
987 * @stream: a #GOutputStream.
988 * @result: a #GAsyncResult.
989 * @error: a #GError location to store the error occuring, or %NULL to
992 * Closes an output stream.
994 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
997 g_output_stream_close_finish (GOutputStream *stream,
998 GAsyncResult *result,
1001 GSimpleAsyncResult *simple;
1002 GOutputStreamClass *class;
1004 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1005 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1007 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1009 simple = G_SIMPLE_ASYNC_RESULT (result);
1010 if (g_simple_async_result_propagate_error (simple, error))
1013 /* Special case already closed */
1014 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1018 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1019 return class->close_finish (stream, result, error);
1023 * g_output_stream_is_closed:
1024 * @stream: a #GOutputStream.
1026 * Checks if an output stream has already been closed.
1028 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1031 g_output_stream_is_closed (GOutputStream *stream)
1033 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1035 return stream->priv->closed;
1039 * g_output_stream_has_pending:
1040 * @stream: a #GOutputStream.
1042 * Checks if an ouput stream has pending actions.
1044 * Returns: %TRUE if @stream has pending actions.
1047 g_output_stream_has_pending (GOutputStream *stream)
1049 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1051 return stream->priv->pending;
1055 * g_output_stream_set_pending:
1056 * @stream: a #GOutputStream.
1057 * @error: a #GError location to store the error occuring, or %NULL to
1060 * Sets @stream to have actions pending. If the pending flag is
1061 * already set or @stream is closed, it will return %FALSE and set
1064 * Return value: %TRUE if pending was previously unset and is now set.
1067 g_output_stream_set_pending (GOutputStream *stream,
1070 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1072 if (stream->priv->closed)
1074 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1075 _("Stream is already closed"));
1079 if (stream->priv->pending)
1081 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1082 _("Stream has outstanding operation"));
1086 stream->priv->pending = TRUE;
1091 * g_output_stream_clear_pending:
1092 * @stream: output stream
1094 * Clears the pending flag on @stream.
1097 g_output_stream_clear_pending (GOutputStream *stream)
1099 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1101 stream->priv->pending = FALSE;
1105 /********************************************
1106 * Default implementation of async ops *
1107 ********************************************/
1111 gsize count_requested;
1112 gssize count_written;
1116 write_async_thread (GSimpleAsyncResult *res,
1118 GCancellable *cancellable)
1121 GOutputStreamClass *class;
1122 GError *error = NULL;
1124 class = G_OUTPUT_STREAM_GET_CLASS (object);
1125 op = g_simple_async_result_get_op_res_gpointer (res);
1126 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1127 cancellable, &error);
1128 if (op->count_written == -1)
1130 g_simple_async_result_set_from_error (res, error);
1131 g_error_free (error);
1136 g_output_stream_real_write_async (GOutputStream *stream,
1140 GCancellable *cancellable,
1141 GAsyncReadyCallback callback,
1144 GSimpleAsyncResult *res;
1147 op = g_new0 (WriteData, 1);
1148 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1149 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1150 op->buffer = buffer;
1151 op->count_requested = count;
1153 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1154 g_object_unref (res);
1158 g_output_stream_real_write_finish (GOutputStream *stream,
1159 GAsyncResult *result,
1162 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1165 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1166 op = g_simple_async_result_get_op_res_gpointer (simple);
1167 return op->count_written;
1171 GInputStream *source;
1172 GOutputStreamSpliceFlags flags;
1173 gssize bytes_copied;
1177 splice_async_thread (GSimpleAsyncResult *result,
1179 GCancellable *cancellable)
1182 GOutputStreamClass *class;
1183 GError *error = NULL;
1184 GOutputStream *stream;
1186 stream = G_OUTPUT_STREAM (object);
1187 class = G_OUTPUT_STREAM_GET_CLASS (object);
1188 op = g_simple_async_result_get_op_res_gpointer (result);
1190 op->bytes_copied = class->splice (stream,
1195 if (op->bytes_copied == -1)
1197 g_simple_async_result_set_from_error (result, error);
1198 g_error_free (error);
1203 g_output_stream_real_splice_async (GOutputStream *stream,
1204 GInputStream *source,
1205 GOutputStreamSpliceFlags flags,
1207 GCancellable *cancellable,
1208 GAsyncReadyCallback callback,
1211 GSimpleAsyncResult *res;
1214 op = g_new0 (SpliceData, 1);
1215 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1216 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1218 op->source = source;
1220 /* TODO: In the case where both source and destintion have
1221 non-threadbased async calls we can use a true async copy here */
1223 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1224 g_object_unref (res);
1228 g_output_stream_real_splice_finish (GOutputStream *stream,
1229 GAsyncResult *result,
1232 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1235 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1236 op = g_simple_async_result_get_op_res_gpointer (simple);
1237 return op->bytes_copied;
1242 flush_async_thread (GSimpleAsyncResult *res,
1244 GCancellable *cancellable)
1246 GOutputStreamClass *class;
1248 GError *error = NULL;
1250 class = G_OUTPUT_STREAM_GET_CLASS (object);
1253 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1257 g_simple_async_result_set_from_error (res, error);
1258 g_error_free (error);
1263 g_output_stream_real_flush_async (GOutputStream *stream,
1265 GCancellable *cancellable,
1266 GAsyncReadyCallback callback,
1269 GSimpleAsyncResult *res;
1271 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1273 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1274 g_object_unref (res);
1278 g_output_stream_real_flush_finish (GOutputStream *stream,
1279 GAsyncResult *result,
1286 close_async_thread (GSimpleAsyncResult *res,
1288 GCancellable *cancellable)
1290 GOutputStreamClass *class;
1291 GError *error = NULL;
1294 /* Auto handling of cancelation disabled, and ignore
1295 cancellation, since we want to close things anyway, although
1296 possibly in a quick-n-dirty way. At least we never want to leak
1299 class = G_OUTPUT_STREAM_GET_CLASS (object);
1300 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1303 g_simple_async_result_set_from_error (res, error);
1304 g_error_free (error);
1309 g_output_stream_real_close_async (GOutputStream *stream,
1311 GCancellable *cancellable,
1312 GAsyncReadyCallback callback,
1315 GSimpleAsyncResult *res;
1317 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1319 g_simple_async_result_set_handle_cancellation (res, FALSE);
1321 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1322 g_object_unref (res);
1326 g_output_stream_real_close_finish (GOutputStream *stream,
1327 GAsyncResult *result,
1330 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1331 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1335 #define __G_OUTPUT_STREAM_C__
1336 #include "gioaliasdef.c"