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>
27 #include "ginputstream.h"
28 #include "gseekable.h"
29 #include "gcancellable.h"
30 #include "gasyncresult.h"
32 #include "gpollableinputstream.h"
35 * SECTION:ginputstream
36 * @short_description: Base class for implementing streaming input
39 * #GInputStream has functions to read from a stream (g_input_stream_read()),
40 * to close a stream (g_input_stream_close()) and to skip some content
41 * (g_input_stream_skip()).
43 * To copy the content of an input stream to an output stream without
44 * manually handling the reads and writes, use g_output_stream_splice().
46 * All of these functions have async variants too.
49 G_DEFINE_ABSTRACT_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
51 struct _GInputStreamPrivate {
54 GAsyncReadyCallback outstanding_callback;
57 static gssize g_input_stream_real_skip (GInputStream *stream,
59 GCancellable *cancellable,
61 static void g_input_stream_real_read_async (GInputStream *stream,
65 GCancellable *cancellable,
66 GAsyncReadyCallback callback,
68 static gssize g_input_stream_real_read_finish (GInputStream *stream,
71 static void g_input_stream_real_skip_async (GInputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
80 static void g_input_stream_real_close_async (GInputStream *stream,
82 GCancellable *cancellable,
83 GAsyncReadyCallback callback,
85 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
90 g_input_stream_finalize (GObject *object)
92 G_OBJECT_CLASS (g_input_stream_parent_class)->finalize (object);
96 g_input_stream_dispose (GObject *object)
100 stream = G_INPUT_STREAM (object);
102 if (!stream->priv->closed)
103 g_input_stream_close (stream, NULL, NULL);
105 G_OBJECT_CLASS (g_input_stream_parent_class)->dispose (object);
110 g_input_stream_class_init (GInputStreamClass *klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
116 gobject_class->finalize = g_input_stream_finalize;
117 gobject_class->dispose = g_input_stream_dispose;
119 klass->skip = g_input_stream_real_skip;
120 klass->read_async = g_input_stream_real_read_async;
121 klass->read_finish = g_input_stream_real_read_finish;
122 klass->skip_async = g_input_stream_real_skip_async;
123 klass->skip_finish = g_input_stream_real_skip_finish;
124 klass->close_async = g_input_stream_real_close_async;
125 klass->close_finish = g_input_stream_real_close_finish;
129 g_input_stream_init (GInputStream *stream)
131 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
133 GInputStreamPrivate);
137 * g_input_stream_read:
138 * @stream: a #GInputStream.
139 * @buffer: a buffer to read data into (which should be at least count bytes long).
140 * @count: the number of bytes that will be read from the stream
141 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
142 * @error: location to store the error occurring, or %NULL to ignore
144 * Tries to read @count bytes from the stream into the buffer starting at
145 * @buffer. Will block during this read.
147 * If count is zero returns zero and does nothing. A value of @count
148 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
150 * On success, the number of bytes read into the buffer is returned.
151 * It is not an error if this is not the same as the requested size, as it
152 * can happen e.g. near the end of a file. Zero is returned on end of file
153 * (or if @count is zero), but never otherwise.
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 read, or -1 on error, or 0 on end of file.
166 g_input_stream_read (GInputStream *stream,
169 GCancellable *cancellable,
172 GInputStreamClass *class;
175 g_return_val_if_fail (G_IS_INPUT_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 %s"), G_STRFUNC);
188 class = G_INPUT_STREAM_GET_CLASS (stream);
190 if (class->read_fn == NULL)
192 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
193 _("Input stream doesn't implement read"));
197 if (!g_input_stream_set_pending (stream, error))
201 g_cancellable_push_current (cancellable);
203 res = class->read_fn (stream, buffer, count, cancellable, error);
206 g_cancellable_pop_current (cancellable);
208 g_input_stream_clear_pending (stream);
214 * g_input_stream_read_all:
215 * @stream: a #GInputStream.
216 * @buffer: a buffer to read data into (which should be at least count bytes long).
217 * @count: the number of bytes that will be read from the stream
218 * @bytes_read: (out): location to store the number of bytes that was read from the stream
219 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
220 * @error: location to store the error occurring, or %NULL to ignore
222 * Tries to read @count bytes from the stream into the buffer starting at
223 * @buffer. Will block during this read.
225 * This function is similar to g_input_stream_read(), except it tries to
226 * read as many bytes as requested, only stopping on an error or end of stream.
228 * On a successful read of @count bytes, or if we reached the end of the
229 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
232 * If there is an error during the operation %FALSE is returned and @error
233 * is set to indicate the error status, @bytes_read is updated to contain
234 * the number of bytes read into @buffer before the error occurred.
236 * Return value: %TRUE on success, %FALSE if there was an error
239 g_input_stream_read_all (GInputStream *stream,
243 GCancellable *cancellable,
249 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
250 g_return_val_if_fail (buffer != NULL, FALSE);
253 while (_bytes_read < count)
255 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
260 *bytes_read = _bytes_read;
271 *bytes_read = _bytes_read;
276 * g_input_stream_read_bytes:
277 * @stream: a #GInputStream.
278 * @count: maximum number of bytes that will be read from the stream. Common
279 * values include 4096 and 8192.
280 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
281 * @error: location to store the error occurring, or %NULL to ignore
283 * Like g_input_stream_read(), this tries to read @count bytes from
284 * the stream in a blocking fashion. However, rather than reading into
285 * a user-supplied buffer, this will create a new #GBytes containing
286 * the data that was read. This may be easier to use from language
289 * If count is zero, returns a zero-length #GBytes and does nothing. A
290 * value of @count larger than %G_MAXSSIZE will cause a
291 * %G_IO_ERROR_INVALID_ARGUMENT error.
293 * On success, a new #GBytes is returned. It is not an error if the
294 * size of this object is not the same as the requested size, as it
295 * can happen e.g. near the end of a file. A zero-length #GBytes is
296 * returned on end of file (or if @count is zero), but never
299 * If @cancellable is not %NULL, then the operation can be cancelled by
300 * triggering the cancellable object from another thread. If the operation
301 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
302 * operation was partially finished when the operation was cancelled the
303 * partial result will be returned, without an error.
305 * On error %NULL is returned and @error is set accordingly.
307 * Return value: a new #GBytes, or %NULL on error
310 g_input_stream_read_bytes (GInputStream *stream,
312 GCancellable *cancellable,
318 buf = g_malloc (count);
319 nread = g_input_stream_read (stream, buf, count, cancellable, error);
328 return g_bytes_new_static ("", 0);
331 return g_bytes_new_take (buf, nread);
335 * g_input_stream_skip:
336 * @stream: a #GInputStream.
337 * @count: the number of bytes that will be skipped from the stream
338 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
339 * @error: location to store the error occurring, or %NULL to ignore
341 * Tries to skip @count bytes from the stream. Will block during the operation.
343 * This is identical to g_input_stream_read(), from a behaviour standpoint,
344 * but the bytes that are skipped are not returned to the user. Some
345 * streams have an implementation that is more efficient than reading the data.
347 * This function is optional for inherited classes, as the default implementation
348 * emulates it using read.
350 * If @cancellable is not %NULL, then the operation can be cancelled by
351 * triggering the cancellable object from another thread. If the operation
352 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
353 * operation was partially finished when the operation was cancelled the
354 * partial result will be returned, without an error.
356 * Return value: Number of bytes skipped, or -1 on error
359 g_input_stream_skip (GInputStream *stream,
361 GCancellable *cancellable,
364 GInputStreamClass *class;
367 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
372 if (((gssize) count) < 0)
374 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
375 _("Too large count value passed to %s"), G_STRFUNC);
379 class = G_INPUT_STREAM_GET_CLASS (stream);
381 if (!g_input_stream_set_pending (stream, error))
385 g_cancellable_push_current (cancellable);
387 res = class->skip (stream, count, cancellable, error);
390 g_cancellable_pop_current (cancellable);
392 g_input_stream_clear_pending (stream);
398 g_input_stream_real_skip (GInputStream *stream,
400 GCancellable *cancellable,
403 GInputStreamClass *class;
404 gssize ret, read_bytes;
408 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
410 if (g_seekable_seek (G_SEEKABLE (stream),
418 /* If not seekable, or seek failed, fall back to reading data: */
420 class = G_INPUT_STREAM_GET_CLASS (stream);
427 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
428 cancellable, &my_error);
431 if (read_bytes > 0 &&
432 my_error->domain == G_IO_ERROR &&
433 my_error->code == G_IO_ERROR_CANCELLED)
435 g_error_free (my_error);
439 g_propagate_error (error, my_error);
446 if (ret == 0 || count == 0)
452 * g_input_stream_close:
453 * @stream: A #GInputStream.
454 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
455 * @error: location to store the error occurring, or %NULL to ignore
457 * Closes the stream, releasing resources related to it.
459 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
460 * Closing a stream multiple times will not return an error.
462 * Streams will be automatically closed when the last reference
463 * is dropped, but you might want to call this function to make sure
464 * resources are released as early as possible.
466 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
467 * open after the stream is closed. See the documentation for the individual
468 * stream for details.
470 * On failure the first error that happened will be reported, but the close
471 * operation will finish as much as possible. A stream that failed to
472 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
473 * is important to check and report the error to the user.
475 * If @cancellable is not %NULL, then the operation can be cancelled by
476 * triggering the cancellable object from another thread. If the operation
477 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
478 * Cancelling a close will still leave the stream closed, but some streams
479 * can use a faster close that doesn't block to e.g. check errors.
481 * Return value: %TRUE on success, %FALSE on failure
484 g_input_stream_close (GInputStream *stream,
485 GCancellable *cancellable,
488 GInputStreamClass *class;
491 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
493 class = G_INPUT_STREAM_GET_CLASS (stream);
495 if (stream->priv->closed)
500 if (!g_input_stream_set_pending (stream, error))
504 g_cancellable_push_current (cancellable);
507 res = class->close_fn (stream, cancellable, error);
510 g_cancellable_pop_current (cancellable);
512 g_input_stream_clear_pending (stream);
514 stream->priv->closed = TRUE;
520 async_ready_callback_wrapper (GObject *source_object,
524 GInputStream *stream = G_INPUT_STREAM (source_object);
526 g_input_stream_clear_pending (stream);
527 if (stream->priv->outstanding_callback)
528 (*stream->priv->outstanding_callback) (source_object, res, user_data);
529 g_object_unref (stream);
533 async_ready_close_callback_wrapper (GObject *source_object,
537 GInputStream *stream = G_INPUT_STREAM (source_object);
539 g_input_stream_clear_pending (stream);
540 stream->priv->closed = TRUE;
541 if (stream->priv->outstanding_callback)
542 (*stream->priv->outstanding_callback) (source_object, res, user_data);
543 g_object_unref (stream);
547 * g_input_stream_read_async:
548 * @stream: A #GInputStream.
549 * @buffer: a buffer to read data into (which should be at least count bytes long).
550 * @count: the number of bytes that will be read from the stream
551 * @io_priority: the <link linkend="io-priority">I/O priority</link>
553 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
554 * @callback: (scope async): callback to call when the request is satisfied
555 * @user_data: (closure): the data to pass to callback function
557 * Request an asynchronous read of @count bytes from the stream into the buffer
558 * starting at @buffer. When the operation is finished @callback will be called.
559 * You can then call g_input_stream_read_finish() to get the result of the
562 * During an async request no other sync and async calls are allowed on @stream, and will
563 * result in %G_IO_ERROR_PENDING errors.
565 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
567 * On success, the number of bytes read into the buffer will be passed to the
568 * callback. It is not an error if this is not the same as the requested size, as it
569 * can happen e.g. near the end of a file, but generally we try to read
570 * as many bytes as requested. Zero is returned on end of file
571 * (or if @count is zero), but never otherwise.
573 * Any outstanding i/o request with higher priority (lower numerical value) will
574 * be executed before an outstanding request with lower priority. Default
575 * priority is %G_PRIORITY_DEFAULT.
577 * The asyncronous methods have a default fallback that uses threads to implement
578 * asynchronicity, so they are optional for inheriting classes. However, if you
579 * override one you must override all.
582 g_input_stream_read_async (GInputStream *stream,
586 GCancellable *cancellable,
587 GAsyncReadyCallback callback,
590 GInputStreamClass *class;
591 GError *error = NULL;
593 g_return_if_fail (G_IS_INPUT_STREAM (stream));
594 g_return_if_fail (buffer != NULL);
600 task = g_task_new (stream, cancellable, callback, user_data);
601 g_task_set_source_tag (task, g_input_stream_read_async);
602 g_task_return_int (task, 0);
603 g_object_unref (task);
607 if (((gssize) count) < 0)
609 g_task_report_new_error (stream, callback, user_data,
610 g_input_stream_read_async,
611 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
612 _("Too large count value passed to %s"),
617 if (!g_input_stream_set_pending (stream, &error))
619 g_task_report_error (stream, callback, user_data,
620 g_input_stream_read_async,
625 class = G_INPUT_STREAM_GET_CLASS (stream);
626 stream->priv->outstanding_callback = callback;
627 g_object_ref (stream);
628 class->read_async (stream, buffer, count, io_priority, cancellable,
629 async_ready_callback_wrapper, user_data);
633 * g_input_stream_read_finish:
634 * @stream: a #GInputStream.
635 * @result: a #GAsyncResult.
636 * @error: a #GError location to store the error occurring, or %NULL to
639 * Finishes an asynchronous stream read operation.
641 * Returns: number of bytes read in, or -1 on error, or 0 on end of file.
644 g_input_stream_read_finish (GInputStream *stream,
645 GAsyncResult *result,
648 GInputStreamClass *class;
650 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
651 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
653 if (g_async_result_legacy_propagate_error (result, error))
655 else if (g_async_result_is_tagged (result, g_input_stream_read_async))
656 return g_task_propagate_int (G_TASK (result), error);
658 class = G_INPUT_STREAM_GET_CLASS (stream);
659 return class->read_finish (stream, result, error);
663 read_bytes_callback (GObject *stream,
664 GAsyncResult *result,
667 GTask *task = user_data;
668 guchar *buf = g_task_get_task_data (task);
669 GError *error = NULL;
671 GBytes *bytes = NULL;
673 nread = g_input_stream_read_finish (G_INPUT_STREAM (stream),
678 g_task_return_error (task, error);
683 bytes = g_bytes_new_static ("", 0);
686 bytes = g_bytes_new_take (buf, nread);
689 g_task_return_pointer (task, bytes, (GDestroyNotify)g_bytes_unref);
691 g_object_unref (task);
695 * g_input_stream_read_bytes_async:
696 * @stream: A #GInputStream.
697 * @count: the number of bytes that will be read from the stream
698 * @io_priority: the <link linkend="io-priority">I/O priority</link>
700 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
701 * @callback: (scope async): callback to call when the request is satisfied
702 * @user_data: (closure): the data to pass to callback function
704 * Request an asynchronous read of @count bytes from the stream into a
705 * new #GBytes. When the operation is finished @callback will be
706 * called. You can then call g_input_stream_read_bytes_finish() to get the
707 * result of the operation.
709 * During an async request no other sync and async calls are allowed
710 * on @stream, and will result in %G_IO_ERROR_PENDING errors.
712 * A value of @count larger than %G_MAXSSIZE will cause a
713 * %G_IO_ERROR_INVALID_ARGUMENT error.
715 * On success, the new #GBytes will be passed to the callback. It is
716 * not an error if this is smaller than the requested size, as it can
717 * happen e.g. near the end of a file, but generally we try to read as
718 * many bytes as requested. Zero is returned on end of file (or if
719 * @count is zero), but never otherwise.
721 * Any outstanding I/O request with higher priority (lower numerical
722 * value) will be executed before an outstanding request with lower
723 * priority. Default priority is %G_PRIORITY_DEFAULT.
726 g_input_stream_read_bytes_async (GInputStream *stream,
729 GCancellable *cancellable,
730 GAsyncReadyCallback callback,
736 task = g_task_new (stream, cancellable, callback, user_data);
737 buf = g_malloc (count);
738 g_task_set_task_data (task, buf, NULL);
740 g_input_stream_read_async (stream, buf, count,
741 io_priority, cancellable,
742 read_bytes_callback, task);
746 * g_input_stream_read_bytes_finish:
747 * @stream: a #GInputStream.
748 * @result: a #GAsyncResult.
749 * @error: a #GError location to store the error occurring, or %NULL to
752 * Finishes an asynchronous stream read-into-#GBytes operation.
754 * Returns: the newly-allocated #GBytes, or %NULL on error
757 g_input_stream_read_bytes_finish (GInputStream *stream,
758 GAsyncResult *result,
761 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
762 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
764 return g_task_propagate_pointer (G_TASK (result), error);
768 * g_input_stream_skip_async:
769 * @stream: A #GInputStream.
770 * @count: the number of bytes that will be skipped from the stream
771 * @io_priority: the <link linkend="io-priority">I/O priority</link>
773 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
774 * @callback: (scope async): callback to call when the request is satisfied
775 * @user_data: (closure): the data to pass to callback function
777 * Request an asynchronous skip of @count bytes from the stream.
778 * When the operation is finished @callback will be called.
779 * You can then call g_input_stream_skip_finish() to get the result
782 * During an async request no other sync and async calls are allowed,
783 * and will result in %G_IO_ERROR_PENDING errors.
785 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
787 * On success, the number of bytes skipped will be passed to the callback.
788 * It is not an error if this is not the same as the requested size, as it
789 * can happen e.g. near the end of a file, but generally we try to skip
790 * as many bytes as requested. Zero is returned on end of file
791 * (or if @count is zero), but never otherwise.
793 * Any outstanding i/o request with higher priority (lower numerical value)
794 * will be executed before an outstanding request with lower priority.
795 * Default priority is %G_PRIORITY_DEFAULT.
797 * The asynchronous methods have a default fallback that uses threads to
798 * implement asynchronicity, so they are optional for inheriting classes.
799 * However, if you override one, you must override all.
802 g_input_stream_skip_async (GInputStream *stream,
805 GCancellable *cancellable,
806 GAsyncReadyCallback callback,
809 GInputStreamClass *class;
810 GError *error = NULL;
812 g_return_if_fail (G_IS_INPUT_STREAM (stream));
818 task = g_task_new (stream, cancellable, callback, user_data);
819 g_task_set_source_tag (task, g_input_stream_skip_async);
820 g_task_return_int (task, 0);
821 g_object_unref (task);
825 if (((gssize) count) < 0)
827 g_task_report_new_error (stream, callback, user_data,
828 g_input_stream_skip_async,
829 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
830 _("Too large count value passed to %s"),
835 if (!g_input_stream_set_pending (stream, &error))
837 g_task_report_error (stream, callback, user_data,
838 g_input_stream_skip_async,
843 class = G_INPUT_STREAM_GET_CLASS (stream);
844 stream->priv->outstanding_callback = callback;
845 g_object_ref (stream);
846 class->skip_async (stream, count, io_priority, cancellable,
847 async_ready_callback_wrapper, user_data);
851 * g_input_stream_skip_finish:
852 * @stream: a #GInputStream.
853 * @result: a #GAsyncResult.
854 * @error: a #GError location to store the error occurring, or %NULL to
857 * Finishes a stream skip operation.
859 * Returns: the size of the bytes skipped, or %-1 on error.
862 g_input_stream_skip_finish (GInputStream *stream,
863 GAsyncResult *result,
866 GInputStreamClass *class;
868 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
869 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
871 if (g_async_result_legacy_propagate_error (result, error))
873 else if (g_async_result_is_tagged (result, g_input_stream_skip_async))
874 return g_task_propagate_int (G_TASK (result), error);
876 class = G_INPUT_STREAM_GET_CLASS (stream);
877 return class->skip_finish (stream, result, error);
881 * g_input_stream_close_async:
882 * @stream: A #GInputStream.
883 * @io_priority: the <link linkend="io-priority">I/O priority</link>
885 * @cancellable: (allow-none): optional cancellable object
886 * @callback: (scope async): callback to call when the request is satisfied
887 * @user_data: (closure): the data to pass to callback function
889 * Requests an asynchronous closes of the stream, releasing resources related to it.
890 * When the operation is finished @callback will be called.
891 * You can then call g_input_stream_close_finish() to get the result of the
894 * For behaviour details see g_input_stream_close().
896 * The asyncronous methods have a default fallback that uses threads to implement
897 * asynchronicity, so they are optional for inheriting classes. However, if you
898 * override one you must override all.
901 g_input_stream_close_async (GInputStream *stream,
903 GCancellable *cancellable,
904 GAsyncReadyCallback callback,
907 GInputStreamClass *class;
908 GError *error = NULL;
910 g_return_if_fail (G_IS_INPUT_STREAM (stream));
912 if (stream->priv->closed)
916 task = g_task_new (stream, cancellable, callback, user_data);
917 g_task_set_source_tag (task, g_input_stream_close_async);
918 g_task_return_boolean (task, TRUE);
919 g_object_unref (task);
923 if (!g_input_stream_set_pending (stream, &error))
925 g_task_report_error (stream, callback, user_data,
926 g_input_stream_close_async,
931 class = G_INPUT_STREAM_GET_CLASS (stream);
932 stream->priv->outstanding_callback = callback;
933 g_object_ref (stream);
934 class->close_async (stream, io_priority, cancellable,
935 async_ready_close_callback_wrapper, user_data);
939 * g_input_stream_close_finish:
940 * @stream: a #GInputStream.
941 * @result: a #GAsyncResult.
942 * @error: a #GError location to store the error occurring, or %NULL to
945 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
947 * Returns: %TRUE if the stream was closed successfully.
950 g_input_stream_close_finish (GInputStream *stream,
951 GAsyncResult *result,
954 GInputStreamClass *class;
956 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
957 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
959 if (g_async_result_legacy_propagate_error (result, error))
961 else if (g_async_result_is_tagged (result, g_input_stream_close_async))
962 return g_task_propagate_boolean (G_TASK (result), error);
964 class = G_INPUT_STREAM_GET_CLASS (stream);
965 return class->close_finish (stream, result, error);
969 * g_input_stream_is_closed:
970 * @stream: input stream.
972 * Checks if an input stream is closed.
974 * Returns: %TRUE if the stream is closed.
977 g_input_stream_is_closed (GInputStream *stream)
979 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
981 return stream->priv->closed;
985 * g_input_stream_has_pending:
986 * @stream: input stream.
988 * Checks if an input stream has pending actions.
990 * Returns: %TRUE if @stream has pending actions.
993 g_input_stream_has_pending (GInputStream *stream)
995 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
997 return stream->priv->pending;
1001 * g_input_stream_set_pending:
1002 * @stream: input stream
1003 * @error: a #GError location to store the error occurring, or %NULL to
1006 * Sets @stream to have actions pending. If the pending flag is
1007 * already set or @stream is closed, it will return %FALSE and set
1010 * Return value: %TRUE if pending was previously unset and is now set.
1013 g_input_stream_set_pending (GInputStream *stream, GError **error)
1015 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
1017 if (stream->priv->closed)
1019 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1020 _("Stream is already closed"));
1024 if (stream->priv->pending)
1026 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1027 /* Translators: This is an error you get if there is already an
1028 * operation running against this stream when you try to start
1030 _("Stream has outstanding operation"));
1034 stream->priv->pending = TRUE;
1039 * g_input_stream_clear_pending:
1040 * @stream: input stream
1042 * Clears the pending flag on @stream.
1045 g_input_stream_clear_pending (GInputStream *stream)
1047 g_return_if_fail (G_IS_INPUT_STREAM (stream));
1049 stream->priv->pending = FALSE;
1052 /********************************************
1053 * Default implementation of async ops *
1054 ********************************************/
1062 free_read_data (ReadData *op)
1064 g_slice_free (ReadData, op);
1068 read_async_thread (GTask *task,
1069 gpointer source_object,
1071 GCancellable *cancellable)
1073 GInputStream *stream = source_object;
1074 ReadData *op = task_data;
1075 GInputStreamClass *class;
1076 GError *error = NULL;
1079 class = G_INPUT_STREAM_GET_CLASS (stream);
1081 nread = class->read_fn (stream,
1082 op->buffer, op->count,
1083 g_task_get_cancellable (task),
1086 g_task_return_error (task, error);
1088 g_task_return_int (task, nread);
1091 static void read_async_pollable (GPollableInputStream *stream,
1095 read_async_pollable_ready (GPollableInputStream *stream,
1098 GTask *task = user_data;
1100 read_async_pollable (stream, task);
1105 read_async_pollable (GPollableInputStream *stream,
1108 ReadData *op = g_task_get_task_data (task);
1109 GError *error = NULL;
1112 if (g_task_return_error_if_cancelled (task))
1115 nread = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
1116 read_nonblocking (stream, op->buffer, op->count, &error);
1118 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1122 g_error_free (error);
1124 source = g_pollable_input_stream_create_source (stream,
1125 g_task_get_cancellable (task));
1126 g_task_attach_source (task, source,
1127 (GSourceFunc) read_async_pollable_ready);
1128 g_source_unref (source);
1133 g_task_return_error (task, error);
1135 g_task_return_int (task, nread);
1136 /* g_input_stream_real_read_async() unrefs task */
1140 g_input_stream_real_read_async (GInputStream *stream,
1144 GCancellable *cancellable,
1145 GAsyncReadyCallback callback,
1151 op = g_slice_new0 (ReadData);
1152 task = g_task_new (stream, cancellable, callback, user_data);
1153 g_task_set_task_data (task, op, (GDestroyNotify) free_read_data);
1154 g_task_set_priority (task, io_priority);
1155 op->buffer = buffer;
1158 if (G_IS_POLLABLE_INPUT_STREAM (stream) &&
1159 g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (stream)))
1160 read_async_pollable (G_POLLABLE_INPUT_STREAM (stream), task);
1162 g_task_run_in_thread (task, read_async_thread);
1163 g_object_unref (task);
1167 g_input_stream_real_read_finish (GInputStream *stream,
1168 GAsyncResult *result,
1171 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1173 return g_task_propagate_int (G_TASK (result), error);
1178 skip_async_thread (GTask *task,
1179 gpointer source_object,
1181 GCancellable *cancellable)
1183 GInputStream *stream = source_object;
1184 gsize count = GPOINTER_TO_SIZE (task_data);
1185 GInputStreamClass *class;
1186 GError *error = NULL;
1189 class = G_INPUT_STREAM_GET_CLASS (stream);
1190 ret = class->skip (stream, count,
1191 g_task_get_cancellable (task),
1194 g_task_return_error (task, error);
1196 g_task_return_int (task, ret);
1202 gsize count_skipped;
1204 GAsyncReadyCallback callback;
1205 } SkipFallbackAsyncData;
1208 skip_callback_wrapper (GObject *source_object,
1212 GInputStreamClass *class;
1213 GTask *task = user_data;
1214 SkipFallbackAsyncData *data = g_task_get_task_data (task);
1215 GError *error = NULL;
1218 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1223 data->count_skipped += ret;
1225 if (data->count > 0)
1227 class = G_INPUT_STREAM_GET_CLASS (source_object);
1228 class->read_async (G_INPUT_STREAM (source_object),
1229 data->buffer, MIN (8192, data->count),
1230 g_task_get_priority (task),
1231 g_task_get_cancellable (task),
1232 skip_callback_wrapper, data);
1238 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
1239 data->count_skipped)
1241 /* No error, return partial read */
1242 g_clear_error (&error);
1246 g_task_return_error (task, error);
1248 g_task_return_int (task, data->count_skipped);
1249 g_object_unref (task);
1253 g_input_stream_real_skip_async (GInputStream *stream,
1256 GCancellable *cancellable,
1257 GAsyncReadyCallback callback,
1260 GInputStreamClass *class;
1261 SkipFallbackAsyncData *data;
1264 class = G_INPUT_STREAM_GET_CLASS (stream);
1266 task = g_task_new (stream, cancellable, callback, user_data);
1267 g_task_set_priority (task, io_priority);
1269 if (class->read_async == g_input_stream_real_read_async)
1271 /* Read is thread-using async fallback.
1272 * Make skip use threads too, so that we can use a possible sync skip
1273 * implementation. */
1274 g_task_set_task_data (task, GSIZE_TO_POINTER (count), NULL);
1276 g_task_run_in_thread (task, skip_async_thread);
1277 g_object_unref (task);
1281 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1283 /* There is a custom async read function, lets use that. */
1284 data = g_new (SkipFallbackAsyncData, 1);
1285 data->count = count;
1286 data->count_skipped = 0;
1287 data->callback = callback;
1288 data->user_data = user_data;
1289 g_task_set_task_data (task, data, g_free);
1290 g_task_set_check_cancellable (task, FALSE);
1291 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1292 skip_callback_wrapper, data);
1298 g_input_stream_real_skip_finish (GInputStream *stream,
1299 GAsyncResult *result,
1302 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1304 return g_task_propagate_int (G_TASK (result), error);
1308 close_async_thread (GTask *task,
1309 gpointer source_object,
1311 GCancellable *cancellable)
1313 GInputStream *stream = source_object;
1314 GInputStreamClass *class;
1315 GError *error = NULL;
1318 class = G_INPUT_STREAM_GET_CLASS (stream);
1319 if (class->close_fn)
1321 result = class->close_fn (stream,
1322 g_task_get_cancellable (task),
1326 g_task_return_error (task, error);
1331 g_task_return_boolean (task, TRUE);
1335 g_input_stream_real_close_async (GInputStream *stream,
1337 GCancellable *cancellable,
1338 GAsyncReadyCallback callback,
1343 task = g_task_new (stream, cancellable, callback, user_data);
1344 g_task_set_check_cancellable (task, FALSE);
1345 g_task_set_priority (task, io_priority);
1347 g_task_run_in_thread (task, close_async_thread);
1348 g_object_unref (task);
1352 g_input_stream_real_close_finish (GInputStream *stream,
1353 GAsyncResult *result,
1356 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1358 return g_task_propagate_boolean (G_TASK (result), error);