1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Jürg Billeter
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Christian Kellner <gicmo@gnome.org>
25 #include "gbufferedinputstream.h"
26 #include "ginputstream.h"
27 #include "gcancellable.h"
28 #include "gasyncresult.h"
29 #include "gsimpleasyncresult.h"
36 * SECTION:gbufferedinputstream
37 * @short_description: Buffered Input Stream
39 * @see_also: #GFilterInputStream, #GInputStream
41 * Buffered input stream implements #GFilterInputStream and provides
44 * By default, #GBufferedInputStream's buffer size is set at 4 kilobytes.
46 * To create a buffered input stream, use g_buffered_input_stream_new(),
47 * or g_buffered_input_stream_new_sized() to specify the buffer's size at
50 * To get the size of a buffer within a buffered input stream, use
51 * g_buffered_input_stream_get_buffer_size(). To change the size of a
52 * buffered input stream's buffer, use
53 * g_buffered_input_stream_set_buffer_size(). Note that the buffer's size
54 * cannot be reduced below the size of the data within the buffer.
58 #define DEFAULT_BUFFER_SIZE 4096
60 struct _GBufferedInputStreamPrivate {
65 GAsyncReadyCallback outstanding_callback;
73 static void g_buffered_input_stream_set_property (GObject *object,
78 static void g_buffered_input_stream_get_property (GObject *object,
82 static void g_buffered_input_stream_finalize (GObject *object);
85 static gssize g_buffered_input_stream_skip (GInputStream *stream,
87 GCancellable *cancellable,
89 static void g_buffered_input_stream_skip_async (GInputStream *stream,
92 GCancellable *cancellable,
93 GAsyncReadyCallback callback,
95 static gssize g_buffered_input_stream_skip_finish (GInputStream *stream,
98 static gssize g_buffered_input_stream_read (GInputStream *stream,
101 GCancellable *cancellable,
103 static gssize g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
105 GCancellable *cancellable,
107 static void g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
110 GCancellable *cancellable,
111 GAsyncReadyCallback callback,
113 static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
114 GAsyncResult *result,
117 static void compact_buffer (GBufferedInputStream *stream);
119 G_DEFINE_TYPE (GBufferedInputStream,
120 g_buffered_input_stream,
121 G_TYPE_FILTER_INPUT_STREAM)
125 g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
127 GObjectClass *object_class;
128 GInputStreamClass *istream_class;
129 GBufferedInputStreamClass *bstream_class;
131 g_type_class_add_private (klass, sizeof (GBufferedInputStreamPrivate));
133 object_class = G_OBJECT_CLASS (klass);
134 object_class->get_property = g_buffered_input_stream_get_property;
135 object_class->set_property = g_buffered_input_stream_set_property;
136 object_class->finalize = g_buffered_input_stream_finalize;
138 istream_class = G_INPUT_STREAM_CLASS (klass);
139 istream_class->skip = g_buffered_input_stream_skip;
140 istream_class->skip_async = g_buffered_input_stream_skip_async;
141 istream_class->skip_finish = g_buffered_input_stream_skip_finish;
142 istream_class->read_fn = g_buffered_input_stream_read;
144 bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
145 bstream_class->fill = g_buffered_input_stream_real_fill;
146 bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
147 bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
149 g_object_class_install_property (object_class,
151 g_param_spec_uint ("buffer-size",
153 P_("The size of the backend buffer"),
157 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
158 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
164 * g_buffered_input_stream_get_buffer_size:
165 * @stream: a #GBufferedInputStream
167 * Gets the size of the input buffer.
169 * Returns: the current buffer size.
172 g_buffered_input_stream_get_buffer_size (GBufferedInputStream *stream)
174 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), 0);
176 return stream->priv->len;
180 * g_buffered_input_stream_set_buffer_size:
181 * @stream: a #GBufferedInputStream
184 * Sets the size of the internal buffer of @stream to @size, or to the
185 * size of the contents of the buffer. The buffer can never be resized
186 * smaller than its current contents.
189 g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
192 GBufferedInputStreamPrivate *priv;
196 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
200 if (priv->len == size)
205 in_buffer = priv->end - priv->pos;
207 /* Never resize smaller than current buffer contents */
208 size = MAX (size, in_buffer);
210 buffer = g_malloc (size);
211 memcpy (buffer, priv->buffer + priv->pos, in_buffer);
214 priv->end = in_buffer;
215 g_free (priv->buffer);
216 priv->buffer = buffer;
223 priv->buffer = g_malloc (size);
226 g_object_notify (G_OBJECT (stream), "buffer-size");
230 g_buffered_input_stream_set_property (GObject *object,
235 GBufferedInputStream *bstream;
237 bstream = G_BUFFERED_INPUT_STREAM (object);
242 g_buffered_input_stream_set_buffer_size (bstream, g_value_get_uint (value));
246 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 g_buffered_input_stream_get_property (GObject *object,
257 GBufferedInputStreamPrivate *priv;
258 GBufferedInputStream *bstream;
260 bstream = G_BUFFERED_INPUT_STREAM (object);
261 priv = bstream->priv;
266 g_value_set_uint (value, priv->len);
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
276 g_buffered_input_stream_finalize (GObject *object)
278 GBufferedInputStreamPrivate *priv;
279 GBufferedInputStream *stream;
281 stream = G_BUFFERED_INPUT_STREAM (object);
284 g_free (priv->buffer);
286 G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize (object);
290 g_buffered_input_stream_init (GBufferedInputStream *stream)
292 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
293 G_TYPE_BUFFERED_INPUT_STREAM,
294 GBufferedInputStreamPrivate);
299 * g_buffered_input_stream_new:
300 * @base_stream: a #GInputStream
302 * Creates a new #GInputStream from the given @base_stream, with
303 * a buffer set to the default size (4 kilobytes).
305 * Returns: a #GInputStream for the given @base_stream.
308 g_buffered_input_stream_new (GInputStream *base_stream)
310 GInputStream *stream;
312 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
314 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
315 "base-stream", base_stream,
322 * g_buffered_input_stream_new_sized:
323 * @base_stream: a #GInputStream
326 * Creates a new #GBufferedInputStream from the given @base_stream,
327 * with a buffer set to @size.
329 * Returns: a #GInputStream.
332 g_buffered_input_stream_new_sized (GInputStream *base_stream,
335 GInputStream *stream;
337 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
339 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
340 "base-stream", base_stream,
341 "buffer-size", (guint)size,
348 * g_buffered_input_stream_fill:
349 * @stream: a #GBufferedInputStream
350 * @count: the number of bytes that will be read from the stream
351 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
352 * @error: location to store the error occurring, or %NULL to ignore
354 * Tries to read @count bytes from the stream into the buffer.
355 * Will block during this read.
357 * If @count is zero, returns zero and does nothing. A value of @count
358 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
360 * On success, the number of bytes read into the buffer is returned.
361 * It is not an error if this is not the same as the requested size, as it
362 * can happen e.g. near the end of a file. Zero is returned on end of file
363 * (or if @count is zero), but never otherwise.
365 * If @count is -1 then the attempted read size is equal to the number of
366 * bytes that are required to fill the buffer.
368 * If @cancellable is not %NULL, then the operation can be cancelled by
369 * triggering the cancellable object from another thread. If the operation
370 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
371 * operation was partially finished when the operation was cancelled the
372 * partial result will be returned, without an error.
374 * On error -1 is returned and @error is set accordingly.
376 * For the asynchronous, non-blocking, version of this function, see
377 * g_buffered_input_stream_fill_async().
379 * Returns: the number of bytes read into @stream's buffer, up to @count,
383 g_buffered_input_stream_fill (GBufferedInputStream *stream,
385 GCancellable *cancellable,
388 GBufferedInputStreamClass *class;
389 GInputStream *input_stream;
392 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
394 input_stream = G_INPUT_STREAM (stream);
398 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
399 _("Too large count value passed to %s"), G_STRFUNC);
403 if (!g_input_stream_set_pending (input_stream, error))
407 g_cancellable_push_current (cancellable);
409 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
410 res = class->fill (stream, count, cancellable, error);
413 g_cancellable_pop_current (cancellable);
415 g_input_stream_clear_pending (input_stream);
421 async_fill_callback_wrapper (GObject *source_object,
425 GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
427 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
428 (*stream->priv->outstanding_callback) (source_object, res, user_data);
429 g_object_unref (stream);
433 * g_buffered_input_stream_fill_async:
434 * @stream: a #GBufferedInputStream
435 * @count: the number of bytes that will be read from the stream
436 * @io_priority: the <link linkend="io-priority">I/O priority</link>
438 * @cancellable: (allow-none): optional #GCancellable object
439 * @callback: (scope async): a #GAsyncReadyCallback
440 * @user_data: (closure): a #gpointer
442 * Reads data into @stream's buffer asynchronously, up to @count size.
443 * @io_priority can be used to prioritize reads. For the synchronous
444 * version of this function, see g_buffered_input_stream_fill().
446 * If @count is -1 then the attempted read size is equal to the number
447 * of bytes that are required to fill the buffer.
450 g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
453 GCancellable *cancellable,
454 GAsyncReadyCallback callback,
457 GBufferedInputStreamClass *class;
458 GSimpleAsyncResult *simple;
459 GError *error = NULL;
461 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
465 simple = g_simple_async_result_new (G_OBJECT (stream),
468 g_buffered_input_stream_fill_async);
469 g_simple_async_result_complete_in_idle (simple);
470 g_object_unref (simple);
476 g_simple_async_report_error_in_idle (G_OBJECT (stream),
479 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
480 _("Too large count value passed to %s"),
485 if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
487 g_simple_async_report_take_gerror_in_idle (G_OBJECT (stream),
494 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
496 stream->priv->outstanding_callback = callback;
497 g_object_ref (stream);
498 class->fill_async (stream, count, io_priority, cancellable,
499 async_fill_callback_wrapper, user_data);
503 * g_buffered_input_stream_fill_finish:
504 * @stream: a #GBufferedInputStream
505 * @result: a #GAsyncResult
508 * Finishes an asynchronous read.
510 * Returns: a #gssize of the read stream, or %-1 on an error.
513 g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
514 GAsyncResult *result,
517 GSimpleAsyncResult *simple;
518 GBufferedInputStreamClass *class;
520 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
521 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
523 if (G_IS_SIMPLE_ASYNC_RESULT (result))
525 simple = G_SIMPLE_ASYNC_RESULT (result);
526 if (g_simple_async_result_propagate_error (simple, error))
529 /* Special case read of 0 bytes */
530 if (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_fill_async)
534 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
535 return class->fill_finish (stream, result, error);
539 * g_buffered_input_stream_get_available:
540 * @stream: #GBufferedInputStream
542 * Gets the size of the available data within the stream.
544 * Returns: size of the available stream.
547 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
549 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
551 return stream->priv->end - stream->priv->pos;
555 * g_buffered_input_stream_peek:
556 * @stream: a #GBufferedInputStream
557 * @buffer: (array length=count) (element-type guint8): a pointer to
558 * an allocated chunk of memory
562 * Peeks in the buffer, copying data of size @count into @buffer,
563 * offset @offset bytes.
565 * Returns: a #gsize of the number of bytes peeked, or -1 on error.
568 g_buffered_input_stream_peek (GBufferedInputStream *stream,
576 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
577 g_return_val_if_fail (buffer != NULL, -1);
579 available = g_buffered_input_stream_get_available (stream);
581 if (offset > available)
584 end = MIN (offset + count, available);
585 count = end - offset;
587 memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
592 * g_buffered_input_stream_peek_buffer:
593 * @stream: a #GBufferedInputStream
594 * @count: (out): a #gsize to get the number of bytes available in the buffer
596 * Returns the buffer with the currently available bytes. The returned
597 * buffer must not be modified and will become invalid when reading from
598 * the stream or filling the buffer.
600 * Returns: (array length=count) (element-type guint8) (transfer none):
604 g_buffered_input_stream_peek_buffer (GBufferedInputStream *stream,
607 GBufferedInputStreamPrivate *priv;
609 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), NULL);
614 *count = priv->end - priv->pos;
616 return priv->buffer + priv->pos;
620 compact_buffer (GBufferedInputStream *stream)
622 GBufferedInputStreamPrivate *priv;
627 current_size = priv->end - priv->pos;
629 g_memmove (priv->buffer, priv->buffer + priv->pos, current_size);
632 priv->end = current_size;
636 g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
638 GCancellable *cancellable,
641 GBufferedInputStreamPrivate *priv;
642 GInputStream *base_stream;
651 in_buffer = priv->end - priv->pos;
653 /* Never fill more than can fit in the buffer */
654 count = MIN (count, priv->len - in_buffer);
656 /* If requested length does not fit at end, compact */
657 if (priv->len - priv->end < count)
658 compact_buffer (stream);
660 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
661 nread = g_input_stream_read (base_stream,
662 priv->buffer + priv->end,
674 g_buffered_input_stream_skip (GInputStream *stream,
676 GCancellable *cancellable,
679 GBufferedInputStream *bstream;
680 GBufferedInputStreamPrivate *priv;
681 GBufferedInputStreamClass *class;
682 GInputStream *base_stream;
683 gsize available, bytes_skipped;
686 bstream = G_BUFFERED_INPUT_STREAM (stream);
687 priv = bstream->priv;
689 available = priv->end - priv->pos;
691 if (count <= available)
697 /* Full request not available, skip all currently available and
698 * request refill for more
703 bytes_skipped = available;
706 if (bytes_skipped > 0)
707 error = NULL; /* Ignore further errors if we already read some data */
709 if (count > priv->len)
711 /* Large request, shortcut buffer */
713 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
715 nread = g_input_stream_skip (base_stream,
720 if (nread < 0 && bytes_skipped == 0)
724 bytes_skipped += nread;
726 return bytes_skipped;
729 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
730 nread = class->fill (bstream, priv->len, cancellable, error);
734 if (bytes_skipped == 0)
737 return bytes_skipped;
740 available = priv->end - priv->pos;
741 count = MIN (count, available);
743 bytes_skipped += count;
746 return bytes_skipped;
750 g_buffered_input_stream_read (GInputStream *stream,
753 GCancellable *cancellable,
756 GBufferedInputStream *bstream;
757 GBufferedInputStreamPrivate *priv;
758 GBufferedInputStreamClass *class;
759 GInputStream *base_stream;
760 gsize available, bytes_read;
763 bstream = G_BUFFERED_INPUT_STREAM (stream);
764 priv = bstream->priv;
766 available = priv->end - priv->pos;
768 if (count <= available)
770 memcpy (buffer, priv->buffer + priv->pos, count);
775 /* Full request not available, read all currently available and
776 * request refill for more
779 memcpy (buffer, priv->buffer + priv->pos, available);
782 bytes_read = available;
786 error = NULL; /* Ignore further errors if we already read some data */
788 if (count > priv->len)
790 /* Large request, shortcut buffer */
792 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
794 nread = g_input_stream_read (base_stream,
795 (char *)buffer + bytes_read,
800 if (nread < 0 && bytes_read == 0)
809 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
810 nread = class->fill (bstream, priv->len, cancellable, error);
819 available = priv->end - priv->pos;
820 count = MIN (count, available);
822 memcpy ((char *)buffer + bytes_read, (char *)priv->buffer + priv->pos, count);
830 * g_buffered_input_stream_read_byte:
831 * @stream: a #GBufferedInputStream
832 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
833 * @error: location to store the error occurring, or %NULL to ignore
835 * Tries to read a single byte from the stream or the buffer. Will block
838 * On success, the byte read from the stream is returned. On end of stream
839 * -1 is returned but it's not an exceptional error and @error is not set.
841 * If @cancellable is not %NULL, then the operation can be cancelled by
842 * triggering the cancellable object from another thread. If the operation
843 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
844 * operation was partially finished when the operation was cancelled the
845 * partial result will be returned, without an error.
847 * On error -1 is returned and @error is set accordingly.
849 * Returns: the byte read from the @stream, or -1 on end of stream or error.
852 g_buffered_input_stream_read_byte (GBufferedInputStream *stream,
853 GCancellable *cancellable,
856 GBufferedInputStreamPrivate *priv;
857 GBufferedInputStreamClass *class;
858 GInputStream *input_stream;
862 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
865 input_stream = G_INPUT_STREAM (stream);
867 if (g_input_stream_is_closed (input_stream))
869 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
870 _("Stream is already closed"));
874 if (!g_input_stream_set_pending (input_stream, error))
877 available = priv->end - priv->pos;
881 g_input_stream_clear_pending (input_stream);
882 return priv->buffer[priv->pos++];
885 /* Byte not available, request refill for more */
888 g_cancellable_push_current (cancellable);
893 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
894 nread = class->fill (stream, priv->len, cancellable, error);
897 g_cancellable_pop_current (cancellable);
899 g_input_stream_clear_pending (input_stream);
902 return -1; /* error or end of stream */
904 return priv->buffer[priv->pos++];
907 /* ************************** */
908 /* Async stuff implementation */
909 /* ************************** */
912 fill_async_callback (GObject *source_object,
913 GAsyncResult *result,
918 GSimpleAsyncResult *simple;
923 res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
926 g_simple_async_result_set_op_res_gssize (simple, res);
929 g_simple_async_result_take_error (simple, error);
933 GBufferedInputStreamPrivate *priv;
936 object = g_async_result_get_source_object (G_ASYNC_RESULT (simple));
937 priv = G_BUFFERED_INPUT_STREAM (object)->priv;
939 g_assert_cmpint (priv->end + res, <=, priv->len);
942 g_object_unref (object);
945 /* Complete immediately, not in idle, since we're already
946 * in a mainloop callout
948 g_simple_async_result_complete (simple);
949 g_object_unref (simple);
953 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
956 GCancellable *cancellable,
957 GAsyncReadyCallback callback,
960 GBufferedInputStreamPrivate *priv;
961 GInputStream *base_stream;
962 GSimpleAsyncResult *simple;
970 in_buffer = priv->end - priv->pos;
972 /* Never fill more than can fit in the buffer */
973 count = MIN (count, priv->len - in_buffer);
975 /* If requested length does not fit at end, compact */
976 if (priv->len - priv->end < count)
977 compact_buffer (stream);
979 simple = g_simple_async_result_new (G_OBJECT (stream),
981 g_buffered_input_stream_real_fill_async);
983 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
984 g_input_stream_read_async (base_stream,
985 priv->buffer + priv->end,
994 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
995 GAsyncResult *result,
998 GSimpleAsyncResult *simple;
1001 simple = G_SIMPLE_ASYNC_RESULT (result);
1002 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_real_fill_async);
1004 nread = g_simple_async_result_get_op_res_gssize (simple);
1010 gssize bytes_skipped;
1015 free_skip_async_data (gpointer _data)
1017 SkipAsyncData *data = _data;
1018 g_slice_free (SkipAsyncData, data);
1022 large_skip_callback (GObject *source_object,
1023 GAsyncResult *result,
1026 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1027 SkipAsyncData *data;
1031 data = g_simple_async_result_get_op_res_gpointer (simple);
1034 nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1037 /* Only report the error if we've not already read some data */
1038 if (nread < 0 && data->bytes_skipped == 0)
1039 g_simple_async_result_take_error (simple, error);
1041 g_error_free (error);
1044 data->bytes_skipped += nread;
1046 /* Complete immediately, not in idle, since we're already
1047 * in a mainloop callout
1049 g_simple_async_result_complete (simple);
1050 g_object_unref (simple);
1054 skip_fill_buffer_callback (GObject *source_object,
1055 GAsyncResult *result,
1058 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1059 GBufferedInputStream *bstream;
1060 GBufferedInputStreamPrivate *priv;
1061 SkipAsyncData *data;
1066 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1067 priv = bstream->priv;
1069 data = g_simple_async_result_get_op_res_gpointer (simple);
1072 nread = g_buffered_input_stream_fill_finish (bstream,
1075 if (nread < 0 && data->bytes_skipped == 0)
1076 g_simple_async_result_take_error (simple, error);
1078 g_error_free (error);
1082 available = priv->end - priv->pos;
1083 data->count = MIN (data->count, available);
1085 data->bytes_skipped += data->count;
1086 priv->pos += data->count;
1089 /* Complete immediately, not in idle, since we're already
1090 * in a mainloop callout
1092 g_simple_async_result_complete (simple);
1093 g_object_unref (simple);
1097 g_buffered_input_stream_skip_async (GInputStream *stream,
1100 GCancellable *cancellable,
1101 GAsyncReadyCallback callback,
1104 GBufferedInputStream *bstream;
1105 GBufferedInputStreamPrivate *priv;
1106 GBufferedInputStreamClass *class;
1107 GInputStream *base_stream;
1109 GSimpleAsyncResult *simple;
1110 SkipAsyncData *data;
1112 bstream = G_BUFFERED_INPUT_STREAM (stream);
1113 priv = bstream->priv;
1115 data = g_slice_new (SkipAsyncData);
1116 data->bytes_skipped = 0;
1117 simple = g_simple_async_result_new (G_OBJECT (stream),
1118 callback, user_data,
1119 g_buffered_input_stream_skip_async);
1120 g_simple_async_result_set_op_res_gpointer (simple, data, free_skip_async_data);
1122 available = priv->end - priv->pos;
1124 if (count <= available)
1127 data->bytes_skipped = count;
1129 g_simple_async_result_complete_in_idle (simple);
1130 g_object_unref (simple);
1134 /* Full request not available, skip all currently available
1135 * and request refill for more
1143 data->bytes_skipped = available;
1144 data->count = count;
1146 if (count > priv->len)
1148 /* Large request, shortcut buffer */
1150 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1152 g_input_stream_skip_async (base_stream,
1154 io_priority, cancellable,
1155 large_skip_callback,
1160 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1161 class->fill_async (bstream, priv->len, io_priority, cancellable,
1162 skip_fill_buffer_callback, simple);
1167 g_buffered_input_stream_skip_finish (GInputStream *stream,
1168 GAsyncResult *result,
1171 GSimpleAsyncResult *simple;
1172 SkipAsyncData *data;
1174 simple = G_SIMPLE_ASYNC_RESULT (result);
1176 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_skip_async);
1178 data = g_simple_async_result_get_op_res_gpointer (simple);
1180 return data->bytes_skipped;