regex: unicode: Update to Unicode 6.1.0
[glib.git] / gio / goutputstream.c
blobf40af9c1fabbed74fb9f6814e7414804ee2375ae
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
24 #include "goutputstream.h"
25 #include "gcancellable.h"
26 #include "gasyncresult.h"
27 #include "gsimpleasyncresult.h"
28 #include "ginputstream.h"
29 #include "gioerror.h"
30 #include "glibintl.h"
33 /**
34 * SECTION:goutputstream
35 * @short_description: Base class for implementing streaming output
36 * @include: gio/gio.h
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.
46 **/
48 G_DEFINE_ABSTRACT_TYPE (GOutputStream, g_output_stream, G_TYPE_OBJECT);
50 struct _GOutputStreamPrivate {
51 guint closed : 1;
52 guint pending : 1;
53 guint closing : 1;
54 GAsyncReadyCallback outstanding_callback;
57 static gssize g_output_stream_real_splice (GOutputStream *stream,
58 GInputStream *source,
59 GOutputStreamSpliceFlags flags,
60 GCancellable *cancellable,
61 GError **error);
62 static void g_output_stream_real_write_async (GOutputStream *stream,
63 const void *buffer,
64 gsize count,
65 int io_priority,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
68 gpointer data);
69 static gssize g_output_stream_real_write_finish (GOutputStream *stream,
70 GAsyncResult *result,
71 GError **error);
72 static void g_output_stream_real_splice_async (GOutputStream *stream,
73 GInputStream *source,
74 GOutputStreamSpliceFlags flags,
75 int io_priority,
76 GCancellable *cancellable,
77 GAsyncReadyCallback callback,
78 gpointer data);
79 static gssize g_output_stream_real_splice_finish (GOutputStream *stream,
80 GAsyncResult *result,
81 GError **error);
82 static void g_output_stream_real_flush_async (GOutputStream *stream,
83 int io_priority,
84 GCancellable *cancellable,
85 GAsyncReadyCallback callback,
86 gpointer data);
87 static gboolean g_output_stream_real_flush_finish (GOutputStream *stream,
88 GAsyncResult *result,
89 GError **error);
90 static void g_output_stream_real_close_async (GOutputStream *stream,
91 int io_priority,
92 GCancellable *cancellable,
93 GAsyncReadyCallback callback,
94 gpointer data);
95 static gboolean g_output_stream_real_close_finish (GOutputStream *stream,
96 GAsyncResult *result,
97 GError **error);
98 static gboolean _g_output_stream_close_internal (GOutputStream *stream,
99 GCancellable *cancellable,
100 GError **error);
102 static void
103 g_output_stream_finalize (GObject *object)
105 G_OBJECT_CLASS (g_output_stream_parent_class)->finalize (object);
108 static void
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);
121 static void
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;
143 static void
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
170 * @count is 0).
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 * Virtual: write_fn
182 * Return value: Number of bytes written, or -1 on error
184 gssize
185 g_output_stream_write (GOutputStream *stream,
186 const void *buffer,
187 gsize count,
188 GCancellable *cancellable,
189 GError **error)
191 GOutputStreamClass *class;
192 gssize res;
194 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
195 g_return_val_if_fail (buffer != NULL, 0);
197 if (count == 0)
198 return 0;
200 if (((gssize) count) < 0)
202 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
203 _("Too large count value passed to %s"), G_STRFUNC);
204 return -1;
207 class = G_OUTPUT_STREAM_GET_CLASS (stream);
209 if (class->write_fn == NULL)
211 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
212 _("Output stream doesn't implement write"));
213 return -1;
216 if (!g_output_stream_set_pending (stream, error))
217 return -1;
219 if (cancellable)
220 g_cancellable_push_current (cancellable);
222 res = class->write_fn (stream, buffer, count, cancellable, error);
224 if (cancellable)
225 g_cancellable_pop_current (cancellable);
227 g_output_stream_clear_pending (stream);
229 return res;
233 * g_output_stream_write_all:
234 * @stream: a #GOutputStream.
235 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
236 * @count: the number of bytes to write
237 * @bytes_written: (out): location to store the number of bytes that was
238 * written to the stream
239 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
240 * @error: location to store the error occurring, or %NULL to ignore
242 * Tries to write @count bytes from @buffer into the stream. Will block
243 * during the operation.
245 * This function is similar to g_output_stream_write(), except it tries to
246 * write as many bytes as requested, only stopping on an error.
248 * On a successful write of @count bytes, %TRUE is returned, and @bytes_written
249 * is set to @count.
251 * If there is an error during the operation FALSE is returned and @error
252 * is set to indicate the error status, @bytes_written is updated to contain
253 * the number of bytes written into the stream before the error occurred.
255 * Return value: %TRUE on success, %FALSE if there was an error
257 gboolean
258 g_output_stream_write_all (GOutputStream *stream,
259 const void *buffer,
260 gsize count,
261 gsize *bytes_written,
262 GCancellable *cancellable,
263 GError **error)
265 gsize _bytes_written;
266 gssize res;
268 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
269 g_return_val_if_fail (buffer != NULL, FALSE);
271 _bytes_written = 0;
272 while (_bytes_written < count)
274 res = g_output_stream_write (stream, (char *)buffer + _bytes_written, count - _bytes_written,
275 cancellable, error);
276 if (res == -1)
278 if (bytes_written)
279 *bytes_written = _bytes_written;
280 return FALSE;
283 if (res == 0)
284 g_warning ("Write returned zero without error");
286 _bytes_written += res;
289 if (bytes_written)
290 *bytes_written = _bytes_written;
292 return TRUE;
296 * g_output_stream_flush:
297 * @stream: a #GOutputStream.
298 * @cancellable: (allow-none): optional cancellable object
299 * @error: location to store the error occurring, or %NULL to ignore
301 * Forces a write of all user-space buffered data for the given
302 * @stream. Will block during the operation. Closing the stream will
303 * implicitly cause a flush.
305 * This function is optional for inherited classes.
307 * If @cancellable is not %NULL, then the operation can be cancelled by
308 * triggering the cancellable object from another thread. If the operation
309 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
311 * Return value: %TRUE on success, %FALSE on error
313 gboolean
314 g_output_stream_flush (GOutputStream *stream,
315 GCancellable *cancellable,
316 GError **error)
318 GOutputStreamClass *class;
319 gboolean res;
321 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
323 if (!g_output_stream_set_pending (stream, error))
324 return FALSE;
326 class = G_OUTPUT_STREAM_GET_CLASS (stream);
328 res = TRUE;
329 if (class->flush)
331 if (cancellable)
332 g_cancellable_push_current (cancellable);
334 res = class->flush (stream, cancellable, error);
336 if (cancellable)
337 g_cancellable_pop_current (cancellable);
340 g_output_stream_clear_pending (stream);
342 return res;
346 * g_output_stream_splice:
347 * @stream: a #GOutputStream.
348 * @source: a #GInputStream.
349 * @flags: a set of #GOutputStreamSpliceFlags.
350 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
351 * @error: a #GError location to store the error occurring, or %NULL to
352 * ignore.
354 * Splices an input stream into an output stream.
356 * Returns: a #gssize containing the size of the data spliced, or
357 * -1 if an error occurred. Note that if the number of bytes
358 * spliced is greater than %G_MAXSSIZE, then that will be
359 * returned, and there is no way to determine the actual number
360 * of bytes spliced.
362 gssize
363 g_output_stream_splice (GOutputStream *stream,
364 GInputStream *source,
365 GOutputStreamSpliceFlags flags,
366 GCancellable *cancellable,
367 GError **error)
369 GOutputStreamClass *class;
370 gssize bytes_copied;
372 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
373 g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
375 if (g_input_stream_is_closed (source))
377 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
378 _("Source stream is already closed"));
379 return -1;
382 if (!g_output_stream_set_pending (stream, error))
383 return -1;
385 class = G_OUTPUT_STREAM_GET_CLASS (stream);
387 if (cancellable)
388 g_cancellable_push_current (cancellable);
390 bytes_copied = class->splice (stream, source, flags, cancellable, error);
392 if (cancellable)
393 g_cancellable_pop_current (cancellable);
395 g_output_stream_clear_pending (stream);
397 return bytes_copied;
400 static gssize
401 g_output_stream_real_splice (GOutputStream *stream,
402 GInputStream *source,
403 GOutputStreamSpliceFlags flags,
404 GCancellable *cancellable,
405 GError **error)
407 GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
408 gssize n_read, n_written;
409 gsize bytes_copied;
410 char buffer[8192], *p;
411 gboolean res;
413 bytes_copied = 0;
414 if (class->write_fn == NULL)
416 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
417 _("Output stream doesn't implement write"));
418 res = FALSE;
419 goto notsupported;
422 res = TRUE;
425 n_read = g_input_stream_read (source, buffer, sizeof (buffer), cancellable, error);
426 if (n_read == -1)
428 res = FALSE;
429 break;
432 if (n_read == 0)
433 break;
435 p = buffer;
436 while (n_read > 0)
438 n_written = class->write_fn (stream, p, n_read, cancellable, error);
439 if (n_written == -1)
441 res = FALSE;
442 break;
445 p += n_written;
446 n_read -= n_written;
447 bytes_copied += n_written;
450 if (bytes_copied > G_MAXSSIZE)
451 bytes_copied = G_MAXSSIZE;
453 while (res);
455 notsupported:
456 if (!res)
457 error = NULL; /* Ignore further errors */
459 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE)
461 /* Don't care about errors in source here */
462 g_input_stream_close (source, cancellable, NULL);
465 if (flags & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET)
467 /* But write errors on close are bad! */
468 res = _g_output_stream_close_internal (stream, cancellable, error);
471 if (res)
472 return bytes_copied;
474 return -1;
477 /* Must always be called inside
478 * g_output_stream_set_pending()/g_output_stream_clear_pending(). */
479 static gboolean
480 _g_output_stream_close_internal (GOutputStream *stream,
481 GCancellable *cancellable,
482 GError **error)
484 GOutputStreamClass *class;
485 gboolean res;
487 if (stream->priv->closed)
488 return TRUE;
490 class = G_OUTPUT_STREAM_GET_CLASS (stream);
492 stream->priv->closing = TRUE;
494 if (cancellable)
495 g_cancellable_push_current (cancellable);
497 if (class->flush)
498 res = class->flush (stream, cancellable, error);
499 else
500 res = TRUE;
502 if (!res)
504 /* flushing caused the error that we want to return,
505 * but we still want to close the underlying stream if possible
507 if (class->close_fn)
508 class->close_fn (stream, cancellable, NULL);
510 else
512 res = TRUE;
513 if (class->close_fn)
514 res = class->close_fn (stream, cancellable, error);
517 if (cancellable)
518 g_cancellable_pop_current (cancellable);
520 stream->priv->closing = FALSE;
521 stream->priv->closed = TRUE;
523 return res;
527 * g_output_stream_close:
528 * @stream: A #GOutputStream.
529 * @cancellable: (allow-none): optional cancellable object
530 * @error: location to store the error occurring, or %NULL to ignore
532 * Closes the stream, releasing resources related to it.
534 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
535 * Closing a stream multiple times will not return an error.
537 * Closing a stream will automatically flush any outstanding buffers in the
538 * stream.
540 * Streams will be automatically closed when the last reference
541 * is dropped, but you might want to call this function to make sure
542 * resources are released as early as possible.
544 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
545 * open after the stream is closed. See the documentation for the individual
546 * stream for details.
548 * On failure the first error that happened will be reported, but the close
549 * operation will finish as much as possible. A stream that failed to
550 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
551 * is important to check and report the error to the user, otherwise
552 * there might be a loss of data as all data might not be written.
554 * If @cancellable is not NULL, then the operation can be cancelled by
555 * triggering the cancellable object from another thread. If the operation
556 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
557 * Cancelling a close will still leave the stream closed, but there some streams
558 * can use a faster close that doesn't block to e.g. check errors. On
559 * cancellation (as with any error) there is no guarantee that all written
560 * data will reach the target.
562 * Return value: %TRUE on success, %FALSE on failure
564 gboolean
565 g_output_stream_close (GOutputStream *stream,
566 GCancellable *cancellable,
567 GError **error)
569 gboolean res;
571 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
573 if (stream->priv->closed)
574 return TRUE;
576 if (!g_output_stream_set_pending (stream, error))
577 return FALSE;
579 res = _g_output_stream_close_internal (stream, cancellable, error);
581 g_output_stream_clear_pending (stream);
583 return res;
586 static void
587 async_ready_callback_wrapper (GObject *source_object,
588 GAsyncResult *res,
589 gpointer user_data)
591 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
593 g_output_stream_clear_pending (stream);
594 if (stream->priv->outstanding_callback)
595 (*stream->priv->outstanding_callback) (source_object, res, user_data);
596 g_object_unref (stream);
599 typedef struct {
600 gint io_priority;
601 GCancellable *cancellable;
602 GError *flush_error;
603 gpointer user_data;
604 } CloseUserData;
606 static void
607 async_ready_close_callback_wrapper (GObject *source_object,
608 GAsyncResult *res,
609 gpointer user_data)
611 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
612 CloseUserData *data = user_data;
614 stream->priv->closing = FALSE;
615 stream->priv->closed = TRUE;
617 g_output_stream_clear_pending (stream);
619 if (stream->priv->outstanding_callback)
621 if (data->flush_error != NULL)
623 GSimpleAsyncResult *err;
625 err = g_simple_async_result_new_take_error (source_object,
626 stream->priv->outstanding_callback,
627 data->user_data,
628 data->flush_error);
629 data->flush_error = NULL;
631 (*stream->priv->outstanding_callback) (source_object,
632 G_ASYNC_RESULT (err),
633 data->user_data);
634 g_object_unref (err);
636 else
638 (*stream->priv->outstanding_callback) (source_object,
639 res,
640 data->user_data);
644 g_object_unref (stream);
646 if (data->cancellable)
647 g_object_unref (data->cancellable);
649 if (data->flush_error)
650 g_error_free (data->flush_error);
652 g_slice_free (CloseUserData, data);
655 static void
656 async_ready_close_flushed_callback_wrapper (GObject *source_object,
657 GAsyncResult *res,
658 gpointer user_data)
660 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
661 GOutputStreamClass *class;
662 CloseUserData *data = user_data;
663 GSimpleAsyncResult *simple;
665 /* propagate the possible error */
666 if (G_IS_SIMPLE_ASYNC_RESULT (res))
668 simple = G_SIMPLE_ASYNC_RESULT (res);
669 g_simple_async_result_propagate_error (simple, &data->flush_error);
672 class = G_OUTPUT_STREAM_GET_CLASS (stream);
674 /* we still close, even if there was a flush error */
675 class->close_async (stream, data->io_priority, data->cancellable,
676 async_ready_close_callback_wrapper, user_data);
680 * g_output_stream_write_async:
681 * @stream: A #GOutputStream.
682 * @buffer: (array length=count) (element-type guint8): the buffer containing the data to write.
683 * @count: the number of bytes to write
684 * @io_priority: the io priority of the request.
685 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
686 * @callback: (scope async): callback to call when the request is satisfied
687 * @user_data: (closure): the data to pass to callback function
689 * Request an asynchronous write of @count bytes from @buffer into
690 * the stream. When the operation is finished @callback will be called.
691 * You can then call g_output_stream_write_finish() to get the result of the
692 * operation.
694 * During an async request no other sync and async calls are allowed,
695 * and will result in %G_IO_ERROR_PENDING errors.
697 * A value of @count larger than %G_MAXSSIZE will cause a
698 * %G_IO_ERROR_INVALID_ARGUMENT error.
700 * On success, the number of bytes written will be passed to the
701 * @callback. It is not an error if this is not the same as the
702 * requested size, as it can happen e.g. on a partial I/O error,
703 * but generally we try to write as many bytes as requested.
705 * You are guaranteed that this method will never fail with
706 * %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the
707 * method will just wait until this changes.
709 * Any outstanding I/O request with higher priority (lower numerical
710 * value) will be executed before an outstanding request with lower
711 * priority. Default priority is %G_PRIORITY_DEFAULT.
713 * The asyncronous methods have a default fallback that uses threads
714 * to implement asynchronicity, so they are optional for inheriting
715 * classes. However, if you override one you must override all.
717 * For the synchronous, blocking version of this function, see
718 * g_output_stream_write().
720 void
721 g_output_stream_write_async (GOutputStream *stream,
722 const void *buffer,
723 gsize count,
724 int io_priority,
725 GCancellable *cancellable,
726 GAsyncReadyCallback callback,
727 gpointer user_data)
729 GOutputStreamClass *class;
730 GSimpleAsyncResult *simple;
731 GError *error = NULL;
733 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
734 g_return_if_fail (buffer != NULL);
736 if (count == 0)
738 simple = g_simple_async_result_new (G_OBJECT (stream),
739 callback,
740 user_data,
741 g_output_stream_write_async);
742 g_simple_async_result_complete_in_idle (simple);
743 g_object_unref (simple);
744 return;
747 if (((gssize) count) < 0)
749 g_simple_async_report_error_in_idle (G_OBJECT (stream),
750 callback,
751 user_data,
752 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
753 _("Too large count value passed to %s"),
754 G_STRFUNC);
755 return;
758 if (!g_output_stream_set_pending (stream, &error))
760 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
761 callback,
762 user_data,
763 error);
764 return;
767 class = G_OUTPUT_STREAM_GET_CLASS (stream);
769 stream->priv->outstanding_callback = callback;
770 g_object_ref (stream);
771 class->write_async (stream, buffer, count, io_priority, cancellable,
772 async_ready_callback_wrapper, user_data);
776 * g_output_stream_write_finish:
777 * @stream: a #GOutputStream.
778 * @result: a #GAsyncResult.
779 * @error: a #GError location to store the error occurring, or %NULL to
780 * ignore.
782 * Finishes a stream write operation.
784 * Returns: a #gssize containing the number of bytes written to the stream.
786 gssize
787 g_output_stream_write_finish (GOutputStream *stream,
788 GAsyncResult *result,
789 GError **error)
791 GSimpleAsyncResult *simple;
792 GOutputStreamClass *class;
794 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
795 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
797 if (G_IS_SIMPLE_ASYNC_RESULT (result))
799 simple = G_SIMPLE_ASYNC_RESULT (result);
800 if (g_simple_async_result_propagate_error (simple, error))
801 return -1;
803 /* Special case writes of 0 bytes */
804 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_write_async)
805 return 0;
808 class = G_OUTPUT_STREAM_GET_CLASS (stream);
809 return class->write_finish (stream, result, error);
812 typedef struct {
813 GInputStream *source;
814 gpointer user_data;
815 GAsyncReadyCallback callback;
816 } SpliceUserData;
818 static void
819 async_ready_splice_callback_wrapper (GObject *source_object,
820 GAsyncResult *res,
821 gpointer _data)
823 GOutputStream *stream = G_OUTPUT_STREAM (source_object);
824 SpliceUserData *data = _data;
826 g_output_stream_clear_pending (stream);
828 if (data->callback)
829 (*data->callback) (source_object, res, data->user_data);
831 g_object_unref (stream);
832 g_object_unref (data->source);
833 g_free (data);
837 * g_output_stream_splice_async:
838 * @stream: a #GOutputStream.
839 * @source: a #GInputStream.
840 * @flags: a set of #GOutputStreamSpliceFlags.
841 * @io_priority: the io priority of the request.
842 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
843 * @callback: (scope async): a #GAsyncReadyCallback.
844 * @user_data: (closure): user data passed to @callback.
846 * Splices a stream asynchronously.
847 * When the operation is finished @callback will be called.
848 * You can then call g_output_stream_splice_finish() to get the
849 * result of the operation.
851 * For the synchronous, blocking version of this function, see
852 * g_output_stream_splice().
854 void
855 g_output_stream_splice_async (GOutputStream *stream,
856 GInputStream *source,
857 GOutputStreamSpliceFlags flags,
858 int io_priority,
859 GCancellable *cancellable,
860 GAsyncReadyCallback callback,
861 gpointer user_data)
863 GOutputStreamClass *class;
864 SpliceUserData *data;
865 GError *error = NULL;
867 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
868 g_return_if_fail (G_IS_INPUT_STREAM (source));
870 if (g_input_stream_is_closed (source))
872 g_simple_async_report_error_in_idle (G_OBJECT (stream),
873 callback,
874 user_data,
875 G_IO_ERROR, G_IO_ERROR_CLOSED,
876 _("Source stream is already closed"));
877 return;
880 if (!g_output_stream_set_pending (stream, &error))
882 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
883 callback,
884 user_data,
885 error);
886 return;
889 class = G_OUTPUT_STREAM_GET_CLASS (stream);
891 data = g_new0 (SpliceUserData, 1);
892 data->callback = callback;
893 data->user_data = user_data;
894 data->source = g_object_ref (source);
896 g_object_ref (stream);
897 class->splice_async (stream, source, flags, io_priority, cancellable,
898 async_ready_splice_callback_wrapper, data);
902 * g_output_stream_splice_finish:
903 * @stream: a #GOutputStream.
904 * @result: a #GAsyncResult.
905 * @error: a #GError location to store the error occurring, or %NULL to
906 * ignore.
908 * Finishes an asynchronous stream splice operation.
910 * Returns: a #gssize of the number of bytes spliced. Note that if the
911 * number of bytes spliced is greater than %G_MAXSSIZE, then that
912 * will be returned, and there is no way to determine the actual
913 * number of bytes spliced.
915 gssize
916 g_output_stream_splice_finish (GOutputStream *stream,
917 GAsyncResult *result,
918 GError **error)
920 GSimpleAsyncResult *simple;
921 GOutputStreamClass *class;
923 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
924 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
926 if (G_IS_SIMPLE_ASYNC_RESULT (result))
928 simple = G_SIMPLE_ASYNC_RESULT (result);
929 if (g_simple_async_result_propagate_error (simple, error))
930 return -1;
933 class = G_OUTPUT_STREAM_GET_CLASS (stream);
934 return class->splice_finish (stream, result, error);
938 * g_output_stream_flush_async:
939 * @stream: a #GOutputStream.
940 * @io_priority: the io priority of the request.
941 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
942 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
943 * @user_data: (closure): the data to pass to callback function
945 * Forces an asynchronous write of all user-space buffered data for
946 * the given @stream.
947 * For behaviour details see g_output_stream_flush().
949 * When the operation is finished @callback will be
950 * called. You can then call g_output_stream_flush_finish() to get the
951 * result of the operation.
953 void
954 g_output_stream_flush_async (GOutputStream *stream,
955 int io_priority,
956 GCancellable *cancellable,
957 GAsyncReadyCallback callback,
958 gpointer user_data)
960 GOutputStreamClass *class;
961 GSimpleAsyncResult *simple;
962 GError *error = NULL;
964 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
966 if (!g_output_stream_set_pending (stream, &error))
968 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
969 callback,
970 user_data,
971 error);
972 return;
975 stream->priv->outstanding_callback = callback;
976 g_object_ref (stream);
978 class = G_OUTPUT_STREAM_GET_CLASS (stream);
980 if (class->flush_async == NULL)
982 simple = g_simple_async_result_new (G_OBJECT (stream),
983 async_ready_callback_wrapper,
984 user_data,
985 g_output_stream_flush_async);
986 g_simple_async_result_complete_in_idle (simple);
987 g_object_unref (simple);
988 return;
991 class->flush_async (stream, io_priority, cancellable,
992 async_ready_callback_wrapper, user_data);
996 * g_output_stream_flush_finish:
997 * @stream: a #GOutputStream.
998 * @result: a GAsyncResult.
999 * @error: a #GError location to store the error occurring, or %NULL to
1000 * ignore.
1002 * Finishes flushing an output stream.
1004 * Returns: %TRUE if flush operation succeeded, %FALSE otherwise.
1006 gboolean
1007 g_output_stream_flush_finish (GOutputStream *stream,
1008 GAsyncResult *result,
1009 GError **error)
1011 GSimpleAsyncResult *simple;
1012 GOutputStreamClass *klass;
1014 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1015 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1017 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1019 simple = G_SIMPLE_ASYNC_RESULT (result);
1020 if (g_simple_async_result_propagate_error (simple, error))
1021 return FALSE;
1023 /* Special case default implementation */
1024 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_flush_async)
1025 return TRUE;
1028 klass = G_OUTPUT_STREAM_GET_CLASS (stream);
1029 return klass->flush_finish (stream, result, error);
1034 * g_output_stream_close_async:
1035 * @stream: A #GOutputStream.
1036 * @io_priority: the io priority of the request.
1037 * @cancellable: (allow-none): optional cancellable object
1038 * @callback: (scope async): callback to call when the request is satisfied
1039 * @user_data: (closure): the data to pass to callback function
1041 * Requests an asynchronous close of the stream, releasing resources
1042 * related to it. When the operation is finished @callback will be
1043 * called. You can then call g_output_stream_close_finish() to get
1044 * the result of the operation.
1046 * For behaviour details see g_output_stream_close().
1048 * The asyncronous methods have a default fallback that uses threads
1049 * to implement asynchronicity, so they are optional for inheriting
1050 * classes. However, if you override one you must override all.
1052 void
1053 g_output_stream_close_async (GOutputStream *stream,
1054 int io_priority,
1055 GCancellable *cancellable,
1056 GAsyncReadyCallback callback,
1057 gpointer user_data)
1059 GOutputStreamClass *class;
1060 GSimpleAsyncResult *simple;
1061 GError *error = NULL;
1062 CloseUserData *data;
1064 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1066 if (stream->priv->closed)
1068 simple = g_simple_async_result_new (G_OBJECT (stream),
1069 callback,
1070 user_data,
1071 g_output_stream_close_async);
1072 g_simple_async_result_complete_in_idle (simple);
1073 g_object_unref (simple);
1074 return;
1077 if (!g_output_stream_set_pending (stream, &error))
1079 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
1080 callback,
1081 user_data,
1082 error);
1083 return;
1086 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1087 stream->priv->closing = TRUE;
1088 stream->priv->outstanding_callback = callback;
1089 g_object_ref (stream);
1091 data = g_slice_new0 (CloseUserData);
1093 if (cancellable != NULL)
1094 data->cancellable = g_object_ref (cancellable);
1096 data->io_priority = io_priority;
1097 data->user_data = user_data;
1099 /* Call close_async directly if there is no need to flush, or if the flush
1100 can be done sync (in the output stream async close thread) */
1101 if (class->flush_async == NULL ||
1102 (class->flush_async == g_output_stream_real_flush_async &&
1103 (class->flush == NULL || class->close_async == g_output_stream_real_close_async)))
1105 class->close_async (stream, io_priority, cancellable,
1106 async_ready_close_callback_wrapper, data);
1108 else
1110 /* First do an async flush, then do the async close in the callback
1111 wrapper (see async_ready_close_flushed_callback_wrapper) */
1112 class->flush_async (stream, io_priority, cancellable,
1113 async_ready_close_flushed_callback_wrapper, data);
1118 * g_output_stream_close_finish:
1119 * @stream: a #GOutputStream.
1120 * @result: a #GAsyncResult.
1121 * @error: a #GError location to store the error occurring, or %NULL to
1122 * ignore.
1124 * Closes an output stream.
1126 * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
1128 gboolean
1129 g_output_stream_close_finish (GOutputStream *stream,
1130 GAsyncResult *result,
1131 GError **error)
1133 GSimpleAsyncResult *simple;
1134 GOutputStreamClass *class;
1136 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1137 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
1139 if (G_IS_SIMPLE_ASYNC_RESULT (result))
1141 simple = G_SIMPLE_ASYNC_RESULT (result);
1142 if (g_simple_async_result_propagate_error (simple, error))
1143 return FALSE;
1145 /* Special case already closed */
1146 if (g_simple_async_result_get_source_tag (simple) == g_output_stream_close_async)
1147 return TRUE;
1150 class = G_OUTPUT_STREAM_GET_CLASS (stream);
1151 return class->close_finish (stream, result, error);
1155 * g_output_stream_is_closed:
1156 * @stream: a #GOutputStream.
1158 * Checks if an output stream has already been closed.
1160 * Returns: %TRUE if @stream is closed. %FALSE otherwise.
1162 gboolean
1163 g_output_stream_is_closed (GOutputStream *stream)
1165 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1167 return stream->priv->closed;
1171 * g_output_stream_is_closing:
1172 * @stream: a #GOutputStream.
1174 * Checks if an output stream is being closed. This can be
1175 * used inside e.g. a flush implementation to see if the
1176 * flush (or other i/o operation) is called from within
1177 * the closing operation.
1179 * Returns: %TRUE if @stream is being closed. %FALSE otherwise.
1181 * Since: 2.24
1183 gboolean
1184 g_output_stream_is_closing (GOutputStream *stream)
1186 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), TRUE);
1188 return stream->priv->closing;
1192 * g_output_stream_has_pending:
1193 * @stream: a #GOutputStream.
1195 * Checks if an ouput stream has pending actions.
1197 * Returns: %TRUE if @stream has pending actions.
1199 gboolean
1200 g_output_stream_has_pending (GOutputStream *stream)
1202 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1204 return stream->priv->pending;
1208 * g_output_stream_set_pending:
1209 * @stream: a #GOutputStream.
1210 * @error: a #GError location to store the error occurring, or %NULL to
1211 * ignore.
1213 * Sets @stream to have actions pending. If the pending flag is
1214 * already set or @stream is closed, it will return %FALSE and set
1215 * @error.
1217 * Return value: %TRUE if pending was previously unset and is now set.
1219 gboolean
1220 g_output_stream_set_pending (GOutputStream *stream,
1221 GError **error)
1223 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
1225 if (stream->priv->closed)
1227 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1228 _("Stream is already closed"));
1229 return FALSE;
1232 if (stream->priv->pending)
1234 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1235 /* Translators: This is an error you get if there is
1236 * already an operation running against this stream when
1237 * you try to start one */
1238 _("Stream has outstanding operation"));
1239 return FALSE;
1242 stream->priv->pending = TRUE;
1243 return TRUE;
1247 * g_output_stream_clear_pending:
1248 * @stream: output stream
1250 * Clears the pending flag on @stream.
1252 void
1253 g_output_stream_clear_pending (GOutputStream *stream)
1255 g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
1257 stream->priv->pending = FALSE;
1261 /********************************************
1262 * Default implementation of async ops *
1263 ********************************************/
1265 typedef struct {
1266 const void *buffer;
1267 gsize count_requested;
1268 gssize count_written;
1269 } WriteData;
1271 static void
1272 write_async_thread (GSimpleAsyncResult *res,
1273 GObject *object,
1274 GCancellable *cancellable)
1276 WriteData *op;
1277 GOutputStreamClass *class;
1278 GError *error = NULL;
1280 class = G_OUTPUT_STREAM_GET_CLASS (object);
1281 op = g_simple_async_result_get_op_res_gpointer (res);
1282 op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
1283 cancellable, &error);
1284 if (op->count_written == -1)
1285 g_simple_async_result_take_error (res, error);
1288 static void
1289 g_output_stream_real_write_async (GOutputStream *stream,
1290 const void *buffer,
1291 gsize count,
1292 int io_priority,
1293 GCancellable *cancellable,
1294 GAsyncReadyCallback callback,
1295 gpointer user_data)
1297 GSimpleAsyncResult *res;
1298 WriteData *op;
1300 op = g_new0 (WriteData, 1);
1301 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1302 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1303 op->buffer = buffer;
1304 op->count_requested = count;
1306 g_simple_async_result_run_in_thread (res, write_async_thread, io_priority, cancellable);
1307 g_object_unref (res);
1310 static gssize
1311 g_output_stream_real_write_finish (GOutputStream *stream,
1312 GAsyncResult *result,
1313 GError **error)
1315 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1316 WriteData *op;
1318 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_write_async);
1319 op = g_simple_async_result_get_op_res_gpointer (simple);
1320 return op->count_written;
1323 typedef struct {
1324 GInputStream *source;
1325 GOutputStreamSpliceFlags flags;
1326 gssize bytes_copied;
1327 } SpliceData;
1329 static void
1330 splice_async_thread (GSimpleAsyncResult *result,
1331 GObject *object,
1332 GCancellable *cancellable)
1334 SpliceData *op;
1335 GOutputStreamClass *class;
1336 GError *error = NULL;
1337 GOutputStream *stream;
1339 stream = G_OUTPUT_STREAM (object);
1340 class = G_OUTPUT_STREAM_GET_CLASS (object);
1341 op = g_simple_async_result_get_op_res_gpointer (result);
1343 op->bytes_copied = class->splice (stream,
1344 op->source,
1345 op->flags,
1346 cancellable,
1347 &error);
1348 if (op->bytes_copied == -1)
1349 g_simple_async_result_take_error (result, error);
1352 static void
1353 g_output_stream_real_splice_async (GOutputStream *stream,
1354 GInputStream *source,
1355 GOutputStreamSpliceFlags flags,
1356 int io_priority,
1357 GCancellable *cancellable,
1358 GAsyncReadyCallback callback,
1359 gpointer user_data)
1361 GSimpleAsyncResult *res;
1362 SpliceData *op;
1364 op = g_new0 (SpliceData, 1);
1365 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_splice_async);
1366 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1367 op->flags = flags;
1368 op->source = source;
1370 /* TODO: In the case where both source and destintion have
1371 non-threadbased async calls we can use a true async copy here */
1373 g_simple_async_result_run_in_thread (res, splice_async_thread, io_priority, cancellable);
1374 g_object_unref (res);
1377 static gssize
1378 g_output_stream_real_splice_finish (GOutputStream *stream,
1379 GAsyncResult *result,
1380 GError **error)
1382 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1383 SpliceData *op;
1385 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_splice_async);
1386 op = g_simple_async_result_get_op_res_gpointer (simple);
1387 return op->bytes_copied;
1391 static void
1392 flush_async_thread (GSimpleAsyncResult *res,
1393 GObject *object,
1394 GCancellable *cancellable)
1396 GOutputStreamClass *class;
1397 gboolean result;
1398 GError *error = NULL;
1400 class = G_OUTPUT_STREAM_GET_CLASS (object);
1401 result = TRUE;
1402 if (class->flush)
1403 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1405 if (!result)
1406 g_simple_async_result_take_error (res, error);
1409 static void
1410 g_output_stream_real_flush_async (GOutputStream *stream,
1411 int io_priority,
1412 GCancellable *cancellable,
1413 GAsyncReadyCallback callback,
1414 gpointer user_data)
1416 GSimpleAsyncResult *res;
1418 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_write_async);
1420 g_simple_async_result_run_in_thread (res, flush_async_thread, io_priority, cancellable);
1421 g_object_unref (res);
1424 static gboolean
1425 g_output_stream_real_flush_finish (GOutputStream *stream,
1426 GAsyncResult *result,
1427 GError **error)
1429 return TRUE;
1432 static void
1433 close_async_thread (GSimpleAsyncResult *res,
1434 GObject *object,
1435 GCancellable *cancellable)
1437 GOutputStreamClass *class;
1438 GError *error = NULL;
1439 gboolean result = TRUE;
1441 class = G_OUTPUT_STREAM_GET_CLASS (object);
1443 /* Do a flush here if there is a flush function, and we did not have to do
1444 an async flush before (see g_output_stream_close_async) */
1445 if (class->flush != NULL &&
1446 (class->flush_async == NULL ||
1447 class->flush_async == g_output_stream_real_flush_async))
1449 result = class->flush (G_OUTPUT_STREAM (object), cancellable, &error);
1452 /* Auto handling of cancelation disabled, and ignore
1453 cancellation, since we want to close things anyway, although
1454 possibly in a quick-n-dirty way. At least we never want to leak
1455 open handles */
1457 if (class->close_fn)
1459 /* Make sure to close, even if the flush failed (see sync close) */
1460 if (!result)
1461 class->close_fn (G_OUTPUT_STREAM (object), cancellable, NULL);
1462 else
1463 result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
1465 if (!result)
1466 g_simple_async_result_take_error (res, error);
1470 static void
1471 g_output_stream_real_close_async (GOutputStream *stream,
1472 int io_priority,
1473 GCancellable *cancellable,
1474 GAsyncReadyCallback callback,
1475 gpointer user_data)
1477 GSimpleAsyncResult *res;
1479 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_output_stream_real_close_async);
1481 g_simple_async_result_set_handle_cancellation (res, FALSE);
1483 g_simple_async_result_run_in_thread (res, close_async_thread, io_priority, cancellable);
1484 g_object_unref (res);
1487 static gboolean
1488 g_output_stream_real_close_finish (GOutputStream *stream,
1489 GAsyncResult *result,
1490 GError **error)
1492 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1493 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
1494 return TRUE;