1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "ginputstream.h"
28 #include "gseekable.h"
29 #include "gcancellable.h"
30 #include "gasyncresult.h"
31 #include "gsimpleasyncresult.h"
37 * SECTION:ginputstream
38 * @short_description: Base class for implementing streaming input
41 * GInputStream has functions to read from a stream (g_input_stream_read()),
42 * to close a stream (g_input_stream_close()) and to skip some content
43 * (g_input_stream_skip()).
45 * To copy the content of an input stream to an output stream without
46 * manually handling the reads and writes, use g_output_stream_splice().
48 * All of these functions have async variants too.
51 G_DEFINE_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
53 struct _GInputStreamPrivate {
56 GAsyncReadyCallback outstanding_callback;
59 static gssize g_input_stream_real_skip (GInputStream *stream,
61 GCancellable *cancellable,
63 static void g_input_stream_real_read_async (GInputStream *stream,
67 GCancellable *cancellable,
68 GAsyncReadyCallback callback,
70 static gssize g_input_stream_real_read_finish (GInputStream *stream,
73 static void g_input_stream_real_skip_async (GInputStream *stream,
76 GCancellable *cancellable,
77 GAsyncReadyCallback callback,
79 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
82 static void g_input_stream_real_close_async (GInputStream *stream,
84 GCancellable *cancellable,
85 GAsyncReadyCallback callback,
87 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
92 g_input_stream_finalize (GObject *object)
96 stream = G_INPUT_STREAM (object);
98 if (!stream->priv->closed)
99 g_input_stream_close (stream, NULL, NULL);
101 G_OBJECT_CLASS (g_input_stream_parent_class)->finalize (object);
105 g_input_stream_dispose (GObject *object)
107 GInputStream *stream;
109 stream = G_INPUT_STREAM (object);
111 if (!stream->priv->closed)
112 g_input_stream_close (stream, NULL, NULL);
114 G_OBJECT_CLASS (g_input_stream_parent_class)->dispose (object);
119 g_input_stream_class_init (GInputStreamClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
123 g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
125 gobject_class->finalize = g_input_stream_finalize;
126 gobject_class->dispose = g_input_stream_dispose;
128 klass->skip = g_input_stream_real_skip;
129 klass->read_async = g_input_stream_real_read_async;
130 klass->read_finish = g_input_stream_real_read_finish;
131 klass->skip_async = g_input_stream_real_skip_async;
132 klass->skip_finish = g_input_stream_real_skip_finish;
133 klass->close_async = g_input_stream_real_close_async;
134 klass->close_finish = g_input_stream_real_close_finish;
138 g_input_stream_init (GInputStream *stream)
140 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
142 GInputStreamPrivate);
146 * g_input_stream_read:
147 * @stream: a #GInputStream.
148 * @buffer: a buffer to read data into (which should be at least count bytes long).
149 * @count: the number of bytes that will be read from the stream
150 * @cancellable: optional #GCancellable object, %NULL to ignore.
151 * @error: location to store the error occuring, or %NULL to ignore
153 * Tries to read @count bytes from the stream into the buffer starting at
154 * @buffer. Will block during this read.
156 * If count is zero returns zero and does nothing. A value of @count
157 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
159 * On success, the number of bytes read into the buffer is returned.
160 * It is not an error if this is not the same as the requested size, as it
161 * can happen e.g. near the end of a file. Zero is returned on end of file
162 * (or if @count is zero), but never otherwise.
164 * If @cancellable is not NULL, then the operation can be cancelled by
165 * triggering the cancellable object from another thread. If the operation
166 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
167 * operation was partially finished when the operation was cancelled the
168 * partial result will be returned, without an error.
170 * On error -1 is returned and @error is set accordingly.
172 * Return value: Number of bytes read, or -1 on error
175 g_input_stream_read (GInputStream *stream,
178 GCancellable *cancellable,
181 GInputStreamClass *class;
184 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
185 g_return_val_if_fail (buffer != NULL, 0);
190 if (((gssize) count) < 0)
192 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
193 _("Too large count value passed to %s"), G_STRFUNC);
197 class = G_INPUT_STREAM_GET_CLASS (stream);
199 if (class->read_fn == NULL)
201 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
202 _("Input stream doesn't implement read"));
206 if (!g_input_stream_set_pending (stream, error))
210 g_cancellable_push_current (cancellable);
212 res = class->read_fn (stream, buffer, count, cancellable, error);
215 g_cancellable_pop_current (cancellable);
217 g_input_stream_clear_pending (stream);
223 * g_input_stream_read_all:
224 * @stream: a #GInputStream.
225 * @buffer: a buffer to read data into (which should be at least count bytes long).
226 * @count: the number of bytes that will be read from the stream
227 * @bytes_read: location to store the number of bytes that was read from the stream
228 * @cancellable: optional #GCancellable object, %NULL to ignore.
229 * @error: location to store the error occuring, or %NULL to ignore
231 * Tries to read @count bytes from the stream into the buffer starting at
232 * @buffer. Will block during this read.
234 * This function is similar to g_input_stream_read(), except it tries to
235 * read as many bytes as requested, only stopping on an error or end of stream.
237 * On a successful read of @count bytes, or if we reached the end of the
238 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
241 * If there is an error during the operation %FALSE is returned and @error
242 * is set to indicate the error status, @bytes_read is updated to contain
243 * the number of bytes read into @buffer before the error occurred.
245 * Return value: %TRUE on success, %FALSE if there was an error
248 g_input_stream_read_all (GInputStream *stream,
252 GCancellable *cancellable,
258 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
259 g_return_val_if_fail (buffer != NULL, FALSE);
262 while (_bytes_read < count)
264 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
269 *bytes_read = _bytes_read;
280 *bytes_read = _bytes_read;
285 * g_input_stream_skip:
286 * @stream: a #GInputStream.
287 * @count: the number of bytes that will be skipped from the stream
288 * @cancellable: optional #GCancellable object, %NULL to ignore.
289 * @error: location to store the error occuring, or %NULL to ignore
291 * Tries to skip @count bytes from the stream. Will block during the operation.
293 * This is identical to g_input_stream_read(), from a behaviour standpoint,
294 * but the bytes that are skipped are not returned to the user. Some
295 * streams have an implementation that is more efficient than reading the data.
297 * This function is optional for inherited classes, as the default implementation
298 * emulates it using read.
300 * If @cancellable is not %NULL, then the operation can be cancelled by
301 * triggering the cancellable object from another thread. If the operation
302 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
303 * operation was partially finished when the operation was cancelled the
304 * partial result will be returned, without an error.
306 * Return value: Number of bytes skipped, or -1 on error
309 g_input_stream_skip (GInputStream *stream,
311 GCancellable *cancellable,
314 GInputStreamClass *class;
317 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
322 if (((gssize) count) < 0)
324 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
325 _("Too large count value passed to %s"), G_STRFUNC);
329 class = G_INPUT_STREAM_GET_CLASS (stream);
331 if (!g_input_stream_set_pending (stream, error))
335 g_cancellable_push_current (cancellable);
337 res = class->skip (stream, count, cancellable, error);
340 g_cancellable_pop_current (cancellable);
342 g_input_stream_clear_pending (stream);
348 g_input_stream_real_skip (GInputStream *stream,
350 GCancellable *cancellable,
353 GInputStreamClass *class;
354 gssize ret, read_bytes;
358 class = G_INPUT_STREAM_GET_CLASS (stream);
360 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
362 if (g_seekable_seek (G_SEEKABLE (stream),
370 /* If not seekable, or seek failed, fall back to reading data: */
372 class = G_INPUT_STREAM_GET_CLASS (stream);
379 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
380 cancellable, &my_error);
383 if (read_bytes > 0 &&
384 my_error->domain == G_IO_ERROR &&
385 my_error->code == G_IO_ERROR_CANCELLED)
387 g_error_free (my_error);
391 g_propagate_error (error, my_error);
398 if (ret == 0 || count == 0)
404 * g_input_stream_close:
405 * @stream: A #GInputStream.
406 * @cancellable: optional #GCancellable object, %NULL to ignore.
407 * @error: location to store the error occuring, or %NULL to ignore
409 * Closes the stream, releasing resources related to it.
411 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
412 * Closing a stream multiple times will not return an error.
414 * Streams will be automatically closed when the last reference
415 * is dropped, but you might want to call this function to make sure
416 * resources are released as early as possible.
418 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
419 * open after the stream is closed. See the documentation for the individual
420 * stream for details.
422 * On failure the first error that happened will be reported, but the close
423 * operation will finish as much as possible. A stream that failed to
424 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
425 * is important to check and report the error to the user.
427 * If @cancellable is not NULL, then the operation can be cancelled by
428 * triggering the cancellable object from another thread. If the operation
429 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
430 * Cancelling a close will still leave the stream closed, but some streams
431 * can use a faster close that doesn't block to e.g. check errors.
433 * Return value: %TRUE on success, %FALSE on failure
436 g_input_stream_close (GInputStream *stream,
437 GCancellable *cancellable,
440 GInputStreamClass *class;
443 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
445 class = G_INPUT_STREAM_GET_CLASS (stream);
447 if (stream->priv->closed)
452 if (!g_input_stream_set_pending (stream, error))
456 g_cancellable_push_current (cancellable);
459 res = class->close_fn (stream, cancellable, error);
462 g_cancellable_pop_current (cancellable);
464 g_input_stream_clear_pending (stream);
466 stream->priv->closed = TRUE;
472 async_ready_callback_wrapper (GObject *source_object,
476 GInputStream *stream = G_INPUT_STREAM (source_object);
478 g_input_stream_clear_pending (stream);
479 if (stream->priv->outstanding_callback)
480 (*stream->priv->outstanding_callback) (source_object, res, user_data);
481 g_object_unref (stream);
485 async_ready_close_callback_wrapper (GObject *source_object,
489 GInputStream *stream = G_INPUT_STREAM (source_object);
491 g_input_stream_clear_pending (stream);
492 stream->priv->closed = TRUE;
493 if (stream->priv->outstanding_callback)
494 (*stream->priv->outstanding_callback) (source_object, res, user_data);
495 g_object_unref (stream);
499 * g_input_stream_read_async:
500 * @stream: A #GInputStream.
501 * @buffer: a buffer to read data into (which should be at least count bytes long).
502 * @count: the number of bytes that will be read from the stream
503 * @io_priority: the <link linkend="io-priority">I/O priority</link>
505 * @cancellable: optional #GCancellable object, %NULL to ignore.
506 * @callback: callback to call when the request is satisfied
507 * @user_data: the data to pass to callback function
509 * Request an asynchronous read of @count bytes from the stream into the buffer
510 * starting at @buffer. When the operation is finished @callback will be called.
511 * You can then call g_input_stream_read_finish() to get the result of the
514 * During an async request no other sync and async calls are allowed, and will
515 * result in %G_IO_ERROR_PENDING errors.
517 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
519 * On success, the number of bytes read into the buffer will be passed to the
520 * callback. It is not an error if this is not the same as the requested size, as it
521 * can happen e.g. near the end of a file, but generally we try to read
522 * as many bytes as requested. Zero is returned on end of file
523 * (or if @count is zero), but never otherwise.
525 * Any outstanding i/o request with higher priority (lower numerical value) will
526 * be executed before an outstanding request with lower priority. Default
527 * priority is %G_PRIORITY_DEFAULT.
529 * The asyncronous methods have a default fallback that uses threads to implement
530 * asynchronicity, so they are optional for inheriting classes. However, if you
531 * override one you must override all.
534 g_input_stream_read_async (GInputStream *stream,
538 GCancellable *cancellable,
539 GAsyncReadyCallback callback,
542 GInputStreamClass *class;
543 GSimpleAsyncResult *simple;
544 GError *error = NULL;
546 g_return_if_fail (G_IS_INPUT_STREAM (stream));
547 g_return_if_fail (buffer != NULL);
551 simple = g_simple_async_result_new (G_OBJECT (stream),
554 g_input_stream_read_async);
555 g_simple_async_result_complete_in_idle (simple);
556 g_object_unref (simple);
560 if (((gssize) count) < 0)
562 g_simple_async_report_error_in_idle (G_OBJECT (stream),
565 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
566 _("Too large count value passed to %s"),
571 if (!g_input_stream_set_pending (stream, &error))
573 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
577 g_error_free (error);
581 class = G_INPUT_STREAM_GET_CLASS (stream);
582 stream->priv->outstanding_callback = callback;
583 g_object_ref (stream);
584 class->read_async (stream, buffer, count, io_priority, cancellable,
585 async_ready_callback_wrapper, user_data);
589 * g_input_stream_read_finish:
590 * @stream: a #GInputStream.
591 * @result: a #GAsyncResult.
592 * @error: a #GError location to store the error occuring, or %NULL to
595 * Finishes an asynchronous stream read operation.
597 * Returns: number of bytes read in, or -1 on error.
600 g_input_stream_read_finish (GInputStream *stream,
601 GAsyncResult *result,
604 GSimpleAsyncResult *simple;
605 GInputStreamClass *class;
607 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
608 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
610 if (G_IS_SIMPLE_ASYNC_RESULT (result))
612 simple = G_SIMPLE_ASYNC_RESULT (result);
613 if (g_simple_async_result_propagate_error (simple, error))
616 /* Special case read of 0 bytes */
617 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
621 class = G_INPUT_STREAM_GET_CLASS (stream);
622 return class->read_finish (stream, result, error);
626 * g_input_stream_skip_async:
627 * @stream: A #GInputStream.
628 * @count: the number of bytes that will be skipped from the stream
629 * @io_priority: the <link linkend="io-priority">I/O priority</link>
631 * @cancellable: optional #GCancellable object, %NULL to ignore.
632 * @callback: callback to call when the request is satisfied
633 * @user_data: the data to pass to callback function
635 * Request an asynchronous skip of @count bytes from the stream.
636 * When the operation is finished @callback will be called.
637 * You can then call g_input_stream_skip_finish() to get the result of the
640 * During an async request no other sync and async calls are allowed, and will
641 * result in %G_IO_ERROR_PENDING errors.
643 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
645 * On success, the number of bytes skipped will be passed to the
646 * callback. It is not an error if this is not the same as the requested size, as it
647 * can happen e.g. near the end of a file, but generally we try to skip
648 * as many bytes as requested. Zero is returned on end of file
649 * (or if @count is zero), but never otherwise.
651 * Any outstanding i/o request with higher priority (lower numerical value) will
652 * be executed before an outstanding request with lower priority. Default
653 * priority is %G_PRIORITY_DEFAULT.
655 * The asyncronous methods have a default fallback that uses threads to implement
656 * asynchronicity, so they are optional for inheriting classes. However, if you
657 * override one you must override all.
660 g_input_stream_skip_async (GInputStream *stream,
663 GCancellable *cancellable,
664 GAsyncReadyCallback callback,
667 GInputStreamClass *class;
668 GSimpleAsyncResult *simple;
669 GError *error = NULL;
671 g_return_if_fail (G_IS_INPUT_STREAM (stream));
675 simple = g_simple_async_result_new (G_OBJECT (stream),
678 g_input_stream_skip_async);
680 g_simple_async_result_complete_in_idle (simple);
681 g_object_unref (simple);
685 if (((gssize) count) < 0)
687 g_simple_async_report_error_in_idle (G_OBJECT (stream),
690 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
691 _("Too large count value passed to %s"),
696 if (!g_input_stream_set_pending (stream, &error))
698 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
702 g_error_free (error);
706 class = G_INPUT_STREAM_GET_CLASS (stream);
707 stream->priv->outstanding_callback = callback;
708 g_object_ref (stream);
709 class->skip_async (stream, count, io_priority, cancellable,
710 async_ready_callback_wrapper, user_data);
714 * g_input_stream_skip_finish:
715 * @stream: a #GInputStream.
716 * @result: a #GAsyncResult.
717 * @error: a #GError location to store the error occuring, or %NULL to
720 * Finishes a stream skip operation.
722 * Returns: the size of the bytes skipped, or %-1 on error.
725 g_input_stream_skip_finish (GInputStream *stream,
726 GAsyncResult *result,
729 GSimpleAsyncResult *simple;
730 GInputStreamClass *class;
732 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
733 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
735 if (G_IS_SIMPLE_ASYNC_RESULT (result))
737 simple = G_SIMPLE_ASYNC_RESULT (result);
738 if (g_simple_async_result_propagate_error (simple, error))
741 /* Special case skip of 0 bytes */
742 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
746 class = G_INPUT_STREAM_GET_CLASS (stream);
747 return class->skip_finish (stream, result, error);
751 * g_input_stream_close_async:
752 * @stream: A #GInputStream.
753 * @io_priority: the <link linkend="io-priority">I/O priority</link>
755 * @cancellable: optional cancellable object
756 * @callback: callback to call when the request is satisfied
757 * @user_data: the data to pass to callback function
759 * Requests an asynchronous closes of the stream, releasing resources related to it.
760 * When the operation is finished @callback will be called.
761 * You can then call g_input_stream_close_finish() to get the result of the
764 * For behaviour details see g_input_stream_close().
766 * The asyncronous methods have a default fallback that uses threads to implement
767 * asynchronicity, so they are optional for inheriting classes. However, if you
768 * override one you must override all.
771 g_input_stream_close_async (GInputStream *stream,
773 GCancellable *cancellable,
774 GAsyncReadyCallback callback,
777 GInputStreamClass *class;
778 GSimpleAsyncResult *simple;
779 GError *error = NULL;
781 g_return_if_fail (G_IS_INPUT_STREAM (stream));
783 if (stream->priv->closed)
785 simple = g_simple_async_result_new (G_OBJECT (stream),
788 g_input_stream_close_async);
790 g_simple_async_result_complete_in_idle (simple);
791 g_object_unref (simple);
795 if (!g_input_stream_set_pending (stream, &error))
797 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
801 g_error_free (error);
805 class = G_INPUT_STREAM_GET_CLASS (stream);
806 stream->priv->outstanding_callback = callback;
807 g_object_ref (stream);
808 class->close_async (stream, io_priority, cancellable,
809 async_ready_close_callback_wrapper, user_data);
813 * g_input_stream_close_finish:
814 * @stream: a #GInputStream.
815 * @result: a #GAsyncResult.
816 * @error: a #GError location to store the error occuring, or %NULL to
819 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
821 * Returns: %TRUE if the stream was closed successfully.
824 g_input_stream_close_finish (GInputStream *stream,
825 GAsyncResult *result,
828 GSimpleAsyncResult *simple;
829 GInputStreamClass *class;
831 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
832 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
834 if (G_IS_SIMPLE_ASYNC_RESULT (result))
836 simple = G_SIMPLE_ASYNC_RESULT (result);
837 if (g_simple_async_result_propagate_error (simple, error))
840 /* Special case already closed */
841 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
845 class = G_INPUT_STREAM_GET_CLASS (stream);
846 return class->close_finish (stream, result, error);
850 * g_input_stream_is_closed:
851 * @stream: input stream.
853 * Checks if an input stream is closed.
855 * Returns: %TRUE if the stream is closed.
858 g_input_stream_is_closed (GInputStream *stream)
860 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
862 return stream->priv->closed;
866 * g_input_stream_has_pending:
867 * @stream: input stream.
869 * Checks if an input stream has pending actions.
871 * Returns: %TRUE if @stream has pending actions.
874 g_input_stream_has_pending (GInputStream *stream)
876 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
878 return stream->priv->pending;
882 * g_input_stream_set_pending:
883 * @stream: input stream
884 * @error: a #GError location to store the error occuring, or %NULL to
887 * Sets @stream to have actions pending. If the pending flag is
888 * already set or @stream is closed, it will return %FALSE and set
891 * Return value: %TRUE if pending was previously unset and is now set.
894 g_input_stream_set_pending (GInputStream *stream, GError **error)
896 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
898 if (stream->priv->closed)
900 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
901 _("Stream is already closed"));
905 if (stream->priv->pending)
907 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PENDING,
908 /* Translators: This is an error you get if there is already an
909 * operation running against this stream when you try to start
911 _("Stream has outstanding operation"));
915 stream->priv->pending = TRUE;
920 * g_input_stream_clear_pending:
921 * @stream: input stream
923 * Clears the pending flag on @stream.
926 g_input_stream_clear_pending (GInputStream *stream)
928 g_return_if_fail (G_IS_INPUT_STREAM (stream));
930 stream->priv->pending = FALSE;
933 /********************************************
934 * Default implementation of async ops *
935 ********************************************/
939 gsize count_requested;
944 read_async_thread (GSimpleAsyncResult *res,
946 GCancellable *cancellable)
949 GInputStreamClass *class;
950 GError *error = NULL;
952 op = g_simple_async_result_get_op_res_gpointer (res);
954 class = G_INPUT_STREAM_GET_CLASS (object);
956 op->count_read = class->read_fn (G_INPUT_STREAM (object),
957 op->buffer, op->count_requested,
958 cancellable, &error);
959 if (op->count_read == -1)
961 g_simple_async_result_set_from_error (res, error);
962 g_error_free (error);
967 g_input_stream_real_read_async (GInputStream *stream,
971 GCancellable *cancellable,
972 GAsyncReadyCallback callback,
975 GSimpleAsyncResult *res;
978 op = g_new (ReadData, 1);
979 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
980 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
982 op->count_requested = count;
984 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
985 g_object_unref (res);
989 g_input_stream_real_read_finish (GInputStream *stream,
990 GAsyncResult *result,
993 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
996 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
997 g_input_stream_real_read_async);
999 op = g_simple_async_result_get_op_res_gpointer (simple);
1001 return op->count_read;
1005 gsize count_requested;
1006 gssize count_skipped;
1011 skip_async_thread (GSimpleAsyncResult *res,
1013 GCancellable *cancellable)
1016 GInputStreamClass *class;
1017 GError *error = NULL;
1019 class = G_INPUT_STREAM_GET_CLASS (object);
1020 op = g_simple_async_result_get_op_res_gpointer (res);
1021 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1022 op->count_requested,
1023 cancellable, &error);
1024 if (op->count_skipped == -1)
1026 g_simple_async_result_set_from_error (res, error);
1027 g_error_free (error);
1034 gsize count_skipped;
1036 GCancellable *cancellable;
1038 GAsyncReadyCallback callback;
1039 } SkipFallbackAsyncData;
1042 skip_callback_wrapper (GObject *source_object,
1046 GInputStreamClass *class;
1047 SkipFallbackAsyncData *data = user_data;
1049 GSimpleAsyncResult *simple;
1050 GError *error = NULL;
1053 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1058 data->count_skipped += ret;
1060 if (data->count > 0)
1062 class = G_INPUT_STREAM_GET_CLASS (source_object);
1063 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1064 skip_callback_wrapper, data);
1069 op = g_new0 (SkipData, 1);
1070 op->count_skipped = data->count_skipped;
1071 simple = g_simple_async_result_new (source_object,
1072 data->callback, data->user_data,
1073 g_input_stream_real_skip_async);
1075 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1079 if (data->count_skipped &&
1080 error->domain == G_IO_ERROR &&
1081 error->code == G_IO_ERROR_CANCELLED)
1082 { /* No error, return partial read */ }
1084 g_simple_async_result_set_from_error (simple, error);
1085 g_error_free (error);
1088 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1089 g_simple_async_result_complete (simple);
1090 g_object_unref (simple);
1096 g_input_stream_real_skip_async (GInputStream *stream,
1099 GCancellable *cancellable,
1100 GAsyncReadyCallback callback,
1103 GInputStreamClass *class;
1105 SkipFallbackAsyncData *data;
1106 GSimpleAsyncResult *res;
1108 class = G_INPUT_STREAM_GET_CLASS (stream);
1110 if (class->read_async == g_input_stream_real_read_async)
1112 /* Read is thread-using async fallback.
1113 * Make skip use threads too, so that we can use a possible sync skip
1114 * implementation. */
1115 op = g_new0 (SkipData, 1);
1117 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1118 g_input_stream_real_skip_async);
1120 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1122 op->count_requested = count;
1124 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1125 g_object_unref (res);
1129 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1131 /* There is a custom async read function, lets use that. */
1132 data = g_new (SkipFallbackAsyncData, 1);
1133 data->count = count;
1134 data->count_skipped = 0;
1135 data->io_prio = io_priority;
1136 data->cancellable = cancellable;
1137 data->callback = callback;
1138 data->user_data = user_data;
1139 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1140 skip_callback_wrapper, data);
1146 g_input_stream_real_skip_finish (GInputStream *stream,
1147 GAsyncResult *result,
1150 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1153 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1154 op = g_simple_async_result_get_op_res_gpointer (simple);
1155 return op->count_skipped;
1159 close_async_thread (GSimpleAsyncResult *res,
1161 GCancellable *cancellable)
1163 GInputStreamClass *class;
1164 GError *error = NULL;
1167 /* Auto handling of cancelation disabled, and ignore
1168 cancellation, since we want to close things anyway, although
1169 possibly in a quick-n-dirty way. At least we never want to leak
1172 class = G_INPUT_STREAM_GET_CLASS (object);
1173 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1176 g_simple_async_result_set_from_error (res, error);
1177 g_error_free (error);
1182 g_input_stream_real_close_async (GInputStream *stream,
1184 GCancellable *cancellable,
1185 GAsyncReadyCallback callback,
1188 GSimpleAsyncResult *res;
1190 res = g_simple_async_result_new (G_OBJECT (stream),
1193 g_input_stream_real_close_async);
1195 g_simple_async_result_set_handle_cancellation (res, FALSE);
1197 g_simple_async_result_run_in_thread (res,
1201 g_object_unref (res);
1205 g_input_stream_real_close_finish (GInputStream *stream,
1206 GAsyncResult *result,
1209 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1210 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1214 #define __G_INPUT_STREAM_C__
1215 #include "gioaliasdef.c"