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"
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 void g_buffered_input_stream_read_async (GInputStream *stream,
108 GCancellable *cancellable,
109 GAsyncReadyCallback callback,
111 static gssize g_buffered_input_stream_read_finish (GInputStream *stream,
112 GAsyncResult *result,
114 static gssize g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
116 GCancellable *cancellable,
118 static void g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
121 GCancellable *cancellable,
122 GAsyncReadyCallback callback,
124 static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
125 GAsyncResult *result,
128 static void compact_buffer (GBufferedInputStream *stream);
130 G_DEFINE_TYPE (GBufferedInputStream,
131 g_buffered_input_stream,
132 G_TYPE_FILTER_INPUT_STREAM)
136 g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
138 GObjectClass *object_class;
139 GInputStreamClass *istream_class;
140 GBufferedInputStreamClass *bstream_class;
142 g_type_class_add_private (klass, sizeof (GBufferedInputStreamPrivate));
144 object_class = G_OBJECT_CLASS (klass);
145 object_class->get_property = g_buffered_input_stream_get_property;
146 object_class->set_property = g_buffered_input_stream_set_property;
147 object_class->finalize = g_buffered_input_stream_finalize;
149 istream_class = G_INPUT_STREAM_CLASS (klass);
150 istream_class->skip = g_buffered_input_stream_skip;
151 istream_class->skip_async = g_buffered_input_stream_skip_async;
152 istream_class->skip_finish = g_buffered_input_stream_skip_finish;
153 istream_class->read_fn = g_buffered_input_stream_read;
154 istream_class->read_async = g_buffered_input_stream_read_async;
155 istream_class->read_finish = g_buffered_input_stream_read_finish;
157 bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
158 bstream_class->fill = g_buffered_input_stream_real_fill;
159 bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
160 bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
162 g_object_class_install_property (object_class,
164 g_param_spec_uint ("buffer-size",
166 P_("The size of the backend buffer"),
170 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
171 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
177 * g_buffered_input_stream_get_buffer_size:
178 * @stream: a #GBufferedInputStream
180 * Gets the size of the input buffer.
182 * Returns: the current buffer size.
185 g_buffered_input_stream_get_buffer_size (GBufferedInputStream *stream)
187 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), 0);
189 return stream->priv->len;
193 * g_buffered_input_stream_set_buffer_size:
194 * @stream: a #GBufferedInputStream
197 * Sets the size of the internal buffer of @stream to @size, or to the
198 * size of the contents of the buffer. The buffer can never be resized
199 * smaller than its current contents.
202 g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
205 GBufferedInputStreamPrivate *priv;
209 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
213 if (priv->len == size)
218 in_buffer = priv->end - priv->pos;
220 /* Never resize smaller than current buffer contents */
221 size = MAX (size, in_buffer);
223 buffer = g_malloc (size);
224 memcpy (buffer, priv->buffer + priv->pos, in_buffer);
227 priv->end = in_buffer;
228 g_free (priv->buffer);
229 priv->buffer = buffer;
236 priv->buffer = g_malloc (size);
239 g_object_notify (G_OBJECT (stream), "buffer-size");
243 g_buffered_input_stream_set_property (GObject *object,
248 GBufferedInputStream *bstream;
250 bstream = G_BUFFERED_INPUT_STREAM (object);
255 g_buffered_input_stream_set_buffer_size (bstream, g_value_get_uint (value));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265 g_buffered_input_stream_get_property (GObject *object,
270 GBufferedInputStreamPrivate *priv;
271 GBufferedInputStream *bstream;
273 bstream = G_BUFFERED_INPUT_STREAM (object);
274 priv = bstream->priv;
279 g_value_set_uint (value, priv->len);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 g_buffered_input_stream_finalize (GObject *object)
291 GBufferedInputStreamPrivate *priv;
292 GBufferedInputStream *stream;
294 stream = G_BUFFERED_INPUT_STREAM (object);
297 g_free (priv->buffer);
299 G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize (object);
303 g_buffered_input_stream_init (GBufferedInputStream *stream)
305 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
306 G_TYPE_BUFFERED_INPUT_STREAM,
307 GBufferedInputStreamPrivate);
312 * g_buffered_input_stream_new:
313 * @base_stream: a #GInputStream
315 * Creates a new #GInputStream from the given @base_stream, with
316 * a buffer set to the default size (4 kilobytes).
318 * Returns: a #GInputStream for the given @base_stream.
321 g_buffered_input_stream_new (GInputStream *base_stream)
323 GInputStream *stream;
325 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
327 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
328 "base-stream", base_stream,
335 * g_buffered_input_stream_new_sized:
336 * @base_stream: a #GInputStream
339 * Creates a new #GBufferedInputStream from the given @base_stream,
340 * with a buffer set to @size.
342 * Returns: a #GInputStream.
345 g_buffered_input_stream_new_sized (GInputStream *base_stream,
348 GInputStream *stream;
350 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
352 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
353 "base-stream", base_stream,
354 "buffer-size", (guint)size,
361 * g_buffered_input_stream_fill:
362 * @stream: a #GBufferedInputStream
363 * @count: the number of bytes that will be read from the stream
364 * @cancellable: optional #GCancellable object, %NULL to ignore
365 * @error: location to store the error occuring, or %NULL to ignore
367 * Tries to read @count bytes from the stream into the buffer.
368 * Will block during this read.
370 * If @count is zero, returns zero and does nothing. A value of @count
371 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
373 * On success, the number of bytes read into the buffer is returned.
374 * It is not an error if this is not the same as the requested size, as it
375 * can happen e.g. near the end of a file. Zero is returned on end of file
376 * (or if @count is zero), but never otherwise.
378 * If @count is -1 then the attempted read size is equal to the number of
379 * bytes that are required to fill the buffer.
381 * If @cancellable is not %NULL, then the operation can be cancelled by
382 * triggering the cancellable object from another thread. If the operation
383 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
384 * operation was partially finished when the operation was cancelled the
385 * partial result will be returned, without an error.
387 * On error -1 is returned and @error is set accordingly.
389 * For the asynchronous, non-blocking, version of this function, see
390 * g_buffered_input_stream_fill_async().
392 * Returns: the number of bytes read into @stream's buffer, up to @count,
396 g_buffered_input_stream_fill (GBufferedInputStream *stream,
398 GCancellable *cancellable,
401 GBufferedInputStreamClass *class;
402 GInputStream *input_stream;
405 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
407 input_stream = G_INPUT_STREAM (stream);
411 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
412 _("Too large count value passed to %s"), G_STRFUNC);
416 if (!g_input_stream_set_pending (input_stream, error))
420 g_cancellable_push_current (cancellable);
422 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
423 res = class->fill (stream, count, cancellable, error);
426 g_cancellable_pop_current (cancellable);
428 g_input_stream_clear_pending (input_stream);
434 async_fill_callback_wrapper (GObject *source_object,
438 GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
440 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
441 (*stream->priv->outstanding_callback) (source_object, res, user_data);
442 g_object_unref (stream);
446 * g_buffered_input_stream_fill_async:
447 * @stream: a #GBufferedInputStream
448 * @count: the number of bytes that will be read from the stream
449 * @io_priority: the <link linkend="io-priority">I/O priority</link>
451 * @cancellable: optional #GCancellable object
452 * @callback: a #GAsyncReadyCallback
453 * @user_data: a #gpointer
455 * Reads data into @stream's buffer asynchronously, up to @count size.
456 * @io_priority can be used to prioritize reads. For the synchronous
457 * version of this function, see g_buffered_input_stream_fill().
459 * If @count is -1 then the attempted read size is equal to the number
460 * of bytes that are required to fill the buffer.
463 g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
466 GCancellable *cancellable,
467 GAsyncReadyCallback callback,
470 GBufferedInputStreamClass *class;
471 GSimpleAsyncResult *simple;
472 GError *error = NULL;
474 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
478 simple = g_simple_async_result_new (G_OBJECT (stream),
481 g_buffered_input_stream_fill_async);
482 g_simple_async_result_complete_in_idle (simple);
483 g_object_unref (simple);
489 g_simple_async_report_error_in_idle (G_OBJECT (stream),
492 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
493 _("Too large count value passed to %s"),
498 if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
500 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
504 g_error_free (error);
508 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
510 stream->priv->outstanding_callback = callback;
511 g_object_ref (stream);
512 class->fill_async (stream, count, io_priority, cancellable,
513 async_fill_callback_wrapper, user_data);
517 * g_buffered_input_stream_fill_finish:
518 * @stream: a #GBufferedInputStream
519 * @result: a #GAsyncResult
522 * Finishes an asynchronous read.
524 * Returns: a #gssize of the read stream, or %-1 on an error.
527 g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
528 GAsyncResult *result,
531 GSimpleAsyncResult *simple;
532 GBufferedInputStreamClass *class;
534 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
535 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
537 if (G_IS_SIMPLE_ASYNC_RESULT (result))
539 simple = G_SIMPLE_ASYNC_RESULT (result);
540 if (g_simple_async_result_propagate_error (simple, error))
543 /* Special case read of 0 bytes */
544 if (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_fill_async)
548 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
549 return class->fill_finish (stream, result, error);
553 * g_buffered_input_stream_get_available:
554 * @stream: #GBufferedInputStream
556 * Gets the size of the available data within the stream.
558 * Returns: size of the available stream.
561 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
563 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
565 return stream->priv->end - stream->priv->pos;
569 * g_buffered_input_stream_peek:
570 * @stream: a #GBufferedInputStream
571 * @buffer: a pointer to an allocated chunk of memory
575 * Peeks in the buffer, copying data of size @count into @buffer,
576 * offset @offset bytes.
578 * Returns: a #gsize of the number of bytes peeked, or -1 on error.
581 g_buffered_input_stream_peek (GBufferedInputStream *stream,
589 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
590 g_return_val_if_fail (buffer != NULL, -1);
592 available = g_buffered_input_stream_get_available (stream);
594 if (offset > available)
597 end = MIN (offset + count, available);
598 count = end - offset;
600 memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
605 * g_buffered_input_stream_peek_buffer:
606 * @stream: a #GBufferedInputStream
607 * @count: a #gsize to get the number of bytes available in the buffer
609 * Returns the buffer with the currently available bytes. The returned
610 * buffer must not be modified and will become invalid when reading from
611 * the stream or filling the buffer.
613 * Returns: read-only buffer
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 g_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 (count, priv->len - in_buffer);
668 /* If requested length does not fit at end, compact */
669 if (priv->len - priv->end < 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_read_byte:
843 * @stream: a #GBufferedInputStream
844 * @cancellable: optional #GCancellable object, %NULL to ignore
845 * @error: location to store the error occuring, or %NULL to ignore
847 * Tries to read a single byte from the stream or the buffer. Will block
850 * On success, the byte read from the stream is returned. On end of stream
851 * -1 is returned but it's not an exceptional error and @error is not set.
853 * If @cancellable is not %NULL, then the operation can be cancelled by
854 * triggering the cancellable object from another thread. If the operation
855 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
856 * operation was partially finished when the operation was cancelled the
857 * partial result will be returned, without an error.
859 * On error -1 is returned and @error is set accordingly.
861 * Returns: the byte read from the @stream, or -1 on end of stream or error.
864 g_buffered_input_stream_read_byte (GBufferedInputStream *stream,
865 GCancellable *cancellable,
868 GBufferedInputStreamPrivate *priv;
869 GBufferedInputStreamClass *class;
870 GInputStream *input_stream;
874 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
877 input_stream = G_INPUT_STREAM (stream);
879 if (g_input_stream_is_closed (input_stream))
881 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
882 _("Stream is already closed"));
886 if (!g_input_stream_set_pending (input_stream, error))
889 available = priv->end - priv->pos;
893 g_input_stream_clear_pending (input_stream);
894 return priv->buffer[priv->pos++];
897 /* Byte not available, request refill for more */
900 g_cancellable_push_current (cancellable);
905 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
906 nread = class->fill (stream, priv->len, cancellable, error);
909 g_cancellable_pop_current (cancellable);
911 g_input_stream_clear_pending (input_stream);
914 return -1; /* error or end of stream */
916 return priv->buffer[priv->pos++];
919 /* ************************** */
920 /* Async stuff implementation */
921 /* ************************** */
924 fill_async_callback (GObject *source_object,
925 GAsyncResult *result,
930 GSimpleAsyncResult *simple;
935 res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
938 g_simple_async_result_set_op_res_gssize (simple, res);
941 g_simple_async_result_set_from_error (simple, error);
942 g_error_free (error);
946 GBufferedInputStreamPrivate *priv;
949 object = g_async_result_get_source_object (G_ASYNC_RESULT (simple));
950 priv = G_BUFFERED_INPUT_STREAM (object)->priv;
952 g_assert_cmpint (priv->end + res, <=, priv->len);
955 g_object_unref (object);
958 /* Complete immediately, not in idle, since we're already
959 * in a mainloop callout
961 g_simple_async_result_complete (simple);
962 g_object_unref (simple);
966 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
969 GCancellable *cancellable,
970 GAsyncReadyCallback callback,
973 GBufferedInputStreamPrivate *priv;
974 GInputStream *base_stream;
975 GSimpleAsyncResult *simple;
983 in_buffer = priv->end - priv->pos;
985 /* Never fill more than can fit in the buffer */
986 count = MIN (count, priv->len - in_buffer);
988 /* If requested length does not fit at end, compact */
989 if (priv->len - priv->end < count)
990 compact_buffer (stream);
992 simple = g_simple_async_result_new (G_OBJECT (stream),
994 g_buffered_input_stream_real_fill_async);
996 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
997 g_input_stream_read_async (base_stream,
998 priv->buffer + priv->end,
1002 fill_async_callback,
1007 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
1008 GAsyncResult *result,
1011 GSimpleAsyncResult *simple;
1014 simple = G_SIMPLE_ASYNC_RESULT (result);
1015 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_real_fill_async);
1017 nread = g_simple_async_result_get_op_res_gssize (simple);
1029 free_read_async_data (gpointer _data)
1031 ReadAsyncData *data = _data;
1032 g_slice_free (ReadAsyncData, data);
1036 large_read_callback (GObject *source_object,
1037 GAsyncResult *result,
1040 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1041 ReadAsyncData *data;
1045 data = g_simple_async_result_get_op_res_gpointer (simple);
1048 nread = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
1051 /* Only report the error if we've not already read some data */
1052 if (nread < 0 && data->bytes_read == 0)
1053 g_simple_async_result_set_from_error (simple, error);
1056 data->bytes_read += nread;
1059 g_error_free (error);
1061 /* Complete immediately, not in idle, since we're already
1062 * in a mainloop callout
1064 g_simple_async_result_complete (simple);
1065 g_object_unref (simple);
1069 read_fill_buffer_callback (GObject *source_object,
1070 GAsyncResult *result,
1073 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1074 GBufferedInputStream *bstream;
1075 GBufferedInputStreamPrivate *priv;
1076 ReadAsyncData *data;
1081 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1082 priv = bstream->priv;
1084 data = g_simple_async_result_get_op_res_gpointer (simple);
1087 nread = g_buffered_input_stream_fill_finish (bstream,
1090 if (nread < 0 && data->bytes_read == 0)
1091 g_simple_async_result_set_from_error (simple, error);
1096 available = priv->end - priv->pos;
1097 data->count = MIN (data->count, available);
1099 memcpy ((char *)data->buffer + data->bytes_read, (char *)priv->buffer + priv->pos, data->count);
1100 data->bytes_read += data->count;
1101 priv->pos += data->count;
1105 g_error_free (error);
1107 /* Complete immediately, not in idle, since we're already
1108 * in a mainloop callout
1110 g_simple_async_result_complete (simple);
1111 g_object_unref (simple);
1115 g_buffered_input_stream_read_async (GInputStream *stream,
1119 GCancellable *cancellable,
1120 GAsyncReadyCallback callback,
1123 GBufferedInputStream *bstream;
1124 GBufferedInputStreamPrivate *priv;
1125 GBufferedInputStreamClass *class;
1126 GInputStream *base_stream;
1128 GSimpleAsyncResult *simple;
1129 ReadAsyncData *data;
1131 bstream = G_BUFFERED_INPUT_STREAM (stream);
1132 priv = bstream->priv;
1134 data = g_slice_new (ReadAsyncData);
1135 data->buffer = buffer;
1136 data->bytes_read = 0;
1137 simple = g_simple_async_result_new (G_OBJECT (stream),
1138 callback, user_data,
1139 g_buffered_input_stream_read_async);
1140 g_simple_async_result_set_op_res_gpointer (simple, data, free_read_async_data);
1142 available = priv->end - priv->pos;
1144 if (count <= available)
1146 memcpy (buffer, priv->buffer + priv->pos, count);
1148 data->bytes_read = count;
1150 g_simple_async_result_complete_in_idle (simple);
1151 g_object_unref (simple);
1156 /* Full request not available, read all currently available
1157 * and request refill for more
1160 memcpy (buffer, priv->buffer + priv->pos, available);
1166 data->bytes_read = available;
1167 data->count = count;
1169 if (count > priv->len)
1171 /* Large request, shortcut buffer */
1173 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1175 g_input_stream_read_async (base_stream,
1176 (char *)buffer + data->bytes_read,
1178 io_priority, cancellable,
1179 large_read_callback,
1184 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1185 class->fill_async (bstream, priv->len, io_priority, cancellable,
1186 read_fill_buffer_callback, simple);
1191 g_buffered_input_stream_read_finish (GInputStream *stream,
1192 GAsyncResult *result,
1195 GSimpleAsyncResult *simple;
1196 ReadAsyncData *data;
1198 simple = G_SIMPLE_ASYNC_RESULT (result);
1200 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_read_async);
1202 data = g_simple_async_result_get_op_res_gpointer (simple);
1204 return data->bytes_read;
1209 gssize bytes_skipped;
1214 free_skip_async_data (gpointer _data)
1216 SkipAsyncData *data = _data;
1217 g_slice_free (SkipAsyncData, data);
1221 large_skip_callback (GObject *source_object,
1222 GAsyncResult *result,
1225 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1226 SkipAsyncData *data;
1230 data = g_simple_async_result_get_op_res_gpointer (simple);
1233 nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1236 /* Only report the error if we've not already read some data */
1237 if (nread < 0 && data->bytes_skipped == 0)
1238 g_simple_async_result_set_from_error (simple, error);
1241 data->bytes_skipped += nread;
1244 g_error_free (error);
1246 /* Complete immediately, not in idle, since we're already
1247 * in a mainloop callout
1249 g_simple_async_result_complete (simple);
1250 g_object_unref (simple);
1254 skip_fill_buffer_callback (GObject *source_object,
1255 GAsyncResult *result,
1258 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1259 GBufferedInputStream *bstream;
1260 GBufferedInputStreamPrivate *priv;
1261 SkipAsyncData *data;
1266 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1267 priv = bstream->priv;
1269 data = g_simple_async_result_get_op_res_gpointer (simple);
1272 nread = g_buffered_input_stream_fill_finish (bstream,
1275 if (nread < 0 && data->bytes_skipped == 0)
1276 g_simple_async_result_set_from_error (simple, error);
1280 available = priv->end - priv->pos;
1281 data->count = MIN (data->count, available);
1283 data->bytes_skipped += data->count;
1284 priv->pos += data->count;
1288 g_error_free (error);
1290 /* Complete immediately, not in idle, since we're already
1291 * in a mainloop callout
1293 g_simple_async_result_complete (simple);
1294 g_object_unref (simple);
1298 g_buffered_input_stream_skip_async (GInputStream *stream,
1301 GCancellable *cancellable,
1302 GAsyncReadyCallback callback,
1305 GBufferedInputStream *bstream;
1306 GBufferedInputStreamPrivate *priv;
1307 GBufferedInputStreamClass *class;
1308 GInputStream *base_stream;
1310 GSimpleAsyncResult *simple;
1311 SkipAsyncData *data;
1313 bstream = G_BUFFERED_INPUT_STREAM (stream);
1314 priv = bstream->priv;
1316 data = g_slice_new (SkipAsyncData);
1317 data->bytes_skipped = 0;
1318 simple = g_simple_async_result_new (G_OBJECT (stream),
1319 callback, user_data,
1320 g_buffered_input_stream_skip_async);
1321 g_simple_async_result_set_op_res_gpointer (simple, data, free_skip_async_data);
1323 available = priv->end - priv->pos;
1325 if (count <= available)
1328 data->bytes_skipped = count;
1330 g_simple_async_result_complete_in_idle (simple);
1331 g_object_unref (simple);
1335 /* Full request not available, skip all currently available
1336 * and request refill for more
1344 data->bytes_skipped = available;
1345 data->count = count;
1347 if (count > priv->len)
1349 /* Large request, shortcut buffer */
1351 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1353 g_input_stream_skip_async (base_stream,
1355 io_priority, cancellable,
1356 large_skip_callback,
1361 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1362 class->fill_async (bstream, priv->len, io_priority, cancellable,
1363 skip_fill_buffer_callback, simple);
1368 g_buffered_input_stream_skip_finish (GInputStream *stream,
1369 GAsyncResult *result,
1372 GSimpleAsyncResult *simple;
1373 SkipAsyncData *data;
1375 simple = G_SIMPLE_ASYNC_RESULT (result);
1377 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_skip_async);
1379 data = g_simple_async_result_get_op_res_gpointer (simple);
1381 return data->bytes_skipped;
1385 #define __G_BUFFERED_INPUT_STREAM_C__
1386 #include "gioaliasdef.c"