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 "gsimpleasyncresult.h"
34 * SECTION:ginputstream
35 * @short_description: Base class for implementing streaming input
42 G_DEFINE_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
44 struct _GInputStreamPrivate {
47 GAsyncReadyCallback outstanding_callback;
50 static gssize g_input_stream_real_skip (GInputStream *stream,
52 GCancellable *cancellable,
54 static void g_input_stream_real_read_async (GInputStream *stream,
58 GCancellable *cancellable,
59 GAsyncReadyCallback callback,
61 static gssize g_input_stream_real_read_finish (GInputStream *stream,
64 static void g_input_stream_real_skip_async (GInputStream *stream,
67 GCancellable *cancellable,
68 GAsyncReadyCallback callback,
70 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
73 static void g_input_stream_real_close_async (GInputStream *stream,
75 GCancellable *cancellable,
76 GAsyncReadyCallback callback,
78 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
83 g_input_stream_finalize (GObject *object)
87 stream = G_INPUT_STREAM (object);
89 if (!stream->priv->closed)
90 g_input_stream_close (stream, NULL, NULL);
92 G_OBJECT_CLASS (g_input_stream_parent_class)->finalize (object);
96 g_input_stream_dispose (GObject *object)
100 stream = G_INPUT_STREAM (object);
102 if (!stream->priv->closed)
103 g_input_stream_close (stream, NULL, NULL);
105 G_OBJECT_CLASS (g_input_stream_parent_class)->dispose (object);
110 g_input_stream_class_init (GInputStreamClass *klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
116 gobject_class->finalize = g_input_stream_finalize;
117 gobject_class->dispose = g_input_stream_dispose;
119 klass->skip = g_input_stream_real_skip;
120 klass->read_async = g_input_stream_real_read_async;
121 klass->read_finish = g_input_stream_real_read_finish;
122 klass->skip_async = g_input_stream_real_skip_async;
123 klass->skip_finish = g_input_stream_real_skip_finish;
124 klass->close_async = g_input_stream_real_close_async;
125 klass->close_finish = g_input_stream_real_close_finish;
129 g_input_stream_init (GInputStream *stream)
131 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
133 GInputStreamPrivate);
137 * g_input_stream_read:
138 * @stream: a #GInputStream.
139 * @buffer: a buffer to read data into (which should be at least count bytes long).
140 * @count: the number of bytes that will be read from the stream
141 * @cancellable: optional #GCancellable object, %NULL to ignore.
142 * @error: location to store the error occuring, or %NULL to ignore
144 * Tries to read @count bytes from the stream into the buffer starting at
145 * @buffer. Will block during this read.
147 * If count is zero returns zero and does nothing. A value of @count
148 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
150 * On success, the number of bytes read into the buffer is returned.
151 * It is not an error if this is not the same as the requested size, as it
152 * can happen e.g. near the end of a file. Zero is returned on end of file
153 * (or if @count is zero), but never otherwise.
155 * If @cancellable is not NULL, then the operation can be cancelled by
156 * triggering the cancellable object from another thread. If the operation
157 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
158 * operation was partially finished when the operation was cancelled the
159 * partial result will be returned, without an error.
161 * On error -1 is returned and @error is set accordingly.
163 * Return value: Number of bytes read, or -1 on error
166 g_input_stream_read (GInputStream *stream,
169 GCancellable *cancellable,
172 GInputStreamClass *class;
175 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
176 g_return_val_if_fail (buffer != NULL, 0);
181 if (((gssize) count) < 0)
183 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
184 _("Too large count value passed to %s"), G_STRFUNC);
188 class = G_INPUT_STREAM_GET_CLASS (stream);
190 if (class->read_fn == NULL)
192 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
193 _("Input stream doesn't implement read"));
197 if (!g_input_stream_set_pending (stream, error))
201 g_cancellable_push_current (cancellable);
203 res = class->read_fn (stream, buffer, count, cancellable, error);
206 g_cancellable_pop_current (cancellable);
208 g_input_stream_clear_pending (stream);
214 * g_input_stream_read_all:
215 * @stream: a #GInputStream.
216 * @buffer: a buffer to read data into (which should be at least count bytes long).
217 * @count: the number of bytes that will be read from the stream
218 * @bytes_read: location to store the number of bytes that was read from the stream
219 * @cancellable: optional #GCancellable object, %NULL to ignore.
220 * @error: location to store the error occuring, or %NULL to ignore
222 * Tries to read @count bytes from the stream into the buffer starting at
223 * @buffer. Will block during this read.
225 * This function is similar to g_input_stream_read(), except it tries to
226 * read as many bytes as requested, only stopping on an error or end of stream.
228 * On a successful read of @count bytes, or if we reached the end of the
229 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
232 * If there is an error during the operation %FALSE is returned and @error
233 * is set to indicate the error status, @bytes_read is updated to contain
234 * the number of bytes read into @buffer before the error occurred.
236 * Return value: %TRUE on success, %FALSE if there was an error
239 g_input_stream_read_all (GInputStream *stream,
243 GCancellable *cancellable,
249 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
250 g_return_val_if_fail (buffer != NULL, FALSE);
253 while (_bytes_read < count)
255 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
260 *bytes_read = _bytes_read;
271 *bytes_read = _bytes_read;
276 * g_input_stream_skip:
277 * @stream: a #GInputStream.
278 * @count: the number of bytes that will be skipped from the stream
279 * @cancellable: optional #GCancellable object, %NULL to ignore.
280 * @error: location to store the error occuring, or %NULL to ignore
282 * Tries to skip @count bytes from the stream. Will block during the operation.
284 * This is identical to g_input_stream_read(), from a behaviour standpoint,
285 * but the bytes that are skipped are not returned to the user. Some
286 * streams have an implementation that is more efficient than reading the data.
288 * This function is optional for inherited classes, as the default implementation
289 * emulates it using read.
291 * If @cancellable is not %NULL, then the operation can be cancelled by
292 * triggering the cancellable object from another thread. If the operation
293 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
294 * operation was partially finished when the operation was cancelled the
295 * partial result will be returned, without an error.
297 * Return value: Number of bytes skipped, or -1 on error
300 g_input_stream_skip (GInputStream *stream,
302 GCancellable *cancellable,
305 GInputStreamClass *class;
308 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
313 if (((gssize) count) < 0)
315 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
316 _("Too large count value passed to %s"), G_STRFUNC);
320 class = G_INPUT_STREAM_GET_CLASS (stream);
322 if (!g_input_stream_set_pending (stream, error))
326 g_cancellable_push_current (cancellable);
328 res = class->skip (stream, count, cancellable, error);
331 g_cancellable_pop_current (cancellable);
333 g_input_stream_clear_pending (stream);
339 g_input_stream_real_skip (GInputStream *stream,
341 GCancellable *cancellable,
344 GInputStreamClass *class;
345 gssize ret, read_bytes;
349 class = G_INPUT_STREAM_GET_CLASS (stream);
351 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
353 if (g_seekable_seek (G_SEEKABLE (stream),
361 /* If not seekable, or seek failed, fall back to reading data: */
363 class = G_INPUT_STREAM_GET_CLASS (stream);
370 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
371 cancellable, &my_error);
374 if (read_bytes > 0 &&
375 my_error->domain == G_IO_ERROR &&
376 my_error->code == G_IO_ERROR_CANCELLED)
378 g_error_free (my_error);
382 g_propagate_error (error, my_error);
389 if (ret == 0 || count == 0)
395 * g_input_stream_close:
396 * @stream: A #GInputStream.
397 * @cancellable: optional #GCancellable object, %NULL to ignore.
398 * @error: location to store the error occuring, or %NULL to ignore
400 * Closes the stream, releasing resources related to it.
402 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
403 * Closing a stream multiple times will not return an error.
405 * Streams will be automatically closed when the last reference
406 * is dropped, but you might want to call this function to make sure
407 * resources are released as early as possible.
409 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
410 * open after the stream is closed. See the documentation for the individual
411 * stream for details.
413 * On failure the first error that happened will be reported, but the close
414 * operation will finish as much as possible. A stream that failed to
415 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
416 * is important to check and report the error to the user.
418 * If @cancellable is not NULL, then the operation can be cancelled by
419 * triggering the cancellable object from another thread. If the operation
420 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
421 * Cancelling a close will still leave the stream closed, but some streams
422 * can use a faster close that doesn't block to e.g. check errors.
424 * Return value: %TRUE on success, %FALSE on failure
427 g_input_stream_close (GInputStream *stream,
428 GCancellable *cancellable,
431 GInputStreamClass *class;
434 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
436 class = G_INPUT_STREAM_GET_CLASS (stream);
438 if (stream->priv->closed)
443 if (!g_input_stream_set_pending (stream, error))
447 g_cancellable_push_current (cancellable);
450 res = class->close_fn (stream, cancellable, error);
453 g_cancellable_pop_current (cancellable);
455 g_input_stream_clear_pending (stream);
457 stream->priv->closed = TRUE;
463 async_ready_callback_wrapper (GObject *source_object,
467 GInputStream *stream = G_INPUT_STREAM (source_object);
469 g_input_stream_clear_pending (stream);
470 if (stream->priv->outstanding_callback)
471 (*stream->priv->outstanding_callback) (source_object, res, user_data);
472 g_object_unref (stream);
476 async_ready_close_callback_wrapper (GObject *source_object,
480 GInputStream *stream = G_INPUT_STREAM (source_object);
482 g_input_stream_clear_pending (stream);
483 stream->priv->closed = TRUE;
484 if (stream->priv->outstanding_callback)
485 (*stream->priv->outstanding_callback) (source_object, res, user_data);
486 g_object_unref (stream);
490 * g_input_stream_read_async:
491 * @stream: A #GInputStream.
492 * @buffer: a buffer to read data into (which should be at least count bytes long).
493 * @count: the number of bytes that will be read from the stream
494 * @io_priority: the <link linkend="io-priority">I/O priority</link>
496 * @cancellable: optional #GCancellable object, %NULL to ignore.
497 * @callback: callback to call when the request is satisfied
498 * @user_data: the data to pass to callback function
500 * Request an asynchronous read of @count bytes from the stream into the buffer
501 * starting at @buffer. When the operation is finished @callback will be called.
502 * You can then call g_input_stream_read_finish() to get the result of the
505 * During an async request no other sync and async calls are allowed, and will
506 * result in %G_IO_ERROR_PENDING errors.
508 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
510 * On success, the number of bytes read into the buffer will be passed to the
511 * callback. It is not an error if this is not the same as the requested size, as it
512 * can happen e.g. near the end of a file, but generally we try to read
513 * as many bytes as requested. Zero is returned on end of file
514 * (or if @count is zero), but never otherwise.
516 * Any outstanding i/o request with higher priority (lower numerical value) will
517 * be executed before an outstanding request with lower priority. Default
518 * priority is %G_PRIORITY_DEFAULT.
520 * The asyncronous methods have a default fallback that uses threads to implement
521 * asynchronicity, so they are optional for inheriting classes. However, if you
522 * override one you must override all.
525 g_input_stream_read_async (GInputStream *stream,
529 GCancellable *cancellable,
530 GAsyncReadyCallback callback,
533 GInputStreamClass *class;
534 GSimpleAsyncResult *simple;
535 GError *error = NULL;
537 g_return_if_fail (G_IS_INPUT_STREAM (stream));
538 g_return_if_fail (buffer != NULL);
542 simple = g_simple_async_result_new (G_OBJECT (stream),
545 g_input_stream_read_async);
546 g_simple_async_result_complete_in_idle (simple);
547 g_object_unref (simple);
551 if (((gssize) count) < 0)
553 g_simple_async_report_error_in_idle (G_OBJECT (stream),
556 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
557 _("Too large count value passed to %s"),
562 if (!g_input_stream_set_pending (stream, &error))
564 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
568 g_error_free (error);
572 class = G_INPUT_STREAM_GET_CLASS (stream);
573 stream->priv->outstanding_callback = callback;
574 g_object_ref (stream);
575 class->read_async (stream, buffer, count, io_priority, cancellable,
576 async_ready_callback_wrapper, user_data);
580 * g_input_stream_read_finish:
581 * @stream: a #GInputStream.
582 * @result: a #GAsyncResult.
583 * @error: a #GError location to store the error occuring, or %NULL to
586 * Finishes an asynchronous stream read operation.
588 * Returns: number of bytes read in, or -1 on error.
591 g_input_stream_read_finish (GInputStream *stream,
592 GAsyncResult *result,
595 GSimpleAsyncResult *simple;
596 GInputStreamClass *class;
598 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
599 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
601 if (G_IS_SIMPLE_ASYNC_RESULT (result))
603 simple = G_SIMPLE_ASYNC_RESULT (result);
604 if (g_simple_async_result_propagate_error (simple, error))
607 /* Special case read of 0 bytes */
608 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
612 class = G_INPUT_STREAM_GET_CLASS (stream);
613 return class->read_finish (stream, result, error);
617 * g_input_stream_skip_async:
618 * @stream: A #GInputStream.
619 * @count: the number of bytes that will be skipped from the stream
620 * @io_priority: the <link linkend="io-priority">I/O priority</link>
622 * @cancellable: optional #GCancellable object, %NULL to ignore.
623 * @callback: callback to call when the request is satisfied
624 * @user_data: the data to pass to callback function
626 * Request an asynchronous skip of @count bytes from the stream into the buffer
627 * starting at @buffer. When the operation is finished @callback will be called.
628 * You can then call g_input_stream_skip_finish() to get the result of the
631 * During an async request no other sync and async calls are allowed, and will
632 * result in %G_IO_ERROR_PENDING errors.
634 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
636 * On success, the number of bytes skipped will be passed to the
637 * callback. It is not an error if this is not the same as the requested size, as it
638 * can happen e.g. near the end of a file, but generally we try to skip
639 * as many bytes as requested. Zero is returned on end of file
640 * (or if @count is zero), but never otherwise.
642 * Any outstanding i/o request with higher priority (lower numerical value) will
643 * be executed before an outstanding request with lower priority. Default
644 * priority is %G_PRIORITY_DEFAULT.
646 * The asyncronous methods have a default fallback that uses threads to implement
647 * asynchronicity, so they are optional for inheriting classes. However, if you
648 * override one you must override all.
651 g_input_stream_skip_async (GInputStream *stream,
654 GCancellable *cancellable,
655 GAsyncReadyCallback callback,
658 GInputStreamClass *class;
659 GSimpleAsyncResult *simple;
660 GError *error = NULL;
662 g_return_if_fail (G_IS_INPUT_STREAM (stream));
666 simple = g_simple_async_result_new (G_OBJECT (stream),
669 g_input_stream_skip_async);
671 g_simple_async_result_complete_in_idle (simple);
672 g_object_unref (simple);
676 if (((gssize) count) < 0)
678 g_simple_async_report_error_in_idle (G_OBJECT (stream),
681 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
682 _("Too large count value passed to %s"),
687 if (!g_input_stream_set_pending (stream, &error))
689 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
693 g_error_free (error);
697 class = G_INPUT_STREAM_GET_CLASS (stream);
698 stream->priv->outstanding_callback = callback;
699 g_object_ref (stream);
700 class->skip_async (stream, count, io_priority, cancellable,
701 async_ready_callback_wrapper, user_data);
705 * g_input_stream_skip_finish:
706 * @stream: a #GInputStream.
707 * @result: a #GAsyncResult.
708 * @error: a #GError location to store the error occuring, or %NULL to
711 * Finishes a stream skip operation.
713 * Returns: the size of the bytes skipped, or %-1 on error.
716 g_input_stream_skip_finish (GInputStream *stream,
717 GAsyncResult *result,
720 GSimpleAsyncResult *simple;
721 GInputStreamClass *class;
723 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
724 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
726 if (G_IS_SIMPLE_ASYNC_RESULT (result))
728 simple = G_SIMPLE_ASYNC_RESULT (result);
729 if (g_simple_async_result_propagate_error (simple, error))
732 /* Special case skip of 0 bytes */
733 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
737 class = G_INPUT_STREAM_GET_CLASS (stream);
738 return class->skip_finish (stream, result, error);
742 * g_input_stream_close_async:
743 * @stream: A #GInputStream.
744 * @io_priority: the <link linkend="io-priority">I/O priority</link>
746 * @cancellable: optional cancellable object
747 * @callback: callback to call when the request is satisfied
748 * @user_data: the data to pass to callback function
750 * Requests an asynchronous closes of the stream, releasing resources related to it.
751 * When the operation is finished @callback will be called.
752 * You can then call g_input_stream_close_finish() to get the result of the
755 * For behaviour details see g_input_stream_close().
757 * The asyncronous methods have a default fallback that uses threads to implement
758 * asynchronicity, so they are optional for inheriting classes. However, if you
759 * override one you must override all.
762 g_input_stream_close_async (GInputStream *stream,
764 GCancellable *cancellable,
765 GAsyncReadyCallback callback,
768 GInputStreamClass *class;
769 GSimpleAsyncResult *simple;
770 GError *error = NULL;
772 g_return_if_fail (G_IS_INPUT_STREAM (stream));
774 if (stream->priv->closed)
776 simple = g_simple_async_result_new (G_OBJECT (stream),
779 g_input_stream_close_async);
781 g_simple_async_result_complete_in_idle (simple);
782 g_object_unref (simple);
786 if (!g_input_stream_set_pending (stream, &error))
788 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
792 g_error_free (error);
796 class = G_INPUT_STREAM_GET_CLASS (stream);
797 stream->priv->outstanding_callback = callback;
798 g_object_ref (stream);
799 class->close_async (stream, io_priority, cancellable,
800 async_ready_close_callback_wrapper, user_data);
804 * g_input_stream_close_finish:
805 * @stream: a #GInputStream.
806 * @result: a #GAsyncResult.
807 * @error: a #GError location to store the error occuring, or %NULL to
810 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
812 * Returns: %TRUE if the stream was closed successfully.
815 g_input_stream_close_finish (GInputStream *stream,
816 GAsyncResult *result,
819 GSimpleAsyncResult *simple;
820 GInputStreamClass *class;
822 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
823 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
825 if (G_IS_SIMPLE_ASYNC_RESULT (result))
827 simple = G_SIMPLE_ASYNC_RESULT (result);
828 if (g_simple_async_result_propagate_error (simple, error))
831 /* Special case already closed */
832 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
836 class = G_INPUT_STREAM_GET_CLASS (stream);
837 return class->close_finish (stream, result, error);
841 * g_input_stream_is_closed:
842 * @stream: input stream.
844 * Checks if an input stream is closed.
846 * Returns: %TRUE if the stream is closed.
849 g_input_stream_is_closed (GInputStream *stream)
851 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
853 return stream->priv->closed;
857 * g_input_stream_has_pending:
858 * @stream: input stream.
860 * Checks if an input stream has pending actions.
862 * Returns: %TRUE if @stream has pending actions.
865 g_input_stream_has_pending (GInputStream *stream)
867 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
869 return stream->priv->pending;
873 * g_input_stream_set_pending:
874 * @stream: input stream
875 * @error: a #GError location to store the error occuring, or %NULL to
878 * Sets @stream to have actions pending. If the pending flag is
879 * already set or @stream is closed, it will return %FALSE and set
882 * Return value: %TRUE if pending was previously unset and is now set.
885 g_input_stream_set_pending (GInputStream *stream, GError **error)
887 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
889 if (stream->priv->closed)
891 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
892 _("Stream is already closed"));
896 if (stream->priv->pending)
898 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
899 /* Translators: This is an error you get if there is already an
900 * operation running against this stream when you try to start
902 _("Stream has outstanding operation"));
906 stream->priv->pending = TRUE;
911 * g_input_stream_clear_pending:
912 * @stream: input stream
914 * Clears the pending flag on @stream.
917 g_input_stream_clear_pending (GInputStream *stream)
919 g_return_if_fail (G_IS_INPUT_STREAM (stream));
921 stream->priv->pending = FALSE;
924 /********************************************
925 * Default implementation of async ops *
926 ********************************************/
930 gsize count_requested;
935 read_async_thread (GSimpleAsyncResult *res,
937 GCancellable *cancellable)
940 GInputStreamClass *class;
941 GError *error = NULL;
943 op = g_simple_async_result_get_op_res_gpointer (res);
945 class = G_INPUT_STREAM_GET_CLASS (object);
947 op->count_read = class->read_fn (G_INPUT_STREAM (object),
948 op->buffer, op->count_requested,
949 cancellable, &error);
950 if (op->count_read == -1)
952 g_simple_async_result_set_from_error (res, error);
953 g_error_free (error);
958 g_input_stream_real_read_async (GInputStream *stream,
962 GCancellable *cancellable,
963 GAsyncReadyCallback callback,
966 GSimpleAsyncResult *res;
969 op = g_new (ReadData, 1);
970 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
971 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
973 op->count_requested = count;
975 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
976 g_object_unref (res);
980 g_input_stream_real_read_finish (GInputStream *stream,
981 GAsyncResult *result,
984 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
987 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
988 g_input_stream_real_read_async);
990 op = g_simple_async_result_get_op_res_gpointer (simple);
992 return op->count_read;
996 gsize count_requested;
997 gssize count_skipped;
1002 skip_async_thread (GSimpleAsyncResult *res,
1004 GCancellable *cancellable)
1007 GInputStreamClass *class;
1008 GError *error = NULL;
1010 class = G_INPUT_STREAM_GET_CLASS (object);
1011 op = g_simple_async_result_get_op_res_gpointer (res);
1012 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1013 op->count_requested,
1014 cancellable, &error);
1015 if (op->count_skipped == -1)
1017 g_simple_async_result_set_from_error (res, error);
1018 g_error_free (error);
1025 gsize count_skipped;
1027 GCancellable *cancellable;
1029 GAsyncReadyCallback callback;
1030 } SkipFallbackAsyncData;
1033 skip_callback_wrapper (GObject *source_object,
1037 GInputStreamClass *class;
1038 SkipFallbackAsyncData *data = user_data;
1040 GSimpleAsyncResult *simple;
1041 GError *error = NULL;
1044 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1049 data->count_skipped += ret;
1051 if (data->count > 0)
1053 class = G_INPUT_STREAM_GET_CLASS (source_object);
1054 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1055 skip_callback_wrapper, data);
1060 op = g_new0 (SkipData, 1);
1061 op->count_skipped = data->count_skipped;
1062 simple = g_simple_async_result_new (source_object,
1063 data->callback, data->user_data,
1064 g_input_stream_real_skip_async);
1066 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1070 if (data->count_skipped &&
1071 error->domain == G_IO_ERROR &&
1072 error->code == G_IO_ERROR_CANCELLED)
1073 { /* No error, return partial read */ }
1075 g_simple_async_result_set_from_error (simple, error);
1076 g_error_free (error);
1079 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1080 g_simple_async_result_complete (simple);
1081 g_object_unref (simple);
1087 g_input_stream_real_skip_async (GInputStream *stream,
1090 GCancellable *cancellable,
1091 GAsyncReadyCallback callback,
1094 GInputStreamClass *class;
1096 SkipFallbackAsyncData *data;
1097 GSimpleAsyncResult *res;
1099 class = G_INPUT_STREAM_GET_CLASS (stream);
1101 if (class->read_async == g_input_stream_real_read_async)
1103 /* Read is thread-using async fallback.
1104 * Make skip use threads too, so that we can use a possible sync skip
1105 * implementation. */
1106 op = g_new0 (SkipData, 1);
1108 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1109 g_input_stream_real_skip_async);
1111 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1113 op->count_requested = count;
1115 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1116 g_object_unref (res);
1120 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1122 /* There is a custom async read function, lets use that. */
1123 data = g_new (SkipFallbackAsyncData, 1);
1124 data->count = count;
1125 data->count_skipped = 0;
1126 data->io_prio = io_priority;
1127 data->cancellable = cancellable;
1128 data->callback = callback;
1129 data->user_data = user_data;
1130 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1131 skip_callback_wrapper, data);
1137 g_input_stream_real_skip_finish (GInputStream *stream,
1138 GAsyncResult *result,
1141 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1144 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1145 op = g_simple_async_result_get_op_res_gpointer (simple);
1146 return op->count_skipped;
1150 close_async_thread (GSimpleAsyncResult *res,
1152 GCancellable *cancellable)
1154 GInputStreamClass *class;
1155 GError *error = NULL;
1158 /* Auto handling of cancelation disabled, and ignore
1159 cancellation, since we want to close things anyway, although
1160 possibly in a quick-n-dirty way. At least we never want to leak
1163 class = G_INPUT_STREAM_GET_CLASS (object);
1164 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1167 g_simple_async_result_set_from_error (res, error);
1168 g_error_free (error);
1173 g_input_stream_real_close_async (GInputStream *stream,
1175 GCancellable *cancellable,
1176 GAsyncReadyCallback callback,
1179 GSimpleAsyncResult *res;
1181 res = g_simple_async_result_new (G_OBJECT (stream),
1184 g_input_stream_real_close_async);
1186 g_simple_async_result_set_handle_cancellation (res, FALSE);
1188 g_simple_async_result_run_in_thread (res,
1192 g_object_unref (res);
1196 g_input_stream_real_close_finish (GInputStream *stream,
1197 GAsyncResult *result,
1200 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1201 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1205 #define __G_INPUT_STREAM_C__
1206 #include "gioaliasdef.c"