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
45 G_DEFINE_TYPE (GInputStream
, g_input_stream
, G_TYPE_OBJECT
);
47 struct _GInputStreamPrivate
{
50 GAsyncReadyCallback outstanding_callback
;
53 static gssize
g_input_stream_real_skip (GInputStream
*stream
,
55 GCancellable
*cancellable
,
57 static void g_input_stream_real_read_async (GInputStream
*stream
,
61 GCancellable
*cancellable
,
62 GAsyncReadyCallback callback
,
64 static gssize
g_input_stream_real_read_finish (GInputStream
*stream
,
67 static void g_input_stream_real_skip_async (GInputStream
*stream
,
70 GCancellable
*cancellable
,
71 GAsyncReadyCallback callback
,
73 static gssize
g_input_stream_real_skip_finish (GInputStream
*stream
,
76 static void g_input_stream_real_close_async (GInputStream
*stream
,
78 GCancellable
*cancellable
,
79 GAsyncReadyCallback callback
,
81 static gboolean
g_input_stream_real_close_finish (GInputStream
*stream
,
86 g_input_stream_finalize (GObject
*object
)
90 stream
= G_INPUT_STREAM (object
);
92 if (!stream
->priv
->closed
)
93 g_input_stream_close (stream
, NULL
, NULL
);
95 G_OBJECT_CLASS (g_input_stream_parent_class
)->finalize (object
);
99 g_input_stream_dispose (GObject
*object
)
101 GInputStream
*stream
;
103 stream
= G_INPUT_STREAM (object
);
105 if (!stream
->priv
->closed
)
106 g_input_stream_close (stream
, NULL
, NULL
);
108 G_OBJECT_CLASS (g_input_stream_parent_class
)->dispose (object
);
113 g_input_stream_class_init (GInputStreamClass
*klass
)
115 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
117 g_type_class_add_private (klass
, sizeof (GInputStreamPrivate
));
119 gobject_class
->finalize
= g_input_stream_finalize
;
120 gobject_class
->dispose
= g_input_stream_dispose
;
122 klass
->skip
= g_input_stream_real_skip
;
123 klass
->read_async
= g_input_stream_real_read_async
;
124 klass
->read_finish
= g_input_stream_real_read_finish
;
125 klass
->skip_async
= g_input_stream_real_skip_async
;
126 klass
->skip_finish
= g_input_stream_real_skip_finish
;
127 klass
->close_async
= g_input_stream_real_close_async
;
128 klass
->close_finish
= g_input_stream_real_close_finish
;
132 g_input_stream_init (GInputStream
*stream
)
134 stream
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (stream
,
136 GInputStreamPrivate
);
140 * g_input_stream_read:
141 * @stream: a #GInputStream.
142 * @buffer: a buffer to read data into (which should be at least count bytes long).
143 * @count: the number of bytes that will be read from the stream
144 * @cancellable: optional #GCancellable object, %NULL to ignore.
145 * @error: location to store the error occuring, or %NULL to ignore
147 * Tries to read @count bytes from the stream into the buffer starting at
148 * @buffer. Will block during this read.
150 * If count is zero returns zero and does nothing. A value of @count
151 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
153 * On success, the number of bytes read into the buffer is returned.
154 * It is not an error if this is not the same as the requested size, as it
155 * can happen e.g. near the end of a file. Zero is returned on end of file
156 * (or if @count is zero), but never otherwise.
158 * If @cancellable is not NULL, then the operation can be cancelled by
159 * triggering the cancellable object from another thread. If the operation
160 * was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
161 * operation was partially finished when the operation was cancelled the
162 * partial result will be returned, without an error.
164 * On error -1 is returned and @error is set accordingly.
166 * Return value: Number of bytes read, or -1 on error
169 g_input_stream_read (GInputStream
*stream
,
172 GCancellable
*cancellable
,
175 GInputStreamClass
*class;
178 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), -1);
179 g_return_val_if_fail (buffer
!= NULL
, 0);
184 if (((gssize
) count
) < 0)
186 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
187 _("Too large count value passed to %s"), G_STRFUNC
);
191 class = G_INPUT_STREAM_GET_CLASS (stream
);
193 if (class->read_fn
== NULL
)
195 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
,
196 _("Input stream doesn't implement read"));
200 if (!g_input_stream_set_pending (stream
, error
))
204 g_cancellable_push_current (cancellable
);
206 res
= class->read_fn (stream
, buffer
, count
, cancellable
, error
);
209 g_cancellable_pop_current (cancellable
);
211 g_input_stream_clear_pending (stream
);
217 * g_input_stream_read_all:
218 * @stream: a #GInputStream.
219 * @buffer: a buffer to read data into (which should be at least count bytes long).
220 * @count: the number of bytes that will be read from the stream
221 * @bytes_read: location to store the number of bytes that was read from the stream
222 * @cancellable: optional #GCancellable object, %NULL to ignore.
223 * @error: location to store the error occuring, or %NULL to ignore
225 * Tries to read @count bytes from the stream into the buffer starting at
226 * @buffer. Will block during this read.
228 * This function is similar to g_input_stream_read(), except it tries to
229 * read as many bytes as requested, only stopping on an error or end of stream.
231 * On a successful read of @count bytes, or if we reached the end of the
232 * stream, %TRUE is returned, and @bytes_read is set to the number of bytes
235 * If there is an error during the operation %FALSE is returned and @error
236 * is set to indicate the error status, @bytes_read is updated to contain
237 * the number of bytes read into @buffer before the error occurred.
239 * Return value: %TRUE on success, %FALSE if there was an error
242 g_input_stream_read_all (GInputStream
*stream
,
246 GCancellable
*cancellable
,
252 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), FALSE
);
253 g_return_val_if_fail (buffer
!= NULL
, FALSE
);
256 while (_bytes_read
< count
)
258 res
= g_input_stream_read (stream
, (char *)buffer
+ _bytes_read
, count
- _bytes_read
,
263 *bytes_read
= _bytes_read
;
274 *bytes_read
= _bytes_read
;
279 * g_input_stream_skip:
280 * @stream: a #GInputStream.
281 * @count: the number of bytes that will be skipped from the stream
282 * @cancellable: optional #GCancellable object, %NULL to ignore.
283 * @error: location to store the error occuring, or %NULL to ignore
285 * Tries to skip @count bytes from the stream. Will block during the operation.
287 * This is identical to g_input_stream_read(), from a behaviour standpoint,
288 * but the bytes that are skipped are not returned to the user. Some
289 * streams have an implementation that is more efficient than reading the data.
291 * This function is optional for inherited classes, as the default implementation
292 * emulates it using read.
294 * If @cancellable is not %NULL, then the operation can be cancelled by
295 * triggering the cancellable object from another thread. If the operation
296 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
297 * operation was partially finished when the operation was cancelled the
298 * partial result will be returned, without an error.
300 * Return value: Number of bytes skipped, or -1 on error
303 g_input_stream_skip (GInputStream
*stream
,
305 GCancellable
*cancellable
,
308 GInputStreamClass
*class;
311 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), -1);
316 if (((gssize
) count
) < 0)
318 g_set_error (error
, G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
319 _("Too large count value passed to %s"), G_STRFUNC
);
323 class = G_INPUT_STREAM_GET_CLASS (stream
);
325 if (!g_input_stream_set_pending (stream
, error
))
329 g_cancellable_push_current (cancellable
);
331 res
= class->skip (stream
, count
, cancellable
, error
);
334 g_cancellable_pop_current (cancellable
);
336 g_input_stream_clear_pending (stream
);
342 g_input_stream_real_skip (GInputStream
*stream
,
344 GCancellable
*cancellable
,
347 GInputStreamClass
*class;
348 gssize ret
, read_bytes
;
352 class = G_INPUT_STREAM_GET_CLASS (stream
);
354 if (G_IS_SEEKABLE (stream
) && g_seekable_can_seek (G_SEEKABLE (stream
)))
356 if (g_seekable_seek (G_SEEKABLE (stream
),
364 /* If not seekable, or seek failed, fall back to reading data: */
366 class = G_INPUT_STREAM_GET_CLASS (stream
);
373 ret
= class->read_fn (stream
, buffer
, MIN (sizeof (buffer
), count
),
374 cancellable
, &my_error
);
377 if (read_bytes
> 0 &&
378 my_error
->domain
== G_IO_ERROR
&&
379 my_error
->code
== G_IO_ERROR_CANCELLED
)
381 g_error_free (my_error
);
385 g_propagate_error (error
, my_error
);
392 if (ret
== 0 || count
== 0)
398 * g_input_stream_close:
399 * @stream: A #GInputStream.
400 * @cancellable: optional #GCancellable object, %NULL to ignore.
401 * @error: location to store the error occuring, or %NULL to ignore
403 * Closes the stream, releasing resources related to it.
405 * Once the stream is closed, all other operations will return %G_IO_ERROR_CLOSED.
406 * Closing a stream multiple times will not return an error.
408 * Streams will be automatically closed when the last reference
409 * is dropped, but you might want to call this function to make sure
410 * resources are released as early as possible.
412 * Some streams might keep the backing store of the stream (e.g. a file descriptor)
413 * open after the stream is closed. See the documentation for the individual
414 * stream for details.
416 * On failure the first error that happened will be reported, but the close
417 * operation will finish as much as possible. A stream that failed to
418 * close will still return %G_IO_ERROR_CLOSED for all operations. Still, it
419 * is important to check and report the error to the user.
421 * If @cancellable is not NULL, then the operation can be cancelled by
422 * triggering the cancellable object from another thread. If the operation
423 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned.
424 * Cancelling a close will still leave the stream closed, but some streams
425 * can use a faster close that doesn't block to e.g. check errors.
427 * Return value: %TRUE on success, %FALSE on failure
430 g_input_stream_close (GInputStream
*stream
,
431 GCancellable
*cancellable
,
434 GInputStreamClass
*class;
437 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), FALSE
);
439 class = G_INPUT_STREAM_GET_CLASS (stream
);
441 if (stream
->priv
->closed
)
446 if (!g_input_stream_set_pending (stream
, error
))
450 g_cancellable_push_current (cancellable
);
453 res
= class->close_fn (stream
, cancellable
, error
);
456 g_cancellable_pop_current (cancellable
);
458 g_input_stream_clear_pending (stream
);
460 stream
->priv
->closed
= TRUE
;
466 async_ready_callback_wrapper (GObject
*source_object
,
470 GInputStream
*stream
= G_INPUT_STREAM (source_object
);
472 g_input_stream_clear_pending (stream
);
473 if (stream
->priv
->outstanding_callback
)
474 (*stream
->priv
->outstanding_callback
) (source_object
, res
, user_data
);
475 g_object_unref (stream
);
479 async_ready_close_callback_wrapper (GObject
*source_object
,
483 GInputStream
*stream
= G_INPUT_STREAM (source_object
);
485 g_input_stream_clear_pending (stream
);
486 stream
->priv
->closed
= TRUE
;
487 if (stream
->priv
->outstanding_callback
)
488 (*stream
->priv
->outstanding_callback
) (source_object
, res
, user_data
);
489 g_object_unref (stream
);
493 * g_input_stream_read_async:
494 * @stream: A #GInputStream.
495 * @buffer: a buffer to read data into (which should be at least count bytes long).
496 * @count: the number of bytes that will be read from the stream
497 * @io_priority: the <link linkend="io-priority">I/O priority</link>
499 * @cancellable: optional #GCancellable object, %NULL to ignore.
500 * @callback: callback to call when the request is satisfied
501 * @user_data: the data to pass to callback function
503 * Request an asynchronous read of @count bytes from the stream into the buffer
504 * starting at @buffer. When the operation is finished @callback will be called.
505 * You can then call g_input_stream_read_finish() to get the result of the
508 * During an async request no other sync and async calls are allowed, and will
509 * result in %G_IO_ERROR_PENDING errors.
511 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
513 * On success, the number of bytes read into the buffer will be passed to the
514 * callback. It is not an error if this is not the same as the requested size, as it
515 * can happen e.g. near the end of a file, but generally we try to read
516 * as many bytes as requested. Zero is returned on end of file
517 * (or if @count is zero), but never otherwise.
519 * Any outstanding i/o request with higher priority (lower numerical value) will
520 * be executed before an outstanding request with lower priority. Default
521 * priority is %G_PRIORITY_DEFAULT.
523 * The asyncronous methods have a default fallback that uses threads to implement
524 * asynchronicity, so they are optional for inheriting classes. However, if you
525 * override one you must override all.
528 g_input_stream_read_async (GInputStream
*stream
,
532 GCancellable
*cancellable
,
533 GAsyncReadyCallback callback
,
536 GInputStreamClass
*class;
537 GSimpleAsyncResult
*simple
;
538 GError
*error
= NULL
;
540 g_return_if_fail (G_IS_INPUT_STREAM (stream
));
541 g_return_if_fail (buffer
!= NULL
);
545 simple
= g_simple_async_result_new (G_OBJECT (stream
),
548 g_input_stream_read_async
);
549 g_simple_async_result_complete_in_idle (simple
);
550 g_object_unref (simple
);
554 if (((gssize
) count
) < 0)
556 g_simple_async_report_error_in_idle (G_OBJECT (stream
),
559 G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
560 _("Too large count value passed to %s"),
565 if (!g_input_stream_set_pending (stream
, &error
))
567 g_simple_async_report_gerror_in_idle (G_OBJECT (stream
),
571 g_error_free (error
);
575 class = G_INPUT_STREAM_GET_CLASS (stream
);
576 stream
->priv
->outstanding_callback
= callback
;
577 g_object_ref (stream
);
578 class->read_async (stream
, buffer
, count
, io_priority
, cancellable
,
579 async_ready_callback_wrapper
, user_data
);
583 * g_input_stream_read_finish:
584 * @stream: a #GInputStream.
585 * @result: a #GAsyncResult.
586 * @error: a #GError location to store the error occuring, or %NULL to
589 * Finishes an asynchronous stream read operation.
591 * Returns: number of bytes read in, or -1 on error.
594 g_input_stream_read_finish (GInputStream
*stream
,
595 GAsyncResult
*result
,
598 GSimpleAsyncResult
*simple
;
599 GInputStreamClass
*class;
601 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), -1);
602 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), -1);
604 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
606 simple
= G_SIMPLE_ASYNC_RESULT (result
);
607 if (g_simple_async_result_propagate_error (simple
, error
))
610 /* Special case read of 0 bytes */
611 if (g_simple_async_result_get_source_tag (simple
) == g_input_stream_read_async
)
615 class = G_INPUT_STREAM_GET_CLASS (stream
);
616 return class->read_finish (stream
, result
, error
);
620 * g_input_stream_skip_async:
621 * @stream: A #GInputStream.
622 * @count: the number of bytes that will be skipped from the stream
623 * @io_priority: the <link linkend="io-priority">I/O priority</link>
625 * @cancellable: optional #GCancellable object, %NULL to ignore.
626 * @callback: callback to call when the request is satisfied
627 * @user_data: the data to pass to callback function
629 * Request an asynchronous skip of @count bytes from the stream into the buffer
630 * starting at @buffer. When the operation is finished @callback will be called.
631 * You can then call g_input_stream_skip_finish() to get the result of the
634 * During an async request no other sync and async calls are allowed, and will
635 * result in %G_IO_ERROR_PENDING errors.
637 * A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
639 * On success, the number of bytes skipped will be passed to the
640 * callback. It is not an error if this is not the same as the requested size, as it
641 * can happen e.g. near the end of a file, but generally we try to skip
642 * as many bytes as requested. Zero is returned on end of file
643 * (or if @count is zero), but never otherwise.
645 * Any outstanding i/o request with higher priority (lower numerical value) will
646 * be executed before an outstanding request with lower priority. Default
647 * priority is %G_PRIORITY_DEFAULT.
649 * The asyncronous methods have a default fallback that uses threads to implement
650 * asynchronicity, so they are optional for inheriting classes. However, if you
651 * override one you must override all.
654 g_input_stream_skip_async (GInputStream
*stream
,
657 GCancellable
*cancellable
,
658 GAsyncReadyCallback callback
,
661 GInputStreamClass
*class;
662 GSimpleAsyncResult
*simple
;
663 GError
*error
= NULL
;
665 g_return_if_fail (G_IS_INPUT_STREAM (stream
));
669 simple
= g_simple_async_result_new (G_OBJECT (stream
),
672 g_input_stream_skip_async
);
674 g_simple_async_result_complete_in_idle (simple
);
675 g_object_unref (simple
);
679 if (((gssize
) count
) < 0)
681 g_simple_async_report_error_in_idle (G_OBJECT (stream
),
684 G_IO_ERROR
, G_IO_ERROR_INVALID_ARGUMENT
,
685 _("Too large count value passed to %s"),
690 if (!g_input_stream_set_pending (stream
, &error
))
692 g_simple_async_report_gerror_in_idle (G_OBJECT (stream
),
696 g_error_free (error
);
700 class = G_INPUT_STREAM_GET_CLASS (stream
);
701 stream
->priv
->outstanding_callback
= callback
;
702 g_object_ref (stream
);
703 class->skip_async (stream
, count
, io_priority
, cancellable
,
704 async_ready_callback_wrapper
, user_data
);
708 * g_input_stream_skip_finish:
709 * @stream: a #GInputStream.
710 * @result: a #GAsyncResult.
711 * @error: a #GError location to store the error occuring, or %NULL to
714 * Finishes a stream skip operation.
716 * Returns: the size of the bytes skipped, or %-1 on error.
719 g_input_stream_skip_finish (GInputStream
*stream
,
720 GAsyncResult
*result
,
723 GSimpleAsyncResult
*simple
;
724 GInputStreamClass
*class;
726 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), -1);
727 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), -1);
729 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
731 simple
= G_SIMPLE_ASYNC_RESULT (result
);
732 if (g_simple_async_result_propagate_error (simple
, error
))
735 /* Special case skip of 0 bytes */
736 if (g_simple_async_result_get_source_tag (simple
) == g_input_stream_skip_async
)
740 class = G_INPUT_STREAM_GET_CLASS (stream
);
741 return class->skip_finish (stream
, result
, error
);
745 * g_input_stream_close_async:
746 * @stream: A #GInputStream.
747 * @io_priority: the <link linkend="io-priority">I/O priority</link>
749 * @cancellable: optional cancellable object
750 * @callback: callback to call when the request is satisfied
751 * @user_data: the data to pass to callback function
753 * Requests an asynchronous closes of the stream, releasing resources related to it.
754 * When the operation is finished @callback will be called.
755 * You can then call g_input_stream_close_finish() to get the result of the
758 * For behaviour details see g_input_stream_close().
760 * The asyncronous methods have a default fallback that uses threads to implement
761 * asynchronicity, so they are optional for inheriting classes. However, if you
762 * override one you must override all.
765 g_input_stream_close_async (GInputStream
*stream
,
767 GCancellable
*cancellable
,
768 GAsyncReadyCallback callback
,
771 GInputStreamClass
*class;
772 GSimpleAsyncResult
*simple
;
773 GError
*error
= NULL
;
775 g_return_if_fail (G_IS_INPUT_STREAM (stream
));
777 if (stream
->priv
->closed
)
779 simple
= g_simple_async_result_new (G_OBJECT (stream
),
782 g_input_stream_close_async
);
784 g_simple_async_result_complete_in_idle (simple
);
785 g_object_unref (simple
);
789 if (!g_input_stream_set_pending (stream
, &error
))
791 g_simple_async_report_gerror_in_idle (G_OBJECT (stream
),
795 g_error_free (error
);
799 class = G_INPUT_STREAM_GET_CLASS (stream
);
800 stream
->priv
->outstanding_callback
= callback
;
801 g_object_ref (stream
);
802 class->close_async (stream
, io_priority
, cancellable
,
803 async_ready_close_callback_wrapper
, user_data
);
807 * g_input_stream_close_finish:
808 * @stream: a #GInputStream.
809 * @result: a #GAsyncResult.
810 * @error: a #GError location to store the error occuring, or %NULL to
813 * Finishes closing a stream asynchronously, started from g_input_stream_close_async().
815 * Returns: %TRUE if the stream was closed successfully.
818 g_input_stream_close_finish (GInputStream
*stream
,
819 GAsyncResult
*result
,
822 GSimpleAsyncResult
*simple
;
823 GInputStreamClass
*class;
825 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), FALSE
);
826 g_return_val_if_fail (G_IS_ASYNC_RESULT (result
), FALSE
);
828 if (G_IS_SIMPLE_ASYNC_RESULT (result
))
830 simple
= G_SIMPLE_ASYNC_RESULT (result
);
831 if (g_simple_async_result_propagate_error (simple
, error
))
834 /* Special case already closed */
835 if (g_simple_async_result_get_source_tag (simple
) == g_input_stream_close_async
)
839 class = G_INPUT_STREAM_GET_CLASS (stream
);
840 return class->close_finish (stream
, result
, error
);
844 * g_input_stream_is_closed:
845 * @stream: input stream.
847 * Checks if an input stream is closed.
849 * Returns: %TRUE if the stream is closed.
852 g_input_stream_is_closed (GInputStream
*stream
)
854 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), TRUE
);
856 return stream
->priv
->closed
;
860 * g_input_stream_has_pending:
861 * @stream: input stream.
863 * Checks if an input stream has pending actions.
865 * Returns: %TRUE if @stream has pending actions.
868 g_input_stream_has_pending (GInputStream
*stream
)
870 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), TRUE
);
872 return stream
->priv
->pending
;
876 * g_input_stream_set_pending:
877 * @stream: input stream
878 * @error: a #GError location to store the error occuring, or %NULL to
881 * Sets @stream to have actions pending. If the pending flag is
882 * already set or @stream is closed, it will return %FALSE and set
885 * Return value: %TRUE if pending was previously unset and is now set.
888 g_input_stream_set_pending (GInputStream
*stream
, GError
**error
)
890 g_return_val_if_fail (G_IS_INPUT_STREAM (stream
), FALSE
);
892 if (stream
->priv
->closed
)
894 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_CLOSED
,
895 _("Stream is already closed"));
899 if (stream
->priv
->pending
)
901 g_set_error_literal (error
, G_IO_ERROR
, G_IO_ERROR_PENDING
,
902 /* Translators: This is an error you get if there is already an
903 * operation running against this stream when you try to start
905 _("Stream has outstanding operation"));
909 stream
->priv
->pending
= TRUE
;
914 * g_input_stream_clear_pending:
915 * @stream: input stream
917 * Clears the pending flag on @stream.
920 g_input_stream_clear_pending (GInputStream
*stream
)
922 g_return_if_fail (G_IS_INPUT_STREAM (stream
));
924 stream
->priv
->pending
= FALSE
;
927 /********************************************
928 * Default implementation of async ops *
929 ********************************************/
933 gsize count_requested
;
938 read_async_thread (GSimpleAsyncResult
*res
,
940 GCancellable
*cancellable
)
943 GInputStreamClass
*class;
944 GError
*error
= NULL
;
946 op
= g_simple_async_result_get_op_res_gpointer (res
);
948 class = G_INPUT_STREAM_GET_CLASS (object
);
950 op
->count_read
= class->read_fn (G_INPUT_STREAM (object
),
951 op
->buffer
, op
->count_requested
,
952 cancellable
, &error
);
953 if (op
->count_read
== -1)
955 g_simple_async_result_set_from_error (res
, error
);
956 g_error_free (error
);
961 g_input_stream_real_read_async (GInputStream
*stream
,
965 GCancellable
*cancellable
,
966 GAsyncReadyCallback callback
,
969 GSimpleAsyncResult
*res
;
972 op
= g_new (ReadData
, 1);
973 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
, g_input_stream_real_read_async
);
974 g_simple_async_result_set_op_res_gpointer (res
, op
, g_free
);
976 op
->count_requested
= count
;
978 g_simple_async_result_run_in_thread (res
, read_async_thread
, io_priority
, cancellable
);
979 g_object_unref (res
);
983 g_input_stream_real_read_finish (GInputStream
*stream
,
984 GAsyncResult
*result
,
987 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
990 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) ==
991 g_input_stream_real_read_async
);
993 op
= g_simple_async_result_get_op_res_gpointer (simple
);
995 return op
->count_read
;
999 gsize count_requested
;
1000 gssize count_skipped
;
1005 skip_async_thread (GSimpleAsyncResult
*res
,
1007 GCancellable
*cancellable
)
1010 GInputStreamClass
*class;
1011 GError
*error
= NULL
;
1013 class = G_INPUT_STREAM_GET_CLASS (object
);
1014 op
= g_simple_async_result_get_op_res_gpointer (res
);
1015 op
->count_skipped
= class->skip (G_INPUT_STREAM (object
),
1016 op
->count_requested
,
1017 cancellable
, &error
);
1018 if (op
->count_skipped
== -1)
1020 g_simple_async_result_set_from_error (res
, error
);
1021 g_error_free (error
);
1028 gsize count_skipped
;
1030 GCancellable
*cancellable
;
1032 GAsyncReadyCallback callback
;
1033 } SkipFallbackAsyncData
;
1036 skip_callback_wrapper (GObject
*source_object
,
1040 GInputStreamClass
*class;
1041 SkipFallbackAsyncData
*data
= user_data
;
1043 GSimpleAsyncResult
*simple
;
1044 GError
*error
= NULL
;
1047 ret
= g_input_stream_read_finish (G_INPUT_STREAM (source_object
), res
, &error
);
1052 data
->count_skipped
+= ret
;
1054 if (data
->count
> 0)
1056 class = G_INPUT_STREAM_GET_CLASS (source_object
);
1057 class->read_async (G_INPUT_STREAM (source_object
), data
->buffer
, MIN (8192, data
->count
), data
->io_prio
, data
->cancellable
,
1058 skip_callback_wrapper
, data
);
1063 op
= g_new0 (SkipData
, 1);
1064 op
->count_skipped
= data
->count_skipped
;
1065 simple
= g_simple_async_result_new (source_object
,
1066 data
->callback
, data
->user_data
,
1067 g_input_stream_real_skip_async
);
1069 g_simple_async_result_set_op_res_gpointer (simple
, op
, g_free
);
1073 if (data
->count_skipped
&&
1074 error
->domain
== G_IO_ERROR
&&
1075 error
->code
== G_IO_ERROR_CANCELLED
)
1076 { /* No error, return partial read */ }
1078 g_simple_async_result_set_from_error (simple
, error
);
1079 g_error_free (error
);
1082 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1083 g_simple_async_result_complete (simple
);
1084 g_object_unref (simple
);
1090 g_input_stream_real_skip_async (GInputStream
*stream
,
1093 GCancellable
*cancellable
,
1094 GAsyncReadyCallback callback
,
1097 GInputStreamClass
*class;
1099 SkipFallbackAsyncData
*data
;
1100 GSimpleAsyncResult
*res
;
1102 class = G_INPUT_STREAM_GET_CLASS (stream
);
1104 if (class->read_async
== g_input_stream_real_read_async
)
1106 /* Read is thread-using async fallback.
1107 * Make skip use threads too, so that we can use a possible sync skip
1108 * implementation. */
1109 op
= g_new0 (SkipData
, 1);
1111 res
= g_simple_async_result_new (G_OBJECT (stream
), callback
, user_data
,
1112 g_input_stream_real_skip_async
);
1114 g_simple_async_result_set_op_res_gpointer (res
, op
, g_free
);
1116 op
->count_requested
= count
;
1118 g_simple_async_result_run_in_thread (res
, skip_async_thread
, io_priority
, cancellable
);
1119 g_object_unref (res
);
1123 /* TODO: Skip fallback uses too much memory, should do multiple read calls */
1125 /* There is a custom async read function, lets use that. */
1126 data
= g_new (SkipFallbackAsyncData
, 1);
1127 data
->count
= count
;
1128 data
->count_skipped
= 0;
1129 data
->io_prio
= io_priority
;
1130 data
->cancellable
= cancellable
;
1131 data
->callback
= callback
;
1132 data
->user_data
= user_data
;
1133 class->read_async (stream
, data
->buffer
, MIN (8192, count
), io_priority
, cancellable
,
1134 skip_callback_wrapper
, data
);
1140 g_input_stream_real_skip_finish (GInputStream
*stream
,
1141 GAsyncResult
*result
,
1144 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1147 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_input_stream_real_skip_async
);
1148 op
= g_simple_async_result_get_op_res_gpointer (simple
);
1149 return op
->count_skipped
;
1153 close_async_thread (GSimpleAsyncResult
*res
,
1155 GCancellable
*cancellable
)
1157 GInputStreamClass
*class;
1158 GError
*error
= NULL
;
1161 /* Auto handling of cancelation disabled, and ignore
1162 cancellation, since we want to close things anyway, although
1163 possibly in a quick-n-dirty way. At least we never want to leak
1166 class = G_INPUT_STREAM_GET_CLASS (object
);
1167 result
= class->close_fn (G_INPUT_STREAM (object
), cancellable
, &error
);
1170 g_simple_async_result_set_from_error (res
, error
);
1171 g_error_free (error
);
1176 g_input_stream_real_close_async (GInputStream
*stream
,
1178 GCancellable
*cancellable
,
1179 GAsyncReadyCallback callback
,
1182 GSimpleAsyncResult
*res
;
1184 res
= g_simple_async_result_new (G_OBJECT (stream
),
1187 g_input_stream_real_close_async
);
1189 g_simple_async_result_set_handle_cancellation (res
, FALSE
);
1191 g_simple_async_result_run_in_thread (res
,
1195 g_object_unref (res
);
1199 g_input_stream_real_close_finish (GInputStream
*stream
,
1200 GAsyncResult
*result
,
1203 GSimpleAsyncResult
*simple
= G_SIMPLE_ASYNC_RESULT (result
);
1204 g_warn_if_fail (g_simple_async_result_get_source_tag (simple
) == g_input_stream_real_close_async
);
1208 #define __G_INPUT_STREAM_C__
1209 #include "gioaliasdef.c"