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 if (G_OBJECT_CLASS (g_input_stream_parent_class)->finalize)
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 if (G_OBJECT_CLASS (g_input_stream_parent_class)->dispose)
107 (*G_OBJECT_CLASS (g_input_stream_parent_class)->dispose) (object);
112 g_input_stream_class_init (GInputStreamClass *klass)
114 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
118 gobject_class->finalize = g_input_stream_finalize;
119 gobject_class->dispose = g_input_stream_dispose;
121 klass->skip = g_input_stream_real_skip;
122 klass->read_async = g_input_stream_real_read_async;
123 klass->read_finish = g_input_stream_real_read_finish;
124 klass->skip_async = g_input_stream_real_skip_async;
125 klass->skip_finish = g_input_stream_real_skip_finish;
126 klass->close_async = g_input_stream_real_close_async;
127 klass->close_finish = g_input_stream_real_close_finish;
131 g_input_stream_init (GInputStream *stream)
133 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
135 GInputStreamPrivate);
139 * g_input_stream_read:
140 * @stream: a #GInputStream.
141 * @buffer: a buffer to read data into (which should be at least count bytes long).
142 * @count: the number of bytes that will be read from the stream
143 * @cancellable: optional #GCancellable object, %NULL to ignore.
144 * @error: location to store the error occuring, or %NULL to ignore
146 * Tries to read @count bytes from the stream into the buffer starting at
147 * @buffer. Will block during this read.
149 * If count is zero returns zero and does nothing. A value of @count
150 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
152 * On success, the number of bytes read into the buffer is returned.
153 * It is not an error if this is not the same as the requested size, as it
154 * can happen e.g. near the end of a file. Zero is returned on end of file
155 * (or if @count is zero), but never otherwise.
157 * If @cancellable is not NULL, then the operation can be cancelled by
158 * triggering the cancellable object from another thread. If the operation
159 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
160 * operation was partially finished when the operation was cancelled the
161 * partial result will be returned, without an error.
163 * On error -1 is returned and @error is set accordingly.
165 * Return value: Number of bytes read, or -1 on error
168 g_input_stream_read (GInputStream *stream,
171 GCancellable *cancellable,
174 GInputStreamClass *class;
177 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
178 g_return_val_if_fail (buffer != NULL, 0);
183 if (((gssize) count) < 0)
185 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
186 _("Too large count value passed to %s"), G_STRFUNC);
190 class = G_INPUT_STREAM_GET_CLASS (stream);
192 if (class->read_fn == NULL)
194 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
195 _("Input stream doesn't implement read"));
199 if (!g_input_stream_set_pending (stream, error))
203 g_cancellable_push_current (cancellable);
205 res = class->read_fn (stream, buffer, count, cancellable, error);
208 g_cancellable_pop_current (cancellable);
210 g_input_stream_clear_pending (stream);
216 * g_input_stream_read_all:
217 * @stream: a #GInputStream.
218 * @buffer: a buffer to read data into (which should be at least count bytes long).
219 * @count: the number of bytes that will be read from the stream
220 * @bytes_read: location to store the number of bytes that was read from the stream
221 * @cancellable: optional #GCancellable object, %NULL to ignore.
222 * @error: location to store the error occuring, or %NULL to ignore
224 * Tries to read @count bytes from the stream into the buffer starting at
225 * @buffer. Will block during this read.
227 * This function is similar to g_input_stream_read(), except it tries to
228 * read as many bytes as requested, only stopping on an error or end of stream.
230 * On a successful read of @count bytes, or if we reached the end of the
231 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
234 * If there is an error during the operation %FALSE is returned and @error
235 * is set to indicate the error status, @bytes_read is updated to contain
236 * the number of bytes read into @buffer before the error occurred.
238 * Return value: %TRUE on success, %FALSE if there was an error
241 g_input_stream_read_all (GInputStream *stream,
245 GCancellable *cancellable,
251 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
252 g_return_val_if_fail (buffer != NULL, FALSE);
255 while (_bytes_read < count)
257 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
262 *bytes_read = _bytes_read;
273 *bytes_read = _bytes_read;
278 * g_input_stream_skip:
279 * @stream: a #GInputStream.
280 * @count: the number of bytes that will be skipped from the stream
281 * @cancellable: optional #GCancellable object, %NULL to ignore.
282 * @error: location to store the error occuring, or %NULL to ignore
284 * Tries to skip @count bytes from the stream. Will block during the operation.
286 * This is identical to g_input_stream_read(), from a behaviour standpoint,
287 * but the bytes that are skipped are not returned to the user. Some
288 * streams have an implementation that is more efficient than reading the data.
290 * This function is optional for inherited classes, as the default implementation
291 * emulates it using read.
293 * If @cancellable is not %NULL, then the operation can be cancelled by
294 * triggering the cancellable object from another thread. If the operation
295 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
296 * operation was partially finished when the operation was cancelled the
297 * partial result will be returned, without an error.
299 * Return value: Number of bytes skipped, or -1 on error
302 g_input_stream_skip (GInputStream *stream,
304 GCancellable *cancellable,
307 GInputStreamClass *class;
310 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
315 if (((gssize) count) < 0)
317 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
318 _("Too large count value passed to %s"), G_STRFUNC);
322 class = G_INPUT_STREAM_GET_CLASS (stream);
324 if (!g_input_stream_set_pending (stream, error))
328 g_cancellable_push_current (cancellable);
330 res = class->skip (stream, count, cancellable, error);
333 g_cancellable_pop_current (cancellable);
335 g_input_stream_clear_pending (stream);
341 g_input_stream_real_skip (GInputStream *stream,
343 GCancellable *cancellable,
346 GInputStreamClass *class;
347 gssize ret, read_bytes;
351 class = G_INPUT_STREAM_GET_CLASS (stream);
353 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
355 if (g_seekable_seek (G_SEEKABLE (stream),
363 /* If not seekable, or seek failed, fall back to reading data: */
365 class = G_INPUT_STREAM_GET_CLASS (stream);
372 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
373 cancellable, &my_error);
376 if (read_bytes > 0 &&
377 my_error->domain == G_IO_ERROR &&
378 my_error->code == G_IO_ERROR_CANCELLED)
380 g_error_free (my_error);
384 g_propagate_error (error, my_error);
391 if (ret == 0 || count == 0)
397 * g_input_stream_close:
398 * @stream: A #GInputStream.
399 * @cancellable: optional #GCancellable object, %NULL to ignore.
400 * @error: location to store the error occuring, or %NULL to ignore
402 * Closes the stream, releasing resources related to it.
404 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
405 * Closing a stream multiple times will not return an error.
407 * Streams will be automatically closed when the last reference
408 * is dropped, but you might want to call this function to make sure
409 * resources are released as early as possible.
411 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
412 * open after the stream is closed. See the documentation for the individual
413 * stream for details.
415 * On failure the first error that happened will be reported, but the close
416 * operation will finish as much as possible. A stream that failed to
417 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
418 * is important to check and report the error to the user.
420 * If @cancellable is not NULL, then the operation can be cancelled by
421 * triggering the cancellable object from another thread. If the operation
422 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
423 * Cancelling a close will still leave the stream closed, but some streams
424 * can use a faster close that doesn't block to e.g. check errors.
426 * Return value: %TRUE on success, %FALSE on failure
429 g_input_stream_close (GInputStream *stream,
430 GCancellable *cancellable,
433 GInputStreamClass *class;
436 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
438 class = G_INPUT_STREAM_GET_CLASS (stream);
440 if (stream->priv->closed)
445 if (!g_input_stream_set_pending (stream, error))
449 g_cancellable_push_current (cancellable);
452 res = class->close_fn (stream, cancellable, error);
455 g_cancellable_pop_current (cancellable);
457 g_input_stream_clear_pending (stream);
459 stream->priv->closed = TRUE;
465 async_ready_callback_wrapper (GObject *source_object,
469 GInputStream *stream = G_INPUT_STREAM (source_object);
471 g_input_stream_clear_pending (stream);
472 if (stream->priv->outstanding_callback)
473 (*stream->priv->outstanding_callback) (source_object, res, user_data);
474 g_object_unref (stream);
478 async_ready_close_callback_wrapper (GObject *source_object,
482 GInputStream *stream = G_INPUT_STREAM (source_object);
484 g_input_stream_clear_pending (stream);
485 stream->priv->closed = TRUE;
486 if (stream->priv->outstanding_callback)
487 (*stream->priv->outstanding_callback) (source_object, res, user_data);
488 g_object_unref (stream);
492 * g_input_stream_read_async:
493 * @stream: A #GInputStream.
494 * @buffer: a buffer to read data into (which should be at least count bytes long).
495 * @count: the number of bytes that will be read from the stream
496 * @io_priority: the <link linkend="io-priority">I/O priority</link>
498 * @cancellable: optional #GCancellable object, %NULL to ignore.
499 * @callback: callback to call when the request is satisfied
500 * @user_data: the data to pass to callback function
502 * Request an asynchronous read of @count bytes from the stream into the buffer
503 * starting at @buffer. When the operation is finished @callback will be called.
504 * You can then call g_input_stream_read_finish() to get the result of the
507 * During an async request no other sync and async calls are allowed, and will
508 * result in %G_IO_ERROR_PENDING errors.
510 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
512 * On success, the number of bytes read into the buffer will be passed to the
513 * callback. It is not an error if this is not the same as the requested size, as it
514 * can happen e.g. near the end of a file, but generally we try to read
515 * as many bytes as requested. Zero is returned on end of file
516 * (or if @count is zero), but never otherwise.
518 * Any outstanding i/o request with higher priority (lower numerical value) will
519 * be executed before an outstanding request with lower priority. Default
520 * priority is %G_PRIORITY_DEFAULT.
522 * The asyncronous methods have a default fallback that uses threads to implement
523 * asynchronicity, so they are optional for inheriting classes. However, if you
524 * override one you must override all.
527 g_input_stream_read_async (GInputStream *stream,
531 GCancellable *cancellable,
532 GAsyncReadyCallback callback,
535 GInputStreamClass *class;
536 GSimpleAsyncResult *simple;
537 GError *error = NULL;
539 g_return_if_fail (G_IS_INPUT_STREAM (stream));
540 g_return_if_fail (buffer != NULL);
544 simple = g_simple_async_result_new (G_OBJECT (stream),
547 g_input_stream_read_async);
548 g_simple_async_result_complete_in_idle (simple);
549 g_object_unref (simple);
553 if (((gssize) count) < 0)
555 g_simple_async_report_error_in_idle (G_OBJECT (stream),
558 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
559 _("Too large count value passed to %s"),
564 if (!g_input_stream_set_pending (stream, &error))
566 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
570 g_error_free (error);
574 class = G_INPUT_STREAM_GET_CLASS (stream);
575 stream->priv->outstanding_callback = callback;
576 g_object_ref (stream);
577 class->read_async (stream, buffer, count, io_priority, cancellable,
578 async_ready_callback_wrapper, user_data);
582 * g_input_stream_read_finish:
583 * @stream: a #GInputStream.
584 * @result: a #GAsyncResult.
585 * @error: a #GError location to store the error occuring, or %NULL to
588 * Finishes an asynchronous stream read operation.
590 * Returns: number of bytes read in, or -1 on error.
593 g_input_stream_read_finish (GInputStream *stream,
594 GAsyncResult *result,
597 GSimpleAsyncResult *simple;
598 GInputStreamClass *class;
600 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
601 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
603 if (G_IS_SIMPLE_ASYNC_RESULT (result))
605 simple = G_SIMPLE_ASYNC_RESULT (result);
606 if (g_simple_async_result_propagate_error (simple, error))
609 /* Special case read of 0 bytes */
610 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
614 class = G_INPUT_STREAM_GET_CLASS (stream);
615 return class->read_finish (stream, result, error);
619 * g_input_stream_skip_async:
620 * @stream: A #GInputStream.
621 * @count: the number of bytes that will be skipped from the stream
622 * @io_priority: the <link linkend="io-priority">I/O priority</link>
624 * @cancellable: optional #GCancellable object, %NULL to ignore.
625 * @callback: callback to call when the request is satisfied
626 * @user_data: the data to pass to callback function
628 * Request an asynchronous skip of @count bytes from the stream into the buffer
629 * starting at @buffer. When the operation is finished @callback will be called.
630 * You can then call g_input_stream_skip_finish() to get the result of the
633 * During an async request no other sync and async calls are allowed, and will
634 * result in %G_IO_ERROR_PENDING errors.
636 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
638 * On success, the number of bytes skipped will be passed to the
639 * callback. It is not an error if this is not the same as the requested size, as it
640 * can happen e.g. near the end of a file, but generally we try to skip
641 * as many bytes as requested. Zero is returned on end of file
642 * (or if @count is zero), but never otherwise.
644 * Any outstanding i/o request with higher priority (lower numerical value) will
645 * be executed before an outstanding request with lower priority. Default
646 * priority is %G_PRIORITY_DEFAULT.
648 * The asyncronous methods have a default fallback that uses threads to implement
649 * asynchronicity, so they are optional for inheriting classes. However, if you
650 * override one you must override all.
653 g_input_stream_skip_async (GInputStream *stream,
656 GCancellable *cancellable,
657 GAsyncReadyCallback callback,
660 GInputStreamClass *class;
661 GSimpleAsyncResult *simple;
662 GError *error = NULL;
664 g_return_if_fail (G_IS_INPUT_STREAM (stream));
668 simple = g_simple_async_result_new (G_OBJECT (stream),
671 g_input_stream_skip_async);
673 g_simple_async_result_complete_in_idle (simple);
674 g_object_unref (simple);
678 if (((gssize) count) < 0)
680 g_simple_async_report_error_in_idle (G_OBJECT (stream),
683 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
684 _("Too large count value passed to %s"),
689 if (!g_input_stream_set_pending (stream, &error))
691 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
695 g_error_free (error);
699 class = G_INPUT_STREAM_GET_CLASS (stream);
700 stream->priv->outstanding_callback = callback;
701 g_object_ref (stream);
702 class->skip_async (stream, count, io_priority, cancellable,
703 async_ready_callback_wrapper, user_data);
707 * g_input_stream_skip_finish:
708 * @stream: a #GInputStream.
709 * @result: a #GAsyncResult.
710 * @error: a #GError location to store the error occuring, or %NULL to
713 * Finishes a stream skip operation.
715 * Returns: the size of the bytes skipped, or %-1 on error.
718 g_input_stream_skip_finish (GInputStream *stream,
719 GAsyncResult *result,
722 GSimpleAsyncResult *simple;
723 GInputStreamClass *class;
725 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
726 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
728 if (G_IS_SIMPLE_ASYNC_RESULT (result))
730 simple = G_SIMPLE_ASYNC_RESULT (result);
731 if (g_simple_async_result_propagate_error (simple, error))
734 /* Special case skip of 0 bytes */
735 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
739 class = G_INPUT_STREAM_GET_CLASS (stream);
740 return class->skip_finish (stream, result, error);
744 * g_input_stream_close_async:
745 * @stream: A #GInputStream.
746 * @io_priority: the <link linkend="io-priority">I/O priority</link>
748 * @cancellable: optional cancellable object
749 * @callback: callback to call when the request is satisfied
750 * @user_data: the data to pass to callback function
752 * Requests an asynchronous closes of the stream, releasing resources related to it.
753 * When the operation is finished @callback will be called.
754 * You can then call g_input_stream_close_finish() to get the result of the
757 * For behaviour details see g_input_stream_close().
759 * The asyncronous methods have a default fallback that uses threads to implement
760 * asynchronicity, so they are optional for inheriting classes. However, if you
761 * override one you must override all.
764 g_input_stream_close_async (GInputStream *stream,
766 GCancellable *cancellable,
767 GAsyncReadyCallback callback,
770 GInputStreamClass *class;
771 GSimpleAsyncResult *simple;
772 GError *error = NULL;
774 g_return_if_fail (G_IS_INPUT_STREAM (stream));
776 if (stream->priv->closed)
778 simple = g_simple_async_result_new (G_OBJECT (stream),
781 g_input_stream_close_async);
783 g_simple_async_result_complete_in_idle (simple);
784 g_object_unref (simple);
788 if (!g_input_stream_set_pending (stream, &error))
790 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
794 g_error_free (error);
798 class = G_INPUT_STREAM_GET_CLASS (stream);
799 stream->priv->outstanding_callback = callback;
800 g_object_ref (stream);
801 class->close_async (stream, io_priority, cancellable,
802 async_ready_close_callback_wrapper, user_data);
806 * g_input_stream_close_finish:
807 * @stream: a #GInputStream.
808 * @result: a #GAsyncResult.
809 * @error: a #GError location to store the error occuring, or %NULL to
812 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
814 * Returns: %TRUE if the stream was closed successfully.
817 g_input_stream_close_finish (GInputStream *stream,
818 GAsyncResult *result,
821 GSimpleAsyncResult *simple;
822 GInputStreamClass *class;
824 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
825 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
827 if (G_IS_SIMPLE_ASYNC_RESULT (result))
829 simple = G_SIMPLE_ASYNC_RESULT (result);
830 if (g_simple_async_result_propagate_error (simple, error))
833 /* Special case already closed */
834 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
838 class = G_INPUT_STREAM_GET_CLASS (stream);
839 return class->close_finish (stream, result, error);
843 * g_input_stream_is_closed:
844 * @stream: input stream.
846 * Checks if an input stream is closed.
848 * Returns: %TRUE if the stream is closed.
851 g_input_stream_is_closed (GInputStream *stream)
853 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
855 return stream->priv->closed;
859 * g_input_stream_has_pending:
860 * @stream: input stream.
862 * Checks if an input stream has pending actions.
864 * Returns: %TRUE if @stream has pending actions.
867 g_input_stream_has_pending (GInputStream *stream)
869 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
871 return stream->priv->pending;
875 * g_input_stream_set_pending:
876 * @stream: input stream
877 * @error: a #GError location to store the error occuring, or %NULL to
880 * Sets @stream to have actions pending. If the pending flag is
881 * already set or @stream is closed, it will return %FALSE and set
884 * Return value: %TRUE if pending was previously unset and is now set.
887 g_input_stream_set_pending (GInputStream *stream, GError **error)
889 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
891 if (stream->priv->closed)
893 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
894 _("Stream is already closed"));
898 if (stream->priv->pending)
900 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
901 /* Translators: This is an error you get if there is already an
902 * operation running against this stream when you try to start
904 _("Stream has outstanding operation"));
908 stream->priv->pending = TRUE;
913 * g_input_stream_clear_pending:
914 * @stream: input stream
916 * Clears the pending flag on @stream.
919 g_input_stream_clear_pending (GInputStream *stream)
921 g_return_if_fail (G_IS_INPUT_STREAM (stream));
923 stream->priv->pending = FALSE;
926 /********************************************
927 * Default implementation of async ops *
928 ********************************************/
932 gsize count_requested;
937 read_async_thread (GSimpleAsyncResult *res,
939 GCancellable *cancellable)
942 GInputStreamClass *class;
943 GError *error = NULL;
945 op = g_simple_async_result_get_op_res_gpointer (res);
947 class = G_INPUT_STREAM_GET_CLASS (object);
949 op->count_read = class->read_fn (G_INPUT_STREAM (object),
950 op->buffer, op->count_requested,
951 cancellable, &error);
952 if (op->count_read == -1)
954 g_simple_async_result_set_from_error (res, error);
955 g_error_free (error);
960 g_input_stream_real_read_async (GInputStream *stream,
964 GCancellable *cancellable,
965 GAsyncReadyCallback callback,
968 GSimpleAsyncResult *res;
971 op = g_new (ReadData, 1);
972 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
973 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
975 op->count_requested = count;
977 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
978 g_object_unref (res);
982 g_input_stream_real_read_finish (GInputStream *stream,
983 GAsyncResult *result,
986 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
989 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
990 g_input_stream_real_read_async);
992 op = g_simple_async_result_get_op_res_gpointer (simple);
994 return op->count_read;
998 gsize count_requested;
999 gssize count_skipped;
1004 skip_async_thread (GSimpleAsyncResult *res,
1006 GCancellable *cancellable)
1009 GInputStreamClass *class;
1010 GError *error = NULL;
1012 class = G_INPUT_STREAM_GET_CLASS (object);
1013 op = g_simple_async_result_get_op_res_gpointer (res);
1014 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1015 op->count_requested,
1016 cancellable, &error);
1017 if (op->count_skipped == -1)
1019 g_simple_async_result_set_from_error (res, error);
1020 g_error_free (error);
1027 gsize count_skipped;
1029 GCancellable *cancellable;
1031 GAsyncReadyCallback callback;
1032 } SkipFallbackAsyncData;
1035 skip_callback_wrapper (GObject *source_object,
1039 GInputStreamClass *class;
1040 SkipFallbackAsyncData *data = user_data;
1042 GSimpleAsyncResult *simple;
1043 GError *error = NULL;
1046 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1051 data->count_skipped += ret;
1053 if (data->count > 0)
1055 class = G_INPUT_STREAM_GET_CLASS (source_object);
1056 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1057 skip_callback_wrapper, data);
1062 op = g_new0 (SkipData, 1);
1063 op->count_skipped = data->count_skipped;
1064 simple = g_simple_async_result_new (source_object,
1065 data->callback, data->user_data,
1066 g_input_stream_real_skip_async);
1068 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1072 if (data->count_skipped &&
1073 error->domain == G_IO_ERROR &&
1074 error->code == G_IO_ERROR_CANCELLED)
1075 { /* No error, return partial read */ }
1077 g_simple_async_result_set_from_error (simple, error);
1078 g_error_free (error);
1081 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1082 g_simple_async_result_complete (simple);
1083 g_object_unref (simple);
1089 g_input_stream_real_skip_async (GInputStream *stream,
1092 GCancellable *cancellable,
1093 GAsyncReadyCallback callback,
1096 GInputStreamClass *class;
1098 SkipFallbackAsyncData *data;
1099 GSimpleAsyncResult *res;
1101 class = G_INPUT_STREAM_GET_CLASS (stream);
1103 if (class->read_async == g_input_stream_real_read_async)
1105 /* Read is thread-using async fallback.
1106 * Make skip use threads too, so that we can use a possible sync skip
1107 * implementation. */
1108 op = g_new0 (SkipData, 1);
1110 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1111 g_input_stream_real_skip_async);
1113 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1115 op->count_requested = count;
1117 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1118 g_object_unref (res);
1122 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1124 /* There is a custom async read function, lets use that. */
1125 data = g_new (SkipFallbackAsyncData, 1);
1126 data->count = count;
1127 data->count_skipped = 0;
1128 data->io_prio = io_priority;
1129 data->cancellable = cancellable;
1130 data->callback = callback;
1131 data->user_data = user_data;
1132 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1133 skip_callback_wrapper, data);
1139 g_input_stream_real_skip_finish (GInputStream *stream,
1140 GAsyncResult *result,
1143 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1146 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1147 op = g_simple_async_result_get_op_res_gpointer (simple);
1148 return op->count_skipped;
1152 close_async_thread (GSimpleAsyncResult *res,
1154 GCancellable *cancellable)
1156 GInputStreamClass *class;
1157 GError *error = NULL;
1160 /* Auto handling of cancelation disabled, and ignore
1161 cancellation, since we want to close things anyway, although
1162 possibly in a quick-n-dirty way. At least we never want to leak
1165 class = G_INPUT_STREAM_GET_CLASS (object);
1166 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1169 g_simple_async_result_set_from_error (res, error);
1170 g_error_free (error);
1175 g_input_stream_real_close_async (GInputStream *stream,
1177 GCancellable *cancellable,
1178 GAsyncReadyCallback callback,
1181 GSimpleAsyncResult *res;
1183 res = g_simple_async_result_new (G_OBJECT (stream),
1186 g_input_stream_real_close_async);
1188 g_simple_async_result_set_handle_cancellation (res, FALSE);
1190 g_simple_async_result_run_in_thread (res,
1194 g_object_unref (res);
1198 g_input_stream_real_close_finish (GInputStream *stream,
1199 GAsyncResult *result,
1202 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1203 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1207 #define __G_INPUT_STREAM_C__
1208 #include "gioaliasdef.c"