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
42 G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
44 struct _GOutputStreamPrivate {
48 GAsyncReadyCallback outstanding_callback;
51 static gssize g_output_stream_real_splice (GOutputStream *stream,
53 GOutputStreamSpliceFlags flags,
54 GCancellable *cancellable,
56 static void g_output_stream_real_write_async (GOutputStream *stream,
60 GCancellable *cancellable,
61 GAsyncReadyCallback callback,
63 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
66 static void g_output_stream_real_splice_async (GOutputStream *stream,
68 GOutputStreamSpliceFlags flags,
70 GCancellable *cancellable,
71 GAsyncReadyCallback callback,
73 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
76 static void g_output_stream_real_flush_async (GOutputStream *stream,
78 GCancellable *cancellable,
79 GAsyncReadyCallback callback,
81 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
84 static void g_output_stream_real_close_async (GOutputStream *stream,
86 GCancellable *cancellable,
87 GAsyncReadyCallback callback,
89 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
94 g_output_stream_finalize (GObject *object)
96 GOutputStream *stream;
98 stream = G_OUTPUT_STREAM (object);
100 G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
104 g_output_stream_dispose (GObject *object)
106 GOutputStream *stream;
108 stream = G_OUTPUT_STREAM (object);
110 if (!stream->priv->closed)
111 g_output_stream_close (stream, NULL, NULL);
113 G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
117 g_output_stream_class_init (GOutputStreamClass *klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
123 gobject_class->finalize = g_output_stream_finalize;
124 gobject_class->dispose = g_output_stream_dispose;
126 klass->splice = g_output_stream_real_splice;
128 klass->write_async = g_output_stream_real_write_async;
129 klass->write_finish = g_output_stream_real_write_finish;
130 klass->splice_async = g_output_stream_real_splice_async;
131 klass->splice_finish = g_output_stream_real_splice_finish;
132 klass->flush_async = g_output_stream_real_flush_async;
133 klass->flush_finish = g_output_stream_real_flush_finish;
134 klass->close_async = g_output_stream_real_close_async;
135 klass->close_finish = g_output_stream_real_close_finish;
139 g_output_stream_init (GOutputStream *stream)
141 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
142 G_TYPE_OUTPUT_STREAM,
143 GOutputStreamPrivate);
147 * g_output_stream_write:
148 * @stream: a #GOutputStream.
149 * @buffer: the buffer containing the data to write.
150 * @count: the number of bytes to write
151 * @cancellable: optional cancellable object
152 * @error: location to store the error occuring, or %NULL to ignore
154 * Tries to write @count bytes from @buffer into the stream. Will block
155 * during the operation.
157 * If count is zero returns zero and does nothing. A value of @count
158 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
160 * On success, the number of bytes written to the stream is returned.
161 * It is not an error if this is not the same as the requested size, as it
162 * can happen e.g. on a partial i/o error, or if there is not enough
163 * storage in the stream. All writes either block until at least one byte
164 * is written, so zero is never returned (unless @count is zero).
166 * If @cancellable is not NULL, then the operation can be cancelled by
167 * triggering the cancellable object from another thread. If the operation
168 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
169 * operation was partially finished when the operation was cancelled the
170 * partial result will be returned, without an error.
172 * On error -1 is returned and @error is set accordingly.
174 * Return value: Number of bytes written, or -1 on error
177 g_output_stream_write (GOutputStream *stream,
180 GCancellable *cancellable,
183 GOutputStreamClass *class;
186 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
187 g_return_val_if_fail (buffer != NULL, 0);
192 if (((gssize) count) < 0)
194 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
195 _("Too large count value passed to %s"), G_STRFUNC);
199 class = G_OUTPUT_STREAM_GET_CLASS (stream);
201 if (class->write_fn == NULL)
203 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
204 _("Output stream doesn't implement write"));
208 if (!g_output_stream_set_pending (stream, error))
212 g_cancellable_push_current (cancellable);
214 res = class->write_fn (stream, buffer, count, cancellable, error);
217 g_cancellable_pop_current (cancellable);
219 g_output_stream_clear_pending (stream);
225 * g_output_stream_write_all:
226 * @stream: a #GOutputStream.
227 * @buffer: the buffer containing the data to write.
228 * @count: the number of bytes to write
229 * @bytes_written: location to store the number of bytes that was
230 * written to the stream
231 * @cancellable: optional #GCancellable object, %NULL to ignore.
232 * @error: location to store the error occuring, or %NULL to ignore
234 * Tries to write @count bytes from @buffer into the stream. Will block
235 * during the operation.
237 * This function is similar to g_output_stream_write(), except it tries to
238 * write as many bytes as requested, only stopping on an error.
240 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
243 * If there is an error during the operation FALSE is returned and @error
244 * is set to indicate the error status, @bytes_written is updated to contain
245 * the number of bytes written into the stream before the error occurred.
247 * Return value: %TRUE on success, %FALSE if there was an error
250 g_output_stream_write_all (GOutputStream *stream,
253 gsize *bytes_written,
254 GCancellable *cancellable,
257 gsize _bytes_written;
260 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
261 g_return_val_if_fail (buffer != NULL, FALSE);
264 while (_bytes_written < count)
266 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
271 *bytes_written = _bytes_written;
276 g_warning ("Write returned zero without error");
278 _bytes_written += res;
282 *bytes_written = _bytes_written;
288 * g_output_stream_flush:
289 * @stream: a #GOutputStream.
290 * @cancellable: optional cancellable object
291 * @error: location to store the error occuring, or %NULL to ignore
293 * Flushed any outstanding buffers in the stream. Will block during
294 * the operation. Closing the stream will implicitly cause a flush.
296 * This function is optional for inherited classes.
298 * If @cancellable is not %NULL, then the operation can be cancelled by
299 * triggering the cancellable object from another thread. If the operation
300 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
302 * Return value: %TRUE on success, %FALSE on error
305 g_output_stream_flush (GOutputStream *stream,
306 GCancellable *cancellable,
309 GOutputStreamClass *class;
312 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
314 if (!g_output_stream_set_pending (stream, error))
317 class = G_OUTPUT_STREAM_GET_CLASS (stream);
323 g_cancellable_push_current (cancellable);
325 res = class->flush (stream, cancellable, error);
328 g_cancellable_pop_current (cancellable);
331 g_output_stream_clear_pending (stream);
337 * g_output_stream_splice:
338 * @stream: a #GOutputStream.
339 * @source: a #GInputStream.
340 * @flags: a set of #GOutputStreamSpliceFlags.
341 * @cancellable: optional #GCancellable object, %NULL to ignore.
342 * @error: a #GError location to store the error occuring, or %NULL to
345 * Splices an input stream into an output stream.
347 * Returns: a #gssize containing the size of the data spliced.
350 g_output_stream_splice (GOutputStream *stream,
351 GInputStream *source,
352 GOutputStreamSpliceFlags flags,
353 GCancellable *cancellable,
356 GOutputStreamClass *class;
359 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
360 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
362 if (g_input_stream_is_closed (source))
364 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
365 _("Source stream is already closed"));
369 if (!g_output_stream_set_pending (stream, error))
372 class = G_OUTPUT_STREAM_GET_CLASS (stream);
376 g_cancellable_push_current (cancellable);
378 res = class->splice (stream, source, flags, cancellable, error);
381 g_cancellable_pop_current (cancellable);
383 g_output_stream_clear_pending (stream);
389 g_output_stream_real_splice (GOutputStream *stream,
390 GInputStream *source,
391 GOutputStreamSpliceFlags flags,
392 GCancellable *cancellable,
395 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
396 gssize n_read, n_written;
398 char buffer[8192], *p;
402 if (class->write_fn == NULL)
404 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
405 _("Output stream doesn't implement write"));
413 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
426 n_written = class->write_fn (stream, p, n_read, cancellable, error);
435 bytes_copied += n_written;
442 error = NULL; /* Ignore further errors */
444 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
446 /* Don't care about errors in source here */
447 g_input_stream_close (source, cancellable, NULL);
450 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
452 /* But write errors on close are bad! */
453 if (!class->close_fn (stream, cancellable, error))
465 * g_output_stream_close:
466 * @stream: A #GOutputStream.
467 * @cancellable: optional cancellable object
468 * @error: location to store the error occuring, or %NULL to ignore
470 * Closes the stream, releasing resources related to it.
472 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
473 * Closing a stream multiple times will not return an error.
475 * Closing a stream will automatically flush any outstanding buffers in the
478 * Streams will be automatically closed when the last reference
479 * is dropped, but you might want to call this function to make sure
480 * resources are released as early as possible.
482 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
483 * open after the stream is closed. See the documentation for the individual
484 * stream for details.
486 * On failure the first error that happened will be reported, but the close
487 * operation will finish as much as possible. A stream that failed to
488 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
489 * is important to check and report the error to the user, otherwise
490 * there might be a loss of data as all data might not be written.
492 * If @cancellable is not NULL, then the operation can be cancelled by
493 * triggering the cancellable object from another thread. If the operation
494 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
495 * Cancelling a close will still leave the stream closed, but there some streams
496 * can use a faster close that doesn't block to e.g. check errors. On
497 * cancellation (as with any error) there is no guarantee that all written
498 * data will reach the target.
500 * Return value: %TRUE on success, %FALSE on failure
503 g_output_stream_close (GOutputStream *stream,
504 GCancellable *cancellable,
507 GOutputStreamClass *class;
510 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
512 class = G_OUTPUT_STREAM_GET_CLASS (stream);
514 if (stream->priv->closed)
517 if (!g_output_stream_set_pending (stream, error))
521 g_cancellable_push_current (cancellable);
524 res = class->flush (stream, cancellable, error);
530 /* flushing caused the error that we want to return,
531 * but we still want to close the underlying stream if possible
534 class->close_fn (stream, cancellable, NULL);
540 res = class->close_fn (stream, cancellable, error);
544 g_cancellable_pop_current (cancellable);
546 stream->priv->closed = TRUE;
547 g_output_stream_clear_pending (stream);
553 async_ready_callback_wrapper (GObject *source_object,
557 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
559 g_output_stream_clear_pending (stream);
560 if (stream->priv->outstanding_callback)
561 (*stream->priv->outstanding_callback) (source_object, res, user_data);
562 g_object_unref (stream);
566 async_ready_close_callback_wrapper (GObject *source_object,
570 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
572 stream->priv->closed = TRUE;
573 g_output_stream_clear_pending (stream);
574 if (stream->priv->outstanding_callback)
575 (*stream->priv->outstanding_callback) (source_object, res, user_data);
576 g_object_unref (stream);
580 * g_output_stream_write_async:
581 * @stream: A #GOutputStream.
582 * @buffer: the buffer containing the data to write.
583 * @count: the number of bytes to write
584 * @io_priority: the io priority of the request.
585 * @cancellable: optional #GCancellable object, %NULL to ignore.
586 * @callback: callback to call when the request is satisfied
587 * @user_data: the data to pass to callback function
589 * Request an asynchronous write of @count bytes from @buffer into
590 * the stream. When the operation is finished @callback will be called.
591 * You can then call g_output_stream_write_finish() to get the result of the
594 * During an async request no other sync and async calls are allowed,
595 * and will result in %G_IO_ERROR_PENDING errors.
597 * A value of @count larger than %G_MAXSSIZE will cause a
598 * %G_IO_ERROR_INVALID_ARGUMENT error.
600 * On success, the number of bytes written will be passed to the
601 * @callback. It is not an error if this is not the same as the
602 * requested size, as it can happen e.g. on a partial I/O error,
603 * but generally we try to write as many bytes as requested.
605 * Any outstanding I/O request with higher priority (lower numerical
606 * value) will be executed before an outstanding request with lower
607 * priority. Default priority is %G_PRIORITY_DEFAULT.
609 * The asyncronous methods have a default fallback that uses threads
610 * to implement asynchronicity, so they are optional for inheriting
611 * classes. However, if you override one you must override all.
613 * For the synchronous, blocking version of this function, see
614 * g_output_stream_write().
617 g_output_stream_write_async (GOutputStream *stream,
621 GCancellable *cancellable,
622 GAsyncReadyCallback callback,
625 GOutputStreamClass *class;
626 GSimpleAsyncResult *simple;
627 GError *error = NULL;
629 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
630 g_return_if_fail (buffer != NULL);
634 simple = g_simple_async_result_new (G_OBJECT (stream),
637 g_output_stream_write_async);
638 g_simple_async_result_complete_in_idle (simple);
639 g_object_unref (simple);
643 if (((gssize) count) < 0)
645 g_simple_async_report_error_in_idle (G_OBJECT (stream),
648 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
649 _("Too large count value passed to %s"),
654 if (!g_output_stream_set_pending (stream, &error))
656 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
660 g_error_free (error);
664 class = G_OUTPUT_STREAM_GET_CLASS (stream);
666 stream->priv->outstanding_callback = callback;
667 g_object_ref (stream);
668 class->write_async (stream, buffer, count, io_priority, cancellable,
669 async_ready_callback_wrapper, user_data);
673 * g_output_stream_write_finish:
674 * @stream: a #GOutputStream.
675 * @result: a #GAsyncResult.
676 * @error: a #GError location to store the error occuring, or %NULL to
679 * Finishes a stream write operation.
681 * Returns: a #gssize containing the number of bytes written to the stream.
684 g_output_stream_write_finish (GOutputStream *stream,
685 GAsyncResult *result,
688 GSimpleAsyncResult *simple;
689 GOutputStreamClass *class;
691 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
692 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
694 if (G_IS_SIMPLE_ASYNC_RESULT (result))
696 simple = G_SIMPLE_ASYNC_RESULT (result);
697 if (g_simple_async_result_propagate_error (simple, error))
700 /* Special case writes of 0 bytes */
701 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
705 class = G_OUTPUT_STREAM_GET_CLASS (stream);
706 return class->write_finish (stream, result, error);
710 GInputStream *source;
712 GAsyncReadyCallback callback;
716 async_ready_splice_callback_wrapper (GObject *source_object,
720 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
721 SpliceUserData *data = _data;
723 g_output_stream_clear_pending (stream);
726 (*data->callback) (source_object, res, data->user_data);
728 g_object_unref (stream);
729 g_object_unref (data->source);
734 * g_output_stream_splice_async:
735 * @stream: a #GOutputStream.
736 * @source: a #GInputStream.
737 * @flags: a set of #GOutputStreamSpliceFlags.
738 * @io_priority: the io priority of the request.
739 * @cancellable: optional #GCancellable object, %NULL to ignore.
740 * @callback: a #GAsyncReadyCallback.
741 * @user_data: user data passed to @callback.
743 * Splices a stream asynchronously.
744 * When the operation is finished @callback will be called.
745 * You can then call g_output_stream_splice_finish() to get the
746 * result of the operation.
748 * For the synchronous, blocking version of this function, see
749 * g_output_stream_splice().
752 g_output_stream_splice_async (GOutputStream *stream,
753 GInputStream *source,
754 GOutputStreamSpliceFlags flags,
756 GCancellable *cancellable,
757 GAsyncReadyCallback callback,
760 GOutputStreamClass *class;
761 SpliceUserData *data;
762 GError *error = NULL;
764 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
765 g_return_if_fail (G_IS_INPUT_STREAM (source));
767 if (g_input_stream_is_closed (source))
769 g_simple_async_report_error_in_idle (G_OBJECT (stream),
772 G_IO_ERROR, G_IO_ERROR_CLOSED,
773 _("Source stream is already closed"));
777 if (!g_output_stream_set_pending (stream, &error))
779 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
783 g_error_free (error);
787 class = G_OUTPUT_STREAM_GET_CLASS (stream);
789 data = g_new0 (SpliceUserData, 1);
790 data->callback = callback;
791 data->user_data = user_data;
792 data->source = g_object_ref (source);
794 g_object_ref (stream);
795 class->splice_async (stream, source, flags, io_priority, cancellable,
796 async_ready_splice_callback_wrapper, data);
800 * g_output_stream_splice_finish:
801 * @stream: a #GOutputStream.
802 * @result: a #GAsyncResult.
803 * @error: a #GError location to store the error occuring, or %NULL to
806 * Finishes an asynchronous stream splice operation.
808 * Returns: a #gssize of the number of bytes spliced.
811 g_output_stream_splice_finish (GOutputStream *stream,
812 GAsyncResult *result,
815 GSimpleAsyncResult *simple;
816 GOutputStreamClass *class;
818 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
819 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
821 if (G_IS_SIMPLE_ASYNC_RESULT (result))
823 simple = G_SIMPLE_ASYNC_RESULT (result);
824 if (g_simple_async_result_propagate_error (simple, error))
828 class = G_OUTPUT_STREAM_GET_CLASS (stream);
829 return class->splice_finish (stream, result, error);
833 * g_output_stream_flush_async:
834 * @stream: a #GOutputStream.
835 * @io_priority: the io priority of the request.
836 * @cancellable: optional #GCancellable object, %NULL to ignore.
837 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
838 * @user_data: the data to pass to callback function
840 * Flushes a stream asynchronously.
841 * For behaviour details see g_output_stream_flush().
843 * When the operation is finished @callback will be
844 * called. You can then call g_output_stream_flush_finish() to get the
845 * result of the operation.
848 g_output_stream_flush_async (GOutputStream *stream,
850 GCancellable *cancellable,
851 GAsyncReadyCallback callback,
854 GOutputStreamClass *class;
855 GSimpleAsyncResult *simple;
856 GError *error = NULL;
858 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
860 if (!g_output_stream_set_pending (stream, &error))
862 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
866 g_error_free (error);
870 stream->priv->outstanding_callback = callback;
871 g_object_ref (stream);
873 class = G_OUTPUT_STREAM_GET_CLASS (stream);
875 if (class->flush_async == NULL)
877 simple = g_simple_async_result_new (G_OBJECT (stream),
878 async_ready_callback_wrapper,
880 g_output_stream_flush_async);
881 g_simple_async_result_complete_in_idle (simple);
882 g_object_unref (simple);
886 class->flush_async (stream, io_priority, cancellable,
887 async_ready_callback_wrapper, user_data);
891 * g_output_stream_flush_finish:
892 * @stream: a #GOutputStream.
893 * @result: a GAsyncResult.
894 * @error: a #GError location to store the error occuring, or %NULL to
897 * Finishes flushing an output stream.
899 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
902 g_output_stream_flush_finish (GOutputStream *stream,
903 GAsyncResult *result,
906 GSimpleAsyncResult *simple;
907 GOutputStreamClass *klass;
909 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
910 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
912 if (G_IS_SIMPLE_ASYNC_RESULT (result))
914 simple = G_SIMPLE_ASYNC_RESULT (result);
915 if (g_simple_async_result_propagate_error (simple, error))
918 /* Special case default implementation */
919 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
923 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
924 return klass->flush_finish (stream, result, error);
929 * g_output_stream_close_async:
930 * @stream: A #GOutputStream.
931 * @io_priority: the io priority of the request.
932 * @callback: callback to call when the request is satisfied
933 * @user_data: the data to pass to callback function
934 * @cancellable: optional cancellable object
936 * Requests an asynchronous close of the stream, releasing resources
937 * related to it. When the operation is finished @callback will be
938 * called. You can then call g_output_stream_close_finish() to get
939 * the result of the operation.
941 * For behaviour details see g_output_stream_close().
943 * The asyncronous methods have a default fallback that uses threads
944 * to implement asynchronicity, so they are optional for inheriting
945 * classes. However, if you override one you must override all.
948 g_output_stream_close_async (GOutputStream *stream,
950 GCancellable *cancellable,
951 GAsyncReadyCallback callback,
954 GOutputStreamClass *class;
955 GSimpleAsyncResult *simple;
956 GError *error = NULL;
958 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
960 if (stream->priv->closed)
962 simple = g_simple_async_result_new (G_OBJECT (stream),
965 g_output_stream_close_async);
966 g_simple_async_result_complete_in_idle (simple);
967 g_object_unref (simple);
971 if (!g_output_stream_set_pending (stream, &error))
973 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
977 g_error_free (error);
981 class = G_OUTPUT_STREAM_GET_CLASS (stream);
982 stream->priv->outstanding_callback = callback;
983 g_object_ref (stream);
984 class->close_async (stream, io_priority, cancellable,
985 async_ready_close_callback_wrapper, user_data);
989 * g_output_stream_close_finish:
990 * @stream: a #GOutputStream.
991 * @result: a #GAsyncResult.
992 * @error: a #GError location to store the error occuring, or %NULL to
995 * Closes an output stream.
997 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1000 g_output_stream_close_finish (GOutputStream *stream,
1001 GAsyncResult *result,
1004 GSimpleAsyncResult *simple;
1005 GOutputStreamClass *class;
1007 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1008 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1010 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1012 simple = G_SIMPLE_ASYNC_RESULT (result);
1013 if (g_simple_async_result_propagate_error (simple, error))
1016 /* Special case already closed */
1017 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1021 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1022 return class->close_finish (stream, result, error);
1026 * g_output_stream_is_closed:
1027 * @stream: a #GOutputStream.
1029 * Checks if an output stream has already been closed.
1031 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1034 g_output_stream_is_closed (GOutputStream *stream)
1036 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1038 return stream->priv->closed;
1042 * g_output_stream_has_pending:
1043 * @stream: a #GOutputStream.
1045 * Checks if an ouput stream has pending actions.
1047 * Returns: %TRUE if @stream has pending actions.
1050 g_output_stream_has_pending (GOutputStream *stream)
1052 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1054 return stream->priv->pending;
1058 * g_output_stream_set_pending:
1059 * @stream: a #GOutputStream.
1060 * @error: a #GError location to store the error occuring, or %NULL to
1063 * Sets @stream to have actions pending. If the pending flag is
1064 * already set or @stream is closed, it will return %FALSE and set
1067 * Return value: %TRUE if pending was previously unset and is now set.
1070 g_output_stream_set_pending (GOutputStream *stream,
1073 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1075 if (stream->priv->closed)
1077 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1078 _("Stream is already closed"));
1082 if (stream->priv->pending)
1084 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1085 /* Translators: This is an error you get if there is
1086 * already an operation running against this stream when
1087 * you try to start one */
1088 _("Stream has outstanding operation"));
1092 stream->priv->pending = TRUE;
1097 * g_output_stream_clear_pending:
1098 * @stream: output stream
1100 * Clears the pending flag on @stream.
1103 g_output_stream_clear_pending (GOutputStream *stream)
1105 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1107 stream->priv->pending = FALSE;
1111 /********************************************
1112 * Default implementation of async ops *
1113 ********************************************/
1117 gsize count_requested;
1118 gssize count_written;
1122 write_async_thread (GSimpleAsyncResult *res,
1124 GCancellable *cancellable)
1127 GOutputStreamClass *class;
1128 GError *error = NULL;
1130 class = G_OUTPUT_STREAM_GET_CLASS (object);
1131 op = g_simple_async_result_get_op_res_gpointer (res);
1132 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1133 cancellable, &error);
1134 if (op->count_written == -1)
1136 g_simple_async_result_set_from_error (res, error);
1137 g_error_free (error);
1142 g_output_stream_real_write_async (GOutputStream *stream,
1146 GCancellable *cancellable,
1147 GAsyncReadyCallback callback,
1150 GSimpleAsyncResult *res;
1153 op = g_new0 (WriteData, 1);
1154 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1155 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1156 op->buffer = buffer;
1157 op->count_requested = count;
1159 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1160 g_object_unref (res);
1164 g_output_stream_real_write_finish (GOutputStream *stream,
1165 GAsyncResult *result,
1168 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1171 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1172 op = g_simple_async_result_get_op_res_gpointer (simple);
1173 return op->count_written;
1177 GInputStream *source;
1178 GOutputStreamSpliceFlags flags;
1179 gssize bytes_copied;
1183 splice_async_thread (GSimpleAsyncResult *result,
1185 GCancellable *cancellable)
1188 GOutputStreamClass *class;
1189 GError *error = NULL;
1190 GOutputStream *stream;
1192 stream = G_OUTPUT_STREAM (object);
1193 class = G_OUTPUT_STREAM_GET_CLASS (object);
1194 op = g_simple_async_result_get_op_res_gpointer (result);
1196 op->bytes_copied = class->splice (stream,
1201 if (op->bytes_copied == -1)
1203 g_simple_async_result_set_from_error (result, error);
1204 g_error_free (error);
1209 g_output_stream_real_splice_async (GOutputStream *stream,
1210 GInputStream *source,
1211 GOutputStreamSpliceFlags flags,
1213 GCancellable *cancellable,
1214 GAsyncReadyCallback callback,
1217 GSimpleAsyncResult *res;
1220 op = g_new0 (SpliceData, 1);
1221 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1222 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1224 op->source = source;
1226 /* TODO: In the case where both source and destintion have
1227 non-threadbased async calls we can use a true async copy here */
1229 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1230 g_object_unref (res);
1234 g_output_stream_real_splice_finish (GOutputStream *stream,
1235 GAsyncResult *result,
1238 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1241 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1242 op = g_simple_async_result_get_op_res_gpointer (simple);
1243 return op->bytes_copied;
1248 flush_async_thread (GSimpleAsyncResult *res,
1250 GCancellable *cancellable)
1252 GOutputStreamClass *class;
1254 GError *error = NULL;
1256 class = G_OUTPUT_STREAM_GET_CLASS (object);
1259 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1263 g_simple_async_result_set_from_error (res, error);
1264 g_error_free (error);
1269 g_output_stream_real_flush_async (GOutputStream *stream,
1271 GCancellable *cancellable,
1272 GAsyncReadyCallback callback,
1275 GSimpleAsyncResult *res;
1277 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1279 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1280 g_object_unref (res);
1284 g_output_stream_real_flush_finish (GOutputStream *stream,
1285 GAsyncResult *result,
1292 close_async_thread (GSimpleAsyncResult *res,
1294 GCancellable *cancellable)
1296 GOutputStreamClass *class;
1297 GError *error = NULL;
1300 /* Auto handling of cancelation disabled, and ignore
1301 cancellation, since we want to close things anyway, although
1302 possibly in a quick-n-dirty way. At least we never want to leak
1305 class = G_OUTPUT_STREAM_GET_CLASS (object);
1306 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1309 g_simple_async_result_set_from_error (res, error);
1310 g_error_free (error);
1315 g_output_stream_real_close_async (GOutputStream *stream,
1317 GCancellable *cancellable,
1318 GAsyncReadyCallback callback,
1321 GSimpleAsyncResult *res;
1323 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1325 g_simple_async_result_set_handle_cancellation (res, FALSE);
1327 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1328 g_object_unref (res);
1332 g_output_stream_real_close_finish (GOutputStream *stream,
1333 GAsyncResult *result,
1336 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1337 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1341 #define __G_OUTPUT_STREAM_C__
1342 #include "gioaliasdef.c"