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: (array length=count) (element-type guint8): a buffer to
140 * read data into (which should be at least count bytes long).
141 * @count: the number of bytes that will be read from the stream
142 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
143 * @error: location to store the error occurring, or %NULL to ignore
145 * Tries to read @count bytes from the stream into the buffer starting at
146 * @buffer. Will block during this read.
148 * If count is zero returns zero and does nothing. A value of @count
149 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
151 * On success, the number of bytes read into the buffer is returned.
152 * It is not an error if this is not the same as the requested size, as it
153 * can happen e.g. near the end of a file. Zero is returned on end of file
154 * (or if @count is zero), but never otherwise.
156 * If @cancellable is not %NULL, then the operation can be cancelled by
157 * triggering the cancellable object from another thread. If the operation
158 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
159 * operation was partially finished when the operation was cancelled the
160 * partial result will be returned, without an error.
162 * On error -1 is returned and @error is set accordingly.
164 * Return value: Number of bytes read, or -1 on error, or 0 on end of file.
167 g_input_stream_read (GInputStream *stream,
170 GCancellable *cancellable,
173 GInputStreamClass *class;
176 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
177 g_return_val_if_fail (buffer != NULL, 0);
182 if (((gssize) count) < 0)
184 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
185 _("Too large count value passed to %s"), G_STRFUNC);
189 class = G_INPUT_STREAM_GET_CLASS (stream);
191 if (class->read_fn == NULL)
193 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
194 _("Input stream doesn't implement read"));
198 if (!g_input_stream_set_pending (stream, error))
202 g_cancellable_push_current (cancellable);
204 res = class->read_fn (stream, buffer, count, cancellable, error);
207 g_cancellable_pop_current (cancellable);
209 g_input_stream_clear_pending (stream);
215 * g_input_stream_read_all:
216 * @stream: a #GInputStream.
217 * @buffer: (array length=count) (element-type guint8): a buffer to
218 * read data into (which should be at least count bytes long).
219 * @count: the number of bytes that will be read from the stream
220 * @bytes_read: (out): location to store the number of bytes that was read from the stream
221 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
222 * @error: location to store the error occurring, or %NULL to ignore
224 * Tries to read @count bytes from the stream into the buffer starting at
225 * @buffer. Will block during this read.
227 * This function is similar to g_input_stream_read(), except it tries to
228 * read as many bytes as requested, only stopping on an error or end of stream.
230 * On a successful read of @count bytes, or if we reached the end of the
231 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
234 * If there is an error during the operation %FALSE is returned and @error
235 * is set to indicate the error status, @bytes_read is updated to contain
236 * the number of bytes read into @buffer before the error occurred.
238 * Return value: %TRUE on success, %FALSE if there was an error
241 g_input_stream_read_all (GInputStream *stream,
245 GCancellable *cancellable,
251 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
252 g_return_val_if_fail (buffer != NULL, FALSE);
255 while (_bytes_read < count)
257 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
262 *bytes_read = _bytes_read;
273 *bytes_read = _bytes_read;
278 * g_input_stream_read_bytes:
279 * @stream: a #GInputStream.
280 * @count: maximum number of bytes that will be read from the stream. Common
281 * values include 4096 and 8192.
282 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
283 * @error: location to store the error occurring, or %NULL to ignore
285 * Like g_input_stream_read(), this tries to read @count bytes from
286 * the stream in a blocking fashion. However, rather than reading into
287 * a user-supplied buffer, this will create a new #GBytes containing
288 * the data that was read. This may be easier to use from language
291 * If count is zero, returns a zero-length #GBytes and does nothing. A
292 * value of @count larger than %G_MAXSSIZE will cause a
293 * %G_IO_ERROR_INVALID_ARGUMENT error.
295 * On success, a new #GBytes is returned. It is not an error if the
296 * size of this object is not the same as the requested size, as it
297 * can happen e.g. near the end of a file. A zero-length #GBytes is
298 * returned on end of file (or if @count is zero), but never
301 * If @cancellable is not %NULL, then the operation can be cancelled by
302 * triggering the cancellable object from another thread. If the operation
303 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
304 * operation was partially finished when the operation was cancelled the
305 * partial result will be returned, without an error.
307 * On error %NULL is returned and @error is set accordingly.
309 * Return value: a new #GBytes, or %NULL on error
312 g_input_stream_read_bytes (GInputStream *stream,
314 GCancellable *cancellable,
320 buf = g_malloc (count);
321 nread = g_input_stream_read (stream, buf, count, cancellable, error);
330 return g_bytes_new_static ("", 0);
333 return g_bytes_new_take (buf, nread);
337 * g_input_stream_skip:
338 * @stream: a #GInputStream.
339 * @count: the number of bytes that will be skipped from the stream
340 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
341 * @error: location to store the error occurring, or %NULL to ignore
343 * Tries to skip @count bytes from the stream. Will block during the operation.
345 * This is identical to g_input_stream_read(), from a behaviour standpoint,
346 * but the bytes that are skipped are not returned to the user. Some
347 * streams have an implementation that is more efficient than reading the data.
349 * This function is optional for inherited classes, as the default implementation
350 * emulates it using read.
352 * If @cancellable is not %NULL, then the operation can be cancelled by
353 * triggering the cancellable object from another thread. If the operation
354 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
355 * operation was partially finished when the operation was cancelled the
356 * partial result will be returned, without an error.
358 * Return value: Number of bytes skipped, or -1 on error
361 g_input_stream_skip (GInputStream *stream,
363 GCancellable *cancellable,
366 GInputStreamClass *class;
369 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
374 if (((gssize) count) < 0)
376 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
377 _("Too large count value passed to %s"), G_STRFUNC);
381 class = G_INPUT_STREAM_GET_CLASS (stream);
383 if (!g_input_stream_set_pending (stream, error))
387 g_cancellable_push_current (cancellable);
389 res = class->skip (stream, count, cancellable, error);
392 g_cancellable_pop_current (cancellable);
394 g_input_stream_clear_pending (stream);
400 g_input_stream_real_skip (GInputStream *stream,
402 GCancellable *cancellable,
405 GInputStreamClass *class;
406 gssize ret, read_bytes;
410 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
412 if (g_seekable_seek (G_SEEKABLE (stream),
420 /* If not seekable, or seek failed, fall back to reading data: */
422 class = G_INPUT_STREAM_GET_CLASS (stream);
429 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
430 cancellable, &my_error);
433 if (read_bytes > 0 &&
434 my_error->domain == G_IO_ERROR &&
435 my_error->code == G_IO_ERROR_CANCELLED)
437 g_error_free (my_error);
441 g_propagate_error (error, my_error);
448 if (ret == 0 || count == 0)
454 * g_input_stream_close:
455 * @stream: A #GInputStream.
456 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
457 * @error: location to store the error occurring, or %NULL to ignore
459 * Closes the stream, releasing resources related to it.
461 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
462 * Closing a stream multiple times will not return an error.
464 * Streams will be automatically closed when the last reference
465 * is dropped, but you might want to call this function to make sure
466 * resources are released as early as possible.
468 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
469 * open after the stream is closed. See the documentation for the individual
470 * stream for details.
472 * On failure the first error that happened will be reported, but the close
473 * operation will finish as much as possible. A stream that failed to
474 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
475 * is important to check and report the error to the user.
477 * If @cancellable is not %NULL, then the operation can be cancelled by
478 * triggering the cancellable object from another thread. If the operation
479 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
480 * Cancelling a close will still leave the stream closed, but some streams
481 * can use a faster close that doesn't block to e.g. check errors.
483 * Return value: %TRUE on success, %FALSE on failure
486 g_input_stream_close (GInputStream *stream,
487 GCancellable *cancellable,
490 GInputStreamClass *class;
493 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
495 class = G_INPUT_STREAM_GET_CLASS (stream);
497 if (stream->priv->closed)
502 if (!g_input_stream_set_pending (stream, error))
506 g_cancellable_push_current (cancellable);
509 res = class->close_fn (stream, cancellable, error);
512 g_cancellable_pop_current (cancellable);
514 g_input_stream_clear_pending (stream);
516 stream->priv->closed = TRUE;
522 async_ready_callback_wrapper (GObject *source_object,
526 GInputStream *stream = G_INPUT_STREAM (source_object);
528 g_input_stream_clear_pending (stream);
529 if (stream->priv->outstanding_callback)
530 (*stream->priv->outstanding_callback) (source_object, res, user_data);
531 g_object_unref (stream);
535 async_ready_close_callback_wrapper (GObject *source_object,
539 GInputStream *stream = G_INPUT_STREAM (source_object);
541 g_input_stream_clear_pending (stream);
542 stream->priv->closed = TRUE;
543 if (stream->priv->outstanding_callback)
544 (*stream->priv->outstanding_callback) (source_object, res, user_data);
545 g_object_unref (stream);
549 * g_input_stream_read_async:
550 * @stream: A #GInputStream.
551 * @buffer: (array length=count) (element-type guint8): a buffer to
552 * read data into (which should be at least count bytes long).
553 * @count: the number of bytes that will be read from the stream
554 * @io_priority: the <link linkend="io-priority">I/O priority</link>
556 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
557 * @callback: (scope async): callback to call when the request is satisfied
558 * @user_data: (closure): the data to pass to callback function
560 * Request an asynchronous read of @count bytes from the stream into the buffer
561 * starting at @buffer. When the operation is finished @callback will be called.
562 * You can then call g_input_stream_read_finish() to get the result of the
565 * During an async request no other sync and async calls are allowed on @stream, and will
566 * result in %G_IO_ERROR_PENDING errors.
568 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
570 * On success, the number of bytes read into the buffer will be passed to the
571 * callback. It is not an error if this is not the same as the requested size, as it
572 * can happen e.g. near the end of a file, but generally we try to read
573 * as many bytes as requested. Zero is returned on end of file
574 * (or if @count is zero), but never otherwise.
576 * Any outstanding i/o request with higher priority (lower numerical value) will
577 * be executed before an outstanding request with lower priority. Default
578 * priority is %G_PRIORITY_DEFAULT.
580 * The asyncronous methods have a default fallback that uses threads to implement
581 * asynchronicity, so they are optional for inheriting classes. However, if you
582 * override one you must override all.
585 g_input_stream_read_async (GInputStream *stream,
589 GCancellable *cancellable,
590 GAsyncReadyCallback callback,
593 GInputStreamClass *class;
594 GError *error = NULL;
596 g_return_if_fail (G_IS_INPUT_STREAM (stream));
597 g_return_if_fail (buffer != NULL);
603 task = g_task_new (stream, cancellable, callback, user_data);
604 g_task_set_source_tag (task, g_input_stream_read_async);
605 g_task_return_int (task, 0);
606 g_object_unref (task);
610 if (((gssize) count) < 0)
612 g_task_report_new_error (stream, callback, user_data,
613 g_input_stream_read_async,
614 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
615 _("Too large count value passed to %s"),
620 if (!g_input_stream_set_pending (stream, &error))
622 g_task_report_error (stream, callback, user_data,
623 g_input_stream_read_async,
628 class = G_INPUT_STREAM_GET_CLASS (stream);
629 stream->priv->outstanding_callback = callback;
630 g_object_ref (stream);
631 class->read_async (stream, buffer, count, io_priority, cancellable,
632 async_ready_callback_wrapper, user_data);
636 * g_input_stream_read_finish:
637 * @stream: a #GInputStream.
638 * @result: a #GAsyncResult.
639 * @error: a #GError location to store the error occurring, or %NULL to
642 * Finishes an asynchronous stream read operation.
644 * Returns: number of bytes read in, or -1 on error, or 0 on end of file.
647 g_input_stream_read_finish (GInputStream *stream,
648 GAsyncResult *result,
651 GInputStreamClass *class;
653 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
654 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
656 if (g_async_result_legacy_propagate_error (result, error))
658 else if (g_async_result_is_tagged (result, g_input_stream_read_async))
659 return g_task_propagate_int (G_TASK (result), error);
661 class = G_INPUT_STREAM_GET_CLASS (stream);
662 return class->read_finish (stream, result, error);
666 read_bytes_callback (GObject *stream,
667 GAsyncResult *result,
670 GTask *task = user_data;
671 guchar *buf = g_task_get_task_data (task);
672 GError *error = NULL;
674 GBytes *bytes = NULL;
676 nread = g_input_stream_read_finish (G_INPUT_STREAM (stream),
681 g_task_return_error (task, error);
686 bytes = g_bytes_new_static ("", 0);
689 bytes = g_bytes_new_take (buf, nread);
692 g_task_return_pointer (task, bytes, (GDestroyNotify)g_bytes_unref);
694 g_object_unref (task);
698 * g_input_stream_read_bytes_async:
699 * @stream: A #GInputStream.
700 * @count: the number of bytes that will be read from the stream
701 * @io_priority: the <link linkend="io-priority">I/O priority</link>
703 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
704 * @callback: (scope async): callback to call when the request is satisfied
705 * @user_data: (closure): the data to pass to callback function
707 * Request an asynchronous read of @count bytes from the stream into a
708 * new #GBytes. When the operation is finished @callback will be
709 * called. You can then call g_input_stream_read_bytes_finish() to get the
710 * result of the operation.
712 * During an async request no other sync and async calls are allowed
713 * on @stream, and will result in %G_IO_ERROR_PENDING errors.
715 * A value of @count larger than %G_MAXSSIZE will cause a
716 * %G_IO_ERROR_INVALID_ARGUMENT error.
718 * On success, the new #GBytes will be passed to the callback. It is
719 * not an error if this is smaller than the requested size, as it can
720 * happen e.g. near the end of a file, but generally we try to read as
721 * many bytes as requested. Zero is returned on end of file (or if
722 * @count is zero), but never otherwise.
724 * Any outstanding I/O request with higher priority (lower numerical
725 * value) will be executed before an outstanding request with lower
726 * priority. Default priority is %G_PRIORITY_DEFAULT.
729 g_input_stream_read_bytes_async (GInputStream *stream,
732 GCancellable *cancellable,
733 GAsyncReadyCallback callback,
739 task = g_task_new (stream, cancellable, callback, user_data);
740 buf = g_malloc (count);
741 g_task_set_task_data (task, buf, NULL);
743 g_input_stream_read_async (stream, buf, count,
744 io_priority, cancellable,
745 read_bytes_callback, task);
749 * g_input_stream_read_bytes_finish:
750 * @stream: a #GInputStream.
751 * @result: a #GAsyncResult.
752 * @error: a #GError location to store the error occurring, or %NULL to
755 * Finishes an asynchronous stream read-into-#GBytes operation.
757 * Returns: the newly-allocated #GBytes, or %NULL on error
760 g_input_stream_read_bytes_finish (GInputStream *stream,
761 GAsyncResult *result,
764 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
765 g_return_val_if_fail (g_task_is_valid (result, stream), NULL);
767 return g_task_propagate_pointer (G_TASK (result), error);
771 * g_input_stream_skip_async:
772 * @stream: A #GInputStream.
773 * @count: the number of bytes that will be skipped from the stream
774 * @io_priority: the <link linkend="io-priority">I/O priority</link>
776 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
777 * @callback: (scope async): callback to call when the request is satisfied
778 * @user_data: (closure): the data to pass to callback function
780 * Request an asynchronous skip of @count bytes from the stream.
781 * When the operation is finished @callback will be called.
782 * You can then call g_input_stream_skip_finish() to get the result
785 * During an async request no other sync and async calls are allowed,
786 * and will result in %G_IO_ERROR_PENDING errors.
788 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
790 * On success, the number of bytes skipped will be passed to the callback.
791 * It is not an error if this is not the same as the requested size, as it
792 * can happen e.g. near the end of a file, but generally we try to skip
793 * as many bytes as requested. Zero is returned on end of file
794 * (or if @count is zero), but never otherwise.
796 * Any outstanding i/o request with higher priority (lower numerical value)
797 * will be executed before an outstanding request with lower priority.
798 * Default priority is %G_PRIORITY_DEFAULT.
800 * The asynchronous methods have a default fallback that uses threads to
801 * implement asynchronicity, so they are optional for inheriting classes.
802 * However, if you override one, you must override all.
805 g_input_stream_skip_async (GInputStream *stream,
808 GCancellable *cancellable,
809 GAsyncReadyCallback callback,
812 GInputStreamClass *class;
813 GError *error = NULL;
815 g_return_if_fail (G_IS_INPUT_STREAM (stream));
821 task = g_task_new (stream, cancellable, callback, user_data);
822 g_task_set_source_tag (task, g_input_stream_skip_async);
823 g_task_return_int (task, 0);
824 g_object_unref (task);
828 if (((gssize) count) < 0)
830 g_task_report_new_error (stream, callback, user_data,
831 g_input_stream_skip_async,
832 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
833 _("Too large count value passed to %s"),
838 if (!g_input_stream_set_pending (stream, &error))
840 g_task_report_error (stream, callback, user_data,
841 g_input_stream_skip_async,
846 class = G_INPUT_STREAM_GET_CLASS (stream);
847 stream->priv->outstanding_callback = callback;
848 g_object_ref (stream);
849 class->skip_async (stream, count, io_priority, cancellable,
850 async_ready_callback_wrapper, user_data);
854 * g_input_stream_skip_finish:
855 * @stream: a #GInputStream.
856 * @result: a #GAsyncResult.
857 * @error: a #GError location to store the error occurring, or %NULL to
860 * Finishes a stream skip operation.
862 * Returns: the size of the bytes skipped, or %-1 on error.
865 g_input_stream_skip_finish (GInputStream *stream,
866 GAsyncResult *result,
869 GInputStreamClass *class;
871 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
872 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
874 if (g_async_result_legacy_propagate_error (result, error))
876 else if (g_async_result_is_tagged (result, g_input_stream_skip_async))
877 return g_task_propagate_int (G_TASK (result), error);
879 class = G_INPUT_STREAM_GET_CLASS (stream);
880 return class->skip_finish (stream, result, error);
884 * g_input_stream_close_async:
885 * @stream: A #GInputStream.
886 * @io_priority: the <link linkend="io-priority">I/O priority</link>
888 * @cancellable: (allow-none): optional cancellable object
889 * @callback: (scope async): callback to call when the request is satisfied
890 * @user_data: (closure): the data to pass to callback function
892 * Requests an asynchronous closes of the stream, releasing resources related to it.
893 * When the operation is finished @callback will be called.
894 * You can then call g_input_stream_close_finish() to get the result of the
897 * For behaviour details see g_input_stream_close().
899 * The asyncronous methods have a default fallback that uses threads to implement
900 * asynchronicity, so they are optional for inheriting classes. However, if you
901 * override one you must override all.
904 g_input_stream_close_async (GInputStream *stream,
906 GCancellable *cancellable,
907 GAsyncReadyCallback callback,
910 GInputStreamClass *class;
911 GError *error = NULL;
913 g_return_if_fail (G_IS_INPUT_STREAM (stream));
915 if (stream->priv->closed)
919 task = g_task_new (stream, cancellable, callback, user_data);
920 g_task_set_source_tag (task, g_input_stream_close_async);
921 g_task_return_boolean (task, TRUE);
922 g_object_unref (task);
926 if (!g_input_stream_set_pending (stream, &error))
928 g_task_report_error (stream, callback, user_data,
929 g_input_stream_close_async,
934 class = G_INPUT_STREAM_GET_CLASS (stream);
935 stream->priv->outstanding_callback = callback;
936 g_object_ref (stream);
937 class->close_async (stream, io_priority, cancellable,
938 async_ready_close_callback_wrapper, user_data);
942 * g_input_stream_close_finish:
943 * @stream: a #GInputStream.
944 * @result: a #GAsyncResult.
945 * @error: a #GError location to store the error occurring, or %NULL to
948 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
950 * Returns: %TRUE if the stream was closed successfully.
953 g_input_stream_close_finish (GInputStream *stream,
954 GAsyncResult *result,
957 GInputStreamClass *class;
959 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
960 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
962 if (g_async_result_legacy_propagate_error (result, error))
964 else if (g_async_result_is_tagged (result, g_input_stream_close_async))
965 return g_task_propagate_boolean (G_TASK (result), error);
967 class = G_INPUT_STREAM_GET_CLASS (stream);
968 return class->close_finish (stream, result, error);
972 * g_input_stream_is_closed:
973 * @stream: input stream.
975 * Checks if an input stream is closed.
977 * Returns: %TRUE if the stream is closed.
980 g_input_stream_is_closed (GInputStream *stream)
982 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
984 return stream->priv->closed;
988 * g_input_stream_has_pending:
989 * @stream: input stream.
991 * Checks if an input stream has pending actions.
993 * Returns: %TRUE if @stream has pending actions.
996 g_input_stream_has_pending (GInputStream *stream)
998 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
1000 return stream->priv->pending;
1004 * g_input_stream_set_pending:
1005 * @stream: input stream
1006 * @error: a #GError location to store the error occurring, or %NULL to
1009 * Sets @stream to have actions pending. If the pending flag is
1010 * already set or @stream is closed, it will return %FALSE and set
1013 * Return value: %TRUE if pending was previously unset and is now set.
1016 g_input_stream_set_pending (GInputStream *stream, GError **error)
1018 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
1020 if (stream->priv->closed)
1022 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1023 _("Stream is already closed"));
1027 if (stream->priv->pending)
1029 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1030 /* Translators: This is an error you get if there is already an
1031 * operation running against this stream when you try to start
1033 _("Stream has outstanding operation"));
1037 stream->priv->pending = TRUE;
1042 * g_input_stream_clear_pending:
1043 * @stream: input stream
1045 * Clears the pending flag on @stream.
1048 g_input_stream_clear_pending (GInputStream *stream)
1050 g_return_if_fail (G_IS_INPUT_STREAM (stream));
1052 stream->priv->pending = FALSE;
1055 /********************************************
1056 * Default implementation of async ops *
1057 ********************************************/
1065 free_read_data (ReadData *op)
1067 g_slice_free (ReadData, op);
1071 read_async_thread (GTask *task,
1072 gpointer source_object,
1074 GCancellable *cancellable)
1076 GInputStream *stream = source_object;
1077 ReadData *op = task_data;
1078 GInputStreamClass *class;
1079 GError *error = NULL;
1082 class = G_INPUT_STREAM_GET_CLASS (stream);
1084 nread = class->read_fn (stream,
1085 op->buffer, op->count,
1086 g_task_get_cancellable (task),
1089 g_task_return_error (task, error);
1091 g_task_return_int (task, nread);
1094 static void read_async_pollable (GPollableInputStream *stream,
1098 read_async_pollable_ready (GPollableInputStream *stream,
1101 GTask *task = user_data;
1103 read_async_pollable (stream, task);
1108 read_async_pollable (GPollableInputStream *stream,
1111 ReadData *op = g_task_get_task_data (task);
1112 GError *error = NULL;
1115 if (g_task_return_error_if_cancelled (task))
1118 nread = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
1119 read_nonblocking (stream, op->buffer, op->count, &error);
1121 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1125 g_error_free (error);
1127 source = g_pollable_input_stream_create_source (stream,
1128 g_task_get_cancellable (task));
1129 g_task_attach_source (task, source,
1130 (GSourceFunc) read_async_pollable_ready);
1131 g_source_unref (source);
1136 g_task_return_error (task, error);
1138 g_task_return_int (task, nread);
1139 /* g_input_stream_real_read_async() unrefs task */
1142 #define CAN_DO_NONBLOCKING_READS(stream) \
1143 (G_IS_POLLABLE_INPUT_STREAM (stream) && \
1144 g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (stream)))
1148 g_input_stream_real_read_async (GInputStream *stream,
1152 GCancellable *cancellable,
1153 GAsyncReadyCallback callback,
1159 op = g_slice_new0 (ReadData);
1160 task = g_task_new (stream, cancellable, callback, user_data);
1161 g_task_set_task_data (task, op, (GDestroyNotify) free_read_data);
1162 g_task_set_priority (task, io_priority);
1163 op->buffer = buffer;
1166 if (CAN_DO_NONBLOCKING_READS (stream))
1167 read_async_pollable (G_POLLABLE_INPUT_STREAM (stream), task);
1169 g_task_run_in_thread (task, read_async_thread);
1170 g_object_unref (task);
1174 g_input_stream_real_read_finish (GInputStream *stream,
1175 GAsyncResult *result,
1178 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1180 return g_task_propagate_int (G_TASK (result), error);
1185 skip_async_thread (GTask *task,
1186 gpointer source_object,
1188 GCancellable *cancellable)
1190 GInputStream *stream = source_object;
1191 gsize count = GPOINTER_TO_SIZE (task_data);
1192 GInputStreamClass *class;
1193 GError *error = NULL;
1196 class = G_INPUT_STREAM_GET_CLASS (stream);
1197 ret = class->skip (stream, count,
1198 g_task_get_cancellable (task),
1201 g_task_return_error (task, error);
1203 g_task_return_int (task, ret);
1209 gsize count_skipped;
1210 } SkipFallbackAsyncData;
1213 skip_callback_wrapper (GObject *source_object,
1217 GInputStreamClass *class;
1218 GTask *task = user_data;
1219 SkipFallbackAsyncData *data = g_task_get_task_data (task);
1220 GError *error = NULL;
1223 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1228 data->count_skipped += ret;
1230 if (data->count > 0)
1232 class = G_INPUT_STREAM_GET_CLASS (source_object);
1233 class->read_async (G_INPUT_STREAM (source_object),
1234 data->buffer, MIN (8192, data->count),
1235 g_task_get_priority (task),
1236 g_task_get_cancellable (task),
1237 skip_callback_wrapper, data);
1243 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
1244 data->count_skipped)
1246 /* No error, return partial read */
1247 g_clear_error (&error);
1251 g_task_return_error (task, error);
1253 g_task_return_int (task, data->count_skipped);
1254 g_object_unref (task);
1258 g_input_stream_real_skip_async (GInputStream *stream,
1261 GCancellable *cancellable,
1262 GAsyncReadyCallback callback,
1265 GInputStreamClass *class;
1266 SkipFallbackAsyncData *data;
1269 class = G_INPUT_STREAM_GET_CLASS (stream);
1271 task = g_task_new (stream, cancellable, callback, user_data);
1272 g_task_set_priority (task, io_priority);
1274 if (class->read_async == g_input_stream_real_read_async &&
1275 !CAN_DO_NONBLOCKING_READS (stream))
1277 /* Read is thread-using async fallback.
1278 * Make skip use threads too, so that we can use a possible sync skip
1279 * implementation. */
1280 g_task_set_task_data (task, GSIZE_TO_POINTER (count), NULL);
1282 g_task_run_in_thread (task, skip_async_thread);
1283 g_object_unref (task);
1287 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1289 /* There is a custom async read function, lets use that. */
1290 data = g_new (SkipFallbackAsyncData, 1);
1291 data->count = count;
1292 data->count_skipped = 0;
1293 g_task_set_task_data (task, data, g_free);
1294 g_task_set_check_cancellable (task, FALSE);
1295 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1296 skip_callback_wrapper, data);
1302 g_input_stream_real_skip_finish (GInputStream *stream,
1303 GAsyncResult *result,
1306 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1308 return g_task_propagate_int (G_TASK (result), error);
1312 close_async_thread (GTask *task,
1313 gpointer source_object,
1315 GCancellable *cancellable)
1317 GInputStream *stream = source_object;
1318 GInputStreamClass *class;
1319 GError *error = NULL;
1322 class = G_INPUT_STREAM_GET_CLASS (stream);
1323 if (class->close_fn)
1325 result = class->close_fn (stream,
1326 g_task_get_cancellable (task),
1330 g_task_return_error (task, error);
1335 g_task_return_boolean (task, TRUE);
1339 g_input_stream_real_close_async (GInputStream *stream,
1341 GCancellable *cancellable,
1342 GAsyncReadyCallback callback,
1347 task = g_task_new (stream, cancellable, callback, user_data);
1348 g_task_set_check_cancellable (task, FALSE);
1349 g_task_set_priority (task, io_priority);
1351 g_task_run_in_thread (task, close_async_thread);
1352 g_object_unref (task);
1356 g_input_stream_real_close_finish (GInputStream *stream,
1357 GAsyncResult *result,
1360 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
1362 return g_task_propagate_boolean (G_TASK (result), error);