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
36 * @see_also: #GFilterInputStream, #GInputStream
38 * Buffered input stream implements #GFilterInputStream and provides
41 * By default, #GBufferedInputStream's buffer size is set at 4 kilobytes.
43 * To create a buffered input stream, use g_buffered_input_stream_new(),
44 * or g_buffered_input_stream_new_sized() to specify the buffer's size at
47 * To get the size of a buffer within a buffered input stream, use
48 * g_buffered_input_stream_get_buffer_size(). To change the size of a
49 * buffered input stream's buffer, use
50 * g_buffered_input_stream_set_buffer_size(). Note that the buffer's size
51 * cannot be reduced below the size of the data within the buffer.
57 #define DEFAULT_BUFFER_SIZE 4096
59 struct _GBufferedInputStreamPrivate {
64 GAsyncReadyCallback outstanding_callback;
72 static void g_buffered_input_stream_set_property (GObject *object,
77 static void g_buffered_input_stream_get_property (GObject *object,
81 static void g_buffered_input_stream_finalize (GObject *object);
84 static gssize g_buffered_input_stream_skip (GInputStream *stream,
86 GCancellable *cancellable,
88 static void g_buffered_input_stream_skip_async (GInputStream *stream,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
94 static gssize g_buffered_input_stream_skip_finish (GInputStream *stream,
97 static gssize g_buffered_input_stream_read (GInputStream *stream,
100 GCancellable *cancellable,
102 static void g_buffered_input_stream_read_async (GInputStream *stream,
106 GCancellable *cancellable,
107 GAsyncReadyCallback callback,
109 static gssize g_buffered_input_stream_read_finish (GInputStream *stream,
110 GAsyncResult *result,
112 static gssize g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
114 GCancellable *cancellable,
116 static void g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
119 GCancellable *cancellable,
120 GAsyncReadyCallback callback,
122 static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
123 GAsyncResult *result,
126 static void compact_buffer (GBufferedInputStream *stream);
128 G_DEFINE_TYPE (GBufferedInputStream,
129 g_buffered_input_stream,
130 G_TYPE_FILTER_INPUT_STREAM)
134 g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
136 GObjectClass *object_class;
137 GInputStreamClass *istream_class;
138 GBufferedInputStreamClass *bstream_class;
140 g_type_class_add_private (klass, sizeof (GBufferedInputStreamPrivate));
142 object_class = G_OBJECT_CLASS (klass);
143 object_class->get_property = g_buffered_input_stream_get_property;
144 object_class->set_property = g_buffered_input_stream_set_property;
145 object_class->finalize = g_buffered_input_stream_finalize;
147 istream_class = G_INPUT_STREAM_CLASS (klass);
148 istream_class->skip = g_buffered_input_stream_skip;
149 istream_class->skip_async = g_buffered_input_stream_skip_async;
150 istream_class->skip_finish = g_buffered_input_stream_skip_finish;
151 istream_class->read = g_buffered_input_stream_read;
152 istream_class->read_async = g_buffered_input_stream_read_async;
153 istream_class->read_finish = g_buffered_input_stream_read_finish;
155 bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
156 bstream_class->fill = g_buffered_input_stream_real_fill;
157 bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
158 bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
160 g_object_class_install_property (object_class,
162 g_param_spec_uint ("buffer-size",
164 P_("The size of the backend buffer"),
168 G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
169 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
175 * g_buffered_input_stream_get_buffer_size:
176 * @stream: #GBufferedInputStream.
178 * Gets the size of the input buffer.
180 * Returns: the current buffer size, or %-1 on error.
183 g_buffered_input_stream_get_buffer_size (GBufferedInputStream *stream)
185 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
187 return stream->priv->len;
191 * g_buffered_input_stream_set_buffer_size:
192 * @stream: #GBufferedInputStream.
195 * Sets the size of the internal buffer of @stream to @size, or to the
196 * size of the contents of the buffer. The buffer can never be resized
197 * smaller than its current contents.
200 g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
203 GBufferedInputStreamPrivate *priv;
207 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
211 if (priv->len == size)
216 in_buffer = priv->end - priv->pos;
218 /* Never resize smaller than current buffer contents */
219 size = MAX (size, in_buffer);
221 buffer = g_malloc (size);
222 memcpy (buffer, priv->buffer + priv->pos, in_buffer);
225 priv->end = in_buffer;
226 g_free (priv->buffer);
227 priv->buffer = buffer;
234 priv->buffer = g_malloc (size);
237 g_object_notify (G_OBJECT (stream), "buffer-size");
241 g_buffered_input_stream_set_property (GObject *object,
246 GBufferedInputStreamPrivate *priv;
247 GBufferedInputStream *bstream;
249 bstream = G_BUFFERED_INPUT_STREAM (object);
250 priv = bstream->priv;
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);
266 g_buffered_input_stream_get_property (GObject *object,
271 GBufferedInputStreamPrivate *priv;
272 GBufferedInputStream *bstream;
274 bstream = G_BUFFERED_INPUT_STREAM (object);
275 priv = bstream->priv;
280 g_value_set_uint (value, priv->len);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 g_buffered_input_stream_finalize (GObject *object)
292 GBufferedInputStreamPrivate *priv;
293 GBufferedInputStream *stream;
295 stream = G_BUFFERED_INPUT_STREAM (object);
298 g_free (priv->buffer);
300 if (G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize)
301 (*G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize) (object);
305 g_buffered_input_stream_init (GBufferedInputStream *stream)
307 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
308 G_TYPE_BUFFERED_INPUT_STREAM,
309 GBufferedInputStreamPrivate);
314 * g_buffered_input_stream_new:
315 * @base_stream: a #GInputStream.
317 * Creates a new #GInputStream from the given @base_stream, with
318 * a buffer set to the default size (4 kilobytes).
320 * Returns: a #GInputStream for the given @base_stream.
323 g_buffered_input_stream_new (GInputStream *base_stream)
325 GInputStream *stream;
327 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
329 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
330 "base-stream", base_stream,
337 * g_buffered_input_stream_new_sized:
338 * @base_stream: a #GOutputStream.
341 * Creates a new #GBufferedInputStream from the given @base_stream,
342 * with a buffer set to @size.
344 * Returns: a #GInputStream.
347 g_buffered_input_stream_new_sized (GInputStream *base_stream,
350 GInputStream *stream;
352 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
354 stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
355 "base-stream", base_stream,
356 "buffer-size", (guint)size,
363 * g_buffered_input_stream_fill:
364 * @stream: #GBufferedInputStream.
365 * @count: the number of bytes that will be read from the stream.
366 * @cancellable: optional #GCancellable object, %NULL to ignore.
367 * @error: location to store the error occuring, or %NULL to ignore.
369 * Tries to read @count bytes from the stream into the buffer.
370 * Will block during this read.
372 * If @count is zero, returns zero and does nothing. A value of @count
373 * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
375 * On success, the number of bytes read into the buffer is returned.
376 * It is not an error if this is not the same as the requested size, as it
377 * can happen e.g. near the end of a file. Zero is returned on end of file
378 * (or if @count is zero), but never otherwise.
380 * If @cancellable is not %NULL, then the operation can be cancelled by
381 * triggering the cancellable object from another thread. If the operation
382 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
383 * operation was partially finished when the operation was cancelled the
384 * partial result will be returned, without an error.
386 * On error -1 is returned and @error is set accordingly.
388 * For the asynchronous, non-blocking, version of this function, see
389 * g_buffered_input_stream_fill_async().
391 * Returns: the number of bytes read into @stream's buffer, up to @count,
395 g_buffered_input_stream_fill (GBufferedInputStream *stream,
397 GCancellable *cancellable,
400 GBufferedInputStreamClass *class;
401 GInputStream *input_stream;
404 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
406 input_stream = G_INPUT_STREAM (stream);
408 if (!g_input_stream_set_pending (input_stream, error))
412 g_push_current_cancellable (cancellable);
414 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
415 res = class->fill (stream, count, cancellable, error);
418 g_pop_current_cancellable (cancellable);
420 g_input_stream_clear_pending (input_stream);
426 async_fill_callback_wrapper (GObject *source_object,
430 GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
432 g_input_stream_clear_pending (G_INPUT_STREAM (stream));
433 (*stream->priv->outstanding_callback) (source_object, res, user_data);
434 g_object_unref (stream);
438 * g_buffered_input_stream_fill_async:
439 * @stream: #GBufferedInputStream.
441 * @io_priority: the <link linkend="io-priority">I/O priority</link>
443 * @cancellable: optional #GCancellable object
444 * @callback: a #GAsyncReadyCallback.
445 * @user_data: a #gpointer.
447 * Reads data into @stream's buffer asynchronously, up to @count size.
448 * @io_priority can be used to prioritize reads. For the synchronous
449 * version of this function, see g_buffered_input_stream_fill().
452 g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
455 GCancellable *cancellable,
456 GAsyncReadyCallback callback,
459 GBufferedInputStreamClass *class;
460 GSimpleAsyncResult *simple;
461 GError *error = NULL;
463 g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
467 simple = g_simple_async_result_new (G_OBJECT (stream),
470 g_buffered_input_stream_fill_async);
471 g_simple_async_result_complete_in_idle (simple);
472 g_object_unref (simple);
476 if (((gssize) count) < 0)
478 g_simple_async_report_error_in_idle (G_OBJECT (stream),
481 G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
482 _("Too large count value passed to g_input_stream_read_async"));
486 if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
488 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
492 g_error_free (error);
496 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
498 stream->priv->outstanding_callback = callback;
499 g_object_ref (stream);
500 class->fill_async (stream, count, io_priority, cancellable,
501 async_fill_callback_wrapper, user_data);
505 * g_buffered_input_stream_fill_finish:
506 * @stream: a #GBufferedInputStream.
507 * @result: a #GAsyncResult.
510 * Finishes an asynchronous read.
512 * Returns: a #gssize of the read stream, or %-1 on an error.
515 g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
516 GAsyncResult *result,
519 GSimpleAsyncResult *simple;
520 GBufferedInputStreamClass *class;
522 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
523 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
525 if (G_IS_SIMPLE_ASYNC_RESULT (result))
527 simple = G_SIMPLE_ASYNC_RESULT (result);
528 if (g_simple_async_result_propagate_error (simple, error))
531 /* Special case read of 0 bytes */
532 if (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_fill_async)
536 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
537 return class->fill_finish (stream, result, error);
541 * g_buffered_input_stream_get_available:
542 * @stream: #GBufferedInputStream.
544 * Gets the size of the available data within the stream.
546 * Returns: size of the available stream.
549 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
551 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
553 return stream->priv->end - stream->priv->pos;
557 * g_buffered_input_stream_peek:
558 * @stream: a #GBufferedInputStream.
559 * @buffer: a pointer to an allocated chunk of memory.
563 * Peeks in the buffer, copying data of size @count into @buffer,
564 * offset @offset bytes.
566 * Returns: a #gsize of the number of bytes peeked, or %-1 on error.
569 g_buffered_input_stream_peek (GBufferedInputStream *stream,
577 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
578 g_return_val_if_fail (buffer != NULL, -1);
580 available = g_buffered_input_stream_get_available (stream);
582 if (offset > available)
585 end = MIN (offset + count, available);
586 count = end - offset;
588 memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
593 * g_buffered_input_stream_peek_buffer:
594 * @stream: a #GBufferedInputStream.
595 * @count: a #gsize to get the number of bytes available in the buffer.
597 * Returns the buffer with the currently available bytes. The returned
598 * buffer must not be modified and will become invalid when reading from
599 * the stream or filling the buffer.
601 * Returns: read-only buffer
604 g_buffered_input_stream_peek_buffer (GBufferedInputStream *stream,
607 GBufferedInputStreamPrivate *priv;
609 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), NULL);
614 *count = priv->end - priv->pos;
616 return priv->buffer + priv->pos;
620 compact_buffer (GBufferedInputStream *stream)
622 GBufferedInputStreamPrivate *priv;
627 current_size = priv->end - priv->pos;
629 g_memmove (priv->buffer, priv->buffer + priv->pos, current_size);
632 priv->end = current_size;
636 g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
638 GCancellable *cancellable,
641 GBufferedInputStreamPrivate *priv;
642 GInputStream *base_stream;
651 in_buffer = priv->end - priv->pos;
653 /* Never fill more than can fit in the buffer */
654 count = MIN (count, priv->len - in_buffer);
656 /* If requested length does not fit at end, compact */
657 if (priv->len - priv->end < count)
658 compact_buffer (stream);
660 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
661 nread = g_input_stream_read (base_stream,
662 priv->buffer + priv->end,
674 g_buffered_input_stream_skip (GInputStream *stream,
676 GCancellable *cancellable,
679 GBufferedInputStream *bstream;
680 GBufferedInputStreamPrivate *priv;
681 GBufferedInputStreamClass *class;
682 GInputStream *base_stream;
683 gsize available, bytes_skipped;
686 bstream = G_BUFFERED_INPUT_STREAM (stream);
687 priv = bstream->priv;
689 available = priv->end - priv->pos;
691 if (count <= available)
697 /* Full request not available, skip all currently available and
698 * request refill for more
703 bytes_skipped = available;
706 if (bytes_skipped > 0)
707 error = NULL; /* Ignore further errors if we already read some data */
709 if (count > priv->len)
711 /* Large request, shortcut buffer */
713 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
715 nread = g_input_stream_skip (base_stream,
720 if (nread < 0 && bytes_skipped == 0)
724 bytes_skipped += nread;
726 return bytes_skipped;
729 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
730 nread = class->fill (bstream, priv->len, cancellable, error);
734 if (bytes_skipped == 0)
737 return bytes_skipped;
740 available = priv->end - priv->pos;
741 count = MIN (count, available);
743 bytes_skipped += count;
746 return bytes_skipped;
750 g_buffered_input_stream_read (GInputStream *stream,
753 GCancellable *cancellable,
756 GBufferedInputStream *bstream;
757 GBufferedInputStreamPrivate *priv;
758 GBufferedInputStreamClass *class;
759 GInputStream *base_stream;
760 gsize available, bytes_read;
763 bstream = G_BUFFERED_INPUT_STREAM (stream);
764 priv = bstream->priv;
766 available = priv->end - priv->pos;
768 if (count <= available)
770 memcpy (buffer, priv->buffer + priv->pos, count);
775 /* Full request not available, read all currently availbile and request refill for more */
777 memcpy (buffer, priv->buffer + priv->pos, available);
780 bytes_read = available;
784 error = NULL; /* Ignore further errors if we already read some data */
786 if (count > priv->len)
788 /* Large request, shortcut buffer */
790 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
792 nread = g_input_stream_read (base_stream,
793 (char *)buffer + bytes_read,
798 if (nread < 0 && bytes_read == 0)
807 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
808 nread = class->fill (bstream, priv->len, cancellable, error);
817 available = priv->end - priv->pos;
818 count = MIN (count, available);
820 memcpy ((char *)buffer + bytes_read, (char *)priv->buffer + priv->pos, count);
828 * g_buffered_input_stream_read_byte:
829 * @stream: #GBufferedInputStream.
830 * @cancellable: optional #GCancellable object, %NULL to ignore.
831 * @error: location to store the error occuring, or %NULL to ignore.
833 * Tries to read a single byte from the stream or the buffer. Will block
836 * On success, the byte read from the stream is returned. On end of stream
837 * -1 is returned but it's not an exceptional error and @error is not set.
839 * If @cancellable is not %NULL, then the operation can be cancelled by
840 * triggering the cancellable object from another thread. If the operation
841 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
842 * operation was partially finished when the operation was cancelled the
843 * partial result will be returned, without an error.
845 * On error -1 is returned and @error is set accordingly.
847 * Returns: the byte read from the @stream, or -1 on end of stream or error.
850 g_buffered_input_stream_read_byte (GBufferedInputStream *stream,
851 GCancellable *cancellable,
854 GBufferedInputStreamPrivate *priv;
855 GBufferedInputStreamClass *class;
856 GInputStream *input_stream;
860 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
863 input_stream = G_INPUT_STREAM (stream);
865 if (g_input_stream_is_closed (input_stream))
867 g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
868 _("Stream is already closed"));
872 if (!g_input_stream_set_pending (input_stream, error))
875 available = priv->end - priv->pos;
879 g_input_stream_clear_pending (input_stream);
880 return priv->buffer[priv->pos++];
883 /* Byte not available, request refill for more */
886 g_push_current_cancellable (cancellable);
891 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
892 nread = class->fill (stream, priv->len, cancellable, error);
895 g_pop_current_cancellable (cancellable);
897 g_input_stream_clear_pending (input_stream);
900 return -1; /* error or end of stream */
902 return priv->buffer[priv->pos++];
905 /* ************************** */
906 /* Async stuff implementation */
907 /* ************************** */
910 fill_async_callback (GObject *source_object,
911 GAsyncResult *result,
916 GSimpleAsyncResult *simple;
921 res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
924 g_simple_async_result_set_op_res_gssize (simple, res);
927 g_simple_async_result_set_from_error (simple, error);
928 g_error_free (error);
931 /* Complete immediately, not in idle, since we're already in a mainloop callout */
932 g_simple_async_result_complete (simple);
933 g_object_unref (simple);
937 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
940 GCancellable *cancellable,
941 GAsyncReadyCallback callback,
944 GBufferedInputStreamPrivate *priv;
945 GInputStream *base_stream;
946 GSimpleAsyncResult *simple;
954 in_buffer = priv->end - priv->pos;
956 /* Never fill more than can fit in the buffer */
957 count = MIN (count, priv->len - in_buffer);
959 /* If requested length does not fit at end, compact */
960 if (priv->len - priv->end < count)
961 compact_buffer (stream);
963 simple = g_simple_async_result_new (G_OBJECT (stream),
965 g_buffered_input_stream_real_fill_async);
967 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
968 g_input_stream_read_async (base_stream,
969 priv->buffer + priv->end,
978 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
979 GAsyncResult *result,
982 GSimpleAsyncResult *simple;
985 simple = G_SIMPLE_ASYNC_RESULT (result);
986 g_assert (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_real_fill_async);
988 nread = g_simple_async_result_get_op_res_gssize (simple);
999 free_read_async_data (gpointer _data)
1001 ReadAsyncData *data = _data;
1002 g_slice_free (ReadAsyncData, data);
1006 large_read_callback (GObject *source_object,
1007 GAsyncResult *result,
1010 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1011 ReadAsyncData *data;
1015 data = g_simple_async_result_get_op_res_gpointer (simple);
1018 nread = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
1021 /* Only report the error if we've not already read some data */
1022 if (nread < 0 && data->bytes_read == 0)
1023 g_simple_async_result_set_from_error (simple, error);
1026 data->bytes_read += nread;
1029 g_error_free (error);
1031 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1032 g_simple_async_result_complete (simple);
1033 g_object_unref (simple);
1037 read_fill_buffer_callback (GObject *source_object,
1038 GAsyncResult *result,
1041 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1042 GBufferedInputStream *bstream;
1043 GBufferedInputStreamPrivate *priv;
1044 ReadAsyncData *data;
1049 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1050 priv = bstream->priv;
1052 data = g_simple_async_result_get_op_res_gpointer (simple);
1055 nread = g_buffered_input_stream_fill_finish (bstream,
1058 if (nread < 0 && data->bytes_read == 0)
1059 g_simple_async_result_set_from_error (simple, error);
1064 available = priv->end - priv->pos;
1065 data->count = MIN (data->count, available);
1067 memcpy ((char *)data->buffer + data->bytes_read, (char *)priv->buffer + priv->pos, data->count);
1068 data->bytes_read += data->count;
1069 priv->pos += data->count;
1073 g_error_free (error);
1075 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1076 g_simple_async_result_complete (simple);
1077 g_object_unref (simple);
1081 g_buffered_input_stream_read_async (GInputStream *stream,
1085 GCancellable *cancellable,
1086 GAsyncReadyCallback callback,
1089 GBufferedInputStream *bstream;
1090 GBufferedInputStreamPrivate *priv;
1091 GBufferedInputStreamClass *class;
1092 GInputStream *base_stream;
1094 GSimpleAsyncResult *simple;
1095 ReadAsyncData *data;
1097 bstream = G_BUFFERED_INPUT_STREAM (stream);
1098 priv = bstream->priv;
1100 data = g_slice_new (ReadAsyncData);
1101 data->buffer = buffer;
1102 data->bytes_read = 0;
1103 simple = g_simple_async_result_new (G_OBJECT (stream),
1104 callback, user_data,
1105 g_buffered_input_stream_read_async);
1106 g_simple_async_result_set_op_res_gpointer (simple, data, free_read_async_data);
1108 available = priv->end - priv->pos;
1110 if (count <= available)
1112 memcpy (buffer, priv->buffer + priv->pos, count);
1114 data->bytes_read = count;
1116 g_simple_async_result_complete_in_idle (simple);
1117 g_object_unref (simple);
1122 /* Full request not available, read all currently availbile and request refill for more */
1124 memcpy (buffer, priv->buffer + priv->pos, available);
1130 data->bytes_read = available;
1131 data->count = count;
1133 if (count > priv->len)
1135 /* Large request, shortcut buffer */
1137 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1139 g_input_stream_read_async (base_stream,
1140 (char *)buffer + data->bytes_read,
1142 io_priority, cancellable,
1143 large_read_callback,
1148 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1149 class->fill_async (bstream, priv->len, io_priority, cancellable,
1150 read_fill_buffer_callback, simple);
1155 g_buffered_input_stream_read_finish (GInputStream *stream,
1156 GAsyncResult *result,
1159 GSimpleAsyncResult *simple;
1160 ReadAsyncData *data;
1162 simple = G_SIMPLE_ASYNC_RESULT (result);
1164 g_assert (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_read_async);
1166 data = g_simple_async_result_get_op_res_gpointer (simple);
1168 return data->bytes_read;
1172 gssize bytes_skipped;
1177 free_skip_async_data (gpointer _data)
1179 SkipAsyncData *data = _data;
1180 g_slice_free (SkipAsyncData, data);
1184 large_skip_callback (GObject *source_object,
1185 GAsyncResult *result,
1188 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1189 SkipAsyncData *data;
1193 data = g_simple_async_result_get_op_res_gpointer (simple);
1196 nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1199 /* Only report the error if we've not already read some data */
1200 if (nread < 0 && data->bytes_skipped == 0)
1201 g_simple_async_result_set_from_error (simple, error);
1204 data->bytes_skipped += nread;
1207 g_error_free (error);
1209 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1210 g_simple_async_result_complete (simple);
1211 g_object_unref (simple);
1215 skip_fill_buffer_callback (GObject *source_object,
1216 GAsyncResult *result,
1219 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1220 GBufferedInputStream *bstream;
1221 GBufferedInputStreamPrivate *priv;
1222 SkipAsyncData *data;
1227 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1228 priv = bstream->priv;
1230 data = g_simple_async_result_get_op_res_gpointer (simple);
1233 nread = g_buffered_input_stream_fill_finish (bstream,
1236 if (nread < 0 && data->bytes_skipped == 0)
1237 g_simple_async_result_set_from_error (simple, error);
1242 available = priv->end - priv->pos;
1243 data->count = MIN (data->count, available);
1245 data->bytes_skipped += data->count;
1246 priv->pos += data->count;
1250 g_error_free (error);
1252 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1253 g_simple_async_result_complete (simple);
1254 g_object_unref (simple);
1258 g_buffered_input_stream_skip_async (GInputStream *stream,
1261 GCancellable *cancellable,
1262 GAsyncReadyCallback callback,
1265 GBufferedInputStream *bstream;
1266 GBufferedInputStreamPrivate *priv;
1267 GBufferedInputStreamClass *class;
1268 GInputStream *base_stream;
1270 GSimpleAsyncResult *simple;
1271 SkipAsyncData *data;
1273 bstream = G_BUFFERED_INPUT_STREAM (stream);
1274 priv = bstream->priv;
1276 data = g_slice_new (SkipAsyncData);
1277 data->bytes_skipped = 0;
1278 simple = g_simple_async_result_new (G_OBJECT (stream),
1279 callback, user_data,
1280 g_buffered_input_stream_skip_async);
1281 g_simple_async_result_set_op_res_gpointer (simple, data, free_skip_async_data);
1283 available = priv->end - priv->pos;
1285 if (count <= available)
1288 data->bytes_skipped = count;
1290 g_simple_async_result_complete_in_idle (simple);
1291 g_object_unref (simple);
1296 /* Full request not available, skip all currently availbile and request refill for more */
1303 data->bytes_skipped = available;
1304 data->count = count;
1306 if (count > priv->len)
1308 /* Large request, shortcut buffer */
1310 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1312 g_input_stream_skip_async (base_stream,
1314 io_priority, cancellable,
1315 large_skip_callback,
1320 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1321 class->fill_async (bstream, priv->len, io_priority, cancellable,
1322 skip_fill_buffer_callback, simple);
1327 g_buffered_input_stream_skip_finish (GInputStream *stream,
1328 GAsyncResult *result,
1331 GSimpleAsyncResult *simple;
1332 SkipAsyncData *data;
1334 simple = G_SIMPLE_ASYNC_RESULT (result);
1336 g_assert (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_skip_async);
1338 data = g_simple_async_result_get_op_res_gpointer (simple);
1340 return data->bytes_skipped;
1344 #define __G_BUFFERED_INPUT_STREAM_C__
1345 #include "gioaliasdef.c"