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"),
187 G_GNUC_PRETTY_FUNCTION);
191 class = G_INPUT_STREAM_GET_CLASS (stream);
193 if (class->read_fn == NULL)
195 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
196 _("Input stream doesn't implement read"));
200 if (!g_input_stream_set_pending (stream, error))
204 g_cancellable_push_current (cancellable);
206 res = class->read_fn (stream, buffer, count, cancellable, error);
209 g_cancellable_pop_current (cancellable);
211 g_input_stream_clear_pending (stream);
217 * g_input_stream_read_all:
218 * @stream: a #GInputStream.
219 * @buffer: a buffer to read data into (which should be at least count bytes long).
220 * @count: the number of bytes that will be read from the stream
221 * @bytes_read: location to store the number of bytes that was read from the stream
222 * @cancellable: optional #GCancellable object, %NULL to ignore.
223 * @error: location to store the error occuring, or %NULL to ignore
225 * Tries to read @count bytes from the stream into the buffer starting at
226 * @buffer. Will block during this read.
228 * This function is similar to g_input_stream_read(), except it tries to
229 * read as many bytes as requested, only stopping on an error or end of stream.
231 * On a successful read of @count bytes, or if we reached the end of the
232 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
235 * If there is an error during the operation %FALSE is returned and @error
236 * is set to indicate the error status, @bytes_read is updated to contain
237 * the number of bytes read into @buffer before the error occurred.
239 * Return value: %TRUE on success, %FALSE if there was an error
242 g_input_stream_read_all (GInputStream *stream,
246 GCancellable *cancellable,
252 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
253 g_return_val_if_fail (buffer != NULL, FALSE);
256 while (_bytes_read < count)
258 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
263 *bytes_read = _bytes_read;
274 *bytes_read = _bytes_read;
279 * g_input_stream_skip:
280 * @stream: a #GInputStream.
281 * @count: the number of bytes that will be skipped from the stream
282 * @cancellable: optional #GCancellable object, %NULL to ignore.
283 * @error: location to store the error occuring, or %NULL to ignore
285 * Tries to skip @count bytes from the stream. Will block during the operation.
287 * This is identical to g_input_stream_read(), from a behaviour standpoint,
288 * but the bytes that are skipped are not returned to the user. Some
289 * streams have an implementation that is more efficient than reading the data.
291 * This function is optional for inherited classes, as the default implementation
292 * emulates it using read.
294 * If @cancellable is not %NULL, then the operation can be cancelled by
295 * triggering the cancellable object from another thread. If the operation
296 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
297 * operation was partially finished when the operation was cancelled the
298 * partial result will be returned, without an error.
300 * Return value: Number of bytes skipped, or -1 on error
303 g_input_stream_skip (GInputStream *stream,
305 GCancellable *cancellable,
308 GInputStreamClass *class;
311 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
316 if (((gssize) count) < 0)
318 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
319 _("Too large count value passed to %s"),
320 G_GNUC_PRETTY_FUNCTION);
324 class = G_INPUT_STREAM_GET_CLASS (stream);
326 if (!g_input_stream_set_pending (stream, error))
330 g_cancellable_push_current (cancellable);
332 res = class->skip (stream, count, cancellable, error);
335 g_cancellable_pop_current (cancellable);
337 g_input_stream_clear_pending (stream);
343 g_input_stream_real_skip (GInputStream *stream,
345 GCancellable *cancellable,
348 GInputStreamClass *class;
349 gssize ret, read_bytes;
353 class = G_INPUT_STREAM_GET_CLASS (stream);
355 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
357 if (g_seekable_seek (G_SEEKABLE (stream),
365 /* If not seekable, or seek failed, fall back to reading data: */
367 class = G_INPUT_STREAM_GET_CLASS (stream);
374 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
375 cancellable, &my_error);
378 if (read_bytes > 0 &&
379 my_error->domain == G_IO_ERROR &&
380 my_error->code == G_IO_ERROR_CANCELLED)
382 g_error_free (my_error);
386 g_propagate_error (error, my_error);
393 if (ret == 0 || count == 0)
399 * g_input_stream_close:
400 * @stream: A #GInputStream.
401 * @cancellable: optional #GCancellable object, %NULL to ignore.
402 * @error: location to store the error occuring, or %NULL to ignore
404 * Closes the stream, releasing resources related to it.
406 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
407 * Closing a stream multiple times will not return an error.
409 * Streams will be automatically closed when the last reference
410 * is dropped, but you might want to call this function to make sure
411 * resources are released as early as possible.
413 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
414 * open after the stream is closed. See the documentation for the individual
415 * stream for details.
417 * On failure the first error that happened will be reported, but the close
418 * operation will finish as much as possible. A stream that failed to
419 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
420 * is important to check and report the error to the user.
422 * If @cancellable is not NULL, then the operation can be cancelled by
423 * triggering the cancellable object from another thread. If the operation
424 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
425 * Cancelling a close will still leave the stream closed, but some streams
426 * can use a faster close that doesn't block to e.g. check errors.
428 * Return value: %TRUE on success, %FALSE on failure
431 g_input_stream_close (GInputStream *stream,
432 GCancellable *cancellable,
435 GInputStreamClass *class;
438 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
440 class = G_INPUT_STREAM_GET_CLASS (stream);
442 if (stream->priv->closed)
447 if (!g_input_stream_set_pending (stream, error))
451 g_cancellable_push_current (cancellable);
454 res = class->close_fn (stream, cancellable, error);
457 g_cancellable_pop_current (cancellable);
459 g_input_stream_clear_pending (stream);
461 stream->priv->closed = TRUE;
467 async_ready_callback_wrapper (GObject *source_object,
471 GInputStream *stream = G_INPUT_STREAM (source_object);
473 g_input_stream_clear_pending (stream);
474 if (stream->priv->outstanding_callback)
475 (*stream->priv->outstanding_callback) (source_object, res, user_data);
476 g_object_unref (stream);
480 async_ready_close_callback_wrapper (GObject *source_object,
484 GInputStream *stream = G_INPUT_STREAM (source_object);
486 g_input_stream_clear_pending (stream);
487 stream->priv->closed = TRUE;
488 if (stream->priv->outstanding_callback)
489 (*stream->priv->outstanding_callback) (source_object, res, user_data);
490 g_object_unref (stream);
494 * g_input_stream_read_async:
495 * @stream: A #GInputStream.
496 * @buffer: a buffer to read data into (which should be at least count bytes long).
497 * @count: the number of bytes that will be read from the stream
498 * @io_priority: the <link linkend="io-priority">I/O priority</link>
500 * @cancellable: optional #GCancellable object, %NULL to ignore.
501 * @callback: callback to call when the request is satisfied
502 * @user_data: the data to pass to callback function
504 * Request an asynchronous read of @count bytes from the stream into the buffer
505 * starting at @buffer. When the operation is finished @callback will be called.
506 * You can then call g_input_stream_read_finish() to get the result of the
509 * During an async request no other sync and async calls are allowed, and will
510 * result in %G_IO_ERROR_PENDING errors.
512 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
514 * On success, the number of bytes read into the buffer will be passed to the
515 * callback. It is not an error if this is not the same as the requested size, as it
516 * can happen e.g. near the end of a file, but generally we try to read
517 * as many bytes as requested. Zero is returned on end of file
518 * (or if @count is zero), but never otherwise.
520 * Any outstanding i/o request with higher priority (lower numerical value) will
521 * be executed before an outstanding request with lower priority. Default
522 * priority is %G_PRIORITY_DEFAULT.
524 * The asyncronous methods have a default fallback that uses threads to implement
525 * asynchronicity, so they are optional for inheriting classes. However, if you
526 * override one you must override all.
529 g_input_stream_read_async (GInputStream *stream,
533 GCancellable *cancellable,
534 GAsyncReadyCallback callback,
537 GInputStreamClass *class;
538 GSimpleAsyncResult *simple;
539 GError *error = NULL;
541 g_return_if_fail (G_IS_INPUT_STREAM (stream));
542 g_return_if_fail (buffer != NULL);
546 simple = g_simple_async_result_new (G_OBJECT (stream),
549 g_input_stream_read_async);
550 g_simple_async_result_complete_in_idle (simple);
551 g_object_unref (simple);
555 if (((gssize) count) < 0)
557 g_simple_async_report_error_in_idle (G_OBJECT (stream),
560 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
561 _("Too large count value passed to %s"),
562 G_GNUC_PRETTY_FUNCTION);
566 if (!g_input_stream_set_pending (stream, &error))
568 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
572 g_error_free (error);
576 class = G_INPUT_STREAM_GET_CLASS (stream);
577 stream->priv->outstanding_callback = callback;
578 g_object_ref (stream);
579 class->read_async (stream, buffer, count, io_priority, cancellable,
580 async_ready_callback_wrapper, user_data);
584 * g_input_stream_read_finish:
585 * @stream: a #GInputStream.
586 * @result: a #GAsyncResult.
587 * @error: a #GError location to store the error occuring, or %NULL to
590 * Finishes an asynchronous stream read operation.
592 * Returns: number of bytes read in, or -1 on error.
595 g_input_stream_read_finish (GInputStream *stream,
596 GAsyncResult *result,
599 GSimpleAsyncResult *simple;
600 GInputStreamClass *class;
602 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
603 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
605 if (G_IS_SIMPLE_ASYNC_RESULT (result))
607 simple = G_SIMPLE_ASYNC_RESULT (result);
608 if (g_simple_async_result_propagate_error (simple, error))
611 /* Special case read of 0 bytes */
612 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
616 class = G_INPUT_STREAM_GET_CLASS (stream);
617 return class->read_finish (stream, result, error);
621 * g_input_stream_skip_async:
622 * @stream: A #GInputStream.
623 * @count: the number of bytes that will be skipped from the stream
624 * @io_priority: the <link linkend="io-priority">I/O priority</link>
626 * @cancellable: optional #GCancellable object, %NULL to ignore.
627 * @callback: callback to call when the request is satisfied
628 * @user_data: the data to pass to callback function
630 * Request an asynchronous skip of @count bytes from the stream into the buffer
631 * starting at @buffer. When the operation is finished @callback will be called.
632 * You can then call g_input_stream_skip_finish() to get the result of the
635 * During an async request no other sync and async calls are allowed, and will
636 * result in %G_IO_ERROR_PENDING errors.
638 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
640 * On success, the number of bytes skipped will be passed to the
641 * callback. It is not an error if this is not the same as the requested size, as it
642 * can happen e.g. near the end of a file, but generally we try to skip
643 * as many bytes as requested. Zero is returned on end of file
644 * (or if @count is zero), but never otherwise.
646 * Any outstanding i/o request with higher priority (lower numerical value) will
647 * be executed before an outstanding request with lower priority. Default
648 * priority is %G_PRIORITY_DEFAULT.
650 * The asyncronous methods have a default fallback that uses threads to implement
651 * asynchronicity, so they are optional for inheriting classes. However, if you
652 * override one you must override all.
655 g_input_stream_skip_async (GInputStream *stream,
658 GCancellable *cancellable,
659 GAsyncReadyCallback callback,
662 GInputStreamClass *class;
663 GSimpleAsyncResult *simple;
664 GError *error = NULL;
666 g_return_if_fail (G_IS_INPUT_STREAM (stream));
670 simple = g_simple_async_result_new (G_OBJECT (stream),
673 g_input_stream_skip_async);
675 g_simple_async_result_complete_in_idle (simple);
676 g_object_unref (simple);
680 if (((gssize) count) < 0)
682 g_simple_async_report_error_in_idle (G_OBJECT (stream),
685 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
686 _("Too large count value passed to %s"),
687 G_GNUC_PRETTY_FUNCTION);
691 if (!g_input_stream_set_pending (stream, &error))
693 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
697 g_error_free (error);
701 class = G_INPUT_STREAM_GET_CLASS (stream);
702 stream->priv->outstanding_callback = callback;
703 g_object_ref (stream);
704 class->skip_async (stream, count, io_priority, cancellable,
705 async_ready_callback_wrapper, user_data);
709 * g_input_stream_skip_finish:
710 * @stream: a #GInputStream.
711 * @result: a #GAsyncResult.
712 * @error: a #GError location to store the error occuring, or %NULL to
715 * Finishes a stream skip operation.
717 * Returns: the size of the bytes skipped, or %-1 on error.
720 g_input_stream_skip_finish (GInputStream *stream,
721 GAsyncResult *result,
724 GSimpleAsyncResult *simple;
725 GInputStreamClass *class;
727 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
728 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
730 if (G_IS_SIMPLE_ASYNC_RESULT (result))
732 simple = G_SIMPLE_ASYNC_RESULT (result);
733 if (g_simple_async_result_propagate_error (simple, error))
736 /* Special case skip of 0 bytes */
737 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
741 class = G_INPUT_STREAM_GET_CLASS (stream);
742 return class->skip_finish (stream, result, error);
746 * g_input_stream_close_async:
747 * @stream: A #GInputStream.
748 * @io_priority: the <link linkend="io-priority">I/O priority</link>
750 * @cancellable: optional cancellable object
751 * @callback: callback to call when the request is satisfied
752 * @user_data: the data to pass to callback function
754 * Requests an asynchronous closes of the stream, releasing resources related to it.
755 * When the operation is finished @callback will be called.
756 * You can then call g_input_stream_close_finish() to get the result of the
759 * For behaviour details see g_input_stream_close().
761 * The asyncronous methods have a default fallback that uses threads to implement
762 * asynchronicity, so they are optional for inheriting classes. However, if you
763 * override one you must override all.
766 g_input_stream_close_async (GInputStream *stream,
768 GCancellable *cancellable,
769 GAsyncReadyCallback callback,
772 GInputStreamClass *class;
773 GSimpleAsyncResult *simple;
774 GError *error = NULL;
776 g_return_if_fail (G_IS_INPUT_STREAM (stream));
778 if (stream->priv->closed)
780 simple = g_simple_async_result_new (G_OBJECT (stream),
783 g_input_stream_close_async);
785 g_simple_async_result_complete_in_idle (simple);
786 g_object_unref (simple);
790 if (!g_input_stream_set_pending (stream, &error))
792 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
796 g_error_free (error);
800 class = G_INPUT_STREAM_GET_CLASS (stream);
801 stream->priv->outstanding_callback = callback;
802 g_object_ref (stream);
803 class->close_async (stream, io_priority, cancellable,
804 async_ready_close_callback_wrapper, user_data);
808 * g_input_stream_close_finish:
809 * @stream: a #GInputStream.
810 * @result: a #GAsyncResult.
811 * @error: a #GError location to store the error occuring, or %NULL to
814 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
816 * Returns: %TRUE if the stream was closed successfully.
819 g_input_stream_close_finish (GInputStream *stream,
820 GAsyncResult *result,
823 GSimpleAsyncResult *simple;
824 GInputStreamClass *class;
826 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
827 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
829 if (G_IS_SIMPLE_ASYNC_RESULT (result))
831 simple = G_SIMPLE_ASYNC_RESULT (result);
832 if (g_simple_async_result_propagate_error (simple, error))
835 /* Special case already closed */
836 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
840 class = G_INPUT_STREAM_GET_CLASS (stream);
841 return class->close_finish (stream, result, error);
845 * g_input_stream_is_closed:
846 * @stream: input stream.
848 * Checks if an input stream is closed.
850 * Returns: %TRUE if the stream is closed.
853 g_input_stream_is_closed (GInputStream *stream)
855 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
857 return stream->priv->closed;
861 * g_input_stream_has_pending:
862 * @stream: input stream.
864 * Checks if an input stream has pending actions.
866 * Returns: %TRUE if @stream has pending actions.
869 g_input_stream_has_pending (GInputStream *stream)
871 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
873 return stream->priv->pending;
877 * g_input_stream_set_pending:
878 * @stream: input stream
879 * @error: a #GError location to store the error occuring, or %NULL to
882 * Sets @stream to have actions pending. If the pending flag is
883 * already set or @stream is closed, it will return %FALSE and set
886 * Return value: %TRUE if pending was previously unset and is now set.
889 g_input_stream_set_pending (GInputStream *stream, GError **error)
891 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
893 if (stream->priv->closed)
895 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
896 _("Stream is already closed"));
900 if (stream->priv->pending)
902 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
903 _("Stream has outstanding operation"));
907 stream->priv->pending = TRUE;
912 * g_input_stream_clear_pending:
913 * @stream: input stream
915 * Clears the pending flag on @stream.
918 g_input_stream_clear_pending (GInputStream *stream)
920 g_return_if_fail (G_IS_INPUT_STREAM (stream));
922 stream->priv->pending = FALSE;
925 /********************************************
926 * Default implementation of async ops *
927 ********************************************/
931 gsize count_requested;
936 read_async_thread (GSimpleAsyncResult *res,
938 GCancellable *cancellable)
941 GInputStreamClass *class;
942 GError *error = NULL;
944 op = g_simple_async_result_get_op_res_gpointer (res);
946 class = G_INPUT_STREAM_GET_CLASS (object);
948 op->count_read = class->read_fn (G_INPUT_STREAM (object),
949 op->buffer, op->count_requested,
950 cancellable, &error);
951 if (op->count_read == -1)
953 g_simple_async_result_set_from_error (res, error);
954 g_error_free (error);
959 g_input_stream_real_read_async (GInputStream *stream,
963 GCancellable *cancellable,
964 GAsyncReadyCallback callback,
967 GSimpleAsyncResult *res;
970 op = g_new (ReadData, 1);
971 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
972 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
974 op->count_requested = count;
976 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
977 g_object_unref (res);
981 g_input_stream_real_read_finish (GInputStream *stream,
982 GAsyncResult *result,
985 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
988 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
989 g_input_stream_real_read_async);
991 op = g_simple_async_result_get_op_res_gpointer (simple);
993 return op->count_read;
997 gsize count_requested;
998 gssize count_skipped;
1003 skip_async_thread (GSimpleAsyncResult *res,
1005 GCancellable *cancellable)
1008 GInputStreamClass *class;
1009 GError *error = NULL;
1011 class = G_INPUT_STREAM_GET_CLASS (object);
1012 op = g_simple_async_result_get_op_res_gpointer (res);
1013 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1014 op->count_requested,
1015 cancellable, &error);
1016 if (op->count_skipped == -1)
1018 g_simple_async_result_set_from_error (res, error);
1019 g_error_free (error);
1026 gsize count_skipped;
1028 GCancellable *cancellable;
1030 GAsyncReadyCallback callback;
1031 } SkipFallbackAsyncData;
1034 skip_callback_wrapper (GObject *source_object,
1038 GInputStreamClass *class;
1039 SkipFallbackAsyncData *data = user_data;
1041 GSimpleAsyncResult *simple;
1042 GError *error = NULL;
1045 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1050 data->count_skipped += ret;
1052 if (data->count > 0)
1054 class = G_INPUT_STREAM_GET_CLASS (source_object);
1055 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1056 skip_callback_wrapper, data);
1061 op = g_new0 (SkipData, 1);
1062 op->count_skipped = data->count_skipped;
1063 simple = g_simple_async_result_new (source_object,
1064 data->callback, data->user_data,
1065 g_input_stream_real_skip_async);
1067 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1071 if (data->count_skipped &&
1072 error->domain == G_IO_ERROR &&
1073 error->code == G_IO_ERROR_CANCELLED)
1074 { /* No error, return partial read */ }
1076 g_simple_async_result_set_from_error (simple, error);
1077 g_error_free (error);
1080 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1081 g_simple_async_result_complete (simple);
1082 g_object_unref (simple);
1088 g_input_stream_real_skip_async (GInputStream *stream,
1091 GCancellable *cancellable,
1092 GAsyncReadyCallback callback,
1095 GInputStreamClass *class;
1097 SkipFallbackAsyncData *data;
1098 GSimpleAsyncResult *res;
1100 class = G_INPUT_STREAM_GET_CLASS (stream);
1102 if (class->read_async == g_input_stream_real_read_async)
1104 /* Read is thread-using async fallback.
1105 * Make skip use threads too, so that we can use a possible sync skip
1106 * implementation. */
1107 op = g_new0 (SkipData, 1);
1109 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1110 g_input_stream_real_skip_async);
1112 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1114 op->count_requested = count;
1116 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1117 g_object_unref (res);
1121 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1123 /* There is a custom async read function, lets use that. */
1124 data = g_new (SkipFallbackAsyncData, 1);
1125 data->count = count;
1126 data->count_skipped = 0;
1127 data->io_prio = io_priority;
1128 data->cancellable = cancellable;
1129 data->callback = callback;
1130 data->user_data = user_data;
1131 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1132 skip_callback_wrapper, data);
1138 g_input_stream_real_skip_finish (GInputStream *stream,
1139 GAsyncResult *result,
1142 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1145 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1146 op = g_simple_async_result_get_op_res_gpointer (simple);
1147 return op->count_skipped;
1151 close_async_thread (GSimpleAsyncResult *res,
1153 GCancellable *cancellable)
1155 GInputStreamClass *class;
1156 GError *error = NULL;
1159 /* Auto handling of cancelation disabled, and ignore
1160 cancellation, since we want to close things anyway, although
1161 possibly in a quick-n-dirty way. At least we never want to leak
1164 class = G_INPUT_STREAM_GET_CLASS (object);
1165 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1168 g_simple_async_result_set_from_error (res, error);
1169 g_error_free (error);
1174 g_input_stream_real_close_async (GInputStream *stream,
1176 GCancellable *cancellable,
1177 GAsyncReadyCallback callback,
1180 GSimpleAsyncResult *res;
1182 res = g_simple_async_result_new (G_OBJECT (stream),
1185 g_input_stream_real_close_async);
1187 g_simple_async_result_set_handle_cancellation (res, FALSE);
1189 g_simple_async_result_run_in_thread (res,
1193 g_object_unref (res);
1197 g_input_stream_real_close_finish (GInputStream *stream,
1198 GAsyncResult *result,
1201 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1202 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1206 #define __G_INPUT_STREAM_C__
1207 #include "gioaliasdef.c"