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"
34 * SECTION:goutputstream
35 * @short_description: Base class for implementing streaming output
38 * GOutputStream has functions to write to a stream (g_output_stream_write()),
39 * to close a stream (g_output_stream_close()) and to flush pending writes
40 * (g_output_stream_flush()).
42 * To copy the content of an input stream to an output stream without
43 * manually handling the reads and writes, use g_output_stream_splice().
45 * All of these functions have async variants too.
48 G_DEFINE_ABSTRACT_TYPE (GOutputStream
, g_output_stream
, G_TYPE_OBJECT
);
50 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
,
98 static gboolean
_g_output_stream_close_internal (GOutputStream
*stream
,
99 GCancellable
*cancellable
,
103 g_output_stream_finalize (GObject
*object
)
105 G_OBJECT_CLASS (g_output_stream_parent_class
)->finalize (object
);
109 g_output_stream_dispose (GObject
*object
)
111 GOutputStream
*stream
;
113 stream
= G_OUTPUT_STREAM (object
);
115 if (!stream
->priv
->closed
)
116 g_output_stream_close (stream
, NULL
, NULL
);
118 G_OBJECT_CLASS (g_output_stream_parent_class
)->dispose (object
);
122 g_output_stream_class_init (GOutputStreamClass
*klass
)
124 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
126 g_type_class_add_private (klass
, sizeof (GOutputStreamPrivate
));
128 gobject_class
->finalize
= g_output_stream_finalize
;
129 gobject_class
->dispose
= g_output_stream_dispose
;
131 klass
->splice
= g_output_stream_real_splice
;
133 klass
->write_async
= g_output_stream_real_write_async
;
134 klass
->write_finish
= g_output_stream_real_write_finish
;
135 klass
->splice_async
= g_output_stream_real_splice_async
;
136 klass
->splice_finish
= g_output_stream_real_splice_finish
;
137 klass
->flush_async
= g_output_stream_real_flush_async
;
138 klass
->flush_finish
= g_output_stream_real_flush_finish
;
139 klass
->close_async
= g_output_stream_real_close_async
;
140 klass
->close_finish
= g_output_stream_real_close_finish
;
144 g_output_stream_init (GOutputStream
*stream
)
146 stream
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (stream
,
147 G_TYPE_OUTPUT_STREAM
,
148 GOutputStreamPrivate
);
152 * g_output_stream_write:
153 * @stream: a #GOutputStream.
154 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
155 * @count: the number of bytes to write
156 * @cancellable: (allow-none): optional cancellable object
157 * @error: location to store the error occurring, or %NULL to ignore
159 * Tries to write @count bytes from @buffer into the stream. Will block
160 * during the operation.
162 * If count is 0, returns 0 and does nothing. A value of @count
163 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
165 * On success, the number of bytes written to the stream is returned.
166 * It is not an error if this is not the same as the requested size, as it
167 * can happen e.g. on a partial I/O error, or if there is not enough
168 * storage in the stream. All writes block until at least one byte
169 * is written or an error occurs; 0 is never returned (unless
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: (array length=count) (element-type guint8): the buffer containing the data to write.
234 * @count: the number of bytes to write
235 * @bytes_written: (out): location to store the number of bytes that was
236 * written to the stream
237 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
238 * @error: location to store the error occurring, 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: (allow-none): optional cancellable object
297 * @error: location to store the error occurring, or %NULL to ignore
299 * Forces a write of all user-space buffered data for the given
300 * @stream. Will block during the operation. Closing the stream will
301 * implicitly cause a flush.
303 * This function is optional for inherited classes.
305 * If @cancellable is not %NULL, then the operation can be cancelled by
306 * triggering the cancellable object from another thread. If the operation
307 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
309 * Return value: %TRUE on success, %FALSE on error
312 g_output_stream_flush (GOutputStream
*stream
,
313 GCancellable
*cancellable
,
316 GOutputStreamClass
*class;
319 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
321 if (!g_output_stream_set_pending (stream
, error
))
324 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
330 g_cancellable_push_current (cancellable
);
332 res
= class->flush (stream
, cancellable
, error
);
335 g_cancellable_pop_current (cancellable
);
338 g_output_stream_clear_pending (stream
);
344 * g_output_stream_splice:
345 * @stream: a #GOutputStream.
346 * @source: a #GInputStream.
347 * @flags: a set of #GOutputStreamSpliceFlags.
348 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
349 * @error: a #GError location to store the error occurring, or %NULL to
352 * Splices an input stream into an output stream.
354 * Returns: a #gssize containing the size of the data spliced, or
355 * -1 if an error occurred. Note that if the number of bytes
356 * spliced is greater than %G_MAXSSIZE, then that will be
357 * returned, and there is no way to determine the actual number
361 g_output_stream_splice (GOutputStream
*stream
,
362 GInputStream
*source
,
363 GOutputStreamSpliceFlags flags
,
364 GCancellable
*cancellable
,
367 GOutputStreamClass
*class;
370 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), -1);
371 g_return_val_if_fail (G_IS_INPUT_STREAM (source
), -1);
373 if (g_input_stream_is_closed (source
))
375 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_CLOSED
,
376 _("Source stream is already closed"));
380 if (!g_output_stream_set_pending (stream
, error
))
383 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
386 g_cancellable_push_current (cancellable
);
388 bytes_copied
= class->splice (stream
, source
, flags
, cancellable
, error
);
391 g_cancellable_pop_current (cancellable
);
393 g_output_stream_clear_pending (stream
);
399 g_output_stream_real_splice (GOutputStream
*stream
,
400 GInputStream
*source
,
401 GOutputStreamSpliceFlags flags
,
402 GCancellable
*cancellable
,
405 GOutputStreamClass
*class = G_OUTPUT_STREAM_GET_CLASS (stream
);
406 gssize n_read
, n_written
;
408 char buffer
[8192], *p
;
412 if (class->write_fn
== NULL
)
414 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
415 _("Output stream doesn't implement write"));
423 n_read
= g_input_stream_read (source
, buffer
, sizeof (buffer
), cancellable
, error
);
436 n_written
= class->write_fn (stream
, p
, n_read
, cancellable
, error
);
445 bytes_copied
+= n_written
;
448 if (bytes_copied
> G_MAXSSIZE
)
449 bytes_copied
= G_MAXSSIZE
;
455 error
= NULL
; /* Ignore further errors */
457 if (flags
& G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE
)
459 /* Don't care about errors in source here */
460 g_input_stream_close (source
, cancellable
, NULL
);
463 if (flags
& G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET
)
465 /* But write errors on close are bad! */
466 res
= _g_output_stream_close_internal (stream
, cancellable
, error
);
475 /* Must always be called inside
476 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
478 _g_output_stream_close_internal (GOutputStream
*stream
,
479 GCancellable
*cancellable
,
482 GOutputStreamClass
*class;
485 if (stream
->priv
->closed
)
488 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
490 stream
->priv
->closing
= TRUE
;
493 g_cancellable_push_current (cancellable
);
496 res
= class->flush (stream
, cancellable
, error
);
502 /* flushing caused the error that we want to return,
503 * but we still want to close the underlying stream if possible
506 class->close_fn (stream
, cancellable
, NULL
);
512 res
= class->close_fn (stream
, cancellable
, error
);
516 g_cancellable_pop_current (cancellable
);
518 stream
->priv
->closing
= FALSE
;
519 stream
->priv
->closed
= TRUE
;
525 * g_output_stream_close:
526 * @stream: A #GOutputStream.
527 * @cancellable: (allow-none): optional cancellable object
528 * @error: location to store the error occurring, or %NULL to ignore
530 * Closes the stream, releasing resources related to it.
532 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
533 * Closing a stream multiple times will not return an error.
535 * Closing a stream will automatically flush any outstanding buffers in the
538 * Streams will be automatically closed when the last reference
539 * is dropped, but you might want to call this function to make sure
540 * resources are released as early as possible.
542 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
543 * open after the stream is closed. See the documentation for the individual
544 * stream for details.
546 * On failure the first error that happened will be reported, but the close
547 * operation will finish as much as possible. A stream that failed to
548 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
549 * is important to check and report the error to the user, otherwise
550 * there might be a loss of data as all data might not be written.
552 * If @cancellable is not NULL, then the operation can be cancelled by
553 * triggering the cancellable object from another thread. If the operation
554 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
555 * Cancelling a close will still leave the stream closed, but there some streams
556 * can use a faster close that doesn't block to e.g. check errors. On
557 * cancellation (as with any error) there is no guarantee that all written
558 * data will reach the target.
560 * Return value: %TRUE on success, %FALSE on failure
563 g_output_stream_close (GOutputStream
*stream
,
564 GCancellable
*cancellable
,
569 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
571 if (stream
->priv
->closed
)
574 if (!g_output_stream_set_pending (stream
, error
))
577 res
= _g_output_stream_close_internal (stream
, cancellable
, error
);
579 g_output_stream_clear_pending (stream
);
585 async_ready_callback_wrapper (GObject
*source_object
,
589 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
591 g_output_stream_clear_pending (stream
);
592 if (stream
->priv
->outstanding_callback
)
593 (*stream
->priv
->outstanding_callback
) (source_object
, res
, user_data
);
594 g_object_unref (stream
);
599 GCancellable
*cancellable
;
605 async_ready_close_callback_wrapper (GObject
*source_object
,
609 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
610 CloseUserData
*data
= user_data
;
612 stream
->priv
->closing
= FALSE
;
613 stream
->priv
->closed
= TRUE
;
615 g_output_stream_clear_pending (stream
);
617 if (stream
->priv
->outstanding_callback
)
619 if (data
->flush_error
!= NULL
)
621 GSimpleAsyncResult
*err
;
623 err
= g_simple_async_result_new_take_error (source_object
,
624 stream
->priv
->outstanding_callback
,
627 data
->flush_error
= NULL
;
629 (*stream
->priv
->outstanding_callback
) (source_object
,
630 G_ASYNC_RESULT (err
),
632 g_object_unref (err
);
636 (*stream
->priv
->outstanding_callback
) (source_object
,
642 g_object_unref (stream
);
644 if (data
->cancellable
)
645 g_object_unref (data
->cancellable
);
647 if (data
->flush_error
)
648 g_error_free (data
->flush_error
);
650 g_slice_free (CloseUserData
, data
);
654 async_ready_close_flushed_callback_wrapper (GObject
*source_object
,
658 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
659 GOutputStreamClass
*class;
660 CloseUserData
*data
= user_data
;
661 GSimpleAsyncResult
*simple
;
663 /* propagate the possible error */
664 if (G_IS_SIMPLE_ASYNC_RESULT (res
))
666 simple
= G_SIMPLE_ASYNC_RESULT (res
);
667 g_simple_async_result_propagate_error (simple
, &data
->flush_error
);
670 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
672 /* we still close, even if there was a flush error */
673 class->close_async (stream
, data
->io_priority
, data
->cancellable
,
674 async_ready_close_callback_wrapper
, user_data
);
678 * g_output_stream_write_async:
679 * @stream: A #GOutputStream.
680 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
681 * @count: the number of bytes to write
682 * @io_priority: the io priority of the request.
683 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
684 * @callback: (scope async): callback to call when the request is satisfied
685 * @user_data: (closure): the data to pass to callback function
687 * Request an asynchronous write of @count bytes from @buffer into
688 * the stream. When the operation is finished @callback will be called.
689 * You can then call g_output_stream_write_finish() to get the result of the
692 * During an async request no other sync and async calls are allowed,
693 * and will result in %G_IO_ERROR_PENDING errors.
695 * A value of @count larger than %G_MAXSSIZE will cause a
696 * %G_IO_ERROR_INVALID_ARGUMENT error.
698 * On success, the number of bytes written will be passed to the
699 * @callback. It is not an error if this is not the same as the
700 * requested size, as it can happen e.g. on a partial I/O error,
701 * but generally we try to write as many bytes as requested.
703 * You are guaranteed that this method will never fail with
704 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
705 * method will just wait until this changes.
707 * Any outstanding I/O request with higher priority (lower numerical
708 * value) will be executed before an outstanding request with lower
709 * priority. Default priority is %G_PRIORITY_DEFAULT.
711 * The asyncronous methods have a default fallback that uses threads
712 * to implement asynchronicity, so they are optional for inheriting
713 * classes. However, if you override one you must override all.
715 * For the synchronous, blocking version of this function, see
716 * g_output_stream_write().
719 g_output_stream_write_async (GOutputStream
*stream
,
723 GCancellable
*cancellable
,
724 GAsyncReadyCallback callback
,
727 GOutputStreamClass
*class;
728 GSimpleAsyncResult
*simple
;
729 GError
*error
= NULL
;
731 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
732 g_return_if_fail (buffer
!= NULL
);
736 simple
= g_simple_async_result_new (G_OBJECT (stream
),
739 g_output_stream_write_async
);
740 g_simple_async_result_complete_in_idle (simple
);
741 g_object_unref (simple
);
745 if (((gssize
) count
) < 0)
747 g_simple_async_report_error_in_idle (G_OBJECT (stream
),
750 G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
751 _("Too large count value passed to %s"),
756 if (!g_output_stream_set_pending (stream
, &error
))
758 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
765 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
767 stream
->priv
->outstanding_callback
= callback
;
768 g_object_ref (stream
);
769 class->write_async (stream
, buffer
, count
, io_priority
, cancellable
,
770 async_ready_callback_wrapper
, user_data
);
774 * g_output_stream_write_finish:
775 * @stream: a #GOutputStream.
776 * @result: a #GAsyncResult.
777 * @error: a #GError location to store the error occurring, or %NULL to
780 * Finishes a stream write operation.
782 * Returns: a #gssize containing the number of bytes written to the stream.
785 g_output_stream_write_finish (GOutputStream
*stream
,
786 GAsyncResult
*result
,
789 GSimpleAsyncResult
*simple
;
790 GOutputStreamClass
*class;
792 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), -1);
793 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), -1);
795 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
797 simple
= G_SIMPLE_ASYNC_RESULT (result
);
798 if (g_simple_async_result_propagate_error (simple
, error
))
801 /* Special case writes of 0 bytes */
802 if (g_simple_async_result_get_source_tag (simple
) == g_output_stream_write_async
)
806 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
807 return class->write_finish (stream
, result
, error
);
811 GInputStream
*source
;
813 GAsyncReadyCallback callback
;
817 async_ready_splice_callback_wrapper (GObject
*source_object
,
821 GOutputStream
*stream
= G_OUTPUT_STREAM (source_object
);
822 SpliceUserData
*data
= _data
;
824 g_output_stream_clear_pending (stream
);
827 (*data
->callback
) (source_object
, res
, data
->user_data
);
829 g_object_unref (stream
);
830 g_object_unref (data
->source
);
835 * g_output_stream_splice_async:
836 * @stream: a #GOutputStream.
837 * @source: a #GInputStream.
838 * @flags: a set of #GOutputStreamSpliceFlags.
839 * @io_priority: the io priority of the request.
840 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
841 * @callback: (scope async): a #GAsyncReadyCallback.
842 * @user_data: (closure): user data passed to @callback.
844 * Splices a stream asynchronously.
845 * When the operation is finished @callback will be called.
846 * You can then call g_output_stream_splice_finish() to get the
847 * result of the operation.
849 * For the synchronous, blocking version of this function, see
850 * g_output_stream_splice().
853 g_output_stream_splice_async (GOutputStream
*stream
,
854 GInputStream
*source
,
855 GOutputStreamSpliceFlags flags
,
857 GCancellable
*cancellable
,
858 GAsyncReadyCallback callback
,
861 GOutputStreamClass
*class;
862 SpliceUserData
*data
;
863 GError
*error
= NULL
;
865 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
866 g_return_if_fail (G_IS_INPUT_STREAM (source
));
868 if (g_input_stream_is_closed (source
))
870 g_simple_async_report_error_in_idle (G_OBJECT (stream
),
873 G_IO_ERROR
, G_IO_ERROR_CLOSED
,
874 _("Source stream is already closed"));
878 if (!g_output_stream_set_pending (stream
, &error
))
880 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
887 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
889 data
= g_new0 (SpliceUserData
, 1);
890 data
->callback
= callback
;
891 data
->user_data
= user_data
;
892 data
->source
= g_object_ref (source
);
894 g_object_ref (stream
);
895 class->splice_async (stream
, source
, flags
, io_priority
, cancellable
,
896 async_ready_splice_callback_wrapper
, data
);
900 * g_output_stream_splice_finish:
901 * @stream: a #GOutputStream.
902 * @result: a #GAsyncResult.
903 * @error: a #GError location to store the error occurring, or %NULL to
906 * Finishes an asynchronous stream splice operation.
908 * Returns: a #gssize of the number of bytes spliced. Note that if the
909 * number of bytes spliced is greater than %G_MAXSSIZE, then that
910 * will be returned, and there is no way to determine the actual
911 * number of bytes spliced.
914 g_output_stream_splice_finish (GOutputStream
*stream
,
915 GAsyncResult
*result
,
918 GSimpleAsyncResult
*simple
;
919 GOutputStreamClass
*class;
921 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), -1);
922 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), -1);
924 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
926 simple
= G_SIMPLE_ASYNC_RESULT (result
);
927 if (g_simple_async_result_propagate_error (simple
, error
))
931 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
932 return class->splice_finish (stream
, result
, error
);
936 * g_output_stream_flush_async:
937 * @stream: a #GOutputStream.
938 * @io_priority: the io priority of the request.
939 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
940 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
941 * @user_data: (closure): the data to pass to callback function
943 * Forces an asynchronous write of all user-space buffered data for
945 * For behaviour details see g_output_stream_flush().
947 * When the operation is finished @callback will be
948 * called. You can then call g_output_stream_flush_finish() to get the
949 * result of the operation.
952 g_output_stream_flush_async (GOutputStream
*stream
,
954 GCancellable
*cancellable
,
955 GAsyncReadyCallback callback
,
958 GOutputStreamClass
*class;
959 GSimpleAsyncResult
*simple
;
960 GError
*error
= NULL
;
962 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
964 if (!g_output_stream_set_pending (stream
, &error
))
966 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
973 stream
->priv
->outstanding_callback
= callback
;
974 g_object_ref (stream
);
976 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
978 if (class->flush_async
== NULL
)
980 simple
= g_simple_async_result_new (G_OBJECT (stream
),
981 async_ready_callback_wrapper
,
983 g_output_stream_flush_async
);
984 g_simple_async_result_complete_in_idle (simple
);
985 g_object_unref (simple
);
989 class->flush_async (stream
, io_priority
, cancellable
,
990 async_ready_callback_wrapper
, user_data
);
994 * g_output_stream_flush_finish:
995 * @stream: a #GOutputStream.
996 * @result: a GAsyncResult.
997 * @error: a #GError location to store the error occurring, or %NULL to
1000 * Finishes flushing an output stream.
1002 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1005 g_output_stream_flush_finish (GOutputStream
*stream
,
1006 GAsyncResult
*result
,
1009 GSimpleAsyncResult
*simple
;
1010 GOutputStreamClass
*klass
;
1012 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1013 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
1015 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
1017 simple
= G_SIMPLE_ASYNC_RESULT (result
);
1018 if (g_simple_async_result_propagate_error (simple
, error
))
1021 /* Special case default implementation */
1022 if (g_simple_async_result_get_source_tag (simple
) == g_output_stream_flush_async
)
1026 klass
= G_OUTPUT_STREAM_GET_CLASS (stream
);
1027 return klass
->flush_finish (stream
, result
, error
);
1032 * g_output_stream_close_async:
1033 * @stream: A #GOutputStream.
1034 * @io_priority: the io priority of the request.
1035 * @cancellable: (allow-none): optional cancellable object
1036 * @callback: (scope async): callback to call when the request is satisfied
1037 * @user_data: (closure): the data to pass to callback function
1039 * Requests an asynchronous close of the stream, releasing resources
1040 * related to it. When the operation is finished @callback will be
1041 * called. You can then call g_output_stream_close_finish() to get
1042 * the result of the operation.
1044 * For behaviour details see g_output_stream_close().
1046 * The asyncronous methods have a default fallback that uses threads
1047 * to implement asynchronicity, so they are optional for inheriting
1048 * classes. However, if you override one you must override all.
1051 g_output_stream_close_async (GOutputStream
*stream
,
1053 GCancellable
*cancellable
,
1054 GAsyncReadyCallback callback
,
1057 GOutputStreamClass
*class;
1058 GSimpleAsyncResult
*simple
;
1059 GError
*error
= NULL
;
1060 CloseUserData
*data
;
1062 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
1064 if (stream
->priv
->closed
)
1066 simple
= g_simple_async_result_new (G_OBJECT (stream
),
1069 g_output_stream_close_async
);
1070 g_simple_async_result_complete_in_idle (simple
);
1071 g_object_unref (simple
);
1075 if (!g_output_stream_set_pending (stream
, &error
))
1077 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream
),
1084 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
1085 stream
->priv
->closing
= TRUE
;
1086 stream
->priv
->outstanding_callback
= callback
;
1087 g_object_ref (stream
);
1089 data
= g_slice_new0 (CloseUserData
);
1091 if (cancellable
!= NULL
)
1092 data
->cancellable
= g_object_ref (cancellable
);
1094 data
->io_priority
= io_priority
;
1095 data
->user_data
= user_data
;
1097 /* Call close_async directly if there is no need to flush, or if the flush
1098 can be done sync (in the output stream async close thread) */
1099 if (class->flush_async
== NULL
||
1100 (class->flush_async
== g_output_stream_real_flush_async
&&
1101 (class->flush
== NULL
|| class->close_async
== g_output_stream_real_close_async
)))
1103 class->close_async (stream
, io_priority
, cancellable
,
1104 async_ready_close_callback_wrapper
, data
);
1108 /* First do an async flush, then do the async close in the callback
1109 wrapper (see async_ready_close_flushed_callback_wrapper) */
1110 class->flush_async (stream
, io_priority
, cancellable
,
1111 async_ready_close_flushed_callback_wrapper
, data
);
1116 * g_output_stream_close_finish:
1117 * @stream: a #GOutputStream.
1118 * @result: a #GAsyncResult.
1119 * @error: a #GError location to store the error occurring, or %NULL to
1122 * Closes an output stream.
1124 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1127 g_output_stream_close_finish (GOutputStream
*stream
,
1128 GAsyncResult
*result
,
1131 GSimpleAsyncResult
*simple
;
1132 GOutputStreamClass
*class;
1134 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1135 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
1137 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
1139 simple
= G_SIMPLE_ASYNC_RESULT (result
);
1140 if (g_simple_async_result_propagate_error (simple
, error
))
1143 /* Special case already closed */
1144 if (g_simple_async_result_get_source_tag (simple
) == g_output_stream_close_async
)
1148 class = G_OUTPUT_STREAM_GET_CLASS (stream
);
1149 return class->close_finish (stream
, result
, error
);
1153 * g_output_stream_is_closed:
1154 * @stream: a #GOutputStream.
1156 * Checks if an output stream has already been closed.
1158 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1161 g_output_stream_is_closed (GOutputStream
*stream
)
1163 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), TRUE
);
1165 return stream
->priv
->closed
;
1169 * g_output_stream_is_closing:
1170 * @stream: a #GOutputStream.
1172 * Checks if an output stream is being closed. This can be
1173 * used inside e.g. a flush implementation to see if the
1174 * flush (or other i/o operation) is called from within
1175 * the closing operation.
1177 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1182 g_output_stream_is_closing (GOutputStream
*stream
)
1184 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), TRUE
);
1186 return stream
->priv
->closing
;
1190 * g_output_stream_has_pending:
1191 * @stream: a #GOutputStream.
1193 * Checks if an ouput stream has pending actions.
1195 * Returns: %TRUE if @stream has pending actions.
1198 g_output_stream_has_pending (GOutputStream
*stream
)
1200 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1202 return stream
->priv
->pending
;
1206 * g_output_stream_set_pending:
1207 * @stream: a #GOutputStream.
1208 * @error: a #GError location to store the error occurring, or %NULL to
1211 * Sets @stream to have actions pending. If the pending flag is
1212 * already set or @stream is closed, it will return %FALSE and set
1215 * Return value: %TRUE if pending was previously unset and is now set.
1218 g_output_stream_set_pending (GOutputStream
*stream
,
1221 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream
), FALSE
);
1223 if (stream
->priv
->closed
)
1225 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_CLOSED
,
1226 _("Stream is already closed"));
1230 if (stream
->priv
->pending
)
1232 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_PENDING
,
1233 /* Translators: This is an error you get if there is
1234 * already an operation running against this stream when
1235 * you try to start one */
1236 _("Stream has outstanding operation"));
1240 stream
->priv
->pending
= TRUE
;
1245 * g_output_stream_clear_pending:
1246 * @stream: output stream
1248 * Clears the pending flag on @stream.
1251 g_output_stream_clear_pending (GOutputStream
*stream
)
1253 g_return_if_fail (G_IS_OUTPUT_STREAM (stream
));
1255 stream
->priv
->pending
= FALSE
;
1259 /********************************************
1260 * Default implementation of async ops *
1261 ********************************************/
1265 gsize count_requested
;
1266 gssize count_written
;
1270 write_async_thread (GSimpleAsyncResult
*res
,
1272 GCancellable
*cancellable
)
1275 GOutputStreamClass
*class;
1276 GError
*error
= NULL
;
1278 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1279 op
= g_simple_async_result_get_op_res_gpointer (res
);
1280 op
->count_written
= class->write_fn (G_OUTPUT_STREAM (object
), op
->buffer
, op
->count_requested
,
1281 cancellable
, &error
);
1282 if (op
->count_written
== -1)
1283 g_simple_async_result_take_error (res
, error
);
1287 g_output_stream_real_write_async (GOutputStream
*stream
,
1291 GCancellable
*cancellable
,
1292 GAsyncReadyCallback callback
,
1295 GSimpleAsyncResult
*res
;
1298 op
= g_new0 (WriteData
, 1);
1299 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_write_async
);
1300 g_simple_async_result_set_op_res_gpointer (res
, op
, g_free
);
1301 op
->buffer
= buffer
;
1302 op
->count_requested
= count
;
1304 g_simple_async_result_run_in_thread (res
, write_async_thread
, io_priority
, cancellable
);
1305 g_object_unref (res
);
1309 g_output_stream_real_write_finish (GOutputStream
*stream
,
1310 GAsyncResult
*result
,
1313 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1316 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_output_stream_real_write_async
);
1317 op
= g_simple_async_result_get_op_res_gpointer (simple
);
1318 return op
->count_written
;
1322 GInputStream
*source
;
1323 GOutputStreamSpliceFlags flags
;
1324 gssize bytes_copied
;
1328 splice_async_thread (GSimpleAsyncResult
*result
,
1330 GCancellable
*cancellable
)
1333 GOutputStreamClass
*class;
1334 GError
*error
= NULL
;
1335 GOutputStream
*stream
;
1337 stream
= G_OUTPUT_STREAM (object
);
1338 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1339 op
= g_simple_async_result_get_op_res_gpointer (result
);
1341 op
->bytes_copied
= class->splice (stream
,
1346 if (op
->bytes_copied
== -1)
1347 g_simple_async_result_take_error (result
, error
);
1351 g_output_stream_real_splice_async (GOutputStream
*stream
,
1352 GInputStream
*source
,
1353 GOutputStreamSpliceFlags flags
,
1355 GCancellable
*cancellable
,
1356 GAsyncReadyCallback callback
,
1359 GSimpleAsyncResult
*res
;
1362 op
= g_new0 (SpliceData
, 1);
1363 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_splice_async
);
1364 g_simple_async_result_set_op_res_gpointer (res
, op
, g_free
);
1366 op
->source
= source
;
1368 /* TODO: In the case where both source and destintion have
1369 non-threadbased async calls we can use a true async copy here */
1371 g_simple_async_result_run_in_thread (res
, splice_async_thread
, io_priority
, cancellable
);
1372 g_object_unref (res
);
1376 g_output_stream_real_splice_finish (GOutputStream
*stream
,
1377 GAsyncResult
*result
,
1380 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1383 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_output_stream_real_splice_async
);
1384 op
= g_simple_async_result_get_op_res_gpointer (simple
);
1385 return op
->bytes_copied
;
1390 flush_async_thread (GSimpleAsyncResult
*res
,
1392 GCancellable
*cancellable
)
1394 GOutputStreamClass
*class;
1396 GError
*error
= NULL
;
1398 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1401 result
= class->flush (G_OUTPUT_STREAM (object
), cancellable
, &error
);
1404 g_simple_async_result_take_error (res
, error
);
1408 g_output_stream_real_flush_async (GOutputStream
*stream
,
1410 GCancellable
*cancellable
,
1411 GAsyncReadyCallback callback
,
1414 GSimpleAsyncResult
*res
;
1416 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_write_async
);
1418 g_simple_async_result_run_in_thread (res
, flush_async_thread
, io_priority
, cancellable
);
1419 g_object_unref (res
);
1423 g_output_stream_real_flush_finish (GOutputStream
*stream
,
1424 GAsyncResult
*result
,
1431 close_async_thread (GSimpleAsyncResult
*res
,
1433 GCancellable
*cancellable
)
1435 GOutputStreamClass
*class;
1436 GError
*error
= NULL
;
1437 gboolean result
= TRUE
;
1439 class = G_OUTPUT_STREAM_GET_CLASS (object
);
1441 /* Do a flush here if there is a flush function, and we did not have to do
1442 an async flush before (see g_output_stream_close_async) */
1443 if (class->flush
!= NULL
&&
1444 (class->flush_async
== NULL
||
1445 class->flush_async
== g_output_stream_real_flush_async
))
1447 result
= class->flush (G_OUTPUT_STREAM (object
), cancellable
, &error
);
1450 /* Auto handling of cancelation disabled, and ignore
1451 cancellation, since we want to close things anyway, although
1452 possibly in a quick-n-dirty way. At least we never want to leak
1455 if (class->close_fn
)
1457 /* Make sure to close, even if the flush failed (see sync close) */
1459 class->close_fn (G_OUTPUT_STREAM (object
), cancellable
, NULL
);
1461 result
= class->close_fn (G_OUTPUT_STREAM (object
), cancellable
, &error
);
1464 g_simple_async_result_take_error (res
, error
);
1469 g_output_stream_real_close_async (GOutputStream
*stream
,
1471 GCancellable
*cancellable
,
1472 GAsyncReadyCallback callback
,
1475 GSimpleAsyncResult
*res
;
1477 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_output_stream_real_close_async
);
1479 g_simple_async_result_set_handle_cancellation (res
, FALSE
);
1481 g_simple_async_result_run_in_thread (res
, close_async_thread
, io_priority
, cancellable
);
1482 g_object_unref (res
);
1486 g_output_stream_real_close_finish (GOutputStream
*stream
,
1487 GAsyncResult
*result
,
1490 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1491 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_output_stream_real_close_async
);