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"
31 #include "gsimpleasyncresult.h"
33 #include "gpollableinputstream.h"
36 * SECTION:ginputstream
37 * @short_description: Base class for implementing streaming input
40 * #GInputStream has functions to read from a stream (g_input_stream_read()),
41 * to close a stream (g_input_stream_close()) and to skip some content
42 * (g_input_stream_skip()).
44 * To copy the content of an input stream to an output stream without
45 * manually handling the reads and writes, use g_output_stream_splice().
47 * All of these functions have async variants too.
50 G_DEFINE_ABSTRACT_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
52 struct _GInputStreamPrivate {
55 GAsyncReadyCallback outstanding_callback;
58 static gssize g_input_stream_real_skip (GInputStream *stream,
60 GCancellable *cancellable,
62 static void g_input_stream_real_read_async (GInputStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_input_stream_real_read_finish (GInputStream *stream,
72 static void g_input_stream_real_skip_async (GInputStream *stream,
75 GCancellable *cancellable,
76 GAsyncReadyCallback callback,
78 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
81 static void g_input_stream_real_close_async (GInputStream *stream,
83 GCancellable *cancellable,
84 GAsyncReadyCallback callback,
86 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
91 g_input_stream_finalize (GObject *object)
93 G_OBJECT_CLASS (g_input_stream_parent_class)->finalize (object);
97 g_input_stream_dispose (GObject *object)
101 stream = G_INPUT_STREAM (object);
103 if (!stream->priv->closed)
104 g_input_stream_close (stream, NULL, NULL);
106 G_OBJECT_CLASS (g_input_stream_parent_class)->dispose (object);
111 g_input_stream_class_init (GInputStreamClass *klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
117 gobject_class->finalize = g_input_stream_finalize;
118 gobject_class->dispose = g_input_stream_dispose;
120 klass->skip = g_input_stream_real_skip;
121 klass->read_async = g_input_stream_real_read_async;
122 klass->read_finish = g_input_stream_real_read_finish;
123 klass->skip_async = g_input_stream_real_skip_async;
124 klass->skip_finish = g_input_stream_real_skip_finish;
125 klass->close_async = g_input_stream_real_close_async;
126 klass->close_finish = g_input_stream_real_close_finish;
130 g_input_stream_init (GInputStream *stream)
132 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
134 GInputStreamPrivate);
138 * g_input_stream_read:
139 * @stream: a #GInputStream.
140 * @buffer: a buffer to 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: a buffer to read data into (which should be at least count bytes long).
218 * @count: the number of bytes that will be read from the stream
219 * @bytes_read: (out): location to store the number of bytes that was read from the stream
220 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
221 * @error: location to store the error occurring, or %NULL to ignore
223 * Tries to read @count bytes from the stream into the buffer starting at
224 * @buffer. Will block during this read.
226 * This function is similar to g_input_stream_read(), except it tries to
227 * read as many bytes as requested, only stopping on an error or end of stream.
229 * On a successful read of @count bytes, or if we reached the end of the
230 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
233 * If there is an error during the operation %FALSE is returned and @error
234 * is set to indicate the error status, @bytes_read is updated to contain
235 * the number of bytes read into @buffer before the error occurred.
237 * Return value: %TRUE on success, %FALSE if there was an error
240 g_input_stream_read_all (GInputStream *stream,
244 GCancellable *cancellable,
250 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
251 g_return_val_if_fail (buffer != NULL, FALSE);
254 while (_bytes_read < count)
256 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
261 *bytes_read = _bytes_read;
272 *bytes_read = _bytes_read;
277 * g_input_stream_read_bytes:
278 * @stream: a #GInputStream.
279 * @count: maximum number of bytes that will be read from the stream. Common
280 * values include 4096 and 8192.
281 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
282 * @error: location to store the error occurring, or %NULL to ignore
284 * Like g_input_stream_read(), this tries to read @count bytes from
285 * the stream in a blocking fashion. However, rather than reading into
286 * a user-supplied buffer, this will create a new #GBytes containing
287 * the data that was read. This may be easier to use from language
290 * If count is zero, returns a zero-length #GBytes and does nothing. A
291 * value of @count larger than %G_MAXSSIZE will cause a
292 * %G_IO_ERROR_INVALID_ARGUMENT error.
294 * On success, a new #GBytes is returned. It is not an error if the
295 * size of this object is not the same as the requested size, as it
296 * can happen e.g. near the end of a file. A zero-length #GBytes is
297 * returned on end of file (or if @count is zero), but never
300 * If @cancellable is not %NULL, then the operation can be cancelled by
301 * triggering the cancellable object from another thread. If the operation
302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
303 * operation was partially finished when the operation was cancelled the
304 * partial result will be returned, without an error.
306 * On error %NULL is returned and @error is set accordingly.
308 * Return value: a new #GBytes, or %NULL on error
311 g_input_stream_read_bytes (GInputStream *stream,
313 GCancellable *cancellable,
319 buf = g_malloc (count);
320 nread = g_input_stream_read (stream, buf, count, cancellable, error);
329 return g_bytes_new_static ("", 0);
332 return g_bytes_new_take (buf, nread);
336 * g_input_stream_skip:
337 * @stream: a #GInputStream.
338 * @count: the number of bytes that will be skipped from the stream
339 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
340 * @error: location to store the error occurring, or %NULL to ignore
342 * Tries to skip @count bytes from the stream. Will block during the operation.
344 * This is identical to g_input_stream_read(), from a behaviour standpoint,
345 * but the bytes that are skipped are not returned to the user. Some
346 * streams have an implementation that is more efficient than reading the data.
348 * This function is optional for inherited classes, as the default implementation
349 * emulates it using read.
351 * If @cancellable is not %NULL, then the operation can be cancelled by
352 * triggering the cancellable object from another thread. If the operation
353 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
354 * operation was partially finished when the operation was cancelled the
355 * partial result will be returned, without an error.
357 * Return value: Number of bytes skipped, or -1 on error
360 g_input_stream_skip (GInputStream *stream,
362 GCancellable *cancellable,
365 GInputStreamClass *class;
368 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
373 if (((gssize) count) < 0)
375 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
376 _("Too large count value passed to %s"), G_STRFUNC);
380 class = G_INPUT_STREAM_GET_CLASS (stream);
382 if (!g_input_stream_set_pending (stream, error))
386 g_cancellable_push_current (cancellable);
388 res = class->skip (stream, count, cancellable, error);
391 g_cancellable_pop_current (cancellable);
393 g_input_stream_clear_pending (stream);
399 g_input_stream_real_skip (GInputStream *stream,
401 GCancellable *cancellable,
404 GInputStreamClass *class;
405 gssize ret, read_bytes;
409 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
411 if (g_seekable_seek (G_SEEKABLE (stream),
419 /* If not seekable, or seek failed, fall back to reading data: */
421 class = G_INPUT_STREAM_GET_CLASS (stream);
428 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
429 cancellable, &my_error);
432 if (read_bytes > 0 &&
433 my_error->domain == G_IO_ERROR &&
434 my_error->code == G_IO_ERROR_CANCELLED)
436 g_error_free (my_error);
440 g_propagate_error (error, my_error);
447 if (ret == 0 || count == 0)
453 * g_input_stream_close:
454 * @stream: A #GInputStream.
455 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
456 * @error: location to store the error occurring, or %NULL to ignore
458 * Closes the stream, releasing resources related to it.
460 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
461 * Closing a stream multiple times will not return an error.
463 * Streams will be automatically closed when the last reference
464 * is dropped, but you might want to call this function to make sure
465 * resources are released as early as possible.
467 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
468 * open after the stream is closed. See the documentation for the individual
469 * stream for details.
471 * On failure the first error that happened will be reported, but the close
472 * operation will finish as much as possible. A stream that failed to
473 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
474 * is important to check and report the error to the user.
476 * If @cancellable is not %NULL, then the operation can be cancelled by
477 * triggering the cancellable object from another thread. If the operation
478 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
479 * Cancelling a close will still leave the stream closed, but some streams
480 * can use a faster close that doesn't block to e.g. check errors.
482 * Return value: %TRUE on success, %FALSE on failure
485 g_input_stream_close (GInputStream *stream,
486 GCancellable *cancellable,
489 GInputStreamClass *class;
492 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
494 class = G_INPUT_STREAM_GET_CLASS (stream);
496 if (stream->priv->closed)
501 if (!g_input_stream_set_pending (stream, error))
505 g_cancellable_push_current (cancellable);
508 res = class->close_fn (stream, cancellable, error);
511 g_cancellable_pop_current (cancellable);
513 g_input_stream_clear_pending (stream);
515 stream->priv->closed = TRUE;
521 async_ready_callback_wrapper (GObject *source_object,
525 GInputStream *stream = G_INPUT_STREAM (source_object);
527 g_input_stream_clear_pending (stream);
528 if (stream->priv->outstanding_callback)
529 (*stream->priv->outstanding_callback) (source_object, res, user_data);
530 g_object_unref (stream);
534 async_ready_close_callback_wrapper (GObject *source_object,
538 GInputStream *stream = G_INPUT_STREAM (source_object);
540 g_input_stream_clear_pending (stream);
541 stream->priv->closed = TRUE;
542 if (stream->priv->outstanding_callback)
543 (*stream->priv->outstanding_callback) (source_object, res, user_data);
544 g_object_unref (stream);
548 * g_input_stream_read_async:
549 * @stream: A #GInputStream.
550 * @buffer: a buffer to read data into (which should be at least count bytes long).
551 * @count: the number of bytes that will be read from the stream
552 * @io_priority: the <link linkend="io-priority">I/O priority</link>
554 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
555 * @callback: (scope async): callback to call when the request is satisfied
556 * @user_data: (closure): the data to pass to callback function
558 * Request an asynchronous read of @count bytes from the stream into the buffer
559 * starting at @buffer. When the operation is finished @callback will be called.
560 * You can then call g_input_stream_read_finish() to get the result of the
563 * During an async request no other sync and async calls are allowed on @stream, and will
564 * result in %G_IO_ERROR_PENDING errors.
566 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
568 * On success, the number of bytes read into the buffer will be passed to the
569 * callback. It is not an error if this is not the same as the requested size, as it
570 * can happen e.g. near the end of a file, but generally we try to read
571 * as many bytes as requested. Zero is returned on end of file
572 * (or if @count is zero), but never otherwise.
574 * Any outstanding i/o request with higher priority (lower numerical value) will
575 * be executed before an outstanding request with lower priority. Default
576 * priority is %G_PRIORITY_DEFAULT.
578 * The asyncronous methods have a default fallback that uses threads to implement
579 * asynchronicity, so they are optional for inheriting classes. However, if you
580 * override one you must override all.
583 g_input_stream_read_async (GInputStream *stream,
587 GCancellable *cancellable,
588 GAsyncReadyCallback callback,
591 GInputStreamClass *class;
592 GSimpleAsyncResult *simple;
593 GError *error = NULL;
595 g_return_if_fail (G_IS_INPUT_STREAM (stream));
596 g_return_if_fail (buffer != NULL);
600 simple = g_simple_async_result_new (G_OBJECT (stream),
603 g_input_stream_read_async);
604 g_simple_async_result_complete_in_idle (simple);
605 g_object_unref (simple);
609 if (((gssize) count) < 0)
611 g_simple_async_report_error_in_idle (G_OBJECT (stream),
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_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
629 class = G_INPUT_STREAM_GET_CLASS (stream);
630 stream->priv->outstanding_callback = callback;
631 g_object_ref (stream);
632 class->read_async (stream, buffer, count, io_priority, cancellable,
633 async_ready_callback_wrapper, user_data);
637 * g_input_stream_read_finish:
638 * @stream: a #GInputStream.
639 * @result: a #GAsyncResult.
640 * @error: a #GError location to store the error occurring, or %NULL to
643 * Finishes an asynchronous stream read operation.
645 * Returns: number of bytes read in, or -1 on error, or 0 on end of file.
648 g_input_stream_read_finish (GInputStream *stream,
649 GAsyncResult *result,
652 GSimpleAsyncResult *simple;
653 GInputStreamClass *class;
655 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
656 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
658 if (G_IS_SIMPLE_ASYNC_RESULT (result))
660 simple = G_SIMPLE_ASYNC_RESULT (result);
661 if (g_simple_async_result_propagate_error (simple, error))
664 /* Special case read of 0 bytes */
665 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
669 class = G_INPUT_STREAM_GET_CLASS (stream);
670 return class->read_finish (stream, result, error);
674 read_bytes_callback (GObject *stream,
675 GAsyncResult *result,
678 GSimpleAsyncResult *simple = user_data;
679 guchar *buf = g_simple_async_result_get_op_res_gpointer (simple);
680 GError *error = NULL;
682 GBytes *bytes = NULL;
684 nread = g_input_stream_read_finish (G_INPUT_STREAM (stream),
689 g_simple_async_result_take_error (simple, error);
694 bytes = g_bytes_new_static ("", 0);
697 bytes = g_bytes_new_take (buf, nread);
701 g_simple_async_result_set_op_res_gpointer (simple, bytes,
702 (GDestroyNotify)g_bytes_unref);
704 g_simple_async_result_complete (simple);
705 g_object_unref (simple);
709 * g_input_stream_read_bytes_async:
710 * @stream: A #GInputStream.
711 * @count: the number of bytes that will be read from the stream
712 * @io_priority: the <link linkend="io-priority">I/O priority</link>
714 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
715 * @callback: (scope async): callback to call when the request is satisfied
716 * @user_data: (closure): the data to pass to callback function
718 * Request an asynchronous read of @count bytes from the stream into a
719 * new #GBytes. When the operation is finished @callback will be
720 * called. You can then call g_input_stream_read_bytes_finish() to get the
721 * result of the operation.
723 * During an async request no other sync and async calls are allowed
724 * on @stream, and will result in %G_IO_ERROR_PENDING errors.
726 * A value of @count larger than %G_MAXSSIZE will cause a
727 * %G_IO_ERROR_INVALID_ARGUMENT error.
729 * On success, the new #GBytes will be passed to the callback. It is
730 * not an error if this is smaller than the requested size, as it can
731 * happen e.g. near the end of a file, but generally we try to read as
732 * many bytes as requested. Zero is returned on end of file (or if
733 * @count is zero), but never otherwise.
735 * Any outstanding I/O request with higher priority (lower numerical
736 * value) will be executed before an outstanding request with lower
737 * priority. Default priority is %G_PRIORITY_DEFAULT.
740 g_input_stream_read_bytes_async (GInputStream *stream,
743 GCancellable *cancellable,
744 GAsyncReadyCallback callback,
747 GSimpleAsyncResult *simple;
750 simple = g_simple_async_result_new (G_OBJECT (stream),
752 g_input_stream_read_bytes_async);
753 buf = g_malloc (count);
754 g_simple_async_result_set_op_res_gpointer (simple, buf, NULL);
756 g_input_stream_read_async (stream, buf, count,
757 io_priority, cancellable,
758 read_bytes_callback, simple);
762 * g_input_stream_read_bytes_finish:
763 * @stream: a #GInputStream.
764 * @result: a #GAsyncResult.
765 * @error: a #GError location to store the error occurring, or %NULL to
768 * Finishes an asynchronous stream read-into-#GBytes operation.
770 * Returns: the newly-allocated #GBytes, or %NULL on error
773 g_input_stream_read_bytes_finish (GInputStream *stream,
774 GAsyncResult *result,
777 GSimpleAsyncResult *simple;
779 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
780 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (stream), g_input_stream_read_bytes_async), NULL);
782 simple = G_SIMPLE_ASYNC_RESULT (result);
783 if (g_simple_async_result_propagate_error (simple, error))
785 return g_bytes_ref (g_simple_async_result_get_op_res_gpointer (simple));
789 * g_input_stream_skip_async:
790 * @stream: A #GInputStream.
791 * @count: the number of bytes that will be skipped from the stream
792 * @io_priority: the <link linkend="io-priority">I/O priority</link>
794 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
795 * @callback: (scope async): callback to call when the request is satisfied
796 * @user_data: (closure): the data to pass to callback function
798 * Request an asynchronous skip of @count bytes from the stream.
799 * When the operation is finished @callback will be called.
800 * You can then call g_input_stream_skip_finish() to get the result
803 * During an async request no other sync and async calls are allowed,
804 * and will result in %G_IO_ERROR_PENDING errors.
806 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
808 * On success, the number of bytes skipped will be passed to the callback.
809 * It is not an error if this is not the same as the requested size, as it
810 * can happen e.g. near the end of a file, but generally we try to skip
811 * as many bytes as requested. Zero is returned on end of file
812 * (or if @count is zero), but never otherwise.
814 * Any outstanding i/o request with higher priority (lower numerical value)
815 * will be executed before an outstanding request with lower priority.
816 * Default priority is %G_PRIORITY_DEFAULT.
818 * The asynchronous methods have a default fallback that uses threads to
819 * implement asynchronicity, so they are optional for inheriting classes.
820 * However, if you override one, you must override all.
823 g_input_stream_skip_async (GInputStream *stream,
826 GCancellable *cancellable,
827 GAsyncReadyCallback callback,
830 GInputStreamClass *class;
831 GSimpleAsyncResult *simple;
832 GError *error = NULL;
834 g_return_if_fail (G_IS_INPUT_STREAM (stream));
838 simple = g_simple_async_result_new (G_OBJECT (stream),
841 g_input_stream_skip_async);
843 g_simple_async_result_complete_in_idle (simple);
844 g_object_unref (simple);
848 if (((gssize) count) < 0)
850 g_simple_async_report_error_in_idle (G_OBJECT (stream),
853 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
854 _("Too large count value passed to %s"),
859 if (!g_input_stream_set_pending (stream, &error))
861 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
868 class = G_INPUT_STREAM_GET_CLASS (stream);
869 stream->priv->outstanding_callback = callback;
870 g_object_ref (stream);
871 class->skip_async (stream, count, io_priority, cancellable,
872 async_ready_callback_wrapper, user_data);
876 * g_input_stream_skip_finish:
877 * @stream: a #GInputStream.
878 * @result: a #GAsyncResult.
879 * @error: a #GError location to store the error occurring, or %NULL to
882 * Finishes a stream skip operation.
884 * Returns: the size of the bytes skipped, or %-1 on error.
887 g_input_stream_skip_finish (GInputStream *stream,
888 GAsyncResult *result,
891 GSimpleAsyncResult *simple;
892 GInputStreamClass *class;
894 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
895 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
897 if (G_IS_SIMPLE_ASYNC_RESULT (result))
899 simple = G_SIMPLE_ASYNC_RESULT (result);
900 if (g_simple_async_result_propagate_error (simple, error))
903 /* Special case skip of 0 bytes */
904 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
908 class = G_INPUT_STREAM_GET_CLASS (stream);
909 return class->skip_finish (stream, result, error);
913 * g_input_stream_close_async:
914 * @stream: A #GInputStream.
915 * @io_priority: the <link linkend="io-priority">I/O priority</link>
917 * @cancellable: (allow-none): optional cancellable object
918 * @callback: (scope async): callback to call when the request is satisfied
919 * @user_data: (closure): the data to pass to callback function
921 * Requests an asynchronous closes of the stream, releasing resources related to it.
922 * When the operation is finished @callback will be called.
923 * You can then call g_input_stream_close_finish() to get the result of the
926 * For behaviour details see g_input_stream_close().
928 * The asyncronous methods have a default fallback that uses threads to implement
929 * asynchronicity, so they are optional for inheriting classes. However, if you
930 * override one you must override all.
933 g_input_stream_close_async (GInputStream *stream,
935 GCancellable *cancellable,
936 GAsyncReadyCallback callback,
939 GInputStreamClass *class;
940 GSimpleAsyncResult *simple;
941 GError *error = NULL;
943 g_return_if_fail (G_IS_INPUT_STREAM (stream));
945 if (stream->priv->closed)
947 simple = g_simple_async_result_new (G_OBJECT (stream),
950 g_input_stream_close_async);
952 g_simple_async_result_complete_in_idle (simple);
953 g_object_unref (simple);
957 if (!g_input_stream_set_pending (stream, &error))
959 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
966 class = G_INPUT_STREAM_GET_CLASS (stream);
967 stream->priv->outstanding_callback = callback;
968 g_object_ref (stream);
969 class->close_async (stream, io_priority, cancellable,
970 async_ready_close_callback_wrapper, user_data);
974 * g_input_stream_close_finish:
975 * @stream: a #GInputStream.
976 * @result: a #GAsyncResult.
977 * @error: a #GError location to store the error occurring, or %NULL to
980 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
982 * Returns: %TRUE if the stream was closed successfully.
985 g_input_stream_close_finish (GInputStream *stream,
986 GAsyncResult *result,
989 GSimpleAsyncResult *simple;
990 GInputStreamClass *class;
992 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
993 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
995 if (G_IS_SIMPLE_ASYNC_RESULT (result))
997 simple = G_SIMPLE_ASYNC_RESULT (result);
998 if (g_simple_async_result_propagate_error (simple, error))
1001 /* Special case already closed */
1002 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
1006 class = G_INPUT_STREAM_GET_CLASS (stream);
1007 return class->close_finish (stream, result, error);
1011 * g_input_stream_is_closed:
1012 * @stream: input stream.
1014 * Checks if an input stream is closed.
1016 * Returns: %TRUE if the stream is closed.
1019 g_input_stream_is_closed (GInputStream *stream)
1021 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
1023 return stream->priv->closed;
1027 * g_input_stream_has_pending:
1028 * @stream: input stream.
1030 * Checks if an input stream has pending actions.
1032 * Returns: %TRUE if @stream has pending actions.
1035 g_input_stream_has_pending (GInputStream *stream)
1037 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
1039 return stream->priv->pending;
1043 * g_input_stream_set_pending:
1044 * @stream: input stream
1045 * @error: a #GError location to store the error occurring, or %NULL to
1048 * Sets @stream to have actions pending. If the pending flag is
1049 * already set or @stream is closed, it will return %FALSE and set
1052 * Return value: %TRUE if pending was previously unset and is now set.
1055 g_input_stream_set_pending (GInputStream *stream, GError **error)
1057 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
1059 if (stream->priv->closed)
1061 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
1062 _("Stream is already closed"));
1066 if (stream->priv->pending)
1068 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
1069 /* Translators: This is an error you get if there is already an
1070 * operation running against this stream when you try to start
1072 _("Stream has outstanding operation"));
1076 stream->priv->pending = TRUE;
1081 * g_input_stream_clear_pending:
1082 * @stream: input stream
1084 * Clears the pending flag on @stream.
1087 g_input_stream_clear_pending (GInputStream *stream)
1089 g_return_if_fail (G_IS_INPUT_STREAM (stream));
1091 stream->priv->pending = FALSE;
1094 /********************************************
1095 * Default implementation of async ops *
1096 ********************************************/
1100 gsize count_requested;
1103 GCancellable *cancellable;
1109 free_read_data (ReadData *op)
1111 if (op->cancellable)
1112 g_object_unref (op->cancellable);
1113 g_slice_free (ReadData, op);
1117 read_async_thread (GSimpleAsyncResult *res,
1119 GCancellable *cancellable)
1122 GInputStreamClass *class;
1123 GError *error = NULL;
1125 op = g_simple_async_result_get_op_res_gpointer (res);
1127 class = G_INPUT_STREAM_GET_CLASS (object);
1129 op->count_read = class->read_fn (G_INPUT_STREAM (object),
1130 op->buffer, op->count_requested,
1131 cancellable, &error);
1132 if (op->count_read == -1)
1133 g_simple_async_result_take_error (res, error);
1136 static void read_async_pollable (GPollableInputStream *stream,
1137 GSimpleAsyncResult *result);
1140 read_async_pollable_ready (GPollableInputStream *stream,
1143 GSimpleAsyncResult *result = user_data;
1145 read_async_pollable (stream, result);
1150 read_async_pollable (GPollableInputStream *stream,
1151 GSimpleAsyncResult *result)
1153 GError *error = NULL;
1154 ReadData *op = g_simple_async_result_get_op_res_gpointer (result);
1156 if (g_cancellable_set_error_if_cancelled (op->cancellable, &error))
1157 op->count_read = -1;
1160 op->count_read = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
1161 read_nonblocking (stream, op->buffer, op->count_requested, &error);
1164 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
1168 g_error_free (error);
1169 op->need_idle = FALSE;
1171 source = g_pollable_input_stream_create_source (stream, op->cancellable);
1172 g_source_set_callback (source,
1173 (GSourceFunc) read_async_pollable_ready,
1174 g_object_ref (result), g_object_unref);
1175 g_source_set_priority (source, op->io_priority);
1176 g_source_attach (source, g_main_context_get_thread_default ());
1177 g_source_unref (source);
1181 if (op->count_read == -1)
1182 g_simple_async_result_take_error (result, error);
1185 g_simple_async_result_complete_in_idle (result);
1187 g_simple_async_result_complete (result);
1191 g_input_stream_real_read_async (GInputStream *stream,
1195 GCancellable *cancellable,
1196 GAsyncReadyCallback callback,
1199 GSimpleAsyncResult *res;
1202 op = g_slice_new0 (ReadData);
1203 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
1204 g_simple_async_result_set_op_res_gpointer (res, op, (GDestroyNotify) free_read_data);
1205 op->buffer = buffer;
1206 op->count_requested = count;
1207 op->cancellable = cancellable ? g_object_ref (cancellable) : NULL;
1208 op->io_priority = io_priority;
1209 op->need_idle = TRUE;
1211 if (G_IS_POLLABLE_INPUT_STREAM (stream) &&
1212 g_pollable_input_stream_can_poll (G_POLLABLE_INPUT_STREAM (stream)))
1213 read_async_pollable (G_POLLABLE_INPUT_STREAM (stream), res);
1215 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
1216 g_object_unref (res);
1220 g_input_stream_real_read_finish (GInputStream *stream,
1221 GAsyncResult *result,
1224 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1227 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
1228 g_input_stream_real_read_async);
1230 op = g_simple_async_result_get_op_res_gpointer (simple);
1232 return op->count_read;
1236 gsize count_requested;
1237 gssize count_skipped;
1242 skip_async_thread (GSimpleAsyncResult *res,
1244 GCancellable *cancellable)
1247 GInputStreamClass *class;
1248 GError *error = NULL;
1250 class = G_INPUT_STREAM_GET_CLASS (object);
1251 op = g_simple_async_result_get_op_res_gpointer (res);
1252 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1253 op->count_requested,
1254 cancellable, &error);
1255 if (op->count_skipped == -1)
1256 g_simple_async_result_take_error (res, error);
1262 gsize count_skipped;
1264 GCancellable *cancellable;
1266 GAsyncReadyCallback callback;
1267 } SkipFallbackAsyncData;
1270 skip_callback_wrapper (GObject *source_object,
1274 GInputStreamClass *class;
1275 SkipFallbackAsyncData *data = user_data;
1277 GSimpleAsyncResult *simple;
1278 GError *error = NULL;
1281 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1286 data->count_skipped += ret;
1288 if (data->count > 0)
1290 class = G_INPUT_STREAM_GET_CLASS (source_object);
1291 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1292 skip_callback_wrapper, data);
1297 op = g_new0 (SkipData, 1);
1298 op->count_skipped = data->count_skipped;
1299 simple = g_simple_async_result_new (source_object,
1300 data->callback, data->user_data,
1301 g_input_stream_real_skip_async);
1303 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1307 if (data->count_skipped &&
1308 g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
1309 /* No error, return partial read */
1310 g_error_free (error);
1312 g_simple_async_result_take_error (simple, error);
1315 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1316 g_simple_async_result_complete (simple);
1317 g_object_unref (simple);
1323 g_input_stream_real_skip_async (GInputStream *stream,
1326 GCancellable *cancellable,
1327 GAsyncReadyCallback callback,
1330 GInputStreamClass *class;
1332 SkipFallbackAsyncData *data;
1333 GSimpleAsyncResult *res;
1335 class = G_INPUT_STREAM_GET_CLASS (stream);
1337 if (class->read_async == g_input_stream_real_read_async)
1339 /* Read is thread-using async fallback.
1340 * Make skip use threads too, so that we can use a possible sync skip
1341 * implementation. */
1342 op = g_new0 (SkipData, 1);
1344 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1345 g_input_stream_real_skip_async);
1347 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1349 op->count_requested = count;
1351 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1352 g_object_unref (res);
1356 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1358 /* There is a custom async read function, lets use that. */
1359 data = g_new (SkipFallbackAsyncData, 1);
1360 data->count = count;
1361 data->count_skipped = 0;
1362 data->io_prio = io_priority;
1363 data->cancellable = cancellable;
1364 data->callback = callback;
1365 data->user_data = user_data;
1366 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1367 skip_callback_wrapper, data);
1373 g_input_stream_real_skip_finish (GInputStream *stream,
1374 GAsyncResult *result,
1377 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1380 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1381 op = g_simple_async_result_get_op_res_gpointer (simple);
1382 return op->count_skipped;
1386 close_async_thread (GSimpleAsyncResult *res,
1388 GCancellable *cancellable)
1390 GInputStreamClass *class;
1391 GError *error = NULL;
1394 /* Auto handling of cancelation disabled, and ignore
1395 cancellation, since we want to close things anyway, although
1396 possibly in a quick-n-dirty way. At least we never want to leak
1399 class = G_INPUT_STREAM_GET_CLASS (object);
1400 if (class->close_fn)
1402 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1404 g_simple_async_result_take_error (res, error);
1409 g_input_stream_real_close_async (GInputStream *stream,
1411 GCancellable *cancellable,
1412 GAsyncReadyCallback callback,
1415 GSimpleAsyncResult *res;
1417 res = g_simple_async_result_new (G_OBJECT (stream),
1420 g_input_stream_real_close_async);
1422 g_simple_async_result_set_handle_cancellation (res, FALSE);
1424 g_simple_async_result_run_in_thread (res,
1428 g_object_unref (res);
1432 g_input_stream_real_close_finish (GInputStream *stream,
1433 GAsyncResult *result,
1436 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1437 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);