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: Christian Kellner <gicmo@gnome.org>
25 #include "gbufferedinputstream.h"
26 #include "ginputstream.h"
27 #include "gsimpleasyncresult.h"
32 #define DEFAULT_BUFFER_SIZE 4096
34 struct _GBufferedInputStreamPrivate {
39 GAsyncReadyCallback outstanding_callback;
47 static void g_buffered_input_stream_set_property (GObject *object,
52 static void g_buffered_input_stream_get_property (GObject *object,
56 static void g_buffered_input_stream_finalize (GObject *object);
59 static gssize g_buffered_input_stream_skip (GInputStream *stream,
61 GCancellable *cancellable,
63 static void g_buffered_input_stream_skip_async (GInputStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_buffered_input_stream_skip_finish (GInputStream *stream,
72 static gssize g_buffered_input_stream_read (GInputStream *stream,
75 GCancellable *cancellable,
77 static void g_buffered_input_stream_read_async (GInputStream *stream,
81 GCancellable *cancellable,
82 GAsyncReadyCallback callback,
84 static gssize g_buffered_input_stream_read_finish (GInputStream *stream,
87 static gssize g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
89 GCancellable *cancellable,
91 static void g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
94 GCancellable *cancellable,
95 GAsyncReadyCallback callback,
97 static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
101 static void compact_buffer (GBufferedInputStream *stream);
103 G_DEFINE_TYPE (GBufferedInputStream,
104 g_buffered_input_stream,
105 G_TYPE_FILTER_INPUT_STREAM)
109 g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
111 GObjectClass *object_class;
112 GInputStreamClass *istream_class;
113 GBufferedInputStreamClass *bstream_class;
115 g_type_class_add_private (klass, sizeof (GBufferedInputStreamPrivate));
117 object_class = G_OBJECT_CLASS (klass);
118 object_class->get_property = g_buffered_input_stream_get_property;
119 object_class->set_property = g_buffered_input_stream_set_property;
120 object_class->finalize = g_buffered_input_stream_finalize;
122 istream_class = G_INPUT_STREAM_CLASS (klass);
123 istream_class->skip = g_buffered_input_stream_skip;
124 istream_class->skip_async = g_buffered_input_stream_skip_async;
125 istream_class->skip_finish = g_buffered_input_stream_skip_finish;
126 istream_class->read = g_buffered_input_stream_read;
127 istream_class->read_async = g_buffered_input_stream_read_async;
128 istream_class->read_finish = g_buffered_input_stream_read_finish;
130 bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
131 bstream_class->fill = g_buffered_input_stream_real_fill;
132 bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
133 bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
135 g_object_class_install_property (object_class,
137 g_param_spec_uint ("buffer-size",
139 P_("The size of the backend buffer"),
143 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
144 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
150 * g_buffered_input_stream_get_buffer_size:
151 * @stream: #GBufferedInputStream.
153 * Returns: the current buffer size, or -1 on error.
156 g_buffered_input_stream_get_buffer_size (GBufferedInputStream *stream)
158 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
160 return stream->priv->len;
164 * g_buffered_input_stream_set_buffer_size:
165 * @stream: #GBufferedInputStream.
168 * Sets the size of the internal buffer of @stream to @size, or to the
169 * size of the contents of the buffer. The buffer can never be resized
170 * smaller than its current contents.
173 g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
176 GBufferedInputStreamPrivate *priv;
180 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
186 in_buffer = priv->end - priv->pos;
188 /* Never resize smaller than current buffer contents */
189 size = MAX (size, in_buffer);
191 buffer = g_malloc (size);
192 memcpy (buffer, priv->buffer + priv->pos, in_buffer);
195 priv->end = in_buffer;
196 g_free (priv->buffer);
197 priv->buffer = buffer;
204 priv->buffer = g_malloc (size);
209 g_buffered_input_stream_set_property (GObject *object,
214 GBufferedInputStreamPrivate *priv;
215 GBufferedInputStream *bstream;
217 bstream = G_BUFFERED_INPUT_STREAM (object);
218 priv = bstream->priv;
223 g_buffered_input_stream_set_buffer_size (bstream, g_value_get_uint (value));
227 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 g_buffered_input_stream_get_property (GObject *object,
239 GBufferedInputStreamPrivate *priv;
240 GBufferedInputStream *bstream;
242 bstream = G_BUFFERED_INPUT_STREAM (object);
243 priv = bstream->priv;
249 g_value_set_uint (value, priv->len);
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
259 g_buffered_input_stream_finalize (GObject *object)
261 GBufferedInputStreamPrivate *priv;
262 GBufferedInputStream *stream;
264 stream = G_BUFFERED_INPUT_STREAM (object);
267 g_free (priv->buffer);
269 if (G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize)
270 (*G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize) (object);
274 g_buffered_input_stream_init (GBufferedInputStream *stream)
276 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
277 G_TYPE_BUFFERED_INPUT_STREAM,
278 GBufferedInputStreamPrivate);
283 * g_buffered_input_stream_new:
284 * @base_stream: a #GInputStream.
286 * Creates a new #GInputStream from the given @base_stream, with
287 * a buffer set to the default size (4 kilobytes).
289 * Returns: a #GInputStream for the given @base_stream.
292 g_buffered_input_stream_new (GInputStream *base_stream)
294 GInputStream *stream;
296 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
298 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
299 "base-stream", base_stream,
306 * g_buffered_input_stream_new_sized:
307 * @base_stream: a #GOutputStream.
310 * Creates a new #GBufferedInputStream from the given @base_stream, with
311 * a buffer set to @size.
313 * Returns: a #GInputStream.
316 g_buffered_input_stream_new_sized (GInputStream *base_stream,
319 GInputStream *stream;
321 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
323 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
324 "base-stream", base_stream,
325 "buffer-size", (guint)size,
332 * g_buffered_input_stream_fill:
333 * @stream: #GBufferedInputStream.
334 * @count: the number of bytes that will be read from the stream.
335 * @cancellable: optional #GCancellable object, %NULL to ignore.
336 * @error: location to store the error occuring, or %NULL to ignore.
338 * Tries to read @count bytes from the stream into the buffer.
339 * Will block during this read.
341 * If @count is zero, returns zero and does nothing. A value of @count
342 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
344 * On success, the number of bytes read into the buffer is returned.
345 * It is not an error if this is not the same as the requested size, as it
346 * can happen e.g. near the end of a file. Zero is returned on end of file
347 * (or if @count is zero), but never otherwise.
349 * If @cancellable is not %NULL, then the operation can be cancelled by
350 * triggering the cancellable object from another thread. If the operation
351 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
352 * operation was partially finished when the operation was cancelled the
353 * partial result will be returned, without an error.
355 * On error -1 is returned and @error is set accordingly.
357 * For the asynchronous, non-blocking, version of this function, see
358 * g_buffered_input_stream_fill_async().
360 * Returns: the number of bytes read into @stream's buffer, up to @count,
364 g_buffered_input_stream_fill (GBufferedInputStream *stream,
366 GCancellable *cancellable,
369 GBufferedInputStreamClass *class;
370 GInputStream *input_stream;
373 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
375 input_stream = G_INPUT_STREAM (stream);
377 if (g_input_stream_is_closed (input_stream))
379 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
380 _("Stream is already closed"));
384 if (g_input_stream_has_pending (input_stream))
386 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
387 _("Stream has outstanding operation"));
391 g_input_stream_set_pending (input_stream, TRUE);
394 g_push_current_cancellable (cancellable);
396 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
397 res = class->fill (stream, count, cancellable, error);
400 g_pop_current_cancellable (cancellable);
402 g_input_stream_set_pending (input_stream, FALSE);
408 async_fill_callback_wrapper (GObject *source_object,
412 GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
414 g_input_stream_set_pending (G_INPUT_STREAM (stream), FALSE);
415 (*stream->priv->outstanding_callback) (source_object, res, user_data);
416 g_object_unref (stream);
420 * g_buffered_input_stream_fill_async:
421 * @stream: #GBufferedInputStream.
423 * @io_priority: the io priority of the request. the io priority of the request.
424 * @cancellable: optional #GCancellable object
425 * @callback: a #GAsyncReadyCallback.
426 * @user_data: a #gpointer.
428 * Reads data into @stream's buffer asynchronously, up to @count size.
429 * @io_priority can be used to prioritize reads. For the synchronous
430 * version of this function, see g_buffered_input_stream_fill().
435 g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
438 GCancellable *cancellable,
439 GAsyncReadyCallback callback,
442 GBufferedInputStreamClass *class;
443 GSimpleAsyncResult *simple;
445 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
449 simple = g_simple_async_result_new (G_OBJECT (stream),
452 g_buffered_input_stream_fill_async);
453 g_simple_async_result_complete_in_idle (simple);
454 g_object_unref (simple);
458 if (((gssize) count) < 0)
460 g_simple_async_report_error_in_idle (G_OBJECT (stream),
463 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
464 _("Too large count value passed to g_input_stream_read_async"));
468 if (g_input_stream_is_closed (G_INPUT_STREAM (stream)))
470 g_simple_async_report_error_in_idle (G_OBJECT (stream),
473 G_IO_ERROR, G_IO_ERROR_CLOSED,
474 _("Stream is already closed"));
478 if (g_input_stream_has_pending (G_INPUT_STREAM (stream)))
480 g_simple_async_report_error_in_idle (G_OBJECT (stream),
483 G_IO_ERROR, G_IO_ERROR_PENDING,
484 _("Stream has outstanding operation"));
488 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
490 g_input_stream_set_pending (G_INPUT_STREAM (stream), TRUE);
491 stream->priv->outstanding_callback = callback;
492 g_object_ref (stream);
493 class->fill_async (stream, count, io_priority, cancellable,
494 async_fill_callback_wrapper, user_data);
498 * g_buffered_input_stream_fill_finished:
499 * @stream: a #GBufferedInputStream.
500 * @result: a #GAsyncResult.
503 * Finishes an asynchronous read.
505 * Returns: a #gssize of the read stream, or -1 on an error.
508 g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
509 GAsyncResult *result,
512 GSimpleAsyncResult *simple;
513 GBufferedInputStreamClass *class;
515 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
516 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
518 if (G_IS_SIMPLE_ASYNC_RESULT (result))
520 simple = G_SIMPLE_ASYNC_RESULT (result);
521 if (g_simple_async_result_propagate_error (simple, error))
524 /* Special case read of 0 bytes */
525 if (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_fill_async)
529 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
530 return class->fill_finish (stream, result, error);
534 * g_buffered_input_stream_get_available:
535 * @stream: #GBufferedInputStream.
537 * Returns: size of the available stream.
540 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
542 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
544 return stream->priv->end - stream->priv->pos;
548 * g_buffered_input_stream_peek:
549 * @stream: a #GBufferedInputStream.
550 * @buffer: a pointer to an allocated chunk of memory.
557 g_buffered_input_stream_peek (GBufferedInputStream *stream,
565 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
566 g_return_val_if_fail (buffer != NULL, -1);
568 available = g_buffered_input_stream_get_available (stream);
570 if (offset > available)
573 end = MIN (offset + count, available);
574 count = end - offset;
576 memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
581 compact_buffer (GBufferedInputStream *stream)
583 GBufferedInputStreamPrivate *priv;
588 current_size = priv->end - priv->pos;
590 g_memmove (priv->buffer,
591 priv->buffer + priv->pos,
595 priv->end = current_size;
599 g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
601 GCancellable *cancellable,
604 GBufferedInputStreamPrivate *priv;
605 GInputStream *base_stream;
614 in_buffer = priv->end - priv->pos;
616 /* Never fill more than can fit in the buffer */
617 count = MIN (count, priv->len - in_buffer);
619 /* If requested length does not fit at end, compact */
620 if (priv->len - priv->end < count)
621 compact_buffer (stream);
623 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
624 nread = g_input_stream_read (base_stream,
625 priv->buffer + priv->end,
637 g_buffered_input_stream_skip (GInputStream *stream,
639 GCancellable *cancellable,
642 GBufferedInputStream *bstream;
643 GBufferedInputStreamPrivate *priv;
644 GInputStream *base_stream;
645 gsize available, bytes_skipped;
648 bstream = G_BUFFERED_INPUT_STREAM (stream);
649 priv = bstream->priv;
651 available = priv->end - priv->pos;
653 if (count <= available)
659 /* Full request not available, skip all currently availbile and request refill for more */
663 bytes_skipped = available;
666 if (bytes_skipped > 0)
667 error = NULL; /* Ignore further errors if we already read some data */
669 if (count > priv->len)
671 /* Large request, shortcut buffer */
673 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
675 nread = g_input_stream_skip (base_stream,
680 if (nread < 0 && bytes_skipped == 0)
684 bytes_skipped += nread;
686 return bytes_skipped;
689 g_input_stream_set_pending (stream, FALSE); /* to avoid already pending error */
690 nread = g_buffered_input_stream_fill (bstream, priv->len, cancellable, error);
691 g_input_stream_set_pending (stream, TRUE); /* enable again */
695 if (bytes_skipped == 0)
698 return bytes_skipped;
701 available = priv->end - priv->pos;
702 count = MIN (count, available);
704 bytes_skipped += count;
707 return bytes_skipped;
711 g_buffered_input_stream_read (GInputStream *stream,
714 GCancellable *cancellable,
717 GBufferedInputStream *bstream;
718 GBufferedInputStreamPrivate *priv;
719 GInputStream *base_stream;
720 gsize available, bytes_read;
723 bstream = G_BUFFERED_INPUT_STREAM (stream);
724 priv = bstream->priv;
726 available = priv->end - priv->pos;
728 if (count <= available)
730 memcpy (buffer, priv->buffer + priv->pos, count);
735 /* Full request not available, read all currently availbile and request refill for more */
737 memcpy (buffer, priv->buffer + priv->pos, available);
740 bytes_read = available;
744 error = NULL; /* Ignore further errors if we already read some data */
746 if (count > priv->len)
748 /* Large request, shortcut buffer */
750 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
752 nread = g_input_stream_read (base_stream,
753 (char *)buffer + bytes_read,
758 if (nread < 0 && bytes_read == 0)
767 g_input_stream_set_pending (stream, FALSE); /* to avoid already pending error */
768 nread = g_buffered_input_stream_fill (bstream, priv->len, cancellable, error);
769 g_input_stream_set_pending (stream, TRUE); /* enable again */
778 available = priv->end - priv->pos;
779 count = MIN (count, available);
781 memcpy ((char *)buffer + bytes_read, (char *)priv->buffer + priv->pos, count);
788 /* ************************** */
789 /* Async stuff implementation */
790 /* ************************** */
793 fill_async_callback (GObject *source_object,
794 GAsyncResult *result,
799 GSimpleAsyncResult *simple;
804 res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
807 g_simple_async_result_set_op_res_gssize (simple, res);
810 g_simple_async_result_set_from_error (simple, error);
811 g_error_free (error);
814 /* Complete immediately, not in idle, since we're already in a mainloop callout */
815 g_simple_async_result_complete (simple);
816 g_object_unref (simple);
820 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
823 GCancellable *cancellable,
824 GAsyncReadyCallback callback,
827 GBufferedInputStreamPrivate *priv;
828 GInputStream *base_stream;
829 GSimpleAsyncResult *simple;
837 in_buffer = priv->end - priv->pos;
839 /* Never fill more than can fit in the buffer */
840 count = MIN (count, priv->len - in_buffer);
842 /* If requested length does not fit at end, compact */
843 if (priv->len - priv->end < count)
844 compact_buffer (stream);
846 simple = g_simple_async_result_new (G_OBJECT (stream),
848 g_buffered_input_stream_real_fill_async);
850 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
851 g_input_stream_read_async (base_stream,
852 priv->buffer + priv->end,
861 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
862 GAsyncResult *result,
865 GSimpleAsyncResult *simple;
868 simple = G_SIMPLE_ASYNC_RESULT (result);
869 g_assert (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_real_fill_async);
871 nread = g_simple_async_result_get_op_res_gssize (simple);
882 free_read_async_data (gpointer _data)
884 ReadAsyncData *data = _data;
885 g_slice_free (ReadAsyncData, data);
889 large_read_callback (GObject *source_object,
890 GAsyncResult *result,
893 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
898 data = g_simple_async_result_get_op_res_gpointer (simple);
901 nread = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
904 /* Only report the error if we've not already read some data */
905 if (nread < 0 && data->bytes_read == 0)
906 g_simple_async_result_set_from_error (simple, error);
909 data->bytes_read += nread;
912 g_error_free (error);
914 /* Complete immediately, not in idle, since we're already in a mainloop callout */
915 g_simple_async_result_complete (simple);
916 g_object_unref (simple);
920 read_fill_buffer_callback (GObject *source_object,
921 GAsyncResult *result,
924 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
925 GBufferedInputStream *bstream;
926 GBufferedInputStreamPrivate *priv;
932 bstream = G_BUFFERED_INPUT_STREAM (source_object);
933 priv = bstream->priv;
935 g_input_stream_set_pending (G_INPUT_STREAM (bstream), TRUE); /* enable again */
937 data = g_simple_async_result_get_op_res_gpointer (simple);
940 nread = g_buffered_input_stream_fill_finish (bstream,
943 if (nread < 0 && data->bytes_read == 0)
944 g_simple_async_result_set_from_error (simple, error);
949 available = priv->end - priv->pos;
950 data->count = MIN (data->count, available);
952 memcpy ((char *)data->buffer + data->bytes_read, (char *)priv->buffer + priv->pos, data->count);
953 data->bytes_read += data->count;
954 priv->pos += data->count;
958 g_error_free (error);
960 /* Complete immediately, not in idle, since we're already in a mainloop callout */
961 g_simple_async_result_complete (simple);
962 g_object_unref (simple);
966 g_buffered_input_stream_read_async (GInputStream *stream,
970 GCancellable *cancellable,
971 GAsyncReadyCallback callback,
974 GBufferedInputStream *bstream;
975 GBufferedInputStreamPrivate *priv;
976 GInputStream *base_stream;
978 GSimpleAsyncResult *simple;
981 bstream = G_BUFFERED_INPUT_STREAM (stream);
982 priv = bstream->priv;
984 data = g_slice_new (ReadAsyncData);
985 data->buffer = buffer;
986 data->bytes_read = 0;
987 simple = g_simple_async_result_new (G_OBJECT (stream),
989 g_buffered_input_stream_read_async);
990 g_simple_async_result_set_op_res_gpointer (simple, data, free_read_async_data);
992 available = priv->end - priv->pos;
994 if (count <= available)
996 memcpy (buffer, priv->buffer + priv->pos, count);
998 data->bytes_read = count;
1000 g_simple_async_result_complete_in_idle (simple);
1001 g_object_unref (simple);
1006 /* Full request not available, read all currently availbile and request refill for more */
1008 memcpy (buffer, priv->buffer + priv->pos, available);
1014 data->bytes_read = available;
1015 data->count = count;
1017 if (count > priv->len)
1019 /* Large request, shortcut buffer */
1021 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1023 g_input_stream_read_async (base_stream,
1024 (char *)buffer + data->bytes_read,
1026 io_priority, cancellable,
1027 large_read_callback,
1032 g_input_stream_set_pending (stream, FALSE); /* to avoid already pending error */
1033 g_buffered_input_stream_fill_async (bstream, priv->len,
1034 io_priority, cancellable,
1035 read_fill_buffer_callback, simple);
1040 g_buffered_input_stream_read_finish (GInputStream *stream,
1041 GAsyncResult *result,
1044 GSimpleAsyncResult *simple;
1045 ReadAsyncData *data;
1047 simple = G_SIMPLE_ASYNC_RESULT (result);
1049 g_assert (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_read_async);
1051 data = g_simple_async_result_get_op_res_gpointer (simple);
1053 return data->bytes_read;
1057 gssize bytes_skipped;
1062 free_skip_async_data (gpointer _data)
1064 SkipAsyncData *data = _data;
1065 g_slice_free (SkipAsyncData, data);
1069 large_skip_callback (GObject *source_object,
1070 GAsyncResult *result,
1073 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1074 SkipAsyncData *data;
1078 data = g_simple_async_result_get_op_res_gpointer (simple);
1081 nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1084 /* Only report the error if we've not already read some data */
1085 if (nread < 0 && data->bytes_skipped == 0)
1086 g_simple_async_result_set_from_error (simple, error);
1089 data->bytes_skipped += nread;
1092 g_error_free (error);
1094 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1095 g_simple_async_result_complete (simple);
1096 g_object_unref (simple);
1100 skip_fill_buffer_callback (GObject *source_object,
1101 GAsyncResult *result,
1104 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1105 GBufferedInputStream *bstream;
1106 GBufferedInputStreamPrivate *priv;
1107 SkipAsyncData *data;
1112 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1113 priv = bstream->priv;
1115 g_input_stream_set_pending (G_INPUT_STREAM (bstream), TRUE); /* enable again */
1117 data = g_simple_async_result_get_op_res_gpointer (simple);
1120 nread = g_buffered_input_stream_fill_finish (bstream,
1123 if (nread < 0 && data->bytes_skipped == 0)
1124 g_simple_async_result_set_from_error (simple, error);
1129 available = priv->end - priv->pos;
1130 data->count = MIN (data->count, available);
1132 data->bytes_skipped += data->count;
1133 priv->pos += data->count;
1137 g_error_free (error);
1139 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1140 g_simple_async_result_complete (simple);
1141 g_object_unref (simple);
1145 g_buffered_input_stream_skip_async (GInputStream *stream,
1148 GCancellable *cancellable,
1149 GAsyncReadyCallback callback,
1152 GBufferedInputStream *bstream;
1153 GBufferedInputStreamPrivate *priv;
1154 GInputStream *base_stream;
1156 GSimpleAsyncResult *simple;
1157 SkipAsyncData *data;
1159 bstream = G_BUFFERED_INPUT_STREAM (stream);
1160 priv = bstream->priv;
1162 data = g_slice_new (SkipAsyncData);
1163 data->bytes_skipped = 0;
1164 simple = g_simple_async_result_new (G_OBJECT (stream),
1165 callback, user_data,
1166 g_buffered_input_stream_skip_async);
1167 g_simple_async_result_set_op_res_gpointer (simple, data, free_skip_async_data);
1169 available = priv->end - priv->pos;
1171 if (count <= available)
1174 data->bytes_skipped = count;
1176 g_simple_async_result_complete_in_idle (simple);
1177 g_object_unref (simple);
1182 /* Full request not available, skip all currently availbile and request refill for more */
1189 data->bytes_skipped = available;
1190 data->count = count;
1192 if (count > priv->len)
1194 /* Large request, shortcut buffer */
1196 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1198 g_input_stream_skip_async (base_stream,
1200 io_priority, cancellable,
1201 large_skip_callback,
1206 g_input_stream_set_pending (stream, FALSE); /* to avoid already pending error */
1207 g_buffered_input_stream_fill_async (bstream, priv->len,
1208 io_priority, cancellable,
1209 skip_fill_buffer_callback, simple);
1214 g_buffered_input_stream_skip_finish (GInputStream *stream,
1215 GAsyncResult *result,
1218 GSimpleAsyncResult *simple;
1219 SkipAsyncData *data;
1221 simple = G_SIMPLE_ASYNC_RESULT (result);
1223 g_assert (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_skip_async);
1225 data = g_simple_async_result_get_op_res_gpointer (simple);
1227 return data->bytes_skipped;
1230 /* vim: ts=2 sw=2 et */