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 g_input_stream_read"));
190 class = G_INPUT_STREAM_GET_CLASS (stream);
192 if (class->read_fn == NULL)
194 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
195 _("Input stream doesn't implement read"));
199 if (!g_input_stream_set_pending (stream, error))
203 g_cancellable_push_current (cancellable);
205 res = class->read_fn (stream, buffer, count, cancellable, error);
208 g_cancellable_pop_current (cancellable);
210 g_input_stream_clear_pending (stream);
216 * g_input_stream_read_all:
217 * @stream: a #GInputStream.
218 * @buffer: a buffer to read data into (which should be at least count bytes long).
219 * @count: the number of bytes that will be read from the stream
220 * @bytes_read: location to store the number of bytes that was read from the stream
221 * @cancellable: optional #GCancellable object, %NULL to ignore.
222 * @error: location to store the error occuring, or %NULL to ignore
224 * Tries to read @count bytes from the stream into the buffer starting at
225 * @buffer. Will block during this read.
227 * This function is similar to g_input_stream_read(), except it tries to
228 * read as many bytes as requested, only stopping on an error or end of stream.
230 * On a successful read of @count bytes, or if we reached the end of the
231 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
234 * If there is an error during the operation %FALSE is returned and @error
235 * is set to indicate the error status, @bytes_read is updated to contain
236 * the number of bytes read into @buffer before the error occurred.
238 * Return value: %TRUE on success, %FALSE if there was an error
241 g_input_stream_read_all (GInputStream *stream,
245 GCancellable *cancellable,
251 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
252 g_return_val_if_fail (buffer != NULL, FALSE);
255 while (_bytes_read < count)
257 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
262 *bytes_read = _bytes_read;
273 *bytes_read = _bytes_read;
278 * g_input_stream_skip:
279 * @stream: a #GInputStream.
280 * @count: the number of bytes that will be skipped from the stream
281 * @cancellable: optional #GCancellable object, %NULL to ignore.
282 * @error: location to store the error occuring, or %NULL to ignore
284 * Tries to skip @count bytes from the stream. Will block during the operation.
286 * This is identical to g_input_stream_read(), from a behaviour standpoint,
287 * but the bytes that are skipped are not returned to the user. Some
288 * streams have an implementation that is more efficient than reading the data.
290 * This function is optional for inherited classes, as the default implementation
291 * emulates it using read.
293 * If @cancellable is not %NULL, then the operation can be cancelled by
294 * triggering the cancellable object from another thread. If the operation
295 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
296 * operation was partially finished when the operation was cancelled the
297 * partial result will be returned, without an error.
299 * Return value: Number of bytes skipped, or -1 on error
302 g_input_stream_skip (GInputStream *stream,
304 GCancellable *cancellable,
307 GInputStreamClass *class;
310 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
315 if (((gssize) count) < 0)
317 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
318 _("Too large count value passed to g_input_stream_skip"));
322 class = G_INPUT_STREAM_GET_CLASS (stream);
324 if (!g_input_stream_set_pending (stream, error))
328 g_cancellable_push_current (cancellable);
330 res = class->skip (stream, count, cancellable, error);
333 g_cancellable_pop_current (cancellable);
335 g_input_stream_clear_pending (stream);
341 g_input_stream_real_skip (GInputStream *stream,
343 GCancellable *cancellable,
346 GInputStreamClass *class;
347 gssize ret, read_bytes;
351 class = G_INPUT_STREAM_GET_CLASS (stream);
353 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
355 if (g_seekable_seek (G_SEEKABLE (stream),
363 /* If not seekable, or seek failed, fall back to reading data: */
365 class = G_INPUT_STREAM_GET_CLASS (stream);
372 ret = class->read_fn (stream, buffer, MIN (sizeof (buffer), count),
373 cancellable, &my_error);
376 if (read_bytes > 0 &&
377 my_error->domain == G_IO_ERROR &&
378 my_error->code == G_IO_ERROR_CANCELLED)
380 g_error_free (my_error);
384 g_propagate_error (error, my_error);
391 if (ret == 0 || count == 0)
397 * g_input_stream_close:
398 * @stream: A #GInputStream.
399 * @cancellable: optional #GCancellable object, %NULL to ignore.
400 * @error: location to store the error occuring, or %NULL to ignore
402 * Closes the stream, releasing resources related to it.
404 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
405 * Closing a stream multiple times will not return an error.
407 * Streams will be automatically closed when the last reference
408 * is dropped, but you might want to call this function to make sure
409 * resources are released as early as possible.
411 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
412 * open after the stream is closed. See the documentation for the individual
413 * stream for details.
415 * On failure the first error that happened will be reported, but the close
416 * operation will finish as much as possible. A stream that failed to
417 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
418 * is important to check and report the error to the user.
420 * If @cancellable is not NULL, then the operation can be cancelled by
421 * triggering the cancellable object from another thread. If the operation
422 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
423 * Cancelling a close will still leave the stream closed, but some streams
424 * can use a faster close that doesn't block to e.g. check errors.
426 * Return value: %TRUE on success, %FALSE on failure
429 g_input_stream_close (GInputStream *stream,
430 GCancellable *cancellable,
433 GInputStreamClass *class;
436 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
438 class = G_INPUT_STREAM_GET_CLASS (stream);
440 if (stream->priv->closed)
445 if (!g_input_stream_set_pending (stream, error))
449 g_cancellable_push_current (cancellable);
452 res = class->close_fn (stream, cancellable, error);
455 g_cancellable_pop_current (cancellable);
457 g_input_stream_clear_pending (stream);
459 stream->priv->closed = TRUE;
465 async_ready_callback_wrapper (GObject *source_object,
469 GInputStream *stream = G_INPUT_STREAM (source_object);
471 g_input_stream_clear_pending (stream);
472 if (stream->priv->outstanding_callback)
473 (*stream->priv->outstanding_callback) (source_object, res, user_data);
474 g_object_unref (stream);
478 async_ready_close_callback_wrapper (GObject *source_object,
482 GInputStream *stream = G_INPUT_STREAM (source_object);
484 g_input_stream_clear_pending (stream);
485 stream->priv->closed = TRUE;
486 if (stream->priv->outstanding_callback)
487 (*stream->priv->outstanding_callback) (source_object, res, user_data);
488 g_object_unref (stream);
492 * g_input_stream_read_async:
493 * @stream: A #GInputStream.
494 * @buffer: a buffer to read data into (which should be at least count bytes long).
495 * @count: the number of bytes that will be read from the stream
496 * @io_priority: the <link linkend="io-priority">I/O priority</link>
498 * @cancellable: optional #GCancellable object, %NULL to ignore.
499 * @callback: callback to call when the request is satisfied
500 * @user_data: the data to pass to callback function
502 * Request an asynchronous read of @count bytes from the stream into the buffer
503 * starting at @buffer. When the operation is finished @callback will be called.
504 * You can then call g_input_stream_read_finish() to get the result of the
507 * During an async request no other sync and async calls are allowed, and will
508 * result in %G_IO_ERROR_PENDING errors.
510 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
512 * On success, the number of bytes read into the buffer will be passed to the
513 * callback. It is not an error if this is not the same as the requested size, as it
514 * can happen e.g. near the end of a file, but generally we try to read
515 * as many bytes as requested. Zero is returned on end of file
516 * (or if @count is zero), but never otherwise.
518 * Any outstanding i/o request with higher priority (lower numerical value) will
519 * be executed before an outstanding request with lower priority. Default
520 * priority is %G_PRIORITY_DEFAULT.
522 * The asyncronous methods have a default fallback that uses threads to implement
523 * asynchronicity, so they are optional for inheriting classes. However, if you
524 * override one you must override all.
527 g_input_stream_read_async (GInputStream *stream,
531 GCancellable *cancellable,
532 GAsyncReadyCallback callback,
535 GInputStreamClass *class;
536 GSimpleAsyncResult *simple;
537 GError *error = NULL;
539 g_return_if_fail (G_IS_INPUT_STREAM (stream));
540 g_return_if_fail (buffer != NULL);
544 simple = g_simple_async_result_new (G_OBJECT (stream),
547 g_input_stream_read_async);
548 g_simple_async_result_complete_in_idle (simple);
549 g_object_unref (simple);
553 if (((gssize) count) < 0)
555 g_simple_async_report_error_in_idle (G_OBJECT (stream),
558 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
559 _("Too large count value passed to g_input_stream_read_async"));
563 if (!g_input_stream_set_pending (stream, &error))
565 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
569 g_error_free (error);
573 class = G_INPUT_STREAM_GET_CLASS (stream);
574 stream->priv->outstanding_callback = callback;
575 g_object_ref (stream);
576 class->read_async (stream, buffer, count, io_priority, cancellable,
577 async_ready_callback_wrapper, user_data);
581 * g_input_stream_read_finish:
582 * @stream: a #GInputStream.
583 * @result: a #GAsyncResult.
584 * @error: a #GError location to store the error occuring, or %NULL to
587 * Finishes an asynchronous stream read operation.
589 * Returns: number of bytes read in, or -1 on error.
592 g_input_stream_read_finish (GInputStream *stream,
593 GAsyncResult *result,
596 GSimpleAsyncResult *simple;
597 GInputStreamClass *class;
599 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
600 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
602 if (G_IS_SIMPLE_ASYNC_RESULT (result))
604 simple = G_SIMPLE_ASYNC_RESULT (result);
605 if (g_simple_async_result_propagate_error (simple, error))
608 /* Special case read of 0 bytes */
609 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
613 class = G_INPUT_STREAM_GET_CLASS (stream);
614 return class->read_finish (stream, result, error);
618 * g_input_stream_skip_async:
619 * @stream: A #GInputStream.
620 * @count: the number of bytes that will be skipped from the stream
621 * @io_priority: the <link linkend="io-priority">I/O priority</link>
623 * @cancellable: optional #GCancellable object, %NULL to ignore.
624 * @callback: callback to call when the request is satisfied
625 * @user_data: the data to pass to callback function
627 * Request an asynchronous skip of @count bytes from the stream into the buffer
628 * starting at @buffer. When the operation is finished @callback will be called.
629 * You can then call g_input_stream_skip_finish() to get the result of the
632 * During an async request no other sync and async calls are allowed, and will
633 * result in %G_IO_ERROR_PENDING errors.
635 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
637 * On success, the number of bytes skipped will be passed to the
638 * callback. It is not an error if this is not the same as the requested size, as it
639 * can happen e.g. near the end of a file, but generally we try to skip
640 * as many bytes as requested. Zero is returned on end of file
641 * (or if @count is zero), but never otherwise.
643 * Any outstanding i/o request with higher priority (lower numerical value) will
644 * be executed before an outstanding request with lower priority. Default
645 * priority is %G_PRIORITY_DEFAULT.
647 * The asyncronous methods have a default fallback that uses threads to implement
648 * asynchronicity, so they are optional for inheriting classes. However, if you
649 * override one you must override all.
652 g_input_stream_skip_async (GInputStream *stream,
655 GCancellable *cancellable,
656 GAsyncReadyCallback callback,
659 GInputStreamClass *class;
660 GSimpleAsyncResult *simple;
661 GError *error = NULL;
663 g_return_if_fail (G_IS_INPUT_STREAM (stream));
667 simple = g_simple_async_result_new (G_OBJECT (stream),
670 g_input_stream_skip_async);
672 g_simple_async_result_complete_in_idle (simple);
673 g_object_unref (simple);
677 if (((gssize) count) < 0)
679 g_simple_async_report_error_in_idle (G_OBJECT (stream),
682 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
683 _("Too large count value passed to g_input_stream_skip_async"));
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 (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
892 _("Stream is already closed"));
896 if (stream->priv->pending)
898 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
899 _("Stream has outstanding operation"));
903 stream->priv->pending = TRUE;
908 * g_input_stream_clear_pending:
909 * @stream: input stream
911 * Clears the pending flag on @stream.
914 g_input_stream_clear_pending (GInputStream *stream)
916 g_return_if_fail (G_IS_INPUT_STREAM (stream));
918 stream->priv->pending = FALSE;
921 /********************************************
922 * Default implementation of async ops *
923 ********************************************/
927 gsize count_requested;
932 read_async_thread (GSimpleAsyncResult *res,
934 GCancellable *cancellable)
937 GInputStreamClass *class;
938 GError *error = NULL;
940 op = g_simple_async_result_get_op_res_gpointer (res);
942 class = G_INPUT_STREAM_GET_CLASS (object);
944 op->count_read = class->read_fn (G_INPUT_STREAM (object),
945 op->buffer, op->count_requested,
946 cancellable, &error);
947 if (op->count_read == -1)
949 g_simple_async_result_set_from_error (res, error);
950 g_error_free (error);
955 g_input_stream_real_read_async (GInputStream *stream,
959 GCancellable *cancellable,
960 GAsyncReadyCallback callback,
963 GSimpleAsyncResult *res;
966 op = g_new (ReadData, 1);
967 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
968 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
970 op->count_requested = count;
972 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
973 g_object_unref (res);
977 g_input_stream_real_read_finish (GInputStream *stream,
978 GAsyncResult *result,
981 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
984 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
985 g_input_stream_real_read_async);
987 op = g_simple_async_result_get_op_res_gpointer (simple);
989 return op->count_read;
993 gsize count_requested;
994 gssize count_skipped;
999 skip_async_thread (GSimpleAsyncResult *res,
1001 GCancellable *cancellable)
1004 GInputStreamClass *class;
1005 GError *error = NULL;
1007 class = G_INPUT_STREAM_GET_CLASS (object);
1008 op = g_simple_async_result_get_op_res_gpointer (res);
1009 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1010 op->count_requested,
1011 cancellable, &error);
1012 if (op->count_skipped == -1)
1014 g_simple_async_result_set_from_error (res, error);
1015 g_error_free (error);
1022 gsize count_skipped;
1024 GCancellable *cancellable;
1026 GAsyncReadyCallback callback;
1027 } SkipFallbackAsyncData;
1030 skip_callback_wrapper (GObject *source_object,
1034 GInputStreamClass *class;
1035 SkipFallbackAsyncData *data = user_data;
1037 GSimpleAsyncResult *simple;
1038 GError *error = NULL;
1041 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1046 data->count_skipped += ret;
1048 if (data->count > 0)
1050 class = G_INPUT_STREAM_GET_CLASS (source_object);
1051 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1052 skip_callback_wrapper, data);
1057 op = g_new0 (SkipData, 1);
1058 op->count_skipped = data->count_skipped;
1059 simple = g_simple_async_result_new (source_object,
1060 data->callback, data->user_data,
1061 g_input_stream_real_skip_async);
1063 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1067 if (data->count_skipped &&
1068 error->domain == G_IO_ERROR &&
1069 error->code == G_IO_ERROR_CANCELLED)
1070 { /* No error, return partial read */ }
1072 g_simple_async_result_set_from_error (simple, error);
1073 g_error_free (error);
1076 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1077 g_simple_async_result_complete (simple);
1078 g_object_unref (simple);
1084 g_input_stream_real_skip_async (GInputStream *stream,
1087 GCancellable *cancellable,
1088 GAsyncReadyCallback callback,
1091 GInputStreamClass *class;
1093 SkipFallbackAsyncData *data;
1094 GSimpleAsyncResult *res;
1096 class = G_INPUT_STREAM_GET_CLASS (stream);
1098 if (class->read_async == g_input_stream_real_read_async)
1100 /* Read is thread-using async fallback.
1101 * Make skip use threads too, so that we can use a possible sync skip
1102 * implementation. */
1103 op = g_new0 (SkipData, 1);
1105 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1106 g_input_stream_real_skip_async);
1108 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1110 op->count_requested = count;
1112 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1113 g_object_unref (res);
1117 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1119 /* There is a custom async read function, lets use that. */
1120 data = g_new (SkipFallbackAsyncData, 1);
1121 data->count = count;
1122 data->count_skipped = 0;
1123 data->io_prio = io_priority;
1124 data->cancellable = cancellable;
1125 data->callback = callback;
1126 data->user_data = user_data;
1127 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1128 skip_callback_wrapper, data);
1134 g_input_stream_real_skip_finish (GInputStream *stream,
1135 GAsyncResult *result,
1138 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1141 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1142 op = g_simple_async_result_get_op_res_gpointer (simple);
1143 return op->count_skipped;
1147 close_async_thread (GSimpleAsyncResult *res,
1149 GCancellable *cancellable)
1151 GInputStreamClass *class;
1152 GError *error = NULL;
1155 /* Auto handling of cancelation disabled, and ignore
1156 cancellation, since we want to close things anyway, although
1157 possibly in a quick-n-dirty way. At least we never want to leak
1160 class = G_INPUT_STREAM_GET_CLASS (object);
1161 result = class->close_fn (G_INPUT_STREAM (object), cancellable, &error);
1164 g_simple_async_result_set_from_error (res, error);
1165 g_error_free (error);
1170 g_input_stream_real_close_async (GInputStream *stream,
1172 GCancellable *cancellable,
1173 GAsyncReadyCallback callback,
1176 GSimpleAsyncResult *res;
1178 res = g_simple_async_result_new (G_OBJECT (stream),
1181 g_input_stream_real_close_async);
1183 g_simple_async_result_set_handle_cancellation (res, FALSE);
1185 g_simple_async_result_run_in_thread (res,
1189 g_object_unref (res);
1193 g_input_stream_real_close_finish (GInputStream *stream,
1194 GAsyncResult *result,
1197 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1198 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1202 #define __G_INPUT_STREAM_C__
1203 #include "gioaliasdef.c"