1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "ginputstream.h"
28 #include "gseekable.h"
29 #include "gsimpleasyncresult.h"
34 * SECTION:ginputstream
35 * @short_description: Base class for implementing streaming input
41 G_DEFINE_TYPE (GInputStream, g_input_stream, G_TYPE_OBJECT);
43 struct _GInputStreamPrivate {
46 GAsyncReadyCallback outstanding_callback;
49 static gssize g_input_stream_real_skip (GInputStream *stream,
51 GCancellable *cancellable,
53 static void g_input_stream_real_read_async (GInputStream *stream,
57 GCancellable *cancellable,
58 GAsyncReadyCallback callback,
60 static gssize g_input_stream_real_read_finish (GInputStream *stream,
63 static void g_input_stream_real_skip_async (GInputStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_input_stream_real_skip_finish (GInputStream *stream,
72 static void g_input_stream_real_close_async (GInputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gboolean g_input_stream_real_close_finish (GInputStream *stream,
82 g_input_stream_finalize (GObject *object)
86 stream = G_INPUT_STREAM (object);
88 if (!stream->priv->closed)
89 g_input_stream_close (stream, NULL, NULL);
91 if (G_OBJECT_CLASS (g_input_stream_parent_class)->finalize)
92 (*G_OBJECT_CLASS (g_input_stream_parent_class)->finalize) (object);
96 g_input_stream_dispose (GObject *object)
100 stream = G_INPUT_STREAM (object);
102 if (!stream->priv->closed)
103 g_input_stream_close (stream, NULL, NULL);
105 if (G_OBJECT_CLASS (g_input_stream_parent_class)->dispose)
106 (*G_OBJECT_CLASS (g_input_stream_parent_class)->dispose) (object);
111 g_input_stream_class_init (GInputStreamClass *klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 g_type_class_add_private (klass, sizeof (GInputStreamPrivate));
117 gobject_class->finalize = g_input_stream_finalize;
118 gobject_class->dispose = g_input_stream_dispose;
120 klass->skip = g_input_stream_real_skip;
121 klass->read_async = g_input_stream_real_read_async;
122 klass->read_finish = g_input_stream_real_read_finish;
123 klass->skip_async = g_input_stream_real_skip_async;
124 klass->skip_finish = g_input_stream_real_skip_finish;
125 klass->close_async = g_input_stream_real_close_async;
126 klass->close_finish = g_input_stream_real_close_finish;
130 g_input_stream_init (GInputStream *stream)
132 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
134 GInputStreamPrivate);
138 * g_input_stream_read:
139 * @stream: a #GInputStream.
140 * @buffer: a buffer to read data into (which should be at least count bytes long).
141 * @count: the number of bytes that will be read from the stream
142 * @cancellable: optional #GCancellable object, %NULL to ignore.
143 * @error: location to store the error occuring, or %NULL to ignore
145 * Tries to read @count bytes from the stream into the buffer starting at
146 * @buffer. Will block during this read.
148 * If count is zero returns zero and does nothing. A value of @count
149 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
151 * On success, the number of bytes read into the buffer is returned.
152 * It is not an error if this is not the same as the requested size, as it
153 * can happen e.g. near the end of a file. Zero is returned on end of file
154 * (or if @count is zero), but never otherwise.
156 * If @cancellable is not NULL, then the operation can be cancelled by
157 * triggering the cancellable object from another thread. If the operation
158 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
159 * operation was partially finished when the operation was cancelled the
160 * partial result will be returned, without an error.
162 * On error -1 is returned and @error is set accordingly.
164 * Return value: Number of bytes read, or -1 on error
167 g_input_stream_read (GInputStream *stream,
170 GCancellable *cancellable,
173 GInputStreamClass *class;
176 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
177 g_return_val_if_fail (buffer != NULL, 0);
182 if (((gssize) count) < 0)
184 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
185 _("Too large count value passed to g_input_stream_read"));
189 if (stream->priv->closed)
191 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
192 _("Stream is already closed"));
196 if (stream->priv->pending)
198 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
199 _("Stream has outstanding operation"));
203 class = G_INPUT_STREAM_GET_CLASS (stream);
205 if (class->read == NULL)
207 g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
208 _("Input stream doesn't implement read"));
213 g_push_current_cancellable (cancellable);
215 stream->priv->pending = TRUE;
216 res = class->read (stream, buffer, count, cancellable, error);
217 stream->priv->pending = FALSE;
220 g_pop_current_cancellable (cancellable);
226 * g_input_stream_read_all:
227 * @stream: a #GInputStream.
228 * @buffer: a buffer to read data into (which should be at least count bytes long).
229 * @count: the number of bytes that will be read from the stream
230 * @bytes_read: location to store the number of bytes that was read from the stream
231 * @cancellable: optional #GCancellable object, %NULL to ignore.
232 * @error: location to store the error occuring, or %NULL to ignore
234 * Tries to read @count bytes from the stream into the buffer starting at
235 * @buffer. Will block during this read.
237 * This function is similar to g_input_stream_read(), except it tries to
238 * read as many bytes as requested, only stopping on an error or end of stream.
240 * On a successful read of @count bytes, or if we reached the end of the
241 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
244 * If there is an error during the operation %FALSE is returned and @error
245 * is set to indicate the error status, @bytes_read is updated to contain
246 * the number of bytes read into @buffer before the error occured.
248 * Return value: %TRUE on success, %FALSE if there was an error
251 g_input_stream_read_all (GInputStream *stream,
255 GCancellable *cancellable,
261 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
262 g_return_val_if_fail (buffer != NULL, FALSE);
265 while (_bytes_read < count)
267 res = g_input_stream_read (stream, (char *)buffer + _bytes_read, count - _bytes_read,
272 *bytes_read = _bytes_read;
283 *bytes_read = _bytes_read;
288 * g_input_stream_skip:
289 * @stream: a #GInputStream.
290 * @count: the number of bytes that will be skipped from the stream
291 * @cancellable: optional #GCancellable object, %NULL to ignore.
292 * @error: location to store the error occuring, or %NULL to ignore
294 * Tries to skip @count bytes from the stream. Will block during the operation.
296 * This is identical to g_input_stream_read(), from a behaviour standpoint,
297 * but the bytes that are skipped are not returned to the user. Some
298 * streams have an implementation that is more efficient than reading the data.
300 * This function is optional for inherited classes, as the default implementation
301 * emulates it using read.
303 * If @cancellable is not %NULL, then the operation can be cancelled by
304 * triggering the cancellable object from another thread. If the operation
305 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
306 * operation was partially finished when the operation was cancelled the
307 * partial result will be returned, without an error.
309 * Return value: Number of bytes skipped, or -1 on error
312 g_input_stream_skip (GInputStream *stream,
314 GCancellable *cancellable,
317 GInputStreamClass *class;
320 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
325 if (((gssize) count) < 0)
327 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
328 _("Too large count value passed to g_input_stream_skip"));
332 if (stream->priv->closed)
334 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
335 _("Stream is already closed"));
339 if (stream->priv->pending)
341 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
342 _("Stream has outstanding operation"));
346 class = G_INPUT_STREAM_GET_CLASS (stream);
349 g_push_current_cancellable (cancellable);
351 stream->priv->pending = TRUE;
352 res = class->skip (stream, count, cancellable, error);
353 stream->priv->pending = FALSE;
356 g_pop_current_cancellable (cancellable);
362 g_input_stream_real_skip (GInputStream *stream,
364 GCancellable *cancellable,
367 GInputStreamClass *class;
368 gssize ret, read_bytes;
372 class = G_INPUT_STREAM_GET_CLASS (stream);
374 if (G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)))
376 if (g_seekable_seek (G_SEEKABLE (stream),
384 /* If not seekable, or seek failed, fall back to reading data: */
386 class = G_INPUT_STREAM_GET_CLASS (stream);
393 ret = class->read (stream, buffer, MIN (sizeof (buffer), count),
394 cancellable, &my_error);
397 if (read_bytes > 0 &&
398 my_error->domain == G_IO_ERROR &&
399 my_error->code == G_IO_ERROR_CANCELLED)
401 g_error_free (my_error);
405 g_propagate_error (error, my_error);
412 if (ret == 0 || count == 0)
418 * g_input_stream_close:
419 * @stream: A #GInputStream.
420 * @cancellable: optional #GCancellable object, %NULL to ignore.
421 * @error: location to store the error occuring, or %NULL to ignore
423 * Closes the stream, releasing resources related to it.
425 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
426 * Closing a stream multiple times will not return an error.
428 * Streams will be automatically closed when the last reference
429 * is dropped, but you might want to call make sure resources
430 * are released as early as possible.
432 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
433 * open after the stream is closed. See the documentation for the individual
434 * stream for details.
436 * On failure the first error that happened will be reported, but the close
437 * operation will finish as much as possible. A stream that failed to
438 * close will still return %G_IO_ERROR_CLOSED all operations. Still, it
439 * is important to check and report the error to the user.
441 * If @cancellable is not NULL, then the operation can be cancelled by
442 * triggering the cancellable object from another thread. If the operation
443 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
444 * Cancelling a close will still leave the stream closed, but some streams
445 * can use a faster close that doesn't block to e.g. check errors.
447 * Return value: %TRUE on success, %FALSE on failure
450 g_input_stream_close (GInputStream *stream,
451 GCancellable *cancellable,
454 GInputStreamClass *class;
457 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
459 class = G_INPUT_STREAM_GET_CLASS (stream);
461 if (stream->priv->closed)
464 if (stream->priv->pending)
466 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
467 _("Stream has outstanding operation"));
473 stream->priv->pending = TRUE;
476 g_push_current_cancellable (cancellable);
479 res = class->close (stream, cancellable, error);
482 g_pop_current_cancellable (cancellable);
484 stream->priv->closed = TRUE;
486 stream->priv->pending = FALSE;
492 async_ready_callback_wrapper (GObject *source_object,
496 GInputStream *stream = G_INPUT_STREAM (source_object);
498 stream->priv->pending = FALSE;
499 if (stream->priv->outstanding_callback)
500 (*stream->priv->outstanding_callback) (source_object, res, user_data);
501 g_object_unref (stream);
505 async_ready_close_callback_wrapper (GObject *source_object,
509 GInputStream *stream = G_INPUT_STREAM (source_object);
511 stream->priv->pending = FALSE;
512 stream->priv->closed = TRUE;
513 if (stream->priv->outstanding_callback)
514 (*stream->priv->outstanding_callback) (source_object, res, user_data);
515 g_object_unref (stream);
519 * g_input_stream_read_async:
520 * @stream: A #GInputStream.
521 * @buffer: a buffer to read data into (which should be at least count bytes long).
522 * @count: the number of bytes that will be read from the stream
523 * @io_priority: the <link linkend="io-priority">I/O priority</link>
525 * @cancellable: optional #GCancellable object, %NULL to ignore.
526 * @callback: callback to call when the request is satisfied
527 * @user_data: the data to pass to callback function
529 * Request an asynchronous read of @count bytes from the stream into the buffer
530 * starting at @buffer. When the operation is finished @callback will be called,
531 * giving the results.
533 * During an async request no other sync and async calls are allowed, and will
534 * result in %G_IO_ERROR_PENDING errors.
536 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
538 * On success, the number of bytes read into the buffer will be passed to the
539 * callback. It is not an error if this is not the same as the requested size, as it
540 * can happen e.g. near the end of a file, but generally we try to read
541 * as many bytes as requested. Zero is returned on end of file
542 * (or if @count is zero), but never otherwise.
544 * Any outstanding i/o request with higher priority (lower numerical value) will
545 * be executed before an outstanding request with lower priority. Default
546 * priority is %G_PRIORITY_DEFAULT.
548 * The asyncronous methods have a default fallback that uses threads to implement
549 * asynchronicity, so they are optional for inheriting classes. However, if you
550 * override one you must override all.
553 g_input_stream_read_async (GInputStream *stream,
557 GCancellable *cancellable,
558 GAsyncReadyCallback callback,
561 GInputStreamClass *class;
562 GSimpleAsyncResult *simple;
564 g_return_if_fail (G_IS_INPUT_STREAM (stream));
565 g_return_if_fail (buffer != NULL);
569 simple = g_simple_async_result_new (G_OBJECT (stream),
572 g_input_stream_read_async);
573 g_simple_async_result_complete_in_idle (simple);
574 g_object_unref (simple);
578 if (((gssize) count) < 0)
580 g_simple_async_report_error_in_idle (G_OBJECT (stream),
583 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
584 _("Too large count value passed to g_input_stream_read_async"));
588 if (stream->priv->closed)
590 g_simple_async_report_error_in_idle (G_OBJECT (stream),
593 G_IO_ERROR, G_IO_ERROR_CLOSED,
594 _("Stream is already closed"));
598 if (stream->priv->pending)
600 g_simple_async_report_error_in_idle (G_OBJECT (stream),
603 G_IO_ERROR, G_IO_ERROR_PENDING,
604 _("Stream has outstanding operation"));
608 class = G_INPUT_STREAM_GET_CLASS (stream);
610 stream->priv->pending = TRUE;
611 stream->priv->outstanding_callback = callback;
612 g_object_ref (stream);
613 class->read_async (stream, buffer, count, io_priority, cancellable,
614 async_ready_callback_wrapper, user_data);
618 * g_input_stream_read_finish:
619 * @stream: a #GInputStream.
620 * @result: a #GAsyncResult.
621 * @error: a #GError location to store the error occuring, or %NULL to
624 * Finishes an asynchronous stream read operation.
626 * Returns: number of bytes read in, or -1 on error.
629 g_input_stream_read_finish (GInputStream *stream,
630 GAsyncResult *result,
633 GSimpleAsyncResult *simple;
634 GInputStreamClass *class;
636 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
637 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
639 if (G_IS_SIMPLE_ASYNC_RESULT (result))
641 simple = G_SIMPLE_ASYNC_RESULT (result);
642 if (g_simple_async_result_propagate_error (simple, error))
645 /* Special case read of 0 bytes */
646 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
650 class = G_INPUT_STREAM_GET_CLASS (stream);
651 return class->read_finish (stream, result, error);
655 * g_input_stream_skip_async:
656 * @stream: A #GInputStream.
657 * @count: the number of bytes that will be skipped from the stream
658 * @io_priority: the <link linkend="io-priority">I/O priority</link>
660 * @cancellable: optional #GCancellable object, %NULL to ignore.
661 * @callback: callback to call when the request is satisfied
662 * @user_data: the data to pass to callback function
664 * Request an asynchronous skip of @count bytes from the stream into the buffer
665 * starting at @buffer. When the operation is finished @callback will be called,
666 * giving the results.
668 * During an async request no other sync and async calls are allowed, and will
669 * result in %G_IO_ERROR_PENDING errors.
671 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
673 * On success, the number of bytes skipped will be passed to the
674 * callback. It is not an error if this is not the same as the requested size, as it
675 * can happen e.g. near the end of a file, but generally we try to skip
676 * as many bytes as requested. Zero is returned on end of file
677 * (or if @count is zero), but never otherwise.
679 * Any outstanding i/o request with higher priority (lower numerical value) will
680 * be executed before an outstanding request with lower priority. Default
681 * priority is %G_PRIORITY_DEFAULT.
683 * The asyncronous methods have a default fallback that uses threads to implement
684 * asynchronicity, so they are optional for inheriting classes. However, if you
685 * override one you must override all.
688 g_input_stream_skip_async (GInputStream *stream,
691 GCancellable *cancellable,
692 GAsyncReadyCallback callback,
695 GInputStreamClass *class;
696 GSimpleAsyncResult *simple;
698 g_return_if_fail (G_IS_INPUT_STREAM (stream));
702 simple = g_simple_async_result_new (G_OBJECT (stream),
705 g_input_stream_skip_async);
707 g_simple_async_result_complete_in_idle (simple);
708 g_object_unref (simple);
712 if (((gssize) count) < 0)
714 g_simple_async_report_error_in_idle (G_OBJECT (stream),
717 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
718 _("Too large count value passed to g_input_stream_skip_async"));
722 if (stream->priv->closed)
724 g_simple_async_report_error_in_idle (G_OBJECT (stream),
727 G_IO_ERROR, G_IO_ERROR_CLOSED,
728 _("Stream is already closed"));
732 if (stream->priv->pending)
734 g_simple_async_report_error_in_idle (G_OBJECT (stream),
737 G_IO_ERROR, G_IO_ERROR_PENDING,
738 _("Stream has outstanding operation"));
742 class = G_INPUT_STREAM_GET_CLASS (stream);
743 stream->priv->pending = TRUE;
744 stream->priv->outstanding_callback = callback;
745 g_object_ref (stream);
746 class->skip_async (stream, count, io_priority, cancellable,
747 async_ready_callback_wrapper, user_data);
751 * g_input_stream_skip_finish:
752 * @stream: a #GInputStream.
753 * @result: a #GAsyncResult.
754 * @error: a #GError location to store the error occuring, or %NULL to
757 * Finishes a stream skip operation.
759 * Returns: the size of the bytes skipped, or %-1 on error.
762 g_input_stream_skip_finish (GInputStream *stream,
763 GAsyncResult *result,
766 GSimpleAsyncResult *simple;
767 GInputStreamClass *class;
769 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
770 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
772 if (G_IS_SIMPLE_ASYNC_RESULT (result))
774 simple = G_SIMPLE_ASYNC_RESULT (result);
775 if (g_simple_async_result_propagate_error (simple, error))
778 /* Special case skip of 0 bytes */
779 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
783 class = G_INPUT_STREAM_GET_CLASS (stream);
784 return class->skip_finish (stream, result, error);
788 * g_input_stream_close_async:
789 * @stream: A #GInputStream.
790 * @io_priority: the <link linkend="io-priority">I/O priority</link>
792 * @cancellable: optional cancellable object
793 * @callback: callback to call when the request is satisfied
794 * @user_data: the data to pass to callback function
796 * Requests an asynchronous closes of the stream, releasing resources related to it.
797 * When the operation is finished @callback will be called, giving the results.
799 * For behaviour details see g_input_stream_close().
801 * The asyncronous methods have a default fallback that uses threads to implement
802 * asynchronicity, so they are optional for inheriting classes. However, if you
803 * override one you must override all.
806 g_input_stream_close_async (GInputStream *stream,
808 GCancellable *cancellable,
809 GAsyncReadyCallback callback,
812 GInputStreamClass *class;
813 GSimpleAsyncResult *simple;
815 g_return_if_fail (G_IS_INPUT_STREAM (stream));
817 if (stream->priv->closed)
819 simple = g_simple_async_result_new (G_OBJECT (stream),
822 g_input_stream_close_async);
824 g_simple_async_result_complete_in_idle (simple);
825 g_object_unref (simple);
829 if (stream->priv->pending)
831 g_simple_async_report_error_in_idle (G_OBJECT (stream),
834 G_IO_ERROR, G_IO_ERROR_PENDING,
835 _("Stream has outstanding operation"));
839 class = G_INPUT_STREAM_GET_CLASS (stream);
840 stream->priv->pending = TRUE;
841 stream->priv->outstanding_callback = callback;
842 g_object_ref (stream);
843 class->close_async (stream, io_priority, cancellable,
844 async_ready_close_callback_wrapper, user_data);
848 * g_input_stream_close_finish:
849 * @stream: a #GInputStream.
850 * @result: a #GAsyncResult.
851 * @error: a #GError location to store the error occuring, or %NULL to
854 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
856 * Returns: %TRUE if the stream was closed successfully.
859 g_input_stream_close_finish (GInputStream *stream,
860 GAsyncResult *result,
863 GSimpleAsyncResult *simple;
864 GInputStreamClass *class;
866 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
867 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
869 if (G_IS_SIMPLE_ASYNC_RESULT (result))
871 simple = G_SIMPLE_ASYNC_RESULT (result);
872 if (g_simple_async_result_propagate_error (simple, error))
875 /* Special case already closed */
876 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
880 class = G_INPUT_STREAM_GET_CLASS (stream);
881 return class->close_finish (stream, result, error);
885 * g_input_stream_is_closed:
886 * @stream: input stream.
888 * Checks if an input stream is closed.
890 * Returns: %TRUE if the stream is closed.
893 g_input_stream_is_closed (GInputStream *stream)
895 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
897 return stream->priv->closed;
901 * g_input_stream_has_pending:
902 * @stream: input stream.
904 * Checks if an input stream has pending actions.
906 * Returns: %TRUE if @stream has pending actions.
909 g_input_stream_has_pending (GInputStream *stream)
911 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
913 return stream->priv->pending;
917 * g_input_stream_set_pending:
918 * @stream: input stream
921 * Sets @stream has actions pending.
924 g_input_stream_set_pending (GInputStream *stream,
927 g_return_if_fail (G_IS_INPUT_STREAM (stream));
929 stream->priv->pending = pending;
932 /********************************************
933 * Default implementation of async ops *
934 ********************************************/
938 gsize count_requested;
943 read_async_thread (GSimpleAsyncResult *res,
945 GCancellable *cancellable)
948 GInputStreamClass *class;
949 GError *error = NULL;
951 op = g_simple_async_result_get_op_res_gpointer (res);
953 class = G_INPUT_STREAM_GET_CLASS (object);
955 op->count_read = class->read (G_INPUT_STREAM (object),
956 op->buffer, op->count_requested,
957 cancellable, &error);
958 if (op->count_read == -1)
960 g_simple_async_result_set_from_error (res, error);
961 g_error_free (error);
966 g_input_stream_real_read_async (GInputStream *stream,
970 GCancellable *cancellable,
971 GAsyncReadyCallback callback,
974 GSimpleAsyncResult *res;
977 op = g_new (ReadData, 1);
978 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
979 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
981 op->count_requested = count;
983 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
984 g_object_unref (res);
988 g_input_stream_real_read_finish (GInputStream *stream,
989 GAsyncResult *result,
992 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
995 g_assert (g_simple_async_result_get_source_tag (simple) ==
996 g_input_stream_real_read_async);
998 op = g_simple_async_result_get_op_res_gpointer (simple);
1000 return op->count_read;
1004 gsize count_requested;
1005 gssize count_skipped;
1010 skip_async_thread (GSimpleAsyncResult *res,
1012 GCancellable *cancellable)
1015 GInputStreamClass *class;
1016 GError *error = NULL;
1018 class = G_INPUT_STREAM_GET_CLASS (object);
1019 op = g_simple_async_result_get_op_res_gpointer (res);
1020 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1021 op->count_requested,
1022 cancellable, &error);
1023 if (op->count_skipped == -1)
1025 g_simple_async_result_set_from_error (res, error);
1026 g_error_free (error);
1033 gsize count_skipped;
1035 GCancellable *cancellable;
1037 GAsyncReadyCallback callback;
1038 } SkipFallbackAsyncData;
1041 skip_callback_wrapper (GObject *source_object,
1045 GInputStreamClass *class;
1046 SkipFallbackAsyncData *data = user_data;
1048 GSimpleAsyncResult *simple;
1049 GError *error = NULL;
1052 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1057 data->count_skipped += ret;
1059 if (data->count > 0)
1061 class = G_INPUT_STREAM_GET_CLASS (source_object);
1062 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1063 skip_callback_wrapper, data);
1068 op = g_new0 (SkipData, 1);
1069 op->count_skipped = data->count_skipped;
1070 simple = g_simple_async_result_new (source_object,
1071 data->callback, data->user_data,
1072 g_input_stream_real_skip_async);
1074 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1078 if (data->count_skipped &&
1079 error->domain == G_IO_ERROR &&
1080 error->code == G_IO_ERROR_CANCELLED)
1081 { /* No error, return partial read */ }
1083 g_simple_async_result_set_from_error (simple, error);
1084 g_error_free (error);
1087 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1088 g_simple_async_result_complete (simple);
1089 g_object_unref (simple);
1095 g_input_stream_real_skip_async (GInputStream *stream,
1098 GCancellable *cancellable,
1099 GAsyncReadyCallback callback,
1102 GInputStreamClass *class;
1104 SkipFallbackAsyncData *data;
1105 GSimpleAsyncResult *res;
1107 class = G_INPUT_STREAM_GET_CLASS (stream);
1109 if (class->read_async == g_input_stream_real_read_async)
1111 /* Read is thread-using async fallback.
1112 * Make skip use threads too, so that we can use a possible sync skip
1113 * implementation. */
1114 op = g_new0 (SkipData, 1);
1116 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1117 g_input_stream_real_skip_async);
1119 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1121 op->count_requested = count;
1123 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1124 g_object_unref (res);
1128 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1130 /* There is a custom async read function, lets use that. */
1131 data = g_new (SkipFallbackAsyncData, 1);
1132 data->count = count;
1133 data->count_skipped = 0;
1134 data->io_prio = io_priority;
1135 data->cancellable = cancellable;
1136 data->callback = callback;
1137 data->user_data = user_data;
1138 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1139 skip_callback_wrapper, data);
1145 g_input_stream_real_skip_finish (GInputStream *stream,
1146 GAsyncResult *result,
1149 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1152 g_assert (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1153 op = g_simple_async_result_get_op_res_gpointer (simple);
1154 return op->count_skipped;
1158 close_async_thread (GSimpleAsyncResult *res,
1160 GCancellable *cancellable)
1162 GInputStreamClass *class;
1163 GError *error = NULL;
1166 /* Auto handling of cancelation disabled, and ignore
1167 cancellation, since we want to close things anyway, although
1168 possibly in a quick-n-dirty way. At least we never want to leak
1171 class = G_INPUT_STREAM_GET_CLASS (object);
1172 result = class->close (G_INPUT_STREAM (object), cancellable, &error);
1175 g_simple_async_result_set_from_error (res, error);
1176 g_error_free (error);
1181 g_input_stream_real_close_async (GInputStream *stream,
1183 GCancellable *cancellable,
1184 GAsyncReadyCallback callback,
1187 GSimpleAsyncResult *res;
1189 res = g_simple_async_result_new (G_OBJECT (stream),
1192 g_input_stream_real_close_async);
1194 g_simple_async_result_set_handle_cancellation (res, FALSE);
1196 g_simple_async_result_run_in_thread (res,
1200 g_object_unref (res);
1204 g_input_stream_real_close_finish (GInputStream *stream,
1205 GAsyncResult *result,
1208 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1209 g_assert (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1213 #define __G_INPUT_STREAM_C__
1214 #include "gioaliasdef.c"