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
41 G_DEFINE_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
43 struct _GInputStreamPrivate {
46 GAsyncReadyCallback outstanding_callback;
49 static gssize g_input_stream_real_skip (GInputStream *stream,
51 GCancellable *cancellable,
53 static void g_input_stream_real_read_async (GInputStream *stream,
57 GCancellable *cancellable,
58 GAsyncReadyCallback callback,
60 static gssize g_input_stream_real_read_finish (GInputStream *stream,
63 static void g_input_stream_real_skip_async (GInputStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
72 static void g_input_stream_real_close_async (GInputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
82 g_input_stream_finalize (GObject *object)
86 stream = G_INPUT_STREAM (object);
88 if (!stream->priv->closed)
89 g_input_stream_close (stream, NULL, NULL);
91 if (G_OBJECT_CLASS (g_input_stream_parent_class)->finalize)
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 if (G_OBJECT_CLASS (g_input_stream_parent_class)->dispose)
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: optional #GCancellable object, %NULL to ignore.
143 * @error: location to store the error occuring, 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
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 g_input_stream_read"));
189 class = G_INPUT_STREAM_GET_CLASS (stream);
191 if (class->read_fn == NULL)
193 g_set_error (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_push_current_cancellable (cancellable);
204 res = class->read_fn (stream, buffer, count, cancellable, error);
207 g_pop_current_cancellable (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: location to store the number of bytes that was read from the stream
220 * @cancellable: optional #GCancellable object, %NULL to ignore.
221 * @error: location to store the error occuring, 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 occured.
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_skip:
278 * @stream: a #GInputStream.
279 * @count: the number of bytes that will be skipped from the stream
280 * @cancellable: optional #GCancellable object, %NULL to ignore.
281 * @error: location to store the error occuring, or %NULL to ignore
283 * Tries to skip @count bytes from the stream. Will block during the operation.
285 * This is identical to g_input_stream_read(), from a behaviour standpoint,
286 * but the bytes that are skipped are not returned to the user. Some
287 * streams have an implementation that is more efficient than reading the data.
289 * This function is optional for inherited classes, as the default implementation
290 * emulates it using read.
292 * If @cancellable is not %NULL, then the operation can be cancelled by
293 * triggering the cancellable object from another thread. If the operation
294 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
295 * operation was partially finished when the operation was cancelled the
296 * partial result will be returned, without an error.
298 * Return value: Number of bytes skipped, or -1 on error
301 g_input_stream_skip (GInputStream *stream,
303 GCancellable *cancellable,
306 GInputStreamClass *class;
309 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
314 if (((gssize) count) < 0)
316 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
317 _("Too large count value passed to g_input_stream_skip"));
321 class = G_INPUT_STREAM_GET_CLASS (stream);
323 if (!g_input_stream_set_pending (stream, error))
327 g_push_current_cancellable (cancellable);
329 res = class->skip (stream, count, cancellable, error);
332 g_pop_current_cancellable (cancellable);
334 g_input_stream_clear_pending (stream);
340 g_input_stream_real_skip (GInputStream *stream,
342 GCancellable *cancellable,
345 GInputStreamClass *class;
346 gssize ret, read_bytes;
350 class = G_INPUT_STREAM_GET_CLASS (stream);
352 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
354 if (g_seekable_seek (G_SEEKABLE (stream),
362 /* If not seekable, or seek failed, fall back to reading data: */
364 class = G_INPUT_STREAM_GET_CLASS (stream);
371 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
372 cancellable, &my_error);
375 if (read_bytes > 0 &&
376 my_error->domain == G_IO_ERROR &&
377 my_error->code == G_IO_ERROR_CANCELLED)
379 g_error_free (my_error);
383 g_propagate_error (error, my_error);
390 if (ret == 0 || count == 0)
396 * g_input_stream_close:
397 * @stream: A #GInputStream.
398 * @cancellable: optional #GCancellable object, %NULL to ignore.
399 * @error: location to store the error occuring, or %NULL to ignore
401 * Closes the stream, releasing resources related to it.
403 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
404 * Closing a stream multiple times will not return an error.
406 * Streams will be automatically closed when the last reference
407 * is dropped, but you might want to call make sure resources
408 * are released as early as possible.
410 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
411 * open after the stream is closed. See the documentation for the individual
412 * stream for details.
414 * On failure the first error that happened will be reported, but the close
415 * operation will finish as much as possible. A stream that failed to
416 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
417 * is important to check and report the error to the user.
419 * If @cancellable is not NULL, then the operation can be cancelled by
420 * triggering the cancellable object from another thread. If the operation
421 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
422 * Cancelling a close will still leave the stream closed, but some streams
423 * can use a faster close that doesn't block to e.g. check errors.
425 * Return value: %TRUE on success, %FALSE on failure
428 g_input_stream_close (GInputStream *stream,
429 GCancellable *cancellable,
432 GInputStreamClass *class;
435 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
437 class = G_INPUT_STREAM_GET_CLASS (stream);
439 if (stream->priv->closed)
444 if (!g_input_stream_set_pending (stream, error))
448 g_push_current_cancellable (cancellable);
451 res = class->close_fn (stream, cancellable, error);
454 g_pop_current_cancellable (cancellable);
456 g_input_stream_clear_pending (stream);
458 stream->priv->closed = TRUE;
464 async_ready_callback_wrapper (GObject *source_object,
468 GInputStream *stream = G_INPUT_STREAM (source_object);
470 g_input_stream_clear_pending (stream);
471 if (stream->priv->outstanding_callback)
472 (*stream->priv->outstanding_callback) (source_object, res, user_data);
473 g_object_unref (stream);
477 async_ready_close_callback_wrapper (GObject *source_object,
481 GInputStream *stream = G_INPUT_STREAM (source_object);
483 g_input_stream_clear_pending (stream);
484 stream->priv->closed = TRUE;
485 if (stream->priv->outstanding_callback)
486 (*stream->priv->outstanding_callback) (source_object, res, user_data);
487 g_object_unref (stream);
491 * g_input_stream_read_async:
492 * @stream: A #GInputStream.
493 * @buffer: a buffer to read data into (which should be at least count bytes long).
494 * @count: the number of bytes that will be read from the stream
495 * @io_priority: the <link linkend="io-priority">I/O priority</link>
497 * @cancellable: optional #GCancellable object, %NULL to ignore.
498 * @callback: callback to call when the request is satisfied
499 * @user_data: the data to pass to callback function
501 * Request an asynchronous read of @count bytes from the stream into the buffer
502 * starting at @buffer. When the operation is finished @callback will be called,
503 * giving the results.
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 g_input_stream_read_async"));
561 if (!g_input_stream_set_pending (stream, &error))
563 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
567 g_error_free (error);
571 class = G_INPUT_STREAM_GET_CLASS (stream);
572 stream->priv->outstanding_callback = callback;
573 g_object_ref (stream);
574 class->read_async (stream, buffer, count, io_priority, cancellable,
575 async_ready_callback_wrapper, user_data);
579 * g_input_stream_read_finish:
580 * @stream: a #GInputStream.
581 * @result: a #GAsyncResult.
582 * @error: a #GError location to store the error occuring, or %NULL to
585 * Finishes an asynchronous stream read operation.
587 * Returns: number of bytes read in, or -1 on error.
590 g_input_stream_read_finish (GInputStream *stream,
591 GAsyncResult *result,
594 GSimpleAsyncResult *simple;
595 GInputStreamClass *class;
597 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
598 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
600 if (G_IS_SIMPLE_ASYNC_RESULT (result))
602 simple = G_SIMPLE_ASYNC_RESULT (result);
603 if (g_simple_async_result_propagate_error (simple, error))
606 /* Special case read of 0 bytes */
607 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
611 class = G_INPUT_STREAM_GET_CLASS (stream);
612 return class->read_finish (stream, result, error);
616 * g_input_stream_skip_async:
617 * @stream: A #GInputStream.
618 * @count: the number of bytes that will be skipped from the stream
619 * @io_priority: the <link linkend="io-priority">I/O priority</link>
621 * @cancellable: optional #GCancellable object, %NULL to ignore.
622 * @callback: callback to call when the request is satisfied
623 * @user_data: the data to pass to callback function
625 * Request an asynchronous skip of @count bytes from the stream into the buffer
626 * starting at @buffer. When the operation is finished @callback will be called,
627 * giving the results.
629 * During an async request no other sync and async calls are allowed, and will
630 * result in %G_IO_ERROR_PENDING errors.
632 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
634 * On success, the number of bytes skipped will be passed to the
635 * callback. It is not an error if this is not the same as the requested size, as it
636 * can happen e.g. near the end of a file, but generally we try to skip
637 * as many bytes as requested. Zero is returned on end of file
638 * (or if @count is zero), but never otherwise.
640 * Any outstanding i/o request with higher priority (lower numerical value) will
641 * be executed before an outstanding request with lower priority. Default
642 * priority is %G_PRIORITY_DEFAULT.
644 * The asyncronous methods have a default fallback that uses threads to implement
645 * asynchronicity, so they are optional for inheriting classes. However, if you
646 * override one you must override all.
649 g_input_stream_skip_async (GInputStream *stream,
652 GCancellable *cancellable,
653 GAsyncReadyCallback callback,
656 GInputStreamClass *class;
657 GSimpleAsyncResult *simple;
658 GError *error = NULL;
660 g_return_if_fail (G_IS_INPUT_STREAM (stream));
664 simple = g_simple_async_result_new (G_OBJECT (stream),
667 g_input_stream_skip_async);
669 g_simple_async_result_complete_in_idle (simple);
670 g_object_unref (simple);
674 if (((gssize) count) < 0)
676 g_simple_async_report_error_in_idle (G_OBJECT (stream),
679 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
680 _("Too large count value passed to g_input_stream_skip_async"));
684 if (!g_input_stream_set_pending (stream, &error))
686 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
690 g_error_free (error);
694 class = G_INPUT_STREAM_GET_CLASS (stream);
695 stream->priv->outstanding_callback = callback;
696 g_object_ref (stream);
697 class->skip_async (stream, count, io_priority, cancellable,
698 async_ready_callback_wrapper, user_data);
702 * g_input_stream_skip_finish:
703 * @stream: a #GInputStream.
704 * @result: a #GAsyncResult.
705 * @error: a #GError location to store the error occuring, or %NULL to
708 * Finishes a stream skip operation.
710 * Returns: the size of the bytes skipped, or %-1 on error.
713 g_input_stream_skip_finish (GInputStream *stream,
714 GAsyncResult *result,
717 GSimpleAsyncResult *simple;
718 GInputStreamClass *class;
720 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
721 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
723 if (G_IS_SIMPLE_ASYNC_RESULT (result))
725 simple = G_SIMPLE_ASYNC_RESULT (result);
726 if (g_simple_async_result_propagate_error (simple, error))
729 /* Special case skip of 0 bytes */
730 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
734 class = G_INPUT_STREAM_GET_CLASS (stream);
735 return class->skip_finish (stream, result, error);
739 * g_input_stream_close_async:
740 * @stream: A #GInputStream.
741 * @io_priority: the <link linkend="io-priority">I/O priority</link>
743 * @cancellable: optional cancellable object
744 * @callback: callback to call when the request is satisfied
745 * @user_data: the data to pass to callback function
747 * Requests an asynchronous closes of the stream, releasing resources related to it.
748 * When the operation is finished @callback will be called, giving the results.
750 * For behaviour details see g_input_stream_close().
752 * The asyncronous methods have a default fallback that uses threads to implement
753 * asynchronicity, so they are optional for inheriting classes. However, if you
754 * override one you must override all.
757 g_input_stream_close_async (GInputStream *stream,
759 GCancellable *cancellable,
760 GAsyncReadyCallback callback,
763 GInputStreamClass *class;
764 GSimpleAsyncResult *simple;
765 GError *error = NULL;
767 g_return_if_fail (G_IS_INPUT_STREAM (stream));
769 if (stream->priv->closed)
771 simple = g_simple_async_result_new (G_OBJECT (stream),
774 g_input_stream_close_async);
776 g_simple_async_result_complete_in_idle (simple);
777 g_object_unref (simple);
781 if (!g_input_stream_set_pending (stream, &error))
783 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
787 g_error_free (error);
791 class = G_INPUT_STREAM_GET_CLASS (stream);
792 stream->priv->outstanding_callback = callback;
793 g_object_ref (stream);
794 class->close_async (stream, io_priority, cancellable,
795 async_ready_close_callback_wrapper, user_data);
799 * g_input_stream_close_finish:
800 * @stream: a #GInputStream.
801 * @result: a #GAsyncResult.
802 * @error: a #GError location to store the error occuring, or %NULL to
805 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
807 * Returns: %TRUE if the stream was closed successfully.
810 g_input_stream_close_finish (GInputStream *stream,
811 GAsyncResult *result,
814 GSimpleAsyncResult *simple;
815 GInputStreamClass *class;
817 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
818 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
820 if (G_IS_SIMPLE_ASYNC_RESULT (result))
822 simple = G_SIMPLE_ASYNC_RESULT (result);
823 if (g_simple_async_result_propagate_error (simple, error))
826 /* Special case already closed */
827 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
831 class = G_INPUT_STREAM_GET_CLASS (stream);
832 return class->close_finish (stream, result, error);
836 * g_input_stream_is_closed:
837 * @stream: input stream.
839 * Checks if an input stream is closed.
841 * Returns: %TRUE if the stream is closed.
844 g_input_stream_is_closed (GInputStream *stream)
846 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
848 return stream->priv->closed;
852 * g_input_stream_has_pending:
853 * @stream: input stream.
855 * Checks if an input stream has pending actions.
857 * Returns: %TRUE if @stream has pending actions.
860 g_input_stream_has_pending (GInputStream *stream)
862 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
864 return stream->priv->pending;
868 * g_input_stream_set_pending:
869 * @stream: input stream
870 * @error: a #GError location to store the error occuring, or %NULL to
873 * Sets @stream to have actions pending. If the pending flag is
874 * already set or @stream is closed, it will return %FALSE and set
877 * Return value: %TRUE if pending was previously unset and is now set.
880 g_input_stream_set_pending (GInputStream *stream, GError **error)
882 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
884 if (stream->priv->closed)
886 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
887 _("Stream is already closed"));
891 if (stream->priv->pending)
893 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
894 _("Stream has outstanding operation"));
898 stream->priv->pending = TRUE;
903 * g_input_stream_clear_pending:
904 * @stream: input stream
906 * Clears the pending flag on @stream.
909 g_input_stream_clear_pending (GInputStream *stream)
911 g_return_if_fail (G_IS_INPUT_STREAM (stream));
913 stream->priv->pending = FALSE;
916 /********************************************
917 * Default implementation of async ops *
918 ********************************************/
922 gsize count_requested;
927 read_async_thread (GSimpleAsyncResult *res,
929 GCancellable *cancellable)
932 GInputStreamClass *class;
933 GError *error = NULL;
935 op = g_simple_async_result_get_op_res_gpointer (res);
937 class = G_INPUT_STREAM_GET_CLASS (object);
939 op->count_read = class->read_fn (G_INPUT_STREAM (object),
940 op->buffer, op->count_requested,
941 cancellable, &error);
942 if (op->count_read == -1)
944 g_simple_async_result_set_from_error (res, error);
945 g_error_free (error);
950 g_input_stream_real_read_async (GInputStream *stream,
954 GCancellable *cancellable,
955 GAsyncReadyCallback callback,
958 GSimpleAsyncResult *res;
961 op = g_new (ReadData, 1);
962 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
963 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
965 op->count_requested = count;
967 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
968 g_object_unref (res);
972 g_input_stream_real_read_finish (GInputStream *stream,
973 GAsyncResult *result,
976 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
979 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
980 g_input_stream_real_read_async);
982 op = g_simple_async_result_get_op_res_gpointer (simple);
984 return op->count_read;
988 gsize count_requested;
989 gssize count_skipped;
994 skip_async_thread (GSimpleAsyncResult *res,
996 GCancellable *cancellable)
999 GInputStreamClass *class;
1000 GError *error = NULL;
1002 class = G_INPUT_STREAM_GET_CLASS (object);
1003 op = g_simple_async_result_get_op_res_gpointer (res);
1004 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1005 op->count_requested,
1006 cancellable, &error);
1007 if (op->count_skipped == -1)
1009 g_simple_async_result_set_from_error (res, error);
1010 g_error_free (error);
1017 gsize count_skipped;
1019 GCancellable *cancellable;
1021 GAsyncReadyCallback callback;
1022 } SkipFallbackAsyncData;
1025 skip_callback_wrapper (GObject *source_object,
1029 GInputStreamClass *class;
1030 SkipFallbackAsyncData *data = user_data;
1032 GSimpleAsyncResult *simple;
1033 GError *error = NULL;
1036 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1041 data->count_skipped += ret;
1043 if (data->count > 0)
1045 class = G_INPUT_STREAM_GET_CLASS (source_object);
1046 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1047 skip_callback_wrapper, data);
1052 op = g_new0 (SkipData, 1);
1053 op->count_skipped = data->count_skipped;
1054 simple = g_simple_async_result_new (source_object,
1055 data->callback, data->user_data,
1056 g_input_stream_real_skip_async);
1058 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1062 if (data->count_skipped &&
1063 error->domain == G_IO_ERROR &&
1064 error->code == G_IO_ERROR_CANCELLED)
1065 { /* No error, return partial read */ }
1067 g_simple_async_result_set_from_error (simple, error);
1068 g_error_free (error);
1071 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1072 g_simple_async_result_complete (simple);
1073 g_object_unref (simple);
1079 g_input_stream_real_skip_async (GInputStream *stream,
1082 GCancellable *cancellable,
1083 GAsyncReadyCallback callback,
1086 GInputStreamClass *class;
1088 SkipFallbackAsyncData *data;
1089 GSimpleAsyncResult *res;
1091 class = G_INPUT_STREAM_GET_CLASS (stream);
1093 if (class->read_async == g_input_stream_real_read_async)
1095 /* Read is thread-using async fallback.
1096 * Make skip use threads too, so that we can use a possible sync skip
1097 * implementation. */
1098 op = g_new0 (SkipData, 1);
1100 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1101 g_input_stream_real_skip_async);
1103 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1105 op->count_requested = count;
1107 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1108 g_object_unref (res);
1112 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1114 /* There is a custom async read function, lets use that. */
1115 data = g_new (SkipFallbackAsyncData, 1);
1116 data->count = count;
1117 data->count_skipped = 0;
1118 data->io_prio = io_priority;
1119 data->cancellable = cancellable;
1120 data->callback = callback;
1121 data->user_data = user_data;
1122 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1123 skip_callback_wrapper, data);
1129 g_input_stream_real_skip_finish (GInputStream *stream,
1130 GAsyncResult *result,
1133 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1136 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1137 op = g_simple_async_result_get_op_res_gpointer (simple);
1138 return op->count_skipped;
1142 close_async_thread (GSimpleAsyncResult *res,
1144 GCancellable *cancellable)
1146 GInputStreamClass *class;
1147 GError *error = NULL;
1150 /* Auto handling of cancelation disabled, and ignore
1151 cancellation, since we want to close things anyway, although
1152 possibly in a quick-n-dirty way. At least we never want to leak
1155 class = G_INPUT_STREAM_GET_CLASS (object);
1156 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1159 g_simple_async_result_set_from_error (res, error);
1160 g_error_free (error);
1165 g_input_stream_real_close_async (GInputStream *stream,
1167 GCancellable *cancellable,
1168 GAsyncReadyCallback callback,
1171 GSimpleAsyncResult *res;
1173 res = g_simple_async_result_new (G_OBJECT (stream),
1176 g_input_stream_real_close_async);
1178 g_simple_async_result_set_handle_cancellation (res, FALSE);
1180 g_simple_async_result_run_in_thread (res,
1184 g_object_unref (res);
1188 g_input_stream_real_close_finish (GInputStream *stream,
1189 GAsyncResult *result,
1192 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1193 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1197 #define __G_INPUT_STREAM_C__
1198 #include "gioaliasdef.c"