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 "gsimpleasyncresult.h"
34 * SECTION:gbufferedinputstream
35 * @short_description: Buffered Input Stream
37 * @see_also: #GFilterInputStream, #GInputStream
39 * Buffered input stream implements #GFilterInputStream and provides
42 * By default, #GBufferedInputStream's buffer size is set at 4 kilobytes.
44 * To create a buffered input stream, use g_buffered_input_stream_new(),
45 * or g_buffered_input_stream_new_sized() to specify the buffer's size at
48 * To get the size of a buffer within a buffered input stream, use
49 * g_buffered_input_stream_get_buffer_size(). To change the size of a
50 * buffered input stream's buffer, use
51 * g_buffered_input_stream_set_buffer_size(). Note that the buffer's size
52 * 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 void g_buffered_input_stream_read_async (GInputStream *stream,
107 GCancellable *cancellable,
108 GAsyncReadyCallback callback,
110 static gssize g_buffered_input_stream_read_finish (GInputStream *stream,
111 GAsyncResult *result,
113 static gssize g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
115 GCancellable *cancellable,
117 static void g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
120 GCancellable *cancellable,
121 GAsyncReadyCallback callback,
123 static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
124 GAsyncResult *result,
127 static void compact_buffer (GBufferedInputStream *stream);
129 G_DEFINE_TYPE (GBufferedInputStream,
130 g_buffered_input_stream,
131 G_TYPE_FILTER_INPUT_STREAM)
135 g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
137 GObjectClass *object_class;
138 GInputStreamClass *istream_class;
139 GBufferedInputStreamClass *bstream_class;
141 g_type_class_add_private (klass, sizeof (GBufferedInputStreamPrivate));
143 object_class = G_OBJECT_CLASS (klass);
144 object_class->get_property = g_buffered_input_stream_get_property;
145 object_class->set_property = g_buffered_input_stream_set_property;
146 object_class->finalize = g_buffered_input_stream_finalize;
148 istream_class = G_INPUT_STREAM_CLASS (klass);
149 istream_class->skip = g_buffered_input_stream_skip;
150 istream_class->skip_async = g_buffered_input_stream_skip_async;
151 istream_class->skip_finish = g_buffered_input_stream_skip_finish;
152 istream_class->read_fn = g_buffered_input_stream_read;
153 istream_class->read_async = g_buffered_input_stream_read_async;
154 istream_class->read_finish = g_buffered_input_stream_read_finish;
156 bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
157 bstream_class->fill = g_buffered_input_stream_real_fill;
158 bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
159 bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
161 g_object_class_install_property (object_class,
163 g_param_spec_uint ("buffer-size",
165 P_("The size of the backend buffer"),
169 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
170 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
176 * g_buffered_input_stream_get_buffer_size:
177 * @stream: #GBufferedInputStream.
179 * Gets the size of the input buffer.
181 * Returns: the current buffer size.
184 g_buffered_input_stream_get_buffer_size (GBufferedInputStream *stream)
186 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), 0);
188 return stream->priv->len;
192 * g_buffered_input_stream_set_buffer_size:
193 * @stream: #GBufferedInputStream.
196 * Sets the size of the internal buffer of @stream to @size, or to the
197 * size of the contents of the buffer. The buffer can never be resized
198 * smaller than its current contents.
201 g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
204 GBufferedInputStreamPrivate *priv;
208 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
212 if (priv->len == size)
217 in_buffer = priv->end - priv->pos;
219 /* Never resize smaller than current buffer contents */
220 size = MAX (size, in_buffer);
222 buffer = g_malloc (size);
223 memcpy (buffer, priv->buffer + priv->pos, in_buffer);
226 priv->end = in_buffer;
227 g_free (priv->buffer);
228 priv->buffer = buffer;
235 priv->buffer = g_malloc (size);
238 g_object_notify (G_OBJECT (stream), "buffer-size");
242 g_buffered_input_stream_set_property (GObject *object,
247 GBufferedInputStreamPrivate *priv;
248 GBufferedInputStream *bstream;
250 bstream = G_BUFFERED_INPUT_STREAM (object);
251 priv = bstream->priv;
256 g_buffered_input_stream_set_buffer_size (bstream, g_value_get_uint (value));
260 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 if (G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize)
302 (*G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize) (object);
306 g_buffered_input_stream_init (GBufferedInputStream *stream)
308 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
309 G_TYPE_BUFFERED_INPUT_STREAM,
310 GBufferedInputStreamPrivate);
315 * g_buffered_input_stream_new:
316 * @base_stream: a #GInputStream.
318 * Creates a new #GInputStream from the given @base_stream, with
319 * a buffer set to the default size (4 kilobytes).
321 * Returns: a #GInputStream for the given @base_stream.
324 g_buffered_input_stream_new (GInputStream *base_stream)
326 GInputStream *stream;
328 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
330 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
331 "base-stream", base_stream,
338 * g_buffered_input_stream_new_sized:
339 * @base_stream: a #GInputStream.
342 * Creates a new #GBufferedInputStream from the given @base_stream,
343 * with a buffer set to @size.
345 * Returns: a #GInputStream.
348 g_buffered_input_stream_new_sized (GInputStream *base_stream,
351 GInputStream *stream;
353 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
355 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
356 "base-stream", base_stream,
357 "buffer-size", (guint)size,
364 * g_buffered_input_stream_fill:
365 * @stream: #GBufferedInputStream.
366 * @count: the number of bytes that will be read from the stream.
367 * @cancellable: optional #GCancellable object, %NULL to ignore.
368 * @error: location to store the error occuring, or %NULL to ignore.
370 * Tries to read @count bytes from the stream into the buffer.
371 * Will block during this read.
373 * If @count is zero, returns zero and does nothing. A value of @count
374 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
376 * On success, the number of bytes read into the buffer is returned.
377 * It is not an error if this is not the same as the requested size, as it
378 * can happen e.g. near the end of a file. Zero is returned on end of file
379 * (or if @count is zero), but never otherwise.
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);
409 if (!g_input_stream_set_pending (input_stream, error))
413 g_cancellable_push_current (cancellable);
415 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
416 res = class->fill (stream, count, cancellable, error);
419 g_cancellable_pop_current (cancellable);
421 g_input_stream_clear_pending (input_stream);
427 async_fill_callback_wrapper (GObject *source_object,
431 GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
433 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
434 (*stream->priv->outstanding_callback) (source_object, res, user_data);
435 g_object_unref (stream);
439 * g_buffered_input_stream_fill_async:
440 * @stream: #GBufferedInputStream.
442 * @io_priority: the <link linkend="io-priority">I/O priority</link>
444 * @cancellable: optional #GCancellable object
445 * @callback: a #GAsyncReadyCallback.
446 * @user_data: a #gpointer.
448 * Reads data into @stream's buffer asynchronously, up to @count size.
449 * @io_priority can be used to prioritize reads. For the synchronous
450 * version of this function, see g_buffered_input_stream_fill().
453 g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
456 GCancellable *cancellable,
457 GAsyncReadyCallback callback,
460 GBufferedInputStreamClass *class;
461 GSimpleAsyncResult *simple;
462 GError *error = NULL;
464 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
468 simple = g_simple_async_result_new (G_OBJECT (stream),
471 g_buffered_input_stream_fill_async);
472 g_simple_async_result_complete_in_idle (simple);
473 g_object_unref (simple);
477 if (((gssize) count) < 0)
479 g_simple_async_report_error_in_idle (G_OBJECT (stream),
482 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
483 _("Too large count value passed to %s"),
488 if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
490 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
494 g_error_free (error);
498 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
500 stream->priv->outstanding_callback = callback;
501 g_object_ref (stream);
502 class->fill_async (stream, count, io_priority, cancellable,
503 async_fill_callback_wrapper, user_data);
507 * g_buffered_input_stream_fill_finish:
508 * @stream: a #GBufferedInputStream.
509 * @result: a #GAsyncResult.
512 * Finishes an asynchronous read.
514 * Returns: a #gssize of the read stream, or %-1 on an error.
517 g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
518 GAsyncResult *result,
521 GSimpleAsyncResult *simple;
522 GBufferedInputStreamClass *class;
524 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
525 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
527 if (G_IS_SIMPLE_ASYNC_RESULT (result))
529 simple = G_SIMPLE_ASYNC_RESULT (result);
530 if (g_simple_async_result_propagate_error (simple, error))
533 /* Special case read of 0 bytes */
534 if (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_fill_async)
538 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
539 return class->fill_finish (stream, result, error);
543 * g_buffered_input_stream_get_available:
544 * @stream: #GBufferedInputStream.
546 * Gets the size of the available data within the stream.
548 * Returns: size of the available stream.
551 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
553 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
555 return stream->priv->end - stream->priv->pos;
559 * g_buffered_input_stream_peek:
560 * @stream: a #GBufferedInputStream.
561 * @buffer: a pointer to an allocated chunk of memory.
565 * Peeks in the buffer, copying data of size @count into @buffer,
566 * offset @offset bytes.
568 * Returns: a #gsize of the number of bytes peeked, or %-1 on error.
571 g_buffered_input_stream_peek (GBufferedInputStream *stream,
579 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
580 g_return_val_if_fail (buffer != NULL, -1);
582 available = g_buffered_input_stream_get_available (stream);
584 if (offset > available)
587 end = MIN (offset + count, available);
588 count = end - offset;
590 memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
595 * g_buffered_input_stream_peek_buffer:
596 * @stream: a #GBufferedInputStream.
597 * @count: a #gsize to get the number of bytes available in the buffer.
599 * Returns the buffer with the currently available bytes. The returned
600 * buffer must not be modified and will become invalid when reading from
601 * the stream or filling the buffer.
603 * Returns: read-only buffer
606 g_buffered_input_stream_peek_buffer (GBufferedInputStream *stream,
609 GBufferedInputStreamPrivate *priv;
611 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), NULL);
616 *count = priv->end - priv->pos;
618 return priv->buffer + priv->pos;
622 compact_buffer (GBufferedInputStream *stream)
624 GBufferedInputStreamPrivate *priv;
629 current_size = priv->end - priv->pos;
631 g_memmove (priv->buffer, priv->buffer + priv->pos, current_size);
634 priv->end = current_size;
638 g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
640 GCancellable *cancellable,
643 GBufferedInputStreamPrivate *priv;
644 GInputStream *base_stream;
653 in_buffer = priv->end - priv->pos;
655 /* Never fill more than can fit in the buffer */
656 count = MIN (count, priv->len - in_buffer);
658 /* If requested length does not fit at end, compact */
659 if (priv->len - priv->end < count)
660 compact_buffer (stream);
662 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
663 nread = g_input_stream_read (base_stream,
664 priv->buffer + priv->end,
676 g_buffered_input_stream_skip (GInputStream *stream,
678 GCancellable *cancellable,
681 GBufferedInputStream *bstream;
682 GBufferedInputStreamPrivate *priv;
683 GBufferedInputStreamClass *class;
684 GInputStream *base_stream;
685 gsize available, bytes_skipped;
688 bstream = G_BUFFERED_INPUT_STREAM (stream);
689 priv = bstream->priv;
691 available = priv->end - priv->pos;
693 if (count <= available)
699 /* Full request not available, skip all currently available and
700 * request refill for more
705 bytes_skipped = available;
708 if (bytes_skipped > 0)
709 error = NULL; /* Ignore further errors if we already read some data */
711 if (count > priv->len)
713 /* Large request, shortcut buffer */
715 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
717 nread = g_input_stream_skip (base_stream,
722 if (nread < 0 && bytes_skipped == 0)
726 bytes_skipped += nread;
728 return bytes_skipped;
731 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
732 nread = class->fill (bstream, priv->len, cancellable, error);
736 if (bytes_skipped == 0)
739 return bytes_skipped;
742 available = priv->end - priv->pos;
743 count = MIN (count, available);
745 bytes_skipped += count;
748 return bytes_skipped;
752 g_buffered_input_stream_read (GInputStream *stream,
755 GCancellable *cancellable,
758 GBufferedInputStream *bstream;
759 GBufferedInputStreamPrivate *priv;
760 GBufferedInputStreamClass *class;
761 GInputStream *base_stream;
762 gsize available, bytes_read;
765 bstream = G_BUFFERED_INPUT_STREAM (stream);
766 priv = bstream->priv;
768 available = priv->end - priv->pos;
770 if (count <= available)
772 memcpy (buffer, priv->buffer + priv->pos, count);
777 /* Full request not available, read all currently availbile and 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: #GBufferedInputStream.
832 * @cancellable: optional #GCancellable object, %NULL to ignore.
833 * @error: location to store the error occuring, 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 (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_set_from_error (simple, error);
930 g_error_free (error);
933 /* Complete immediately, not in idle, since we're already in a mainloop callout */
934 g_simple_async_result_complete (simple);
935 g_object_unref (simple);
939 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
942 GCancellable *cancellable,
943 GAsyncReadyCallback callback,
946 GBufferedInputStreamPrivate *priv;
947 GInputStream *base_stream;
948 GSimpleAsyncResult *simple;
956 in_buffer = priv->end - priv->pos;
958 /* Never fill more than can fit in the buffer */
959 count = MIN (count, priv->len - in_buffer);
961 /* If requested length does not fit at end, compact */
962 if (priv->len - priv->end < count)
963 compact_buffer (stream);
965 simple = g_simple_async_result_new (G_OBJECT (stream),
967 g_buffered_input_stream_real_fill_async);
969 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
970 g_input_stream_read_async (base_stream,
971 priv->buffer + priv->end,
980 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
981 GAsyncResult *result,
984 GSimpleAsyncResult *simple;
987 simple = G_SIMPLE_ASYNC_RESULT (result);
988 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_real_fill_async);
990 nread = g_simple_async_result_get_op_res_gssize (simple);
1001 free_read_async_data (gpointer _data)
1003 ReadAsyncData *data = _data;
1004 g_slice_free (ReadAsyncData, data);
1008 large_read_callback (GObject *source_object,
1009 GAsyncResult *result,
1012 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1013 ReadAsyncData *data;
1017 data = g_simple_async_result_get_op_res_gpointer (simple);
1020 nread = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
1023 /* Only report the error if we've not already read some data */
1024 if (nread < 0 && data->bytes_read == 0)
1025 g_simple_async_result_set_from_error (simple, error);
1028 data->bytes_read += nread;
1031 g_error_free (error);
1033 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1034 g_simple_async_result_complete (simple);
1035 g_object_unref (simple);
1039 read_fill_buffer_callback (GObject *source_object,
1040 GAsyncResult *result,
1043 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1044 GBufferedInputStream *bstream;
1045 GBufferedInputStreamPrivate *priv;
1046 ReadAsyncData *data;
1051 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1052 priv = bstream->priv;
1054 data = g_simple_async_result_get_op_res_gpointer (simple);
1057 nread = g_buffered_input_stream_fill_finish (bstream,
1060 if (nread < 0 && data->bytes_read == 0)
1061 g_simple_async_result_set_from_error (simple, error);
1066 available = priv->end - priv->pos;
1067 data->count = MIN (data->count, available);
1069 memcpy ((char *)data->buffer + data->bytes_read, (char *)priv->buffer + priv->pos, data->count);
1070 data->bytes_read += data->count;
1071 priv->pos += data->count;
1075 g_error_free (error);
1077 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1078 g_simple_async_result_complete (simple);
1079 g_object_unref (simple);
1083 g_buffered_input_stream_read_async (GInputStream *stream,
1087 GCancellable *cancellable,
1088 GAsyncReadyCallback callback,
1091 GBufferedInputStream *bstream;
1092 GBufferedInputStreamPrivate *priv;
1093 GBufferedInputStreamClass *class;
1094 GInputStream *base_stream;
1096 GSimpleAsyncResult *simple;
1097 ReadAsyncData *data;
1099 bstream = G_BUFFERED_INPUT_STREAM (stream);
1100 priv = bstream->priv;
1102 data = g_slice_new (ReadAsyncData);
1103 data->buffer = buffer;
1104 data->bytes_read = 0;
1105 simple = g_simple_async_result_new (G_OBJECT (stream),
1106 callback, user_data,
1107 g_buffered_input_stream_read_async);
1108 g_simple_async_result_set_op_res_gpointer (simple, data, free_read_async_data);
1110 available = priv->end - priv->pos;
1112 if (count <= available)
1114 memcpy (buffer, priv->buffer + priv->pos, count);
1116 data->bytes_read = count;
1118 g_simple_async_result_complete_in_idle (simple);
1119 g_object_unref (simple);
1124 /* Full request not available, read all currently availbile and request refill for more */
1126 memcpy (buffer, priv->buffer + priv->pos, available);
1132 data->bytes_read = available;
1133 data->count = count;
1135 if (count > priv->len)
1137 /* Large request, shortcut buffer */
1139 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1141 g_input_stream_read_async (base_stream,
1142 (char *)buffer + data->bytes_read,
1144 io_priority, cancellable,
1145 large_read_callback,
1150 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1151 class->fill_async (bstream, priv->len, io_priority, cancellable,
1152 read_fill_buffer_callback, simple);
1157 g_buffered_input_stream_read_finish (GInputStream *stream,
1158 GAsyncResult *result,
1161 GSimpleAsyncResult *simple;
1162 ReadAsyncData *data;
1164 simple = G_SIMPLE_ASYNC_RESULT (result);
1166 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_read_async);
1168 data = g_simple_async_result_get_op_res_gpointer (simple);
1170 return data->bytes_read;
1174 gssize bytes_skipped;
1179 free_skip_async_data (gpointer _data)
1181 SkipAsyncData *data = _data;
1182 g_slice_free (SkipAsyncData, data);
1186 large_skip_callback (GObject *source_object,
1187 GAsyncResult *result,
1190 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1191 SkipAsyncData *data;
1195 data = g_simple_async_result_get_op_res_gpointer (simple);
1198 nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1201 /* Only report the error if we've not already read some data */
1202 if (nread < 0 && data->bytes_skipped == 0)
1203 g_simple_async_result_set_from_error (simple, error);
1206 data->bytes_skipped += nread;
1209 g_error_free (error);
1211 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1212 g_simple_async_result_complete (simple);
1213 g_object_unref (simple);
1217 skip_fill_buffer_callback (GObject *source_object,
1218 GAsyncResult *result,
1221 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1222 GBufferedInputStream *bstream;
1223 GBufferedInputStreamPrivate *priv;
1224 SkipAsyncData *data;
1229 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1230 priv = bstream->priv;
1232 data = g_simple_async_result_get_op_res_gpointer (simple);
1235 nread = g_buffered_input_stream_fill_finish (bstream,
1238 if (nread < 0 && data->bytes_skipped == 0)
1239 g_simple_async_result_set_from_error (simple, error);
1244 available = priv->end - priv->pos;
1245 data->count = MIN (data->count, available);
1247 data->bytes_skipped += data->count;
1248 priv->pos += data->count;
1252 g_error_free (error);
1254 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1255 g_simple_async_result_complete (simple);
1256 g_object_unref (simple);
1260 g_buffered_input_stream_skip_async (GInputStream *stream,
1263 GCancellable *cancellable,
1264 GAsyncReadyCallback callback,
1267 GBufferedInputStream *bstream;
1268 GBufferedInputStreamPrivate *priv;
1269 GBufferedInputStreamClass *class;
1270 GInputStream *base_stream;
1272 GSimpleAsyncResult *simple;
1273 SkipAsyncData *data;
1275 bstream = G_BUFFERED_INPUT_STREAM (stream);
1276 priv = bstream->priv;
1278 data = g_slice_new (SkipAsyncData);
1279 data->bytes_skipped = 0;
1280 simple = g_simple_async_result_new (G_OBJECT (stream),
1281 callback, user_data,
1282 g_buffered_input_stream_skip_async);
1283 g_simple_async_result_set_op_res_gpointer (simple, data, free_skip_async_data);
1285 available = priv->end - priv->pos;
1287 if (count <= available)
1290 data->bytes_skipped = count;
1292 g_simple_async_result_complete_in_idle (simple);
1293 g_object_unref (simple);
1298 /* Full request not available, skip all currently availbile and request refill for more */
1305 data->bytes_skipped = available;
1306 data->count = count;
1308 if (count > priv->len)
1310 /* Large request, shortcut buffer */
1312 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1314 g_input_stream_skip_async (base_stream,
1316 io_priority, cancellable,
1317 large_skip_callback,
1322 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1323 class->fill_async (bstream, priv->len, io_priority, cancellable,
1324 skip_fill_buffer_callback, simple);
1329 g_buffered_input_stream_skip_finish (GInputStream *stream,
1330 GAsyncResult *result,
1333 GSimpleAsyncResult *simple;
1334 SkipAsyncData *data;
1336 simple = G_SIMPLE_ASYNC_RESULT (result);
1338 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_skip_async);
1340 data = g_simple_async_result_get_op_res_gpointer (simple);
1342 return data->bytes_skipped;
1346 #define __G_BUFFERED_INPUT_STREAM_C__
1347 #include "gioaliasdef.c"