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"
37 * SECTION:ginputstream
38 * @short_description: Base class for implementing streaming input
41 * GInputStream has functions to read from a stream (g_input_stream_read()),
42 * to close a stream (g_input_stream_close()) and to skip some content
43 * (g_input_stream_skip()).
45 * To copy the content of an input stream to an output stream without
46 * manually handling the reads and writes, use g_output_stream_splice().
48 * All of these functions have async variants too.
51 G_DEFINE_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
53 struct _GInputStreamPrivate {
56 GAsyncReadyCallback outstanding_callback;
59 static gssize g_input_stream_real_skip (GInputStream *stream,
61 GCancellable *cancellable,
63 static void g_input_stream_real_read_async (GInputStream *stream,
67 GCancellable *cancellable,
68 GAsyncReadyCallback callback,
70 static gssize g_input_stream_real_read_finish (GInputStream *stream,
73 static void g_input_stream_real_skip_async (GInputStream *stream,
76 GCancellable *cancellable,
77 GAsyncReadyCallback callback,
79 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
82 static void g_input_stream_real_close_async (GInputStream *stream,
84 GCancellable *cancellable,
85 GAsyncReadyCallback callback,
87 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
92 g_input_stream_finalize (GObject *object)
96 stream = G_INPUT_STREAM (object);
98 G_OBJECT_CLASS (g_input_stream_parent_class)->finalize (object);
102 g_input_stream_dispose (GObject *object)
104 GInputStream *stream;
106 stream = G_INPUT_STREAM (object);
108 if (!stream->priv->closed)
109 g_input_stream_close (stream, NULL, NULL);
111 G_OBJECT_CLASS (g_input_stream_parent_class)->dispose (object);
116 g_input_stream_class_init (GInputStreamClass *klass)
118 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120 g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
122 gobject_class->finalize = g_input_stream_finalize;
123 gobject_class->dispose = g_input_stream_dispose;
125 klass->skip = g_input_stream_real_skip;
126 klass->read_async = g_input_stream_real_read_async;
127 klass->read_finish = g_input_stream_real_read_finish;
128 klass->skip_async = g_input_stream_real_skip_async;
129 klass->skip_finish = g_input_stream_real_skip_finish;
130 klass->close_async = g_input_stream_real_close_async;
131 klass->close_finish = g_input_stream_real_close_finish;
135 g_input_stream_init (GInputStream *stream)
137 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
139 GInputStreamPrivate);
143 * g_input_stream_read:
144 * @stream: a #GInputStream.
145 * @buffer: a buffer to read data into (which should be at least count bytes long).
146 * @count: the number of bytes that will be read from the stream
147 * @cancellable: optional #GCancellable object, %NULL to ignore.
148 * @error: location to store the error occuring, or %NULL to ignore
150 * Tries to read @count bytes from the stream into the buffer starting at
151 * @buffer. Will block during this read.
153 * If count is zero returns zero and does nothing. A value of @count
154 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
156 * On success, the number of bytes read into the buffer is returned.
157 * It is not an error if this is not the same as the requested size, as it
158 * can happen e.g. near the end of a file. Zero is returned on end of file
159 * (or if @count is zero), but never otherwise.
161 * If @cancellable is not NULL, then the operation can be cancelled by
162 * triggering the cancellable object from another thread. If the operation
163 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
164 * operation was partially finished when the operation was cancelled the
165 * partial result will be returned, without an error.
167 * On error -1 is returned and @error is set accordingly.
169 * Return value: Number of bytes read, or -1 on error
172 g_input_stream_read (GInputStream *stream,
175 GCancellable *cancellable,
178 GInputStreamClass *class;
181 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
182 g_return_val_if_fail (buffer != NULL, 0);
187 if (((gssize) count) < 0)
189 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
190 _("Too large count value passed to %s"), G_STRFUNC);
194 class = G_INPUT_STREAM_GET_CLASS (stream);
196 if (class->read_fn == NULL)
198 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
199 _("Input stream doesn't implement read"));
203 if (!g_input_stream_set_pending (stream, error))
207 g_cancellable_push_current (cancellable);
209 res = class->read_fn (stream, buffer, count, cancellable, error);
212 g_cancellable_pop_current (cancellable);
214 g_input_stream_clear_pending (stream);
220 * g_input_stream_read_all:
221 * @stream: a #GInputStream.
222 * @buffer: a buffer to read data into (which should be at least count bytes long).
223 * @count: the number of bytes that will be read from the stream
224 * @bytes_read: location to store the number of bytes that was read from the stream
225 * @cancellable: optional #GCancellable object, %NULL to ignore.
226 * @error: location to store the error occuring, or %NULL to ignore
228 * Tries to read @count bytes from the stream into the buffer starting at
229 * @buffer. Will block during this read.
231 * This function is similar to g_input_stream_read(), except it tries to
232 * read as many bytes as requested, only stopping on an error or end of stream.
234 * On a successful read of @count bytes, or if we reached the end of the
235 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
238 * If there is an error during the operation %FALSE is returned and @error
239 * is set to indicate the error status, @bytes_read is updated to contain
240 * the number of bytes read into @buffer before the error occurred.
242 * Return value: %TRUE on success, %FALSE if there was an error
245 g_input_stream_read_all (GInputStream *stream,
249 GCancellable *cancellable,
255 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
256 g_return_val_if_fail (buffer != NULL, FALSE);
259 while (_bytes_read < count)
261 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
266 *bytes_read = _bytes_read;
277 *bytes_read = _bytes_read;
282 * g_input_stream_skip:
283 * @stream: a #GInputStream.
284 * @count: the number of bytes that will be skipped from the stream
285 * @cancellable: optional #GCancellable object, %NULL to ignore.
286 * @error: location to store the error occuring, or %NULL to ignore
288 * Tries to skip @count bytes from the stream. Will block during the operation.
290 * This is identical to g_input_stream_read(), from a behaviour standpoint,
291 * but the bytes that are skipped are not returned to the user. Some
292 * streams have an implementation that is more efficient than reading the data.
294 * This function is optional for inherited classes, as the default implementation
295 * emulates it using read.
297 * If @cancellable is not %NULL, then the operation can be cancelled by
298 * triggering the cancellable object from another thread. If the operation
299 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
300 * operation was partially finished when the operation was cancelled the
301 * partial result will be returned, without an error.
303 * Return value: Number of bytes skipped, or -1 on error
306 g_input_stream_skip (GInputStream *stream,
308 GCancellable *cancellable,
311 GInputStreamClass *class;
314 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
319 if (((gssize) count) < 0)
321 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
322 _("Too large count value passed to %s"), G_STRFUNC);
326 class = G_INPUT_STREAM_GET_CLASS (stream);
328 if (!g_input_stream_set_pending (stream, error))
332 g_cancellable_push_current (cancellable);
334 res = class->skip (stream, count, cancellable, error);
337 g_cancellable_pop_current (cancellable);
339 g_input_stream_clear_pending (stream);
345 g_input_stream_real_skip (GInputStream *stream,
347 GCancellable *cancellable,
350 GInputStreamClass *class;
351 gssize ret, read_bytes;
355 class = G_INPUT_STREAM_GET_CLASS (stream);
357 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
359 if (g_seekable_seek (G_SEEKABLE (stream),
367 /* If not seekable, or seek failed, fall back to reading data: */
369 class = G_INPUT_STREAM_GET_CLASS (stream);
376 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
377 cancellable, &my_error);
380 if (read_bytes > 0 &&
381 my_error->domain == G_IO_ERROR &&
382 my_error->code == G_IO_ERROR_CANCELLED)
384 g_error_free (my_error);
388 g_propagate_error (error, my_error);
395 if (ret == 0 || count == 0)
401 * g_input_stream_close:
402 * @stream: A #GInputStream.
403 * @cancellable: optional #GCancellable object, %NULL to ignore.
404 * @error: location to store the error occuring, or %NULL to ignore
406 * Closes the stream, releasing resources related to it.
408 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
409 * Closing a stream multiple times will not return an error.
411 * Streams will be automatically closed when the last reference
412 * is dropped, but you might want to call this function to make sure
413 * resources are released as early as possible.
415 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
416 * open after the stream is closed. See the documentation for the individual
417 * stream for details.
419 * On failure the first error that happened will be reported, but the close
420 * operation will finish as much as possible. A stream that failed to
421 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
422 * is important to check and report the error to the user.
424 * If @cancellable is not NULL, then the operation can be cancelled by
425 * triggering the cancellable object from another thread. If the operation
426 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
427 * Cancelling a close will still leave the stream closed, but some streams
428 * can use a faster close that doesn't block to e.g. check errors.
430 * Return value: %TRUE on success, %FALSE on failure
433 g_input_stream_close (GInputStream *stream,
434 GCancellable *cancellable,
437 GInputStreamClass *class;
440 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
442 class = G_INPUT_STREAM_GET_CLASS (stream);
444 if (stream->priv->closed)
449 if (!g_input_stream_set_pending (stream, error))
453 g_cancellable_push_current (cancellable);
456 res = class->close_fn (stream, cancellable, error);
459 g_cancellable_pop_current (cancellable);
461 g_input_stream_clear_pending (stream);
463 stream->priv->closed = TRUE;
469 async_ready_callback_wrapper (GObject *source_object,
473 GInputStream *stream = G_INPUT_STREAM (source_object);
475 g_input_stream_clear_pending (stream);
476 if (stream->priv->outstanding_callback)
477 (*stream->priv->outstanding_callback) (source_object, res, user_data);
478 g_object_unref (stream);
482 async_ready_close_callback_wrapper (GObject *source_object,
486 GInputStream *stream = G_INPUT_STREAM (source_object);
488 g_input_stream_clear_pending (stream);
489 stream->priv->closed = TRUE;
490 if (stream->priv->outstanding_callback)
491 (*stream->priv->outstanding_callback) (source_object, res, user_data);
492 g_object_unref (stream);
496 * g_input_stream_read_async:
497 * @stream: A #GInputStream.
498 * @buffer: a buffer to read data into (which should be at least count bytes long).
499 * @count: the number of bytes that will be read from the stream
500 * @io_priority: the <link linkend="io-priority">I/O priority</link>
502 * @cancellable: optional #GCancellable object, %NULL to ignore.
503 * @callback: callback to call when the request is satisfied
504 * @user_data: the data to pass to callback function
506 * Request an asynchronous read of @count bytes from the stream into the buffer
507 * starting at @buffer. When the operation is finished @callback will be called.
508 * You can then call g_input_stream_read_finish() to get the result of the
511 * During an async request no other sync and async calls are allowed, and will
512 * result in %G_IO_ERROR_PENDING errors.
514 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
516 * On success, the number of bytes read into the buffer will be passed to the
517 * callback. It is not an error if this is not the same as the requested size, as it
518 * can happen e.g. near the end of a file, but generally we try to read
519 * as many bytes as requested. Zero is returned on end of file
520 * (or if @count is zero), but never otherwise.
522 * Any outstanding i/o request with higher priority (lower numerical value) will
523 * be executed before an outstanding request with lower priority. Default
524 * priority is %G_PRIORITY_DEFAULT.
526 * The asyncronous methods have a default fallback that uses threads to implement
527 * asynchronicity, so they are optional for inheriting classes. However, if you
528 * override one you must override all.
531 g_input_stream_read_async (GInputStream *stream,
535 GCancellable *cancellable,
536 GAsyncReadyCallback callback,
539 GInputStreamClass *class;
540 GSimpleAsyncResult *simple;
541 GError *error = NULL;
543 g_return_if_fail (G_IS_INPUT_STREAM (stream));
544 g_return_if_fail (buffer != NULL);
548 simple = g_simple_async_result_new (G_OBJECT (stream),
551 g_input_stream_read_async);
552 g_simple_async_result_complete_in_idle (simple);
553 g_object_unref (simple);
557 if (((gssize) count) < 0)
559 g_simple_async_report_error_in_idle (G_OBJECT (stream),
562 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
563 _("Too large count value passed to %s"),
568 if (!g_input_stream_set_pending (stream, &error))
570 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
574 g_error_free (error);
578 class = G_INPUT_STREAM_GET_CLASS (stream);
579 stream->priv->outstanding_callback = callback;
580 g_object_ref (stream);
581 class->read_async (stream, buffer, count, io_priority, cancellable,
582 async_ready_callback_wrapper, user_data);
586 * g_input_stream_read_finish:
587 * @stream: a #GInputStream.
588 * @result: a #GAsyncResult.
589 * @error: a #GError location to store the error occuring, or %NULL to
592 * Finishes an asynchronous stream read operation.
594 * Returns: number of bytes read in, or -1 on error.
597 g_input_stream_read_finish (GInputStream *stream,
598 GAsyncResult *result,
601 GSimpleAsyncResult *simple;
602 GInputStreamClass *class;
604 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
605 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
607 if (G_IS_SIMPLE_ASYNC_RESULT (result))
609 simple = G_SIMPLE_ASYNC_RESULT (result);
610 if (g_simple_async_result_propagate_error (simple, error))
613 /* Special case read of 0 bytes */
614 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
618 class = G_INPUT_STREAM_GET_CLASS (stream);
619 return class->read_finish (stream, result, error);
623 * g_input_stream_skip_async:
624 * @stream: A #GInputStream.
625 * @count: the number of bytes that will be skipped from the stream
626 * @io_priority: the <link linkend="io-priority">I/O priority</link>
628 * @cancellable: optional #GCancellable object, %NULL to ignore.
629 * @callback: callback to call when the request is satisfied
630 * @user_data: the data to pass to callback function
632 * Request an asynchronous skip of @count bytes from the stream.
633 * When the operation is finished @callback will be called.
634 * You can then call g_input_stream_skip_finish() to get the result of the
637 * During an async request no other sync and async calls are allowed, and will
638 * result in %G_IO_ERROR_PENDING errors.
640 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
642 * On success, the number of bytes skipped will be passed to the
643 * callback. It is not an error if this is not the same as the requested size, as it
644 * can happen e.g. near the end of a file, but generally we try to skip
645 * as many bytes as requested. Zero is returned on end of file
646 * (or if @count is zero), but never otherwise.
648 * Any outstanding i/o request with higher priority (lower numerical value) will
649 * be executed before an outstanding request with lower priority. Default
650 * priority is %G_PRIORITY_DEFAULT.
652 * The asyncronous methods have a default fallback that uses threads to implement
653 * asynchronicity, so they are optional for inheriting classes. However, if you
654 * override one you must override all.
657 g_input_stream_skip_async (GInputStream *stream,
660 GCancellable *cancellable,
661 GAsyncReadyCallback callback,
664 GInputStreamClass *class;
665 GSimpleAsyncResult *simple;
666 GError *error = NULL;
668 g_return_if_fail (G_IS_INPUT_STREAM (stream));
672 simple = g_simple_async_result_new (G_OBJECT (stream),
675 g_input_stream_skip_async);
677 g_simple_async_result_complete_in_idle (simple);
678 g_object_unref (simple);
682 if (((gssize) count) < 0)
684 g_simple_async_report_error_in_idle (G_OBJECT (stream),
687 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
688 _("Too large count value passed to %s"),
693 if (!g_input_stream_set_pending (stream, &error))
695 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
699 g_error_free (error);
703 class = G_INPUT_STREAM_GET_CLASS (stream);
704 stream->priv->outstanding_callback = callback;
705 g_object_ref (stream);
706 class->skip_async (stream, count, io_priority, cancellable,
707 async_ready_callback_wrapper, user_data);
711 * g_input_stream_skip_finish:
712 * @stream: a #GInputStream.
713 * @result: a #GAsyncResult.
714 * @error: a #GError location to store the error occuring, or %NULL to
717 * Finishes a stream skip operation.
719 * Returns: the size of the bytes skipped, or %-1 on error.
722 g_input_stream_skip_finish (GInputStream *stream,
723 GAsyncResult *result,
726 GSimpleAsyncResult *simple;
727 GInputStreamClass *class;
729 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
730 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
732 if (G_IS_SIMPLE_ASYNC_RESULT (result))
734 simple = G_SIMPLE_ASYNC_RESULT (result);
735 if (g_simple_async_result_propagate_error (simple, error))
738 /* Special case skip of 0 bytes */
739 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
743 class = G_INPUT_STREAM_GET_CLASS (stream);
744 return class->skip_finish (stream, result, error);
748 * g_input_stream_close_async:
749 * @stream: A #GInputStream.
750 * @io_priority: the <link linkend="io-priority">I/O priority</link>
752 * @cancellable: optional cancellable object
753 * @callback: callback to call when the request is satisfied
754 * @user_data: the data to pass to callback function
756 * Requests an asynchronous closes of the stream, releasing resources related to it.
757 * When the operation is finished @callback will be called.
758 * You can then call g_input_stream_close_finish() to get the result of the
761 * For behaviour details see g_input_stream_close().
763 * The asyncronous methods have a default fallback that uses threads to implement
764 * asynchronicity, so they are optional for inheriting classes. However, if you
765 * override one you must override all.
768 g_input_stream_close_async (GInputStream *stream,
770 GCancellable *cancellable,
771 GAsyncReadyCallback callback,
774 GInputStreamClass *class;
775 GSimpleAsyncResult *simple;
776 GError *error = NULL;
778 g_return_if_fail (G_IS_INPUT_STREAM (stream));
780 if (stream->priv->closed)
782 simple = g_simple_async_result_new (G_OBJECT (stream),
785 g_input_stream_close_async);
787 g_simple_async_result_complete_in_idle (simple);
788 g_object_unref (simple);
792 if (!g_input_stream_set_pending (stream, &error))
794 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
798 g_error_free (error);
802 class = G_INPUT_STREAM_GET_CLASS (stream);
803 stream->priv->outstanding_callback = callback;
804 g_object_ref (stream);
805 class->close_async (stream, io_priority, cancellable,
806 async_ready_close_callback_wrapper, user_data);
810 * g_input_stream_close_finish:
811 * @stream: a #GInputStream.
812 * @result: a #GAsyncResult.
813 * @error: a #GError location to store the error occuring, or %NULL to
816 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
818 * Returns: %TRUE if the stream was closed successfully.
821 g_input_stream_close_finish (GInputStream *stream,
822 GAsyncResult *result,
825 GSimpleAsyncResult *simple;
826 GInputStreamClass *class;
828 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
829 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
831 if (G_IS_SIMPLE_ASYNC_RESULT (result))
833 simple = G_SIMPLE_ASYNC_RESULT (result);
834 if (g_simple_async_result_propagate_error (simple, error))
837 /* Special case already closed */
838 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
842 class = G_INPUT_STREAM_GET_CLASS (stream);
843 return class->close_finish (stream, result, error);
847 * g_input_stream_is_closed:
848 * @stream: input stream.
850 * Checks if an input stream is closed.
852 * Returns: %TRUE if the stream is closed.
855 g_input_stream_is_closed (GInputStream *stream)
857 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
859 return stream->priv->closed;
863 * g_input_stream_has_pending:
864 * @stream: input stream.
866 * Checks if an input stream has pending actions.
868 * Returns: %TRUE if @stream has pending actions.
871 g_input_stream_has_pending (GInputStream *stream)
873 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
875 return stream->priv->pending;
879 * g_input_stream_set_pending:
880 * @stream: input stream
881 * @error: a #GError location to store the error occuring, or %NULL to
884 * Sets @stream to have actions pending. If the pending flag is
885 * already set or @stream is closed, it will return %FALSE and set
888 * Return value: %TRUE if pending was previously unset and is now set.
891 g_input_stream_set_pending (GInputStream *stream, GError **error)
893 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
895 if (stream->priv->closed)
897 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
898 _("Stream is already closed"));
902 if (stream->priv->pending)
904 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
905 /* Translators: This is an error you get if there is already an
906 * operation running against this stream when you try to start
908 _("Stream has outstanding operation"));
912 stream->priv->pending = TRUE;
917 * g_input_stream_clear_pending:
918 * @stream: input stream
920 * Clears the pending flag on @stream.
923 g_input_stream_clear_pending (GInputStream *stream)
925 g_return_if_fail (G_IS_INPUT_STREAM (stream));
927 stream->priv->pending = FALSE;
930 /********************************************
931 * Default implementation of async ops *
932 ********************************************/
936 gsize count_requested;
941 read_async_thread (GSimpleAsyncResult *res,
943 GCancellable *cancellable)
946 GInputStreamClass *class;
947 GError *error = NULL;
949 op = g_simple_async_result_get_op_res_gpointer (res);
951 class = G_INPUT_STREAM_GET_CLASS (object);
953 op->count_read = class->read_fn (G_INPUT_STREAM (object),
954 op->buffer, op->count_requested,
955 cancellable, &error);
956 if (op->count_read == -1)
958 g_simple_async_result_set_from_error (res, error);
959 g_error_free (error);
964 g_input_stream_real_read_async (GInputStream *stream,
968 GCancellable *cancellable,
969 GAsyncReadyCallback callback,
972 GSimpleAsyncResult *res;
975 op = g_new (ReadData, 1);
976 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
977 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
979 op->count_requested = count;
981 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
982 g_object_unref (res);
986 g_input_stream_real_read_finish (GInputStream *stream,
987 GAsyncResult *result,
990 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
993 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
994 g_input_stream_real_read_async);
996 op = g_simple_async_result_get_op_res_gpointer (simple);
998 return op->count_read;
1002 gsize count_requested;
1003 gssize count_skipped;
1008 skip_async_thread (GSimpleAsyncResult *res,
1010 GCancellable *cancellable)
1013 GInputStreamClass *class;
1014 GError *error = NULL;
1016 class = G_INPUT_STREAM_GET_CLASS (object);
1017 op = g_simple_async_result_get_op_res_gpointer (res);
1018 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1019 op->count_requested,
1020 cancellable, &error);
1021 if (op->count_skipped == -1)
1023 g_simple_async_result_set_from_error (res, error);
1024 g_error_free (error);
1031 gsize count_skipped;
1033 GCancellable *cancellable;
1035 GAsyncReadyCallback callback;
1036 } SkipFallbackAsyncData;
1039 skip_callback_wrapper (GObject *source_object,
1043 GInputStreamClass *class;
1044 SkipFallbackAsyncData *data = user_data;
1046 GSimpleAsyncResult *simple;
1047 GError *error = NULL;
1050 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1055 data->count_skipped += ret;
1057 if (data->count > 0)
1059 class = G_INPUT_STREAM_GET_CLASS (source_object);
1060 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1061 skip_callback_wrapper, data);
1066 op = g_new0 (SkipData, 1);
1067 op->count_skipped = data->count_skipped;
1068 simple = g_simple_async_result_new (source_object,
1069 data->callback, data->user_data,
1070 g_input_stream_real_skip_async);
1072 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1076 if (data->count_skipped &&
1077 error->domain == G_IO_ERROR &&
1078 error->code == G_IO_ERROR_CANCELLED)
1079 { /* No error, return partial read */ }
1081 g_simple_async_result_set_from_error (simple, error);
1082 g_error_free (error);
1085 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1086 g_simple_async_result_complete (simple);
1087 g_object_unref (simple);
1093 g_input_stream_real_skip_async (GInputStream *stream,
1096 GCancellable *cancellable,
1097 GAsyncReadyCallback callback,
1100 GInputStreamClass *class;
1102 SkipFallbackAsyncData *data;
1103 GSimpleAsyncResult *res;
1105 class = G_INPUT_STREAM_GET_CLASS (stream);
1107 if (class->read_async == g_input_stream_real_read_async)
1109 /* Read is thread-using async fallback.
1110 * Make skip use threads too, so that we can use a possible sync skip
1111 * implementation. */
1112 op = g_new0 (SkipData, 1);
1114 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1115 g_input_stream_real_skip_async);
1117 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1119 op->count_requested = count;
1121 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1122 g_object_unref (res);
1126 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1128 /* There is a custom async read function, lets use that. */
1129 data = g_new (SkipFallbackAsyncData, 1);
1130 data->count = count;
1131 data->count_skipped = 0;
1132 data->io_prio = io_priority;
1133 data->cancellable = cancellable;
1134 data->callback = callback;
1135 data->user_data = user_data;
1136 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1137 skip_callback_wrapper, data);
1143 g_input_stream_real_skip_finish (GInputStream *stream,
1144 GAsyncResult *result,
1147 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1150 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1151 op = g_simple_async_result_get_op_res_gpointer (simple);
1152 return op->count_skipped;
1156 close_async_thread (GSimpleAsyncResult *res,
1158 GCancellable *cancellable)
1160 GInputStreamClass *class;
1161 GError *error = NULL;
1164 /* Auto handling of cancelation disabled, and ignore
1165 cancellation, since we want to close things anyway, although
1166 possibly in a quick-n-dirty way. At least we never want to leak
1169 class = G_INPUT_STREAM_GET_CLASS (object);
1170 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1173 g_simple_async_result_set_from_error (res, error);
1174 g_error_free (error);
1179 g_input_stream_real_close_async (GInputStream *stream,
1181 GCancellable *cancellable,
1182 GAsyncReadyCallback callback,
1185 GSimpleAsyncResult *res;
1187 res = g_simple_async_result_new (G_OBJECT (stream),
1190 g_input_stream_real_close_async);
1192 g_simple_async_result_set_handle_cancellation (res, FALSE);
1194 g_simple_async_result_run_in_thread (res,
1198 g_object_unref (res);
1202 g_input_stream_real_close_finish (GInputStream *stream,
1203 GAsyncResult *result,
1206 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1207 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1211 #define __G_INPUT_STREAM_C__
1212 #include "gioaliasdef.c"