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 io priority of the request. the io priority of the request
524 * @cancellable: optional #GCancellable object, %NULL to ignore.
525 * @callback: callback to call when the request is satisfied
526 * @user_data: the data to pass to callback function
528 * Request an asynchronous read of @count bytes from the stream into the buffer
529 * starting at @buffer. When the operation is finished @callback will be called,
530 * giving the results.
532 * During an async request no other sync and async calls are allowed, and will
533 * result in %G_IO_ERROR_PENDING errors.
535 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
537 * On success, the number of bytes read into the buffer will be passed to the
538 * callback. It is not an error if this is not the same as the requested size, as it
539 * can happen e.g. near the end of a file, but generally we try to read
540 * as many bytes as requested. Zero is returned on end of file
541 * (or if @count is zero), but never otherwise.
543 * Any outstanding i/o request with higher priority (lower numerical value) will
544 * be executed before an outstanding request with lower priority. Default
545 * priority is %G_PRIORITY_DEFAULT.
547 * The asyncronous methods have a default fallback that uses threads to implement
548 * asynchronicity, so they are optional for inheriting classes. However, if you
549 * override one you must override all.
552 g_input_stream_read_async (GInputStream *stream,
556 GCancellable *cancellable,
557 GAsyncReadyCallback callback,
560 GInputStreamClass *class;
561 GSimpleAsyncResult *simple;
563 g_return_if_fail (G_IS_INPUT_STREAM (stream));
564 g_return_if_fail (buffer != NULL);
568 simple = g_simple_async_result_new (G_OBJECT (stream),
571 g_input_stream_read_async);
572 g_simple_async_result_complete_in_idle (simple);
573 g_object_unref (simple);
577 if (((gssize) count) < 0)
579 g_simple_async_report_error_in_idle (G_OBJECT (stream),
582 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
583 _("Too large count value passed to g_input_stream_read_async"));
587 if (stream->priv->closed)
589 g_simple_async_report_error_in_idle (G_OBJECT (stream),
592 G_IO_ERROR, G_IO_ERROR_CLOSED,
593 _("Stream is already closed"));
597 if (stream->priv->pending)
599 g_simple_async_report_error_in_idle (G_OBJECT (stream),
602 G_IO_ERROR, G_IO_ERROR_PENDING,
603 _("Stream has outstanding operation"));
607 class = G_INPUT_STREAM_GET_CLASS (stream);
609 stream->priv->pending = TRUE;
610 stream->priv->outstanding_callback = callback;
611 g_object_ref (stream);
612 class->read_async (stream, buffer, count, io_priority, cancellable,
613 async_ready_callback_wrapper, user_data);
617 * g_input_stream_read_finish:
618 * @stream: a #GInputStream.
619 * @result: a #GAsyncResult.
620 * @error: a #GError location to store the error occuring, or %NULL to
623 * Finishes an asynchronous stream read operation.
625 * Returns: number of bytes read in, or -1 on error.
628 g_input_stream_read_finish (GInputStream *stream,
629 GAsyncResult *result,
632 GSimpleAsyncResult *simple;
633 GInputStreamClass *class;
635 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
636 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
638 if (G_IS_SIMPLE_ASYNC_RESULT (result))
640 simple = G_SIMPLE_ASYNC_RESULT (result);
641 if (g_simple_async_result_propagate_error (simple, error))
644 /* Special case read of 0 bytes */
645 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_read_async)
649 class = G_INPUT_STREAM_GET_CLASS (stream);
650 return class->read_finish (stream, result, error);
654 * g_input_stream_skip_async:
655 * @stream: A #GInputStream.
656 * @count: the number of bytes that will be skipped from the stream
657 * @io_priority: the io priority of the request. the io priority of the request
658 * @cancellable: optional #GCancellable object, %NULL to ignore.
659 * @callback: callback to call when the request is satisfied
660 * @user_data: the data to pass to callback function
662 * Request an asynchronous skip of @count bytes from the stream into the buffer
663 * starting at @buffer. When the operation is finished @callback will be called,
664 * giving the results.
666 * During an async request no other sync and async calls are allowed, and will
667 * result in %G_IO_ERROR_PENDING errors.
669 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
671 * On success, the number of bytes skipped will be passed to the
672 * callback. It is not an error if this is not the same as the requested size, as it
673 * can happen e.g. near the end of a file, but generally we try to skip
674 * as many bytes as requested. Zero is returned on end of file
675 * (or if @count is zero), but never otherwise.
677 * Any outstanding i/o request with higher priority (lower numerical value) will
678 * be executed before an outstanding request with lower priority. Default
679 * priority is %G_PRIORITY_DEFAULT.
681 * The asyncronous methods have a default fallback that uses threads to implement
682 * asynchronicity, so they are optional for inheriting classes. However, if you
683 * override one you must override all.
686 g_input_stream_skip_async (GInputStream *stream,
689 GCancellable *cancellable,
690 GAsyncReadyCallback callback,
693 GInputStreamClass *class;
694 GSimpleAsyncResult *simple;
696 g_return_if_fail (G_IS_INPUT_STREAM (stream));
700 simple = g_simple_async_result_new (G_OBJECT (stream),
703 g_input_stream_skip_async);
705 g_simple_async_result_complete_in_idle (simple);
706 g_object_unref (simple);
710 if (((gssize) count) < 0)
712 g_simple_async_report_error_in_idle (G_OBJECT (stream),
715 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
716 _("Too large count value passed to g_input_stream_skip_async"));
720 if (stream->priv->closed)
722 g_simple_async_report_error_in_idle (G_OBJECT (stream),
725 G_IO_ERROR, G_IO_ERROR_CLOSED,
726 _("Stream is already closed"));
730 if (stream->priv->pending)
732 g_simple_async_report_error_in_idle (G_OBJECT (stream),
735 G_IO_ERROR, G_IO_ERROR_PENDING,
736 _("Stream has outstanding operation"));
740 class = G_INPUT_STREAM_GET_CLASS (stream);
741 stream->priv->pending = TRUE;
742 stream->priv->outstanding_callback = callback;
743 g_object_ref (stream);
744 class->skip_async (stream, count, io_priority, cancellable,
745 async_ready_callback_wrapper, user_data);
749 * g_input_stream_skip_finish:
750 * @stream: a #GInputStream.
751 * @result: a #GAsyncResult.
752 * @error: a #GError location to store the error occuring, or %NULL to
755 * Finishes a stream skip operation.
757 * Returns: the size of the bytes skipped, or %-1 on error.
760 g_input_stream_skip_finish (GInputStream *stream,
761 GAsyncResult *result,
764 GSimpleAsyncResult *simple;
765 GInputStreamClass *class;
767 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), -1);
768 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
770 if (G_IS_SIMPLE_ASYNC_RESULT (result))
772 simple = G_SIMPLE_ASYNC_RESULT (result);
773 if (g_simple_async_result_propagate_error (simple, error))
776 /* Special case skip of 0 bytes */
777 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_skip_async)
781 class = G_INPUT_STREAM_GET_CLASS (stream);
782 return class->skip_finish (stream, result, error);
786 * g_input_stream_close_async:
787 * @stream: A #GInputStream.
788 * @io_priority: the io priority of the request. the io priority of the request
789 * @cancellable: optional cancellable object
790 * @callback: callback to call when the request is satisfied
791 * @user_data: the data to pass to callback function
793 * Requests an asynchronous closes of the stream, releasing resources related to it.
794 * When the operation is finished @callback will be called, giving the results.
796 * For behaviour details see g_input_stream_close().
798 * The asyncronous methods have a default fallback that uses threads to implement
799 * asynchronicity, so they are optional for inheriting classes. However, if you
800 * override one you must override all.
803 g_input_stream_close_async (GInputStream *stream,
805 GCancellable *cancellable,
806 GAsyncReadyCallback callback,
809 GInputStreamClass *class;
810 GSimpleAsyncResult *simple;
812 g_return_if_fail (G_IS_INPUT_STREAM (stream));
814 if (stream->priv->closed)
816 simple = g_simple_async_result_new (G_OBJECT (stream),
819 g_input_stream_close_async);
821 g_simple_async_result_complete_in_idle (simple);
822 g_object_unref (simple);
826 if (stream->priv->pending)
828 g_simple_async_report_error_in_idle (G_OBJECT (stream),
831 G_IO_ERROR, G_IO_ERROR_PENDING,
832 _("Stream has outstanding operation"));
836 class = G_INPUT_STREAM_GET_CLASS (stream);
837 stream->priv->pending = TRUE;
838 stream->priv->outstanding_callback = callback;
839 g_object_ref (stream);
840 class->close_async (stream, io_priority, cancellable,
841 async_ready_close_callback_wrapper, user_data);
845 * g_input_stream_close_finish:
846 * @stream: a #GInputStream.
847 * @result: a #GAsyncResult.
848 * @error: a #GError location to store the error occuring, or %NULL to
851 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
853 * Returns: %TRUE if the stream was closed successfully.
856 g_input_stream_close_finish (GInputStream *stream,
857 GAsyncResult *result,
860 GSimpleAsyncResult *simple;
861 GInputStreamClass *class;
863 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
864 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
866 if (G_IS_SIMPLE_ASYNC_RESULT (result))
868 simple = G_SIMPLE_ASYNC_RESULT (result);
869 if (g_simple_async_result_propagate_error (simple, error))
872 /* Special case already closed */
873 if (g_simple_async_result_get_source_tag (simple) == g_input_stream_close_async)
877 class = G_INPUT_STREAM_GET_CLASS (stream);
878 return class->close_finish (stream, result, error);
882 * g_input_stream_is_closed:
883 * @stream: input stream.
885 * Checks if an input stream is closed.
887 * Returns: %TRUE if the stream is closed.
890 g_input_stream_is_closed (GInputStream *stream)
892 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
894 return stream->priv->closed;
898 * g_input_stream_has_pending:
899 * @stream: input stream.
901 * Checks if an input stream has pending actions.
903 * Returns: %TRUE if @stream has pending actions.
906 g_input_stream_has_pending (GInputStream *stream)
908 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), TRUE);
910 return stream->priv->pending;
914 * g_input_stream_set_pending:
915 * @stream: input stream
918 * Sets @stream has actions pending.
921 g_input_stream_set_pending (GInputStream *stream,
924 g_return_if_fail (G_IS_INPUT_STREAM (stream));
926 stream->priv->pending = pending;
929 /********************************************
930 * Default implementation of async ops *
931 ********************************************/
935 gsize count_requested;
940 read_async_thread (GSimpleAsyncResult *res,
942 GCancellable *cancellable)
945 GInputStreamClass *class;
946 GError *error = NULL;
948 op = g_simple_async_result_get_op_res_gpointer (res);
950 class = G_INPUT_STREAM_GET_CLASS (object);
952 op->count_read = class->read (G_INPUT_STREAM (object),
953 op->buffer, op->count_requested,
954 cancellable, &error);
955 if (op->count_read == -1)
957 g_simple_async_result_set_from_error (res, error);
958 g_error_free (error);
963 g_input_stream_real_read_async (GInputStream *stream,
967 GCancellable *cancellable,
968 GAsyncReadyCallback callback,
971 GSimpleAsyncResult *res;
974 op = g_new (ReadData, 1);
975 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_input_stream_real_read_async);
976 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
978 op->count_requested = count;
980 g_simple_async_result_run_in_thread (res, read_async_thread, io_priority, cancellable);
981 g_object_unref (res);
985 g_input_stream_real_read_finish (GInputStream *stream,
986 GAsyncResult *result,
989 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
992 g_assert (g_simple_async_result_get_source_tag (simple) ==
993 g_input_stream_real_read_async);
995 op = g_simple_async_result_get_op_res_gpointer (simple);
997 return op->count_read;
1001 gsize count_requested;
1002 gssize count_skipped;
1007 skip_async_thread (GSimpleAsyncResult *res,
1009 GCancellable *cancellable)
1012 GInputStreamClass *class;
1013 GError *error = NULL;
1015 class = G_INPUT_STREAM_GET_CLASS (object);
1016 op = g_simple_async_result_get_op_res_gpointer (res);
1017 op->count_skipped = class->skip (G_INPUT_STREAM (object),
1018 op->count_requested,
1019 cancellable, &error);
1020 if (op->count_skipped == -1)
1022 g_simple_async_result_set_from_error (res, error);
1023 g_error_free (error);
1030 gsize count_skipped;
1032 GCancellable *cancellable;
1034 GAsyncReadyCallback callback;
1035 } SkipFallbackAsyncData;
1038 skip_callback_wrapper (GObject *source_object,
1042 GInputStreamClass *class;
1043 SkipFallbackAsyncData *data = user_data;
1045 GSimpleAsyncResult *simple;
1046 GError *error = NULL;
1049 ret = g_input_stream_read_finish (G_INPUT_STREAM (source_object), res, &error);
1054 data->count_skipped += ret;
1056 if (data->count > 0)
1058 class = G_INPUT_STREAM_GET_CLASS (source_object);
1059 class->read_async (G_INPUT_STREAM (source_object), data->buffer, MIN (8192, data->count), data->io_prio, data->cancellable,
1060 skip_callback_wrapper, data);
1065 op = g_new0 (SkipData, 1);
1066 op->count_skipped = data->count_skipped;
1067 simple = g_simple_async_result_new (source_object,
1068 data->callback, data->user_data,
1069 g_input_stream_real_skip_async);
1071 g_simple_async_result_set_op_res_gpointer (simple, op, g_free);
1075 if (data->count_skipped &&
1076 error->domain == G_IO_ERROR &&
1077 error->code == G_IO_ERROR_CANCELLED)
1078 { /* No error, return partial read */ }
1080 g_simple_async_result_set_from_error (simple, error);
1081 g_error_free (error);
1084 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1085 g_simple_async_result_complete (simple);
1086 g_object_unref (simple);
1092 g_input_stream_real_skip_async (GInputStream *stream,
1095 GCancellable *cancellable,
1096 GAsyncReadyCallback callback,
1099 GInputStreamClass *class;
1101 SkipFallbackAsyncData *data;
1102 GSimpleAsyncResult *res;
1104 class = G_INPUT_STREAM_GET_CLASS (stream);
1106 if (class->read_async == g_input_stream_real_read_async)
1108 /* Read is thread-using async fallback.
1109 * Make skip use threads too, so that we can use a possible sync skip
1110 * implementation. */
1111 op = g_new0 (SkipData, 1);
1113 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
1114 g_input_stream_real_skip_async);
1116 g_simple_async_result_set_op_res_gpointer (res, op, g_free);
1118 op->count_requested = count;
1120 g_simple_async_result_run_in_thread (res, skip_async_thread, io_priority, cancellable);
1121 g_object_unref (res);
1125 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1127 /* There is a custom async read function, lets use that. */
1128 data = g_new (SkipFallbackAsyncData, 1);
1129 data->count = count;
1130 data->count_skipped = 0;
1131 data->io_prio = io_priority;
1132 data->cancellable = cancellable;
1133 data->callback = callback;
1134 data->user_data = user_data;
1135 class->read_async (stream, data->buffer, MIN (8192, count), io_priority, cancellable,
1136 skip_callback_wrapper, data);
1142 g_input_stream_real_skip_finish (GInputStream *stream,
1143 GAsyncResult *result,
1146 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1149 g_assert (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_skip_async);
1150 op = g_simple_async_result_get_op_res_gpointer (simple);
1151 return op->count_skipped;
1155 close_async_thread (GSimpleAsyncResult *res,
1157 GCancellable *cancellable)
1159 GInputStreamClass *class;
1160 GError *error = NULL;
1163 /* Auto handling of cancelation disabled, and ignore
1164 cancellation, since we want to close things anyway, although
1165 possibly in a quick-n-dirty way. At least we never want to leak
1168 class = G_INPUT_STREAM_GET_CLASS (object);
1169 result = class->close (G_INPUT_STREAM (object), cancellable, &error);
1172 g_simple_async_result_set_from_error (res, error);
1173 g_error_free (error);
1178 g_input_stream_real_close_async (GInputStream *stream,
1180 GCancellable *cancellable,
1181 GAsyncReadyCallback callback,
1184 GSimpleAsyncResult *res;
1186 res = g_simple_async_result_new (G_OBJECT (stream),
1189 g_input_stream_real_close_async);
1191 g_simple_async_result_set_handle_cancellation (res, FALSE);
1193 g_simple_async_result_run_in_thread (res,
1197 g_object_unref (res);
1201 g_input_stream_real_close_finish (GInputStream *stream,
1202 GAsyncResult *result,
1205 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
1206 g_assert (g_simple_async_result_get_source_tag (simple) == g_input_stream_real_close_async);
1210 #define __G_INPUT_STREAM_C__
1211 #include "gioaliasdef.c"