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)
94 G_OBJECT_CLASS (g_input_stream_parent_class)->finalize (object);
98 g_input_stream_dispose (GObject *object)
100 GInputStream *stream;
102 stream = G_INPUT_STREAM (object);
104 if (!stream->priv->closed)
105 g_input_stream_close (stream, NULL, NULL);
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_literal (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 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
353 if (g_seekable_seek (G_SEEKABLE (stream),
361 /* If not seekable, or seek failed, fall back to reading data: */
363 class = G_INPUT_STREAM_GET_CLASS (stream);
370 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
371 cancellable, &my_error);
374 if (read_bytes > 0 &&
375 my_error->domain == G_IO_ERROR &&
376 my_error->code == G_IO_ERROR_CANCELLED)
378 g_error_free (my_error);
382 g_propagate_error (error, my_error);
389 if (ret == 0 || count == 0)
395 * g_input_stream_close:
396 * @stream: A #GInputStream.
397 * @cancellable: optional #GCancellable object, %NULL to ignore.
398 * @error: location to store the error occuring, or %NULL to ignore
400 * Closes the stream, releasing resources related to it.
402 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
403 * Closing a stream multiple times will not return an error.
405 * Streams will be automatically closed when the last reference
406 * is dropped, but you might want to call this function to make sure
407 * resources are released as early as possible.
409 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
410 * open after the stream is closed. See the documentation for the individual
411 * stream for details.
413 * On failure the first error that happened will be reported, but the close
414 * operation will finish as much as possible. A stream that failed to
415 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
416 * is important to check and report the error to the user.
418 * If @cancellable is not NULL, then the operation can be cancelled by
419 * triggering the cancellable object from another thread. If the operation
420 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
421 * Cancelling a close will still leave the stream closed, but some streams
422 * can use a faster close that doesn't block to e.g. check errors.
424 * Return value: %TRUE on success, %FALSE on failure
427 g_input_stream_close (GInputStream *stream,
428 GCancellable *cancellable,
431 GInputStreamClass *class;
434 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
436 class = G_INPUT_STREAM_GET_CLASS (stream);
438 if (stream->priv->closed)
443 if (!g_input_stream_set_pending (stream, error))
447 g_cancellable_push_current (cancellable);
450 res = class->close_fn (stream, cancellable, error);
453 g_cancellable_pop_current (cancellable);
455 g_input_stream_clear_pending (stream);
457 stream->priv->closed = TRUE;
463 async_ready_callback_wrapper (GObject *source_object,
467 GInputStream *stream = G_INPUT_STREAM (source_object);
469 g_input_stream_clear_pending (stream);
470 if (stream->priv->outstanding_callback)
471 (*stream->priv->outstanding_callback) (source_object, res, user_data);
472 g_object_unref (stream);
476 async_ready_close_callback_wrapper (GObject *source_object,
480 GInputStream *stream = G_INPUT_STREAM (source_object);
482 g_input_stream_clear_pending (stream);
483 stream->priv->closed = TRUE;
484 if (stream->priv->outstanding_callback)
485 (*stream->priv->outstanding_callback) (source_object, res, user_data);
486 g_object_unref (stream);
490 * g_input_stream_read_async:
491 * @stream: A #GInputStream.
492 * @buffer: a buffer to read data into (which should be at least count bytes long).
493 * @count: the number of bytes that will be read from the stream
494 * @io_priority: the <link linkend="io-priority">I/O priority</link>
496 * @cancellable: optional #GCancellable object, %NULL to ignore.
497 * @callback: callback to call when the request is satisfied
498 * @user_data: the data to pass to callback function
500 * Request an asynchronous read of @count bytes from the stream into the buffer
501 * starting at @buffer. When the operation is finished @callback will be called.
502 * You can then call g_input_stream_read_finish() to get the result of the
505 * During an async request no other sync and async calls are allowed on @stream, and will
506 * result in %G_IO_ERROR_PENDING errors.
508 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
510 * On success, the number of bytes read into the buffer will be passed to the
511 * callback. It is not an error if this is not the same as the requested size, as it
512 * can happen e.g. near the end of a file, but generally we try to read
513 * as many bytes as requested. Zero is returned on end of file
514 * (or if @count is zero), but never otherwise.
516 * Any outstanding i/o request with higher priority (lower numerical value) will
517 * be executed before an outstanding request with lower priority. Default
518 * priority is %G_PRIORITY_DEFAULT.
520 * The asyncronous methods have a default fallback that uses threads to implement
521 * asynchronicity, so they are optional for inheriting classes. However, if you
522 * override one you must override all.
525 g_input_stream_read_async (GInputStream *stream,
529 GCancellable *cancellable,
530 GAsyncReadyCallback callback,
533 GInputStreamClass *class;
534 GSimpleAsyncResult *simple;
535 GError *error = NULL;
537 g_return_if_fail (G_IS_INPUT_STREAM (stream));
538 g_return_if_fail (buffer != NULL);
542 simple = g_simple_async_result_new (G_OBJECT (stream),
545 g_input_stream_read_async);
546 g_simple_async_result_complete_in_idle (simple);
547 g_object_unref (simple);
551 if (((gssize) count) < 0)
553 g_simple_async_report_error_in_idle (G_OBJECT (stream),
556 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
557 _("Too large count value passed to %s"),
562 if (!g_input_stream_set_pending (stream, &error))
564 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
568 g_error_free (error);
572 class = G_INPUT_STREAM_GET_CLASS (stream);
573 stream->priv->outstanding_callback = callback;
574 g_object_ref (stream);
575 class->read_async (stream, buffer, count, io_priority, cancellable,
576 async_ready_callback_wrapper, user_data);
580 * g_input_stream_read_finish:
581 * @stream: a #GInputStream.
582 * @result: a #GAsyncResult.
583 * @error: a #GError location to store the error occuring, or %NULL to
586 * Finishes an asynchronous stream read operation.
588 * Returns: number of bytes read in, or -1 on error.
591 g_input_stream_read_finish (GInputStream *stream,
592 GAsyncResult *result,
595 GSimpleAsyncResult *simple;
596 GInputStreamClass *class;
598 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
599 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
601 if (G_IS_SIMPLE_ASYNC_RESULT (result))
603 simple = G_SIMPLE_ASYNC_RESULT (result);
604 if (g_simple_async_result_propagate_error (simple, error))
607 /* Special case read of 0 bytes */
608 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
612 class = G_INPUT_STREAM_GET_CLASS (stream);
613 return class->read_finish (stream, result, error);
617 * g_input_stream_skip_async:
618 * @stream: A #GInputStream.
619 * @count: the number of bytes that will be skipped from the stream
620 * @io_priority: the <link linkend="io-priority">I/O priority</link>
622 * @cancellable: optional #GCancellable object, %NULL to ignore.
623 * @callback: callback to call when the request is satisfied
624 * @user_data: the data to pass to callback function
626 * Request an asynchronous skip of @count bytes from the stream.
627 * When the operation is finished @callback will be called.
628 * You can then call g_input_stream_skip_finish() to get the result of the
631 * During an async request no other sync and async calls are allowed, and will
632 * result in %G_IO_ERROR_PENDING errors.
634 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
636 * On success, the number of bytes skipped will be passed to the
637 * callback. It is not an error if this is not the same as the requested size, as it
638 * can happen e.g. near the end of a file, but generally we try to skip
639 * as many bytes as requested. Zero is returned on end of file
640 * (or if @count is zero), but never otherwise.
642 * Any outstanding i/o request with higher priority (lower numerical value) will
643 * be executed before an outstanding request with lower priority. Default
644 * priority is %G_PRIORITY_DEFAULT.
646 * The asyncronous methods have a default fallback that uses threads to implement
647 * asynchronicity, so they are optional for inheriting classes. However, if you
648 * override one you must override all.
651 g_input_stream_skip_async (GInputStream *stream,
654 GCancellable *cancellable,
655 GAsyncReadyCallback callback,
658 GInputStreamClass *class;
659 GSimpleAsyncResult *simple;
660 GError *error = NULL;
662 g_return_if_fail (G_IS_INPUT_STREAM (stream));
666 simple = g_simple_async_result_new (G_OBJECT (stream),
669 g_input_stream_skip_async);
671 g_simple_async_result_complete_in_idle (simple);
672 g_object_unref (simple);
676 if (((gssize) count) < 0)
678 g_simple_async_report_error_in_idle (G_OBJECT (stream),
681 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
682 _("Too large count value passed to %s"),
687 if (!g_input_stream_set_pending (stream, &error))
689 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
693 g_error_free (error);
697 class = G_INPUT_STREAM_GET_CLASS (stream);
698 stream->priv->outstanding_callback = callback;
699 g_object_ref (stream);
700 class->skip_async (stream, count, io_priority, cancellable,
701 async_ready_callback_wrapper, user_data);
705 * g_input_stream_skip_finish:
706 * @stream: a #GInputStream.
707 * @result: a #GAsyncResult.
708 * @error: a #GError location to store the error occuring, or %NULL to
711 * Finishes a stream skip operation.
713 * Returns: the size of the bytes skipped, or %-1 on error.
716 g_input_stream_skip_finish (GInputStream *stream,
717 GAsyncResult *result,
720 GSimpleAsyncResult *simple;
721 GInputStreamClass *class;
723 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
724 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
726 if (G_IS_SIMPLE_ASYNC_RESULT (result))
728 simple = G_SIMPLE_ASYNC_RESULT (result);
729 if (g_simple_async_result_propagate_error (simple, error))
732 /* Special case skip of 0 bytes */
733 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
737 class = G_INPUT_STREAM_GET_CLASS (stream);
738 return class->skip_finish (stream, result, error);
742 * g_input_stream_close_async:
743 * @stream: A #GInputStream.
744 * @io_priority: the <link linkend="io-priority">I/O priority</link>
746 * @cancellable: optional cancellable object
747 * @callback: callback to call when the request is satisfied
748 * @user_data: the data to pass to callback function
750 * Requests an asynchronous closes of the stream, releasing resources related to it.
751 * When the operation is finished @callback will be called.
752 * You can then call g_input_stream_close_finish() to get the result of the
755 * For behaviour details see g_input_stream_close().
757 * The asyncronous methods have a default fallback that uses threads to implement
758 * asynchronicity, so they are optional for inheriting classes. However, if you
759 * override one you must override all.
762 g_input_stream_close_async (GInputStream *stream,
764 GCancellable *cancellable,
765 GAsyncReadyCallback callback,
768 GInputStreamClass *class;
769 GSimpleAsyncResult *simple;
770 GError *error = NULL;
772 g_return_if_fail (G_IS_INPUT_STREAM (stream));
774 if (stream->priv->closed)
776 simple = g_simple_async_result_new (G_OBJECT (stream),
779 g_input_stream_close_async);
781 g_simple_async_result_complete_in_idle (simple);
782 g_object_unref (simple);
786 if (!g_input_stream_set_pending (stream, &error))
788 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
792 g_error_free (error);
796 class = G_INPUT_STREAM_GET_CLASS (stream);
797 stream->priv->outstanding_callback = callback;
798 g_object_ref (stream);
799 class->close_async (stream, io_priority, cancellable,
800 async_ready_close_callback_wrapper, user_data);
804 * g_input_stream_close_finish:
805 * @stream: a #GInputStream.
806 * @result: a #GAsyncResult.
807 * @error: a #GError location to store the error occuring, or %NULL to
810 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
812 * Returns: %TRUE if the stream was closed successfully.
815 g_input_stream_close_finish (GInputStream *stream,
816 GAsyncResult *result,
819 GSimpleAsyncResult *simple;
820 GInputStreamClass *class;
822 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
823 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
825 if (G_IS_SIMPLE_ASYNC_RESULT (result))
827 simple = G_SIMPLE_ASYNC_RESULT (result);
828 if (g_simple_async_result_propagate_error (simple, error))
831 /* Special case already closed */
832 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
836 class = G_INPUT_STREAM_GET_CLASS (stream);
837 return class->close_finish (stream, result, error);
841 * g_input_stream_is_closed:
842 * @stream: input stream.
844 * Checks if an input stream is closed.
846 * Returns: %TRUE if the stream is closed.
849 g_input_stream_is_closed (GInputStream *stream)
851 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
853 return stream->priv->closed;
857 * g_input_stream_has_pending:
858 * @stream: input stream.
860 * Checks if an input stream has pending actions.
862 * Returns: %TRUE if @stream has pending actions.
865 g_input_stream_has_pending (GInputStream *stream)
867 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
869 return stream->priv->pending;
873 * g_input_stream_set_pending:
874 * @stream: input stream
875 * @error: a #GError location to store the error occuring, or %NULL to
878 * Sets @stream to have actions pending. If the pending flag is
879 * already set or @stream is closed, it will return %FALSE and set
882 * Return value: %TRUE if pending was previously unset and is now set.
885 g_input_stream_set_pending (GInputStream *stream, GError **error)
887 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
889 if (stream->priv->closed)
891 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
892 _("Stream is already closed"));
896 if (stream->priv->pending)
898 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
899 /* Translators: This is an error you get if there is already an
900 * operation running against this stream when you try to start
902 _("Stream has outstanding operation"));
906 stream->priv->pending = TRUE;
911 * g_input_stream_clear_pending:
912 * @stream: input stream
914 * Clears the pending flag on @stream.
917 g_input_stream_clear_pending (GInputStream *stream)
919 g_return_if_fail (G_IS_INPUT_STREAM (stream));
921 stream->priv->pending = FALSE;
924 /********************************************
925 * Default implementation of async ops *
926 ********************************************/
930 gsize count_requested;
935 read_async_thread (GSimpleAsyncResult *res,
937 GCancellable *cancellable)
940 GInputStreamClass *class;
941 GError *error = NULL;
943 op = g_simple_async_result_get_op_res_gpointer (res);
945 class = G_INPUT_STREAM_GET_CLASS (object);
947 op->count_read = class->read_fn (G_INPUT_STREAM (object),
948 op->buffer, op->count_requested,
949 cancellable, &error);
950 if (op->count_read == -1)
952 g_simple_async_result_set_from_error (res, error);
953 g_error_free (error);
958 g_input_stream_real_read_async (GInputStream *stream,
962 GCancellable *cancellable,
963 GAsyncReadyCallback callback,
966 GSimpleAsyncResult *res;
969 op = g_new (ReadData, 1);
970 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
971 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
973 op->count_requested = count;
975 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
976 g_object_unref (res);
980 g_input_stream_real_read_finish (GInputStream *stream,
981 GAsyncResult *result,
984 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
987 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
988 g_input_stream_real_read_async);
990 op = g_simple_async_result_get_op_res_gpointer (simple);
992 return op->count_read;
996 gsize count_requested;
997 gssize count_skipped;
1002 skip_async_thread (GSimpleAsyncResult *res,
1004 GCancellable *cancellable)
1007 GInputStreamClass *class;
1008 GError *error = NULL;
1010 class = G_INPUT_STREAM_GET_CLASS (object);
1011 op = g_simple_async_result_get_op_res_gpointer (res);
1012 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1013 op->count_requested,
1014 cancellable, &error);
1015 if (op->count_skipped == -1)
1017 g_simple_async_result_set_from_error (res, error);
1018 g_error_free (error);
1025 gsize count_skipped;
1027 GCancellable *cancellable;
1029 GAsyncReadyCallback callback;
1030 } SkipFallbackAsyncData;
1033 skip_callback_wrapper (GObject *source_object,
1037 GInputStreamClass *class;
1038 SkipFallbackAsyncData *data = user_data;
1040 GSimpleAsyncResult *simple;
1041 GError *error = NULL;
1044 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1049 data->count_skipped += ret;
1051 if (data->count > 0)
1053 class = G_INPUT_STREAM_GET_CLASS (source_object);
1054 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1055 skip_callback_wrapper, data);
1060 op = g_new0 (SkipData, 1);
1061 op->count_skipped = data->count_skipped;
1062 simple = g_simple_async_result_new (source_object,
1063 data->callback, data->user_data,
1064 g_input_stream_real_skip_async);
1066 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1070 if (data->count_skipped &&
1071 error->domain == G_IO_ERROR &&
1072 error->code == G_IO_ERROR_CANCELLED)
1073 { /* No error, return partial read */ }
1075 g_simple_async_result_set_from_error (simple, error);
1076 g_error_free (error);
1079 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1080 g_simple_async_result_complete (simple);
1081 g_object_unref (simple);
1087 g_input_stream_real_skip_async (GInputStream *stream,
1090 GCancellable *cancellable,
1091 GAsyncReadyCallback callback,
1094 GInputStreamClass *class;
1096 SkipFallbackAsyncData *data;
1097 GSimpleAsyncResult *res;
1099 class = G_INPUT_STREAM_GET_CLASS (stream);
1101 if (class->read_async == g_input_stream_real_read_async)
1103 /* Read is thread-using async fallback.
1104 * Make skip use threads too, so that we can use a possible sync skip
1105 * implementation. */
1106 op = g_new0 (SkipData, 1);
1108 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1109 g_input_stream_real_skip_async);
1111 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1113 op->count_requested = count;
1115 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1116 g_object_unref (res);
1120 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1122 /* There is a custom async read function, lets use that. */
1123 data = g_new (SkipFallbackAsyncData, 1);
1124 data->count = count;
1125 data->count_skipped = 0;
1126 data->io_prio = io_priority;
1127 data->cancellable = cancellable;
1128 data->callback = callback;
1129 data->user_data = user_data;
1130 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1131 skip_callback_wrapper, data);
1137 g_input_stream_real_skip_finish (GInputStream *stream,
1138 GAsyncResult *result,
1141 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1144 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1145 op = g_simple_async_result_get_op_res_gpointer (simple);
1146 return op->count_skipped;
1150 close_async_thread (GSimpleAsyncResult *res,
1152 GCancellable *cancellable)
1154 GInputStreamClass *class;
1155 GError *error = NULL;
1158 /* Auto handling of cancelation disabled, and ignore
1159 cancellation, since we want to close things anyway, although
1160 possibly in a quick-n-dirty way. At least we never want to leak
1163 class = G_INPUT_STREAM_GET_CLASS (object);
1164 if (class->close_fn)
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);
1176 g_input_stream_real_close_async (GInputStream *stream,
1178 GCancellable *cancellable,
1179 GAsyncReadyCallback callback,
1182 GSimpleAsyncResult *res;
1184 res = g_simple_async_result_new (G_OBJECT (stream),
1187 g_input_stream_real_close_async);
1189 g_simple_async_result_set_handle_cancellation (res, FALSE);
1191 g_simple_async_result_run_in_thread (res,
1195 g_object_unref (res);
1199 g_input_stream_real_close_finish (GInputStream *stream,
1200 GAsyncResult *result,
1203 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1204 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1208 #define __G_INPUT_STREAM_C__
1209 #include "gioaliasdef.c"