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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "ginputstream.h"
26 #include "gioprivate.h"
27 #include "gseekable.h"
28 #include "gcancellable.h"
29 #include "gasyncresult.h"
31 #include "gpollableinputstream.h"
34 * SECTION:ginputstream
35 * @short_description: Base class for implementing streaming input
38 * #GInputStream has functions to read from a stream (g_input_stream_read()),
39 * to close a stream (g_input_stream_close()) and to skip some content
40 * (g_input_stream_skip()).
42 * To copy the content of an input stream to an output stream without
43 * manually handling the reads and writes, use g_output_stream_splice().
45 * All of these functions have async variants too.
48 struct _GInputStreamPrivate {
51 GAsyncReadyCallback outstanding_callback;
54 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GInputStream, g_input_stream, G_TYPE_OBJECT)
56 static gssize g_input_stream_real_skip (GInputStream *stream,
58 GCancellable *cancellable,
60 static void g_input_stream_real_read_async (GInputStream *stream,
64 GCancellable *cancellable,
65 GAsyncReadyCallback callback,
67 static gssize g_input_stream_real_read_finish (GInputStream *stream,
70 static void g_input_stream_real_skip_async (GInputStream *stream,
73 GCancellable *cancellable,
74 GAsyncReadyCallback callback,
76 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
79 static void g_input_stream_real_close_async (GInputStream *stream,
81 GCancellable *cancellable,
82 GAsyncReadyCallback callback,
84 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
89 g_input_stream_dispose (GObject *object)
93 stream = G_INPUT_STREAM (object);
95 if (!stream->priv->closed)
96 g_input_stream_close (stream, NULL, NULL);
98 G_OBJECT_CLASS (g_input_stream_parent_class)->dispose (object);
103 g_input_stream_class_init (GInputStreamClass *klass)
105 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
107 gobject_class->dispose = g_input_stream_dispose;
109 klass->skip = g_input_stream_real_skip;
110 klass->read_async = g_input_stream_real_read_async;
111 klass->read_finish = g_input_stream_real_read_finish;
112 klass->skip_async = g_input_stream_real_skip_async;
113 klass->skip_finish = g_input_stream_real_skip_finish;
114 klass->close_async = g_input_stream_real_close_async;
115 klass->close_finish = g_input_stream_real_close_finish;
119 g_input_stream_init (GInputStream *stream)
121 stream->priv = g_input_stream_get_instance_private (stream);
125 * g_input_stream_read:
126 * @stream: a #GInputStream.
127 * @buffer: (array length=count) (element-type guint8): a buffer to
128 * read data into (which should be at least count bytes long).
129 * @count: the number of bytes that will be read from the stream
130 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
131 * @error: location to store the error occurring, or %NULL to ignore
133 * Tries to read @count bytes from the stream into the buffer starting at
134 * @buffer. Will block during this read.
136 * If count is zero returns zero and does nothing. A value of @count
137 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
139 * On success, the number of bytes read into the buffer is returned.
140 * It is not an error if this is not the same as the requested size, as it
141 * can happen e.g. near the end of a file. Zero is returned on end of file
142 * (or if @count is zero), but never otherwise.
144 * If @cancellable is not %NULL, then the operation can be cancelled by
145 * triggering the cancellable object from another thread. If the operation
146 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
147 * operation was partially finished when the operation was cancelled the
148 * partial result will be returned, without an error.
150 * On error -1 is returned and @error is set accordingly.
152 * Return value: Number of bytes read, or -1 on error, or 0 on end of file.
155 g_input_stream_read (GInputStream *stream,
158 GCancellable *cancellable,
161 GInputStreamClass *class;
164 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
165 g_return_val_if_fail (buffer != NULL, 0);
170 if (((gssize) count) < 0)
172 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
173 _("Too large count value passed to %s"), G_STRFUNC);
177 class = G_INPUT_STREAM_GET_CLASS (stream);
179 if (class->read_fn == NULL)
181 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
182 _("Input stream doesn't implement read"));
186 if (!g_input_stream_set_pending (stream, error))
190 g_cancellable_push_current (cancellable);
192 res = class->read_fn (stream, buffer, count, cancellable, error);
195 g_cancellable_pop_current (cancellable);
197 g_input_stream_clear_pending (stream);
203 * g_input_stream_read_all:
204 * @stream: a #GInputStream.
205 * @buffer: (array length=count) (element-type guint8): a buffer to
206 * read data into (which should be at least count bytes long).
207 * @count: the number of bytes that will be read from the stream
208 * @bytes_read: (out): location to store the number of bytes that was read from the stream
209 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
210 * @error: location to store the error occurring, or %NULL to ignore
212 * Tries to read @count bytes from the stream into the buffer starting at
213 * @buffer. Will block during this read.
215 * This function is similar to g_input_stream_read(), except it tries to
216 * read as many bytes as requested, only stopping on an error or end of stream.
218 * On a successful read of @count bytes, or if we reached the end of the
219 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
222 * If there is an error during the operation %FALSE is returned and @error
223 * is set to indicate the error status, @bytes_read is updated to contain
224 * the number of bytes read into @buffer before the error occurred.
226 * Return value: %TRUE on success, %FALSE if there was an error
229 g_input_stream_read_all (GInputStream *stream,
233 GCancellable *cancellable,
239 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
240 g_return_val_if_fail (buffer != NULL, FALSE);
243 while (_bytes_read < count)
245 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
250 *bytes_read = _bytes_read;
261 *bytes_read = _bytes_read;
266 * g_input_stream_read_bytes:
267 * @stream: a #GInputStream.
268 * @count: maximum number of bytes that will be read from the stream. Common
269 * values include 4096 and 8192.
270 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
271 * @error: location to store the error occurring, or %NULL to ignore
273 * Like g_input_stream_read(), this tries to read @count bytes from
274 * the stream in a blocking fashion. However, rather than reading into
275 * a user-supplied buffer, this will create a new #GBytes containing
276 * the data that was read. This may be easier to use from language
279 * If count is zero, returns a zero-length #GBytes and does nothing. A
280 * value of @count larger than %G_MAXSSIZE will cause a
281 * %G_IO_ERROR_INVALID_ARGUMENT error.
283 * On success, a new #GBytes is returned. It is not an error if the
284 * size of this object is not the same as the requested size, as it
285 * can happen e.g. near the end of a file. A zero-length #GBytes is
286 * returned on end of file (or if @count is zero), but never
289 * If @cancellable is not %NULL, then the operation can be cancelled by
290 * triggering the cancellable object from another thread. If the operation
291 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
292 * operation was partially finished when the operation was cancelled the
293 * partial result will be returned, without an error.
295 * On error %NULL is returned and @error is set accordingly.
297 * Return value: a new #GBytes, or %NULL on error
300 g_input_stream_read_bytes (GInputStream *stream,
302 GCancellable *cancellable,
308 buf = g_malloc (count);
309 nread = g_input_stream_read (stream, buf, count, cancellable, error);
318 return g_bytes_new_static ("", 0);
321 return g_bytes_new_take (buf, nread);
325 * g_input_stream_skip:
326 * @stream: a #GInputStream.
327 * @count: the number of bytes that will be skipped from the stream
328 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
329 * @error: location to store the error occurring, or %NULL to ignore
331 * Tries to skip @count bytes from the stream. Will block during the operation.
333 * This is identical to g_input_stream_read(), from a behaviour standpoint,
334 * but the bytes that are skipped are not returned to the user. Some
335 * streams have an implementation that is more efficient than reading the data.
337 * This function is optional for inherited classes, as the default implementation
338 * emulates it using read.
340 * If @cancellable is not %NULL, then the operation can be cancelled by
341 * triggering the cancellable object from another thread. If the operation
342 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
343 * operation was partially finished when the operation was cancelled the
344 * partial result will be returned, without an error.
346 * Return value: Number of bytes skipped, or -1 on error
349 g_input_stream_skip (GInputStream *stream,
351 GCancellable *cancellable,
354 GInputStreamClass *class;
357 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
362 if (((gssize) count) < 0)
364 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
365 _("Too large count value passed to %s"), G_STRFUNC);
369 class = G_INPUT_STREAM_GET_CLASS (stream);
371 if (!g_input_stream_set_pending (stream, error))
375 g_cancellable_push_current (cancellable);
377 res = class->skip (stream, count, cancellable, error);
380 g_cancellable_pop_current (cancellable);
382 g_input_stream_clear_pending (stream);
388 g_input_stream_real_skip (GInputStream *stream,
390 GCancellable *cancellable,
393 GInputStreamClass *class;
394 gssize ret, read_bytes;
398 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
400 if (g_seekable_seek (G_SEEKABLE (stream),
408 /* If not seekable, or seek failed, fall back to reading data: */
410 class = G_INPUT_STREAM_GET_CLASS (stream);
417 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
418 cancellable, &my_error);
421 if (read_bytes > 0 &&
422 my_error->domain == G_IO_ERROR &&
423 my_error->code == G_IO_ERROR_CANCELLED)
425 g_error_free (my_error);
429 g_propagate_error (error, my_error);
436 if (ret == 0 || count == 0)
442 * g_input_stream_close:
443 * @stream: A #GInputStream.
444 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
445 * @error: location to store the error occurring, or %NULL to ignore
447 * Closes the stream, releasing resources related to it.
449 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
450 * Closing a stream multiple times will not return an error.
452 * Streams will be automatically closed when the last reference
453 * is dropped, but you might want to call this function to make sure
454 * resources are released as early as possible.
456 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
457 * open after the stream is closed. See the documentation for the individual
458 * stream for details.
460 * On failure the first error that happened will be reported, but the close
461 * operation will finish as much as possible. A stream that failed to
462 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
463 * is important to check and report the error to the user.
465 * If @cancellable is not %NULL, then the operation can be cancelled by
466 * triggering the cancellable object from another thread. If the operation
467 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
468 * Cancelling a close will still leave the stream closed, but some streams
469 * can use a faster close that doesn't block to e.g. check errors.
471 * Return value: %TRUE on success, %FALSE on failure
474 g_input_stream_close (GInputStream *stream,
475 GCancellable *cancellable,
478 GInputStreamClass *class;
481 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
483 class = G_INPUT_STREAM_GET_CLASS (stream);
485 if (stream->priv->closed)
490 if (!g_input_stream_set_pending (stream, error))
494 g_cancellable_push_current (cancellable);
497 res = class->close_fn (stream, cancellable, error);
500 g_cancellable_pop_current (cancellable);
502 g_input_stream_clear_pending (stream);
504 stream->priv->closed = TRUE;
510 async_ready_callback_wrapper (GObject *source_object,
514 GInputStream *stream = G_INPUT_STREAM (source_object);
516 g_input_stream_clear_pending (stream);
517 if (stream->priv->outstanding_callback)
518 (*stream->priv->outstanding_callback) (source_object, res, user_data);
519 g_object_unref (stream);
523 async_ready_close_callback_wrapper (GObject *source_object,
527 GInputStream *stream = G_INPUT_STREAM (source_object);
529 g_input_stream_clear_pending (stream);
530 stream->priv->closed = TRUE;
531 if (stream->priv->outstanding_callback)
532 (*stream->priv->outstanding_callback) (source_object, res, user_data);
533 g_object_unref (stream);
537 * g_input_stream_read_async:
538 * @stream: A #GInputStream.
539 * @buffer: (array length=count) (element-type guint8): a buffer to
540 * read data into (which should be at least count bytes long).
541 * @count: the number of bytes that will be read from the stream
542 * @io_priority: the <link linkend="io-priority">I/O priority</link>
544 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
545 * @callback: (scope async): callback to call when the request is satisfied
546 * @user_data: (closure): the data to pass to callback function
548 * Request an asynchronous read of @count bytes from the stream into the buffer
549 * starting at @buffer. When the operation is finished @callback will be called.
550 * You can then call g_input_stream_read_finish() to get the result of the
553 * During an async request no other sync and async calls are allowed on @stream, and will
554 * result in %G_IO_ERROR_PENDING errors.
556 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
558 * On success, the number of bytes read into the buffer will be passed to the
559 * callback. It is not an error if this is not the same as the requested size, as it
560 * can happen e.g. near the end of a file, but generally we try to read
561 * as many bytes as requested. Zero is returned on end of file
562 * (or if @count is zero), but never otherwise.
564 * Any outstanding i/o request with higher priority (lower numerical value) will
565 * be executed before an outstanding request with lower priority. Default
566 * priority is %G_PRIORITY_DEFAULT.
568 * The asyncronous methods have a default fallback that uses threads to implement
569 * asynchronicity, so they are optional for inheriting classes. However, if you
570 * override one you must override all.
573 g_input_stream_read_async (GInputStream *stream,
577 GCancellable *cancellable,
578 GAsyncReadyCallback callback,
581 GInputStreamClass *class;
582 GError *error = NULL;
584 g_return_if_fail (G_IS_INPUT_STREAM (stream));
585 g_return_if_fail (buffer != NULL);
591 task = g_task_new (stream, cancellable, callback, user_data);
592 g_task_set_source_tag (task, g_input_stream_read_async);
593 g_task_return_int (task, 0);
594 g_object_unref (task);
598 if (((gssize) count) < 0)
600 g_task_report_new_error (stream, callback, user_data,
601 g_input_stream_read_async,
602 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
603 _("Too large count value passed to %s"),
608 if (!g_input_stream_set_pending (stream, &error))
610 g_task_report_error (stream, callback, user_data,
611 g_input_stream_read_async,
616 class = G_INPUT_STREAM_GET_CLASS (stream);
617 stream->priv->outstanding_callback = callback;
618 g_object_ref (stream);
619 class->read_async (stream, buffer, count, io_priority, cancellable,
620 async_ready_callback_wrapper, user_data);
624 * g_input_stream_read_finish:
625 * @stream: a #GInputStream.
626 * @result: a #GAsyncResult.
627 * @error: a #GError location to store the error occurring, or %NULL to
630 * Finishes an asynchronous stream read operation.
632 * Returns: number of bytes read in, or -1 on error, or 0 on end of file.
635 g_input_stream_read_finish (GInputStream *stream,
636 GAsyncResult *result,
639 GInputStreamClass *class;
641 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
642 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
644 if (g_async_result_legacy_propagate_error (result, error))
646 else if (g_async_result_is_tagged (result, g_input_stream_read_async))
647 return g_task_propagate_int (G_TASK (result), error);
649 class = G_INPUT_STREAM_GET_CLASS (stream);
650 return class->read_finish (stream, result, error);
654 read_bytes_callback (GObject *stream,
655 GAsyncResult *result,
658 GTask *task = user_data;
659 guchar *buf = g_task_get_task_data (task);
660 GError *error = NULL;
662 GBytes *bytes = NULL;
664 nread = g_input_stream_read_finish (G_INPUT_STREAM (stream),
669 g_task_return_error (task, error);
674 bytes = g_bytes_new_static ("", 0);
677 bytes = g_bytes_new_take (buf, nread);
680 g_task_return_pointer (task, bytes, (GDestroyNotify)g_bytes_unref);
682 g_object_unref (task);
686 * g_input_stream_read_bytes_async:
687 * @stream: A #GInputStream.
688 * @count: the number of bytes that will be read from the stream
689 * @io_priority: the <link linkend="io-priority">I/O priority</link>
691 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
692 * @callback: (scope async): callback to call when the request is satisfied
693 * @user_data: (closure): the data to pass to callback function
695 * Request an asynchronous read of @count bytes from the stream into a
696 * new #GBytes. When the operation is finished @callback will be
697 * called. You can then call g_input_stream_read_bytes_finish() to get the
698 * result of the operation.
700 * During an async request no other sync and async calls are allowed
701 * on @stream, and will result in %G_IO_ERROR_PENDING errors.
703 * A value of @count larger than %G_MAXSSIZE will cause a
704 * %G_IO_ERROR_INVALID_ARGUMENT error.
706 * On success, the new #GBytes will be passed to the callback. It is
707 * not an error if this is smaller than the requested size, as it can
708 * happen e.g. near the end of a file, but generally we try to read as
709 * many bytes as requested. Zero is returned on end of file (or if
710 * @count is zero), but never otherwise.
712 * Any outstanding I/O request with higher priority (lower numerical
713 * value) will be executed before an outstanding request with lower
714 * priority. Default priority is %G_PRIORITY_DEFAULT.
717 g_input_stream_read_bytes_async (GInputStream *stream,
720 GCancellable *cancellable,
721 GAsyncReadyCallback callback,
727 task = g_task_new (stream, cancellable, callback, user_data);
728 buf = g_malloc (count);
729 g_task_set_task_data (task, buf, NULL);
731 g_input_stream_read_async (stream, buf, count,
732 io_priority, cancellable,
733 read_bytes_callback, task);
737 * g_input_stream_read_bytes_finish:
738 * @stream: a #GInputStream.
739 * @result: a #GAsyncResult.
740 * @error: a #GError location to store the error occurring, or %NULL to
743 * Finishes an asynchronous stream read-into-#GBytes operation.
745 * Returns: the newly-allocated #GBytes, or %NULL on error
748 g_input_stream_read_bytes_finish (GInputStream *stream,
749 GAsyncResult *result,
752 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
753 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
755 return g_task_propagate_pointer (G_TASK (result), error);
759 * g_input_stream_skip_async:
760 * @stream: A #GInputStream.
761 * @count: the number of bytes that will be skipped from the stream
762 * @io_priority: the <link linkend="io-priority">I/O priority</link>
764 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
765 * @callback: (scope async): callback to call when the request is satisfied
766 * @user_data: (closure): the data to pass to callback function
768 * Request an asynchronous skip of @count bytes from the stream.
769 * When the operation is finished @callback will be called.
770 * You can then call g_input_stream_skip_finish() to get the result
773 * During an async request no other sync and async calls are allowed,
774 * and will result in %G_IO_ERROR_PENDING errors.
776 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
778 * On success, the number of bytes skipped will be passed to the callback.
779 * It is not an error if this is not the same as the requested size, as it
780 * can happen e.g. near the end of a file, but generally we try to skip
781 * as many bytes as requested. Zero is returned on end of file
782 * (or if @count is zero), but never otherwise.
784 * Any outstanding i/o request with higher priority (lower numerical value)
785 * will be executed before an outstanding request with lower priority.
786 * Default priority is %G_PRIORITY_DEFAULT.
788 * The asynchronous methods have a default fallback that uses threads to
789 * implement asynchronicity, so they are optional for inheriting classes.
790 * However, if you override one, you must override all.
793 g_input_stream_skip_async (GInputStream *stream,
796 GCancellable *cancellable,
797 GAsyncReadyCallback callback,
800 GInputStreamClass *class;
801 GError *error = NULL;
803 g_return_if_fail (G_IS_INPUT_STREAM (stream));
809 task = g_task_new (stream, cancellable, callback, user_data);
810 g_task_set_source_tag (task, g_input_stream_skip_async);
811 g_task_return_int (task, 0);
812 g_object_unref (task);
816 if (((gssize) count) < 0)
818 g_task_report_new_error (stream, callback, user_data,
819 g_input_stream_skip_async,
820 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
821 _("Too large count value passed to %s"),
826 if (!g_input_stream_set_pending (stream, &error))
828 g_task_report_error (stream, callback, user_data,
829 g_input_stream_skip_async,
834 class = G_INPUT_STREAM_GET_CLASS (stream);
835 stream->priv->outstanding_callback = callback;
836 g_object_ref (stream);
837 class->skip_async (stream, count, io_priority, cancellable,
838 async_ready_callback_wrapper, user_data);
842 * g_input_stream_skip_finish:
843 * @stream: a #GInputStream.
844 * @result: a #GAsyncResult.
845 * @error: a #GError location to store the error occurring, or %NULL to
848 * Finishes a stream skip operation.
850 * Returns: the size of the bytes skipped, or %-1 on error.
853 g_input_stream_skip_finish (GInputStream *stream,
854 GAsyncResult *result,
857 GInputStreamClass *class;
859 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
860 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
862 if (g_async_result_legacy_propagate_error (result, error))
864 else if (g_async_result_is_tagged (result, g_input_stream_skip_async))
865 return g_task_propagate_int (G_TASK (result), error);
867 class = G_INPUT_STREAM_GET_CLASS (stream);
868 return class->skip_finish (stream, result, error);
872 * g_input_stream_close_async:
873 * @stream: A #GInputStream.
874 * @io_priority: the <link linkend="io-priority">I/O priority</link>
876 * @cancellable: (allow-none): optional cancellable object
877 * @callback: (scope async): callback to call when the request is satisfied
878 * @user_data: (closure): the data to pass to callback function
880 * Requests an asynchronous closes of the stream, releasing resources related to it.
881 * When the operation is finished @callback will be called.
882 * You can then call g_input_stream_close_finish() to get the result of the
885 * For behaviour details see g_input_stream_close().
887 * The asyncronous methods have a default fallback that uses threads to implement
888 * asynchronicity, so they are optional for inheriting classes. However, if you
889 * override one you must override all.
892 g_input_stream_close_async (GInputStream *stream,
894 GCancellable *cancellable,
895 GAsyncReadyCallback callback,
898 GInputStreamClass *class;
899 GError *error = NULL;
901 g_return_if_fail (G_IS_INPUT_STREAM (stream));
903 if (stream->priv->closed)
907 task = g_task_new (stream, cancellable, callback, user_data);
908 g_task_set_source_tag (task, g_input_stream_close_async);
909 g_task_return_boolean (task, TRUE);
910 g_object_unref (task);
914 if (!g_input_stream_set_pending (stream, &error))
916 g_task_report_error (stream, callback, user_data,
917 g_input_stream_close_async,
922 class = G_INPUT_STREAM_GET_CLASS (stream);
923 stream->priv->outstanding_callback = callback;
924 g_object_ref (stream);
925 class->close_async (stream, io_priority, cancellable,
926 async_ready_close_callback_wrapper, user_data);
930 * g_input_stream_close_finish:
931 * @stream: a #GInputStream.
932 * @result: a #GAsyncResult.
933 * @error: a #GError location to store the error occurring, or %NULL to
936 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
938 * Returns: %TRUE if the stream was closed successfully.
941 g_input_stream_close_finish (GInputStream *stream,
942 GAsyncResult *result,
945 GInputStreamClass *class;
947 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
948 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
950 if (g_async_result_legacy_propagate_error (result, error))
952 else if (g_async_result_is_tagged (result, g_input_stream_close_async))
953 return g_task_propagate_boolean (G_TASK (result), error);
955 class = G_INPUT_STREAM_GET_CLASS (stream);
956 return class->close_finish (stream, result, error);
960 * g_input_stream_is_closed:
961 * @stream: input stream.
963 * Checks if an input stream is closed.
965 * Returns: %TRUE if the stream is closed.
968 g_input_stream_is_closed (GInputStream *stream)
970 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
972 return stream->priv->closed;
976 * g_input_stream_has_pending:
977 * @stream: input stream.
979 * Checks if an input stream has pending actions.
981 * Returns: %TRUE if @stream has pending actions.
984 g_input_stream_has_pending (GInputStream *stream)
986 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
988 return stream->priv->pending;
992 * g_input_stream_set_pending:
993 * @stream: input stream
994 * @error: a #GError location to store the error occurring, or %NULL to
997 * Sets @stream to have actions pending. If the pending flag is
998 * already set or @stream is closed, it will return %FALSE and set
1001 * Return value: %TRUE if pending was previously unset and is now set.
1004 g_input_stream_set_pending (GInputStream *stream, GError **error)
1006 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
1008 if (stream->priv->closed)
1010 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1011 _("Stream is already closed"));
1015 if (stream->priv->pending)
1017 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1018 /* Translators: This is an error you get if there is already an
1019 * operation running against this stream when you try to start
1021 _("Stream has outstanding operation"));
1025 stream->priv->pending = TRUE;
1030 * g_input_stream_clear_pending:
1031 * @stream: input stream
1033 * Clears the pending flag on @stream.
1036 g_input_stream_clear_pending (GInputStream *stream)
1038 g_return_if_fail (G_IS_INPUT_STREAM (stream));
1040 stream->priv->pending = FALSE;
1044 * g_input_stream_async_read_is_via_threads:
1045 * @stream: input stream
1047 * Checks if an input stream's read_async function uses threads.
1049 * Returns: %TRUE if @stream's read_async function uses threads.
1052 g_input_stream_async_read_is_via_threads (GInputStream *stream)
1054 GInputStreamClass *class;
1056 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
1058 class = G_INPUT_STREAM_GET_CLASS (stream);
1060 return (class->read_async == g_input_stream_real_read_async &&
1061 !(G_IS_POLLABLE_INPUT_STREAM (stream) &&
1062 g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (stream))));
1065 /********************************************
1066 * Default implementation of async ops *
1067 ********************************************/
1075 free_read_data (ReadData *op)
1077 g_slice_free (ReadData, op);
1081 read_async_thread (GTask *task,
1082 gpointer source_object,
1084 GCancellable *cancellable)
1086 GInputStream *stream = source_object;
1087 ReadData *op = task_data;
1088 GInputStreamClass *class;
1089 GError *error = NULL;
1092 class = G_INPUT_STREAM_GET_CLASS (stream);
1094 nread = class->read_fn (stream,
1095 op->buffer, op->count,
1096 g_task_get_cancellable (task),
1099 g_task_return_error (task, error);
1101 g_task_return_int (task, nread);
1104 static void read_async_pollable (GPollableInputStream *stream,
1108 read_async_pollable_ready (GPollableInputStream *stream,
1111 GTask *task = user_data;
1113 read_async_pollable (stream, task);
1118 read_async_pollable (GPollableInputStream *stream,
1121 ReadData *op = g_task_get_task_data (task);
1122 GError *error = NULL;
1125 if (g_task_return_error_if_cancelled (task))
1128 nread = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
1129 read_nonblocking (stream, op->buffer, op->count, &error);
1131 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1135 g_error_free (error);
1137 source = g_pollable_input_stream_create_source (stream,
1138 g_task_get_cancellable (task));
1139 g_task_attach_source (task, source,
1140 (GSourceFunc) read_async_pollable_ready);
1141 g_source_unref (source);
1146 g_task_return_error (task, error);
1148 g_task_return_int (task, nread);
1149 /* g_input_stream_real_read_async() unrefs task */
1154 g_input_stream_real_read_async (GInputStream *stream,
1158 GCancellable *cancellable,
1159 GAsyncReadyCallback callback,
1165 op = g_slice_new0 (ReadData);
1166 task = g_task_new (stream, cancellable, callback, user_data);
1167 g_task_set_task_data (task, op, (GDestroyNotify) free_read_data);
1168 g_task_set_priority (task, io_priority);
1169 op->buffer = buffer;
1172 if (!g_input_stream_async_read_is_via_threads (stream))
1173 read_async_pollable (G_POLLABLE_INPUT_STREAM (stream), task);
1175 g_task_run_in_thread (task, read_async_thread);
1176 g_object_unref (task);
1180 g_input_stream_real_read_finish (GInputStream *stream,
1181 GAsyncResult *result,
1184 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1186 return g_task_propagate_int (G_TASK (result), error);
1191 skip_async_thread (GTask *task,
1192 gpointer source_object,
1194 GCancellable *cancellable)
1196 GInputStream *stream = source_object;
1197 gsize count = GPOINTER_TO_SIZE (task_data);
1198 GInputStreamClass *class;
1199 GError *error = NULL;
1202 class = G_INPUT_STREAM_GET_CLASS (stream);
1203 ret = class->skip (stream, count,
1204 g_task_get_cancellable (task),
1207 g_task_return_error (task, error);
1209 g_task_return_int (task, ret);
1215 gsize count_skipped;
1216 } SkipFallbackAsyncData;
1219 skip_callback_wrapper (GObject *source_object,
1223 GInputStreamClass *class;
1224 GTask *task = user_data;
1225 SkipFallbackAsyncData *data = g_task_get_task_data (task);
1226 GError *error = NULL;
1229 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1234 data->count_skipped += ret;
1236 if (data->count > 0)
1238 class = G_INPUT_STREAM_GET_CLASS (source_object);
1239 class->read_async (G_INPUT_STREAM (source_object),
1240 data->buffer, MIN (8192, data->count),
1241 g_task_get_priority (task),
1242 g_task_get_cancellable (task),
1243 skip_callback_wrapper, task);
1249 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
1250 data->count_skipped)
1252 /* No error, return partial read */
1253 g_clear_error (&error);
1257 g_task_return_error (task, error);
1259 g_task_return_int (task, data->count_skipped);
1260 g_object_unref (task);
1264 g_input_stream_real_skip_async (GInputStream *stream,
1267 GCancellable *cancellable,
1268 GAsyncReadyCallback callback,
1271 GInputStreamClass *class;
1272 SkipFallbackAsyncData *data;
1275 class = G_INPUT_STREAM_GET_CLASS (stream);
1277 task = g_task_new (stream, cancellable, callback, user_data);
1278 g_task_set_priority (task, io_priority);
1280 if (g_input_stream_async_read_is_via_threads (stream))
1282 /* Read is thread-using async fallback.
1283 * Make skip use threads too, so that we can use a possible sync skip
1284 * implementation. */
1285 g_task_set_task_data (task, GSIZE_TO_POINTER (count), NULL);
1287 g_task_run_in_thread (task, skip_async_thread);
1288 g_object_unref (task);
1292 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1294 /* There is a custom async read function, lets use that. */
1295 data = g_new (SkipFallbackAsyncData, 1);
1296 data->count = count;
1297 data->count_skipped = 0;
1298 g_task_set_task_data (task, data, g_free);
1299 g_task_set_check_cancellable (task, FALSE);
1300 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1301 skip_callback_wrapper, task);
1307 g_input_stream_real_skip_finish (GInputStream *stream,
1308 GAsyncResult *result,
1311 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1313 return g_task_propagate_int (G_TASK (result), error);
1317 close_async_thread (GTask *task,
1318 gpointer source_object,
1320 GCancellable *cancellable)
1322 GInputStream *stream = source_object;
1323 GInputStreamClass *class;
1324 GError *error = NULL;
1327 class = G_INPUT_STREAM_GET_CLASS (stream);
1328 if (class->close_fn)
1330 result = class->close_fn (stream,
1331 g_task_get_cancellable (task),
1335 g_task_return_error (task, error);
1340 g_task_return_boolean (task, TRUE);
1344 g_input_stream_real_close_async (GInputStream *stream,
1346 GCancellable *cancellable,
1347 GAsyncReadyCallback callback,
1352 task = g_task_new (stream, cancellable, callback, user_data);
1353 g_task_set_check_cancellable (task, FALSE);
1354 g_task_set_priority (task, io_priority);
1356 g_task_run_in_thread (task, close_async_thread);
1357 g_object_unref (task);
1361 g_input_stream_real_close_finish (GInputStream *stream,
1362 GAsyncResult *result,
1365 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1367 return g_task_propagate_boolean (G_TASK (result), error);