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>
25 #include "goutputstream.h"
26 #include "gsimpleasyncresult.h"
29 G_DEFINE_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
31 struct _GOutputStreamPrivate {
35 GAsyncReadyCallback outstanding_callback;
38 static gssize g_output_stream_real_splice (GOutputStream *stream,
40 GOutputStreamSpliceFlags flags,
41 GCancellable *cancellable,
43 static void g_output_stream_real_write_async (GOutputStream *stream,
47 GCancellable *cancellable,
48 GAsyncReadyCallback callback,
50 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
53 static void g_output_stream_real_splice_async (GOutputStream *stream,
55 GOutputStreamSpliceFlags flags,
57 GCancellable *cancellable,
58 GAsyncReadyCallback callback,
60 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
63 static void g_output_stream_real_flush_async (GOutputStream *stream,
65 GCancellable *cancellable,
66 GAsyncReadyCallback callback,
68 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
71 static void g_output_stream_real_close_async (GOutputStream *stream,
73 GCancellable *cancellable,
74 GAsyncReadyCallback callback,
76 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
81 g_output_stream_finalize (GObject *object)
83 GOutputStream *stream;
85 stream = G_OUTPUT_STREAM (object);
87 if (G_OBJECT_CLASS (g_output_stream_parent_class)->finalize)
88 (*G_OBJECT_CLASS (g_output_stream_parent_class)->finalize) (object);
92 g_output_stream_dispose (GObject *object)
94 GOutputStream *stream;
96 stream = G_OUTPUT_STREAM (object);
98 if (!stream->priv->closed)
99 g_output_stream_close (stream, NULL, NULL);
101 if (G_OBJECT_CLASS (g_output_stream_parent_class)->dispose)
102 (*G_OBJECT_CLASS (g_output_stream_parent_class)->dispose) (object);
106 g_output_stream_class_init (GOutputStreamClass *klass)
108 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
110 g_type_class_add_private (klass, sizeof (GOutputStreamPrivate));
112 gobject_class->finalize = g_output_stream_finalize;
113 gobject_class->dispose = g_output_stream_dispose;
115 klass->splice = g_output_stream_real_splice;
117 klass->write_async = g_output_stream_real_write_async;
118 klass->write_finish = g_output_stream_real_write_finish;
119 klass->splice_async = g_output_stream_real_splice_async;
120 klass->splice_finish = g_output_stream_real_splice_finish;
121 klass->flush_async = g_output_stream_real_flush_async;
122 klass->flush_finish = g_output_stream_real_flush_finish;
123 klass->close_async = g_output_stream_real_close_async;
124 klass->close_finish = g_output_stream_real_close_finish;
128 g_output_stream_init (GOutputStream *stream)
130 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
131 G_TYPE_OUTPUT_STREAM,
132 GOutputStreamPrivate);
136 * g_output_stream_write:
137 * @stream: a #GOutputStream.
138 * @buffer: the buffer containing the data to write.
139 * @count: the number of bytes to write
140 * @cancellable: optional cancellable object
141 * @error: location to store the error occuring, or %NULL to ignore
143 * Tries to write @count bytes from @buffer into the stream. Will block
144 * during the operation.
146 * If count is zero returns zero and does nothing. A value of @count
147 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
149 * On success, the number of bytes written to the stream is returned.
150 * It is not an error if this is not the same as the requested size, as it
151 * can happen e.g. on a partial i/o error, or if the there is not enough
152 * storage in the stream. All writes either block until at least one byte
153 * is written, so zero is never returned (unless @count is zero).
155 * If @cancellable is not NULL, then the operation can be cancelled by
156 * triggering the cancellable object from another thread. If the operation
157 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
158 * operation was partially finished when the operation was cancelled the
159 * partial result will be returned, without an error.
161 * On error -1 is returned and @error is set accordingly.
163 * Return value: Number of bytes written, or -1 on error
166 g_output_stream_write (GOutputStream *stream,
169 GCancellable *cancellable,
172 GOutputStreamClass *class;
175 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
176 g_return_val_if_fail (buffer != NULL, 0);
181 if (((gssize) count) < 0)
183 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
184 _("Too large count value passed to g_output_stream_write"));
188 if (stream->priv->closed)
190 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
191 _("Stream is already closed"));
195 if (stream->priv->pending)
197 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
198 _("Stream has outstanding operation"));
202 class = G_OUTPUT_STREAM_GET_CLASS (stream);
204 if (class->write == NULL)
206 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
207 _("Output stream doesn't implement write"));
212 g_push_current_cancellable (cancellable);
214 stream->priv->pending = TRUE;
215 res = class->write (stream, buffer, count, cancellable, error);
216 stream->priv->pending = FALSE;
219 g_pop_current_cancellable (cancellable);
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 written to the stream
230 * @cancellable: optional #GCancellable object, %NULL to ignore.
231 * @error: location to store the error occuring, or %NULL to ignore
233 * Tries to write @count bytes from @buffer into the stream. Will block
234 * during the operation.
236 * This function is similar to g_output_stream_write(), except it tries to
237 * write as many bytes as requested, only stopping on an error.
239 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
242 * If there is an error during the operation FALSE is returned and @error
243 * is set to indicate the error status, @bytes_written is updated to contain
244 * the number of bytes written into the stream before the error occured.
246 * Return value: %TRUE on success, %FALSE if there was an error
249 g_output_stream_write_all (GOutputStream *stream,
252 gsize *bytes_written,
253 GCancellable *cancellable,
256 gsize _bytes_written;
259 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
260 g_return_val_if_fail (buffer != NULL, FALSE);
263 while (_bytes_written < count)
265 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
270 *bytes_written = _bytes_written;
275 g_warning ("Write returned zero without error");
277 _bytes_written += res;
281 *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 the operation.
292 * 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 (stream->priv->closed)
314 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
315 _("Stream is already closed"));
319 if (stream->priv->pending)
321 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
322 _("Stream has outstanding operation"));
326 class = G_OUTPUT_STREAM_GET_CLASS (stream);
332 g_push_current_cancellable (cancellable);
334 stream->priv->pending = TRUE;
335 res = class->flush (stream, cancellable, error);
336 stream->priv->pending = FALSE;
339 g_pop_current_cancellable (cancellable);
346 * g_output_stream_splice:
350 * @cancellable: optional #GCancellable object, %NULL to ignore.
351 * @error: a #GError location to store the error occuring, or %NULL to
358 g_output_stream_splice (GOutputStream *stream,
359 GInputStream *source,
360 GOutputStreamSpliceFlags flags,
361 GCancellable *cancellable,
364 GOutputStreamClass *class;
367 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
368 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
370 if (stream->priv->closed)
372 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
373 _("Target stream is already closed"));
377 if (g_input_stream_is_closed (source))
379 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
380 _("Source stream is already closed"));
384 if (stream->priv->pending)
386 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
387 _("Stream has outstanding operation"));
391 class = G_OUTPUT_STREAM_GET_CLASS (stream);
395 g_push_current_cancellable (cancellable);
397 stream->priv->pending = TRUE;
398 res = class->splice (stream, source, flags, cancellable, error);
399 stream->priv->pending = FALSE;
402 g_pop_current_cancellable (cancellable);
408 g_output_stream_real_splice (GOutputStream *stream,
409 GInputStream *source,
410 GOutputStreamSpliceFlags flags,
411 GCancellable *cancellable,
414 gssize n_read, n_written;
416 char buffer[8192], *p;
423 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
436 stream->priv->pending = FALSE;
437 n_written = g_output_stream_write (stream, p, n_read, cancellable, error);
438 stream->priv->pending = TRUE;
447 bytes_copied += n_written;
453 error = NULL; /* Ignore further errors */
455 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_SOURCE)
457 /* Don't care about errors in source here */
458 g_input_stream_close (source, cancellable, NULL);
461 if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_TARGET)
463 /* But write errors on close are bad! */
464 stream->priv->pending = FALSE;
465 if (!g_output_stream_close (stream, cancellable, error))
467 stream->priv->pending = TRUE;
478 * g_output_stream_close:
479 * @stream: A #GOutputStream.
480 * @cancellable: optional cancellable object
481 * @error: location to store the error occuring, or %NULL to ignore
483 * Closes the stream, releasing resources related to it.
485 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
486 * Closing a stream multiple times will not return an error.
488 * Closing a stream will automatically flush any outstanding buffers in the
491 * Streams will be automatically closed when the last reference
492 * is dropped, but you might want to call make sure resources
493 * are released as early as possible.
495 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
496 * open after the stream is closed. See the documentation for the individual
497 * stream for details.
499 * On failure the first error that happened will be reported, but the close
500 * operation will finish as much as possible. A stream that failed to
501 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
502 * is important to check and report the error to the user, otherwise
503 * there might be a loss of data as all data might not be written.
505 * If @cancellable is not NULL, then the operation can be cancelled by
506 * triggering the cancellable object from another thread. If the operation
507 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
508 * Cancelling a close will still leave the stream closed, but there some streams
509 * can use a faster close that doesn't block to e.g. check errors. On
510 * cancellation (as with any error) there is no guarantee that all written
511 * data will reach the target.
513 * Return value: %TRUE on success, %FALSE on failure
516 g_output_stream_close (GOutputStream *stream,
517 GCancellable *cancellable,
520 GOutputStreamClass *class;
523 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
525 class = G_OUTPUT_STREAM_GET_CLASS (stream);
527 if (stream->priv->closed)
530 if (stream->priv->pending)
532 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
533 _("Stream has outstanding operation"));
537 res = g_output_stream_flush (stream, cancellable, error);
539 stream->priv->pending = TRUE;
542 g_push_current_cancellable (cancellable);
546 /* flushing caused the error that we want to return,
547 * but we still want to close the underlying stream if possible
550 class->close (stream, cancellable, NULL);
556 res = class->close (stream, cancellable, error);
560 g_pop_current_cancellable (cancellable);
562 stream->priv->closed = TRUE;
563 stream->priv->pending = FALSE;
569 async_ready_callback_wrapper (GObject *source_object,
573 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
575 stream->priv->pending = FALSE;
576 if (stream->priv->outstanding_callback)
577 (*stream->priv->outstanding_callback) (source_object, res, user_data);
578 g_object_unref (stream);
582 async_ready_close_callback_wrapper (GObject *source_object,
586 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
588 stream->priv->pending = FALSE;
589 stream->priv->closed = TRUE;
590 if (stream->priv->outstanding_callback)
591 (*stream->priv->outstanding_callback) (source_object, res, user_data);
592 g_object_unref (stream);
596 * g_output_stream_write_async:
597 * @stream: A #GOutputStream.
598 * @buffer: the buffer containing the data to write.
599 * @count: the number of bytes to write
600 * @io_priority: the io priority of the request. the io priority of the request
601 * @cancellable: optional #GCancellable object, %NULL to ignore.
602 * @callback: callback to call when the request is satisfied
603 * @user_data: the data to pass to callback function
605 * Request an asynchronous write of @count bytes from @buffer into the stream.
606 * When the operation is finished @callback will be called, giving the results.
608 * During an async request no other sync and async calls are allowed, and will
609 * result in %G_IO_ERROR_PENDING errors.
611 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
613 * On success, the number of bytes written will be passed to the
614 * @callback. It is not an error if this is not the same as the requested size, as it
615 * can happen e.g. on a partial i/o error, but generally we try to write
616 * as many bytes as requested.
618 * Any outstanding i/o request with higher priority (lower numerical value) will
619 * be executed before an outstanding request with lower priority. Default
620 * priority is %G_PRIORITY_DEFAULT.
622 * The asyncronous methods have a default fallback that uses threads to implement
623 * asynchronicity, so they are optional for inheriting classes. However, if you
624 * override one you must override all.
626 * For the synchronous, blocking version of this function, see g_output_stream_write().
629 g_output_stream_write_async (GOutputStream *stream,
633 GCancellable *cancellable,
634 GAsyncReadyCallback callback,
637 GOutputStreamClass *class;
638 GSimpleAsyncResult *simple;
640 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
641 g_return_if_fail (buffer != NULL);
645 simple = g_simple_async_result_new (G_OBJECT (stream),
648 g_output_stream_write_async);
649 g_simple_async_result_complete_in_idle (simple);
650 g_object_unref (simple);
654 if (((gssize) count) < 0)
656 g_simple_async_report_error_in_idle (G_OBJECT (stream),
659 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
660 _("Too large count value passed to g_output_stream_write_async"));
664 if (stream->priv->closed)
666 g_simple_async_report_error_in_idle (G_OBJECT (stream),
669 G_IO_ERROR, G_IO_ERROR_CLOSED,
670 _("Stream is already closed"));
674 if (stream->priv->pending)
676 g_simple_async_report_error_in_idle (G_OBJECT (stream),
679 G_IO_ERROR, G_IO_ERROR_PENDING,
680 _("Stream has outstanding operation"));
684 class = G_OUTPUT_STREAM_GET_CLASS (stream);
686 stream->priv->pending = TRUE;
687 stream->priv->outstanding_callback = callback;
688 g_object_ref (stream);
689 class->write_async (stream, buffer, count, io_priority, cancellable,
690 async_ready_callback_wrapper, user_data);
694 * g_output_stream_write_finish:
695 * @stream: a #GOutputStream.
696 * @result: a #GAsyncResult.
697 * @error: a #GError location to store the error occuring, or %NULL to
704 g_output_stream_write_finish (GOutputStream *stream,
705 GAsyncResult *result,
708 GSimpleAsyncResult *simple;
709 GOutputStreamClass *class;
711 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
712 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
714 if (G_IS_SIMPLE_ASYNC_RESULT (result))
716 simple = G_SIMPLE_ASYNC_RESULT (result);
717 if (g_simple_async_result_propagate_error (simple, error))
720 /* Special case writes of 0 bytes */
721 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
725 class = G_OUTPUT_STREAM_GET_CLASS (stream);
726 return class->write_finish (stream, result, error);
730 GInputStream *source;
732 GAsyncReadyCallback callback;
736 async_ready_splice_callback_wrapper (GObject *source_object,
740 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
741 SpliceUserData *data = _data;
743 stream->priv->pending = FALSE;
746 (*data->callback) (source_object, res, data->user_data);
748 g_object_unref (stream);
749 g_object_unref (data->source);
754 * g_output_stream_splice_async:
758 * @io_priority: the io priority of the request.
759 * @cancellable: optional #GCancellable object, %NULL to ignore.
765 g_output_stream_splice_async (GOutputStream *stream,
766 GInputStream *source,
767 GOutputStreamSpliceFlags flags,
769 GCancellable *cancellable,
770 GAsyncReadyCallback callback,
773 GOutputStreamClass *class;
774 SpliceUserData *data;
776 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
777 g_return_if_fail (G_IS_INPUT_STREAM (source));
779 if (stream->priv->closed)
781 g_simple_async_report_error_in_idle (G_OBJECT (stream),
784 G_IO_ERROR, G_IO_ERROR_CLOSED,
785 _("Target stream is already closed"));
789 if (g_input_stream_is_closed (source))
791 g_simple_async_report_error_in_idle (G_OBJECT (stream),
794 G_IO_ERROR, G_IO_ERROR_CLOSED,
795 _("Source stream is already closed"));
799 if (stream->priv->pending)
801 g_simple_async_report_error_in_idle (G_OBJECT (stream),
804 G_IO_ERROR, G_IO_ERROR_PENDING,
805 _("Stream has outstanding operation"));
809 class = G_OUTPUT_STREAM_GET_CLASS (stream);
811 stream->priv->pending = TRUE;
813 data = g_new0 (SpliceUserData, 1);
814 data->callback = callback;
815 data->user_data = user_data;
816 data->source = g_object_ref (source);
818 g_object_ref (stream);
819 class->splice_async (stream, source, flags, io_priority, cancellable,
820 async_ready_splice_callback_wrapper, data);
824 * g_output_stream_splice_finish:
825 * @stream: a #GOutputStream.
826 * @result: a #GAsyncResult.
827 * @error: a #GError location to store the error occuring, or %NULL to
833 g_output_stream_splice_finish (GOutputStream *stream,
834 GAsyncResult *result,
837 GSimpleAsyncResult *simple;
838 GOutputStreamClass *class;
840 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
841 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
843 if (G_IS_SIMPLE_ASYNC_RESULT (result))
845 simple = G_SIMPLE_ASYNC_RESULT (result);
846 if (g_simple_async_result_propagate_error (simple, error))
850 class = G_OUTPUT_STREAM_GET_CLASS (stream);
851 return class->splice_finish (stream, result, error);
855 * g_output_stream_flush_async:
857 * @io_priority: the io priority of the request.
858 * @cancellable: optional #GCancellable object, %NULL to ignore.
859 * @callback: a #GAsyncReadyCallback.
864 g_output_stream_flush_async (GOutputStream *stream,
866 GCancellable *cancellable,
867 GAsyncReadyCallback callback,
870 GOutputStreamClass *class;
871 GSimpleAsyncResult *simple;
873 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
875 if (stream->priv->closed)
877 g_simple_async_report_error_in_idle (G_OBJECT (stream),
880 G_IO_ERROR, G_IO_ERROR_CLOSED,
881 _("Stream is already closed"));
885 if (stream->priv->pending)
887 g_simple_async_report_error_in_idle (G_OBJECT (stream),
890 G_IO_ERROR, G_IO_ERROR_PENDING,
891 _("Stream has outstanding operation"));
895 class = G_OUTPUT_STREAM_GET_CLASS (stream);
897 if (class->flush_async == NULL)
899 simple = g_simple_async_result_new (G_OBJECT (stream),
902 g_output_stream_flush_async);
903 g_simple_async_result_complete_in_idle (simple);
904 g_object_unref (simple);
908 stream->priv->pending = TRUE;
909 stream->priv->outstanding_callback = callback;
910 g_object_ref (stream);
911 class->flush_async (stream, io_priority, cancellable,
912 async_ready_callback_wrapper, user_data);
916 * g_output_stream_flush_finish:
917 * @stream: a #GOutputStream.
918 * @result: a GAsyncResult.
919 * @error: a #GError location to store the error occuring, or %NULL to
921 * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
924 g_output_stream_flush_finish (GOutputStream *stream,
925 GAsyncResult *result,
928 GSimpleAsyncResult *simple;
929 GOutputStreamClass *klass;
931 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
932 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
934 if (G_IS_SIMPLE_ASYNC_RESULT (result))
936 simple = G_SIMPLE_ASYNC_RESULT (result);
937 if (g_simple_async_result_propagate_error (simple, error))
940 /* Special case default implementation */
941 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
945 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
946 return klass->flush_finish (stream, result, error);
951 * g_output_stream_close_async:
952 * @stream: A #GOutputStream.
953 * @callback: callback to call when the request is satisfied
954 * @user_data: the data to pass to callback function
955 * @cancellable: optional cancellable object
957 * Requests an asynchronous closes of the stream, releasing resources related to it.
958 * When the operation is finished @callback will be called, giving the results.
960 * For behaviour details see g_output_stream_close().
962 * The asyncronous methods have a default fallback that uses threads to implement
963 * asynchronicity, so they are optional for inheriting classes. However, if you
964 * override one you must override all.
967 g_output_stream_close_async (GOutputStream *stream,
969 GCancellable *cancellable,
970 GAsyncReadyCallback callback,
973 GOutputStreamClass *class;
974 GSimpleAsyncResult *simple;
976 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
978 if (stream->priv->closed)
980 simple = g_simple_async_result_new (G_OBJECT (stream),
983 g_output_stream_close_async);
984 g_simple_async_result_complete_in_idle (simple);
985 g_object_unref (simple);
989 if (stream->priv->pending)
991 g_simple_async_report_error_in_idle (G_OBJECT (stream),
994 G_IO_ERROR, G_IO_ERROR_PENDING,
995 _("Stream has outstanding operation"));
999 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1000 stream->priv->pending = TRUE;
1001 stream->priv->outstanding_callback = callback;
1002 g_object_ref (stream);
1003 class->close_async (stream, io_priority, cancellable,
1004 async_ready_close_callback_wrapper, user_data);
1008 * g_output_stream_close_finish:
1011 * @error: a #GError location to store the error occuring, or %NULL to
1013 * Returns: %TRUE, %FALSE otherwise.
1016 g_output_stream_close_finish (GOutputStream *stream,
1017 GAsyncResult *result,
1020 GSimpleAsyncResult *simple;
1021 GOutputStreamClass *class;
1023 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1024 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1026 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1028 simple = G_SIMPLE_ASYNC_RESULT (result);
1029 if (g_simple_async_result_propagate_error (simple, error))
1032 /* Special case already closed */
1033 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1037 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1038 return class->close_finish (stream, result, error);
1042 * g_output_stream_is_closed:
1045 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1048 g_output_stream_is_closed (GOutputStream *stream)
1050 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1052 return stream->priv->closed;
1056 * g_output_stream_has_pending:
1059 * Returns: %TRUE if @stream has pending actions.
1062 g_output_stream_has_pending (GOutputStream *stream)
1064 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1066 return stream->priv->pending;
1070 * g_output_stream_set_pending:
1074 * Sets the @stream as having pending actions.
1078 g_output_stream_set_pending (GOutputStream *stream,
1081 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1083 stream->priv->pending = pending;
1087 /********************************************
1088 * Default implementation of async ops *
1089 ********************************************/
1093 gsize count_requested;
1094 gssize count_written;
1098 write_async_thread (GSimpleAsyncResult *res,
1100 GCancellable *cancellable)
1103 GOutputStreamClass *class;
1104 GError *error = NULL;
1106 class = G_OUTPUT_STREAM_GET_CLASS (object);
1107 op = g_simple_async_result_get_op_res_gpointer (res);
1108 op->count_written = class->write (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1109 cancellable, &error);
1110 if (op->count_written == -1)
1112 g_simple_async_result_set_from_error (res, error);
1113 g_error_free (error);
1118 g_output_stream_real_write_async (GOutputStream *stream,
1122 GCancellable *cancellable,
1123 GAsyncReadyCallback callback,
1126 GSimpleAsyncResult *res;
1129 op = g_new0 (WriteData, 1);
1130 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1131 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1132 op->buffer = buffer;
1133 op->count_requested = count;
1135 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1136 g_object_unref (res);
1140 g_output_stream_real_write_finish (GOutputStream *stream,
1141 GAsyncResult *result,
1144 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1147 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1148 op = g_simple_async_result_get_op_res_gpointer (simple);
1149 return op->count_written;
1153 GInputStream *source;
1154 GOutputStreamSpliceFlags flags;
1155 gssize bytes_copied;
1159 splice_async_thread (GSimpleAsyncResult *result,
1161 GCancellable *cancellable)
1164 GOutputStreamClass *class;
1165 GError *error = NULL;
1166 GOutputStream *stream;
1168 stream = G_OUTPUT_STREAM (object);
1169 class = G_OUTPUT_STREAM_GET_CLASS (object);
1170 op = g_simple_async_result_get_op_res_gpointer (result);
1172 stream->priv->pending = FALSE;
1174 g_output_stream_splice (stream,
1179 stream->priv->pending = TRUE;
1181 if (op->bytes_copied == -1)
1183 g_simple_async_result_set_from_error (result, error);
1184 g_error_free (error);
1189 g_output_stream_real_splice_async (GOutputStream *stream,
1190 GInputStream *source,
1191 GOutputStreamSpliceFlags flags,
1193 GCancellable *cancellable,
1194 GAsyncReadyCallback callback,
1197 GSimpleAsyncResult *res;
1200 op = g_new0 (SpliceData, 1);
1201 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1202 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1204 op->source = source;
1206 /* TODO: In the case where both source and destintion have
1207 non-threadbased async calls we can use a true async copy here */
1209 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1210 g_object_unref (res);
1214 g_output_stream_real_splice_finish (GOutputStream *stream,
1215 GAsyncResult *result,
1218 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1221 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1222 op = g_simple_async_result_get_op_res_gpointer (simple);
1223 return op->bytes_copied;
1229 flush_async_thread (GSimpleAsyncResult *res,
1231 GCancellable *cancellable)
1233 GOutputStreamClass *class;
1235 GError *error = NULL;
1237 class = G_OUTPUT_STREAM_GET_CLASS (object);
1240 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1244 g_simple_async_result_set_from_error (res, error);
1245 g_error_free (error);
1250 g_output_stream_real_flush_async (GOutputStream *stream,
1252 GCancellable *cancellable,
1253 GAsyncReadyCallback callback,
1256 GSimpleAsyncResult *res;
1258 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1260 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1261 g_object_unref (res);
1265 g_output_stream_real_flush_finish (GOutputStream *stream,
1266 GAsyncResult *result,
1273 close_async_thread (GSimpleAsyncResult *res,
1275 GCancellable *cancellable)
1277 GOutputStreamClass *class;
1278 GError *error = NULL;
1281 /* Auto handling of cancelation disabled, and ignore
1282 cancellation, since we want to close things anyway, although
1283 possibly in a quick-n-dirty way. At least we never want to leak
1286 class = G_OUTPUT_STREAM_GET_CLASS (object);
1287 result = class->close (G_OUTPUT_STREAM (object), cancellable, &error);
1290 g_simple_async_result_set_from_error (res, error);
1291 g_error_free (error);
1296 g_output_stream_real_close_async (GOutputStream *stream,
1298 GCancellable *cancellable,
1299 GAsyncReadyCallback callback,
1302 GSimpleAsyncResult *res;
1304 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1306 g_simple_async_result_set_handle_cancellation (res, FALSE);
1308 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1309 g_object_unref (res);
1313 g_output_stream_real_close_finish (GOutputStream *stream,
1314 GAsyncResult *result,
1317 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1318 g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);