1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2007 Jürg Billeter
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Author: Christian Kellner <gicmo@gnome.org>
25 #include "gbufferedinputstream.h"
26 #include "ginputstream.h"
27 #include "gcancellable.h"
28 #include "gasyncresult.h"
30 #include "gseekable.h"
37 * SECTION:gbufferedinputstream
38 * @short_description: Buffered Input Stream
40 * @see_also: #GFilterInputStream, #GInputStream
42 * Buffered input stream implements #GFilterInputStream and provides
45 * By default, #GBufferedInputStream's buffer size is set at 4 kilobytes.
47 * To create a buffered input stream, use g_buffered_input_stream_new(),
48 * or g_buffered_input_stream_new_sized() to specify the buffer's size at
51 * To get the size of a buffer within a buffered input stream, use
52 * g_buffered_input_stream_get_buffer_size(). To change the size of a
53 * buffered input stream's buffer, use
54 * g_buffered_input_stream_set_buffer_size(). Note that the buffer's size
55 * cannot be reduced below the size of the data within the buffer.
59 #define DEFAULT_BUFFER_SIZE 4096
61 struct _GBufferedInputStreamPrivate {
66 GAsyncReadyCallback outstanding_callback;
74 static void g_buffered_input_stream_set_property (GObject *object,
79 static void g_buffered_input_stream_get_property (GObject *object,
83 static void g_buffered_input_stream_finalize (GObject *object);
86 static gssize g_buffered_input_stream_skip (GInputStream *stream,
88 GCancellable *cancellable,
90 static void g_buffered_input_stream_skip_async (GInputStream *stream,
93 GCancellable *cancellable,
94 GAsyncReadyCallback callback,
96 static gssize g_buffered_input_stream_skip_finish (GInputStream *stream,
99 static gssize g_buffered_input_stream_read (GInputStream *stream,
102 GCancellable *cancellable,
104 static gssize g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
106 GCancellable *cancellable,
108 static void g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
111 GCancellable *cancellable,
112 GAsyncReadyCallback callback,
114 static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
115 GAsyncResult *result,
118 static void g_buffered_input_stream_seekable_iface_init (GSeekableIface *iface);
119 static goffset g_buffered_input_stream_tell (GSeekable *seekable);
120 static gboolean g_buffered_input_stream_can_seek (GSeekable *seekable);
121 static gboolean g_buffered_input_stream_seek (GSeekable *seekable,
124 GCancellable *cancellable,
126 static gboolean g_buffered_input_stream_can_truncate (GSeekable *seekable);
127 static gboolean g_buffered_input_stream_truncate (GSeekable *seekable,
129 GCancellable *cancellable,
132 static void compact_buffer (GBufferedInputStream *stream);
134 G_DEFINE_TYPE_WITH_CODE (GBufferedInputStream,
135 g_buffered_input_stream,
136 G_TYPE_FILTER_INPUT_STREAM,
137 G_ADD_PRIVATE (GBufferedInputStream)
138 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
139 g_buffered_input_stream_seekable_iface_init))
142 g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
144 GObjectClass *object_class;
145 GInputStreamClass *istream_class;
146 GBufferedInputStreamClass *bstream_class;
148 object_class = G_OBJECT_CLASS (klass);
149 object_class->get_property = g_buffered_input_stream_get_property;
150 object_class->set_property = g_buffered_input_stream_set_property;
151 object_class->finalize = g_buffered_input_stream_finalize;
153 istream_class = G_INPUT_STREAM_CLASS (klass);
154 istream_class->skip = g_buffered_input_stream_skip;
155 istream_class->skip_async = g_buffered_input_stream_skip_async;
156 istream_class->skip_finish = g_buffered_input_stream_skip_finish;
157 istream_class->read_fn = g_buffered_input_stream_read;
159 bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
160 bstream_class->fill = g_buffered_input_stream_real_fill;
161 bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
162 bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
164 g_object_class_install_property (object_class,
166 g_param_spec_uint ("buffer-size",
168 P_("The size of the backend buffer"),
172 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
173 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
179 * g_buffered_input_stream_get_buffer_size:
180 * @stream: a #GBufferedInputStream
182 * Gets the size of the input buffer.
184 * Returns: the current buffer size.
187 g_buffered_input_stream_get_buffer_size (GBufferedInputStream *stream)
189 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), 0);
191 return stream->priv->len;
195 * g_buffered_input_stream_set_buffer_size:
196 * @stream: a #GBufferedInputStream
199 * Sets the size of the internal buffer of @stream to @size, or to the
200 * size of the contents of the buffer. The buffer can never be resized
201 * smaller than its current contents.
204 g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
207 GBufferedInputStreamPrivate *priv;
211 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
215 if (priv->len == size)
220 in_buffer = priv->end - priv->pos;
222 /* Never resize smaller than current buffer contents */
223 size = MAX (size, in_buffer);
225 buffer = g_malloc (size);
226 memcpy (buffer, priv->buffer + priv->pos, in_buffer);
229 priv->end = in_buffer;
230 g_free (priv->buffer);
231 priv->buffer = buffer;
238 priv->buffer = g_malloc (size);
241 g_object_notify (G_OBJECT (stream), "buffer-size");
245 g_buffered_input_stream_set_property (GObject *object,
250 GBufferedInputStream *bstream;
252 bstream = G_BUFFERED_INPUT_STREAM (object);
257 g_buffered_input_stream_set_buffer_size (bstream, g_value_get_uint (value));
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 g_buffered_input_stream_get_property (GObject *object,
272 GBufferedInputStreamPrivate *priv;
273 GBufferedInputStream *bstream;
275 bstream = G_BUFFERED_INPUT_STREAM (object);
276 priv = bstream->priv;
281 g_value_set_uint (value, priv->len);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 g_buffered_input_stream_finalize (GObject *object)
293 GBufferedInputStreamPrivate *priv;
294 GBufferedInputStream *stream;
296 stream = G_BUFFERED_INPUT_STREAM (object);
299 g_free (priv->buffer);
301 G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize (object);
305 g_buffered_input_stream_seekable_iface_init (GSeekableIface *iface)
307 iface->tell = g_buffered_input_stream_tell;
308 iface->can_seek = g_buffered_input_stream_can_seek;
309 iface->seek = g_buffered_input_stream_seek;
310 iface->can_truncate = g_buffered_input_stream_can_truncate;
311 iface->truncate_fn = g_buffered_input_stream_truncate;
315 g_buffered_input_stream_init (GBufferedInputStream *stream)
317 stream->priv = g_buffered_input_stream_get_instance_private (stream);
322 * g_buffered_input_stream_new:
323 * @base_stream: a #GInputStream
325 * Creates a new #GInputStream from the given @base_stream, with
326 * a buffer set to the default size (4 kilobytes).
328 * Returns: a #GInputStream for the given @base_stream.
331 g_buffered_input_stream_new (GInputStream *base_stream)
333 GInputStream *stream;
335 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
337 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
338 "base-stream", base_stream,
345 * g_buffered_input_stream_new_sized:
346 * @base_stream: a #GInputStream
349 * Creates a new #GBufferedInputStream from the given @base_stream,
350 * with a buffer set to @size.
352 * Returns: a #GInputStream.
355 g_buffered_input_stream_new_sized (GInputStream *base_stream,
358 GInputStream *stream;
360 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
362 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
363 "base-stream", base_stream,
364 "buffer-size", (guint)size,
371 * g_buffered_input_stream_fill:
372 * @stream: a #GBufferedInputStream
373 * @count: the number of bytes that will be read from the stream
374 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
375 * @error: location to store the error occurring, or %NULL to ignore
377 * Tries to read @count bytes from the stream into the buffer.
378 * Will block during this read.
380 * If @count is zero, returns zero and does nothing. A value of @count
381 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
383 * On success, the number of bytes read into the buffer is returned.
384 * It is not an error if this is not the same as the requested size, as it
385 * can happen e.g. near the end of a file. Zero is returned on end of file
386 * (or if @count is zero), but never otherwise.
388 * If @count is -1 then the attempted read size is equal to the number of
389 * bytes that are required to fill the buffer.
391 * If @cancellable is not %NULL, then the operation can be cancelled by
392 * triggering the cancellable object from another thread. If the operation
393 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
394 * operation was partially finished when the operation was cancelled the
395 * partial result will be returned, without an error.
397 * On error -1 is returned and @error is set accordingly.
399 * For the asynchronous, non-blocking, version of this function, see
400 * g_buffered_input_stream_fill_async().
402 * Returns: the number of bytes read into @stream's buffer, up to @count,
406 g_buffered_input_stream_fill (GBufferedInputStream *stream,
408 GCancellable *cancellable,
411 GBufferedInputStreamClass *class;
412 GInputStream *input_stream;
415 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
417 input_stream = G_INPUT_STREAM (stream);
421 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
422 _("Too large count value passed to %s"), G_STRFUNC);
426 if (!g_input_stream_set_pending (input_stream, error))
430 g_cancellable_push_current (cancellable);
432 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
433 res = class->fill (stream, count, cancellable, error);
436 g_cancellable_pop_current (cancellable);
438 g_input_stream_clear_pending (input_stream);
444 async_fill_callback_wrapper (GObject *source_object,
448 GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
450 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
451 (*stream->priv->outstanding_callback) (source_object, res, user_data);
452 g_object_unref (stream);
456 * g_buffered_input_stream_fill_async:
457 * @stream: a #GBufferedInputStream
458 * @count: the number of bytes that will be read from the stream
459 * @io_priority: the [I/O priority][io-priority] of the request
460 * @cancellable: (nullable): optional #GCancellable object
461 * @callback: (scope async) (closure user_data): a #GAsyncReadyCallback
462 * @user_data: a #gpointer
464 * Reads data into @stream's buffer asynchronously, up to @count size.
465 * @io_priority can be used to prioritize reads. For the synchronous
466 * version of this function, see g_buffered_input_stream_fill().
468 * If @count is -1 then the attempted read size is equal to the number
469 * of bytes that are required to fill the buffer.
472 g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
475 GCancellable *cancellable,
476 GAsyncReadyCallback callback,
479 GBufferedInputStreamClass *class;
480 GError *error = NULL;
482 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
488 task = g_task_new (stream, cancellable, callback, user_data);
489 g_task_set_source_tag (task, g_buffered_input_stream_fill_async);
490 g_task_return_int (task, 0);
491 g_object_unref (task);
497 g_task_report_new_error (stream, callback, user_data,
498 g_buffered_input_stream_fill_async,
499 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
500 _("Too large count value passed to %s"),
505 if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
507 g_task_report_error (stream, callback, user_data,
508 g_buffered_input_stream_fill_async,
513 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
515 stream->priv->outstanding_callback = callback;
516 g_object_ref (stream);
517 class->fill_async (stream, count, io_priority, cancellable,
518 async_fill_callback_wrapper, user_data);
522 * g_buffered_input_stream_fill_finish:
523 * @stream: a #GBufferedInputStream
524 * @result: a #GAsyncResult
527 * Finishes an asynchronous read.
529 * Returns: a #gssize of the read stream, or `-1` on an error.
532 g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
533 GAsyncResult *result,
536 GBufferedInputStreamClass *class;
538 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
539 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
541 if (g_async_result_legacy_propagate_error (result, error))
543 else if (g_async_result_is_tagged (result, g_buffered_input_stream_fill_async))
544 return g_task_propagate_int (G_TASK (result), error);
546 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
547 return class->fill_finish (stream, result, error);
551 * g_buffered_input_stream_get_available:
552 * @stream: #GBufferedInputStream
554 * Gets the size of the available data within the stream.
556 * Returns: size of the available stream.
559 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
561 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
563 return stream->priv->end - stream->priv->pos;
567 * g_buffered_input_stream_peek:
568 * @stream: a #GBufferedInputStream
569 * @buffer: (array length=count) (element-type guint8): a pointer to
570 * an allocated chunk of memory
574 * Peeks in the buffer, copying data of size @count into @buffer,
575 * offset @offset bytes.
577 * Returns: a #gsize of the number of bytes peeked, or -1 on error.
580 g_buffered_input_stream_peek (GBufferedInputStream *stream,
588 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
589 g_return_val_if_fail (buffer != NULL, -1);
591 available = g_buffered_input_stream_get_available (stream);
593 if (offset > available)
596 end = MIN (offset + count, available);
597 count = end - offset;
599 memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
604 * g_buffered_input_stream_peek_buffer:
605 * @stream: a #GBufferedInputStream
606 * @count: (out): a #gsize to get the number of bytes available in the buffer
608 * Returns the buffer with the currently available bytes. The returned
609 * buffer must not be modified and will become invalid when reading from
610 * the stream or filling the buffer.
612 * Returns: (array length=count) (element-type guint8) (transfer none):
616 g_buffered_input_stream_peek_buffer (GBufferedInputStream *stream,
619 GBufferedInputStreamPrivate *priv;
621 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), NULL);
626 *count = priv->end - priv->pos;
628 return priv->buffer + priv->pos;
632 compact_buffer (GBufferedInputStream *stream)
634 GBufferedInputStreamPrivate *priv;
639 current_size = priv->end - priv->pos;
641 memmove (priv->buffer, priv->buffer + priv->pos, current_size);
644 priv->end = current_size;
648 g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
650 GCancellable *cancellable,
653 GBufferedInputStreamPrivate *priv;
654 GInputStream *base_stream;
663 in_buffer = priv->end - priv->pos;
665 /* Never fill more than can fit in the buffer */
666 count = MIN ((gsize) count, priv->len - in_buffer);
668 /* If requested length does not fit at end, compact */
669 if (priv->len - priv->end < (gsize) count)
670 compact_buffer (stream);
672 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
673 nread = g_input_stream_read (base_stream,
674 priv->buffer + priv->end,
686 g_buffered_input_stream_skip (GInputStream *stream,
688 GCancellable *cancellable,
691 GBufferedInputStream *bstream;
692 GBufferedInputStreamPrivate *priv;
693 GBufferedInputStreamClass *class;
694 GInputStream *base_stream;
695 gsize available, bytes_skipped;
698 bstream = G_BUFFERED_INPUT_STREAM (stream);
699 priv = bstream->priv;
701 available = priv->end - priv->pos;
703 if (count <= available)
709 /* Full request not available, skip all currently available and
710 * request refill for more
715 bytes_skipped = available;
718 if (bytes_skipped > 0)
719 error = NULL; /* Ignore further errors if we already read some data */
721 if (count > priv->len)
723 /* Large request, shortcut buffer */
725 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
727 nread = g_input_stream_skip (base_stream,
732 if (nread < 0 && bytes_skipped == 0)
736 bytes_skipped += nread;
738 return bytes_skipped;
741 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
742 nread = class->fill (bstream, priv->len, cancellable, error);
746 if (bytes_skipped == 0)
749 return bytes_skipped;
752 available = priv->end - priv->pos;
753 count = MIN (count, available);
755 bytes_skipped += count;
758 return bytes_skipped;
762 g_buffered_input_stream_read (GInputStream *stream,
765 GCancellable *cancellable,
768 GBufferedInputStream *bstream;
769 GBufferedInputStreamPrivate *priv;
770 GBufferedInputStreamClass *class;
771 GInputStream *base_stream;
772 gsize available, bytes_read;
775 bstream = G_BUFFERED_INPUT_STREAM (stream);
776 priv = bstream->priv;
778 available = priv->end - priv->pos;
780 if (count <= available)
782 memcpy (buffer, priv->buffer + priv->pos, count);
787 /* Full request not available, read all currently available and
788 * request refill for more
791 memcpy (buffer, priv->buffer + priv->pos, available);
794 bytes_read = available;
798 error = NULL; /* Ignore further errors if we already read some data */
800 if (count > priv->len)
802 /* Large request, shortcut buffer */
804 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
806 nread = g_input_stream_read (base_stream,
807 (char *)buffer + bytes_read,
812 if (nread < 0 && bytes_read == 0)
821 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
822 nread = class->fill (bstream, priv->len, cancellable, error);
831 available = priv->end - priv->pos;
832 count = MIN (count, available);
834 memcpy ((char *)buffer + bytes_read, (char *)priv->buffer + priv->pos, count);
842 g_buffered_input_stream_tell (GSeekable *seekable)
844 GBufferedInputStream *bstream;
845 GBufferedInputStreamPrivate *priv;
846 GInputStream *base_stream;
847 GSeekable *base_stream_seekable;
851 bstream = G_BUFFERED_INPUT_STREAM (seekable);
852 priv = bstream->priv;
854 base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
855 if (!G_IS_SEEKABLE (base_stream))
857 base_stream_seekable = G_SEEKABLE (base_stream);
859 available = priv->end - priv->pos;
860 base_offset = g_seekable_tell (base_stream_seekable);
862 return base_offset - available;
866 g_buffered_input_stream_can_seek (GSeekable *seekable)
868 GInputStream *base_stream;
870 base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
871 return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
875 g_buffered_input_stream_seek (GSeekable *seekable,
878 GCancellable *cancellable,
881 GBufferedInputStream *bstream;
882 GBufferedInputStreamPrivate *priv;
883 GInputStream *base_stream;
884 GSeekable *base_stream_seekable;
886 bstream = G_BUFFERED_INPUT_STREAM (seekable);
887 priv = bstream->priv;
889 base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
890 if (!G_IS_SEEKABLE (base_stream))
892 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
893 _("Seek not supported on base stream"));
897 base_stream_seekable = G_SEEKABLE (base_stream);
899 if (type == G_SEEK_CUR)
901 if (offset <= (goffset) (priv->end - priv->pos) &&
902 offset >= (goffset) -priv->pos)
909 offset -= priv->end - priv->pos;
913 if (g_seekable_seek (base_stream_seekable, offset, type, cancellable, error))
926 g_buffered_input_stream_can_truncate (GSeekable *seekable)
932 g_buffered_input_stream_truncate (GSeekable *seekable,
934 GCancellable *cancellable,
937 g_set_error_literal (error,
939 G_IO_ERROR_NOT_SUPPORTED,
940 _("Cannot truncate GBufferedInputStream"));
945 * g_buffered_input_stream_read_byte:
946 * @stream: a #GBufferedInputStream
947 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
948 * @error: location to store the error occurring, or %NULL to ignore
950 * Tries to read a single byte from the stream or the buffer. Will block
953 * On success, the byte read from the stream is returned. On end of stream
954 * -1 is returned but it's not an exceptional error and @error is not set.
956 * If @cancellable is not %NULL, then the operation can be cancelled by
957 * triggering the cancellable object from another thread. If the operation
958 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
959 * operation was partially finished when the operation was cancelled the
960 * partial result will be returned, without an error.
962 * On error -1 is returned and @error is set accordingly.
964 * Returns: the byte read from the @stream, or -1 on end of stream or error.
967 g_buffered_input_stream_read_byte (GBufferedInputStream *stream,
968 GCancellable *cancellable,
971 GBufferedInputStreamPrivate *priv;
972 GBufferedInputStreamClass *class;
973 GInputStream *input_stream;
977 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
980 input_stream = G_INPUT_STREAM (stream);
982 if (g_input_stream_is_closed (input_stream))
984 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
985 _("Stream is already closed"));
989 if (!g_input_stream_set_pending (input_stream, error))
992 available = priv->end - priv->pos;
996 g_input_stream_clear_pending (input_stream);
997 return priv->buffer[priv->pos++];
1000 /* Byte not available, request refill for more */
1003 g_cancellable_push_current (cancellable);
1008 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1009 nread = class->fill (stream, priv->len, cancellable, error);
1012 g_cancellable_pop_current (cancellable);
1014 g_input_stream_clear_pending (input_stream);
1017 return -1; /* error or end of stream */
1019 return priv->buffer[priv->pos++];
1022 /* ************************** */
1023 /* Async stuff implementation */
1024 /* ************************** */
1027 fill_async_callback (GObject *source_object,
1028 GAsyncResult *result,
1033 GTask *task = user_data;
1036 res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
1039 g_task_return_error (task, error);
1042 GBufferedInputStream *stream;
1043 GBufferedInputStreamPrivate *priv;
1045 stream = g_task_get_source_object (task);
1046 priv = G_BUFFERED_INPUT_STREAM (stream)->priv;
1048 g_assert_cmpint (priv->end + res, <=, priv->len);
1051 g_task_return_int (task, res);
1054 g_object_unref (task);
1058 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
1061 GCancellable *cancellable,
1062 GAsyncReadyCallback callback,
1065 GBufferedInputStreamPrivate *priv;
1066 GInputStream *base_stream;
1070 priv = stream->priv;
1075 in_buffer = priv->end - priv->pos;
1077 /* Never fill more than can fit in the buffer */
1078 count = MIN ((gsize) count, priv->len - in_buffer);
1080 /* If requested length does not fit at end, compact */
1081 if (priv->len - priv->end < (gsize) count)
1082 compact_buffer (stream);
1084 task = g_task_new (stream, cancellable, callback, user_data);
1085 g_task_set_source_tag (task, g_buffered_input_stream_real_fill_async);
1087 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1088 g_input_stream_read_async (base_stream,
1089 priv->buffer + priv->end,
1093 fill_async_callback,
1098 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
1099 GAsyncResult *result,
1102 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1104 return g_task_propagate_int (G_TASK (result), error);
1109 gsize bytes_skipped;
1114 free_skip_async_data (gpointer _data)
1116 SkipAsyncData *data = _data;
1117 g_slice_free (SkipAsyncData, data);
1121 large_skip_callback (GObject *source_object,
1122 GAsyncResult *result,
1125 GTask *task = G_TASK (user_data);
1126 SkipAsyncData *data;
1130 data = g_task_get_task_data (task);
1133 nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1136 /* Only report the error if we've not already read some data */
1137 if (nread < 0 && data->bytes_skipped == 0)
1138 g_task_return_error (task, error);
1142 g_error_free (error);
1145 data->bytes_skipped += nread;
1147 g_task_return_int (task, data->bytes_skipped);
1150 g_object_unref (task);
1154 skip_fill_buffer_callback (GObject *source_object,
1155 GAsyncResult *result,
1158 GTask *task = G_TASK (user_data);
1159 GBufferedInputStream *bstream;
1160 GBufferedInputStreamPrivate *priv;
1161 SkipAsyncData *data;
1166 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1167 priv = bstream->priv;
1169 data = g_task_get_task_data (task);
1172 nread = g_buffered_input_stream_fill_finish (bstream,
1175 if (nread < 0 && data->bytes_skipped == 0)
1176 g_task_return_error (task, error);
1180 g_error_free (error);
1184 available = priv->end - priv->pos;
1185 data->count = MIN (data->count, available);
1187 data->bytes_skipped += data->count;
1188 priv->pos += data->count;
1191 g_assert (data->bytes_skipped <= G_MAXSSIZE);
1192 g_task_return_int (task, data->bytes_skipped);
1195 g_object_unref (task);
1199 g_buffered_input_stream_skip_async (GInputStream *stream,
1202 GCancellable *cancellable,
1203 GAsyncReadyCallback callback,
1206 GBufferedInputStream *bstream;
1207 GBufferedInputStreamPrivate *priv;
1208 GBufferedInputStreamClass *class;
1209 GInputStream *base_stream;
1212 SkipAsyncData *data;
1214 bstream = G_BUFFERED_INPUT_STREAM (stream);
1215 priv = bstream->priv;
1217 data = g_slice_new (SkipAsyncData);
1218 data->bytes_skipped = 0;
1219 task = g_task_new (stream, cancellable, callback, user_data);
1220 g_task_set_source_tag (task, g_buffered_input_stream_skip_async);
1221 g_task_set_task_data (task, data, free_skip_async_data);
1223 available = priv->end - priv->pos;
1225 if (count <= available)
1229 g_task_return_int (task, count);
1230 g_object_unref (task);
1234 /* Full request not available, skip all currently available
1235 * and request refill for more
1243 data->bytes_skipped = available;
1244 data->count = count;
1246 if (count > priv->len)
1248 /* Large request, shortcut buffer */
1249 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1251 /* If 'count > G_MAXSSIZE then 'g_input_stream_skip_async()'
1252 * will return an error anyway before calling this.
1253 * Assert that this is never called for too big `count` for clarity. */
1254 g_assert ((gssize) count >= 0);
1255 g_input_stream_skip_async (base_stream,
1257 io_priority, cancellable,
1258 large_skip_callback,
1263 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1264 class->fill_async (bstream, priv->len, io_priority, cancellable,
1265 skip_fill_buffer_callback, task);
1270 g_buffered_input_stream_skip_finish (GInputStream *stream,
1271 GAsyncResult *result,
1274 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1276 return g_task_propagate_int (G_TASK (result), error);