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 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 #GInputStream.
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_cancellable_push_current (cancellable);
414 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
415 res = class->fill (stream, count, cancellable, error);
418 g_cancellable_pop_current (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 %s"),
487 if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
489 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
493 g_error_free (error);
497 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
499 stream->priv->outstanding_callback = callback;
500 g_object_ref (stream);
501 class->fill_async (stream, count, io_priority, cancellable,
502 async_fill_callback_wrapper, user_data);
506 * g_buffered_input_stream_fill_finish:
507 * @stream: a #GBufferedInputStream.
508 * @result: a #GAsyncResult.
511 * Finishes an asynchronous read.
513 * Returns: a #gssize of the read stream, or %-1 on an error.
516 g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
517 GAsyncResult *result,
520 GSimpleAsyncResult *simple;
521 GBufferedInputStreamClass *class;
523 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
524 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
526 if (G_IS_SIMPLE_ASYNC_RESULT (result))
528 simple = G_SIMPLE_ASYNC_RESULT (result);
529 if (g_simple_async_result_propagate_error (simple, error))
532 /* Special case read of 0 bytes */
533 if (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_fill_async)
537 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
538 return class->fill_finish (stream, result, error);
542 * g_buffered_input_stream_get_available:
543 * @stream: #GBufferedInputStream.
545 * Gets the size of the available data within the stream.
547 * Returns: size of the available stream.
550 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
552 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
554 return stream->priv->end - stream->priv->pos;
558 * g_buffered_input_stream_peek:
559 * @stream: a #GBufferedInputStream.
560 * @buffer: a pointer to an allocated chunk of memory.
564 * Peeks in the buffer, copying data of size @count into @buffer,
565 * offset @offset bytes.
567 * Returns: a #gsize of the number of bytes peeked, or %-1 on error.
570 g_buffered_input_stream_peek (GBufferedInputStream *stream,
578 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
579 g_return_val_if_fail (buffer != NULL, -1);
581 available = g_buffered_input_stream_get_available (stream);
583 if (offset > available)
586 end = MIN (offset + count, available);
587 count = end - offset;
589 memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
594 * g_buffered_input_stream_peek_buffer:
595 * @stream: a #GBufferedInputStream.
596 * @count: a #gsize to get the number of bytes available in the buffer.
598 * Returns the buffer with the currently available bytes. The returned
599 * buffer must not be modified and will become invalid when reading from
600 * the stream or filling the buffer.
602 * Returns: read-only buffer
605 g_buffered_input_stream_peek_buffer (GBufferedInputStream *stream,
608 GBufferedInputStreamPrivate *priv;
610 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), NULL);
615 *count = priv->end - priv->pos;
617 return priv->buffer + priv->pos;
621 compact_buffer (GBufferedInputStream *stream)
623 GBufferedInputStreamPrivate *priv;
628 current_size = priv->end - priv->pos;
630 g_memmove (priv->buffer, priv->buffer + priv->pos, current_size);
633 priv->end = current_size;
637 g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
639 GCancellable *cancellable,
642 GBufferedInputStreamPrivate *priv;
643 GInputStream *base_stream;
652 in_buffer = priv->end - priv->pos;
654 /* Never fill more than can fit in the buffer */
655 count = MIN (count, priv->len - in_buffer);
657 /* If requested length does not fit at end, compact */
658 if (priv->len - priv->end < count)
659 compact_buffer (stream);
661 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
662 nread = g_input_stream_read (base_stream,
663 priv->buffer + priv->end,
675 g_buffered_input_stream_skip (GInputStream *stream,
677 GCancellable *cancellable,
680 GBufferedInputStream *bstream;
681 GBufferedInputStreamPrivate *priv;
682 GBufferedInputStreamClass *class;
683 GInputStream *base_stream;
684 gsize available, bytes_skipped;
687 bstream = G_BUFFERED_INPUT_STREAM (stream);
688 priv = bstream->priv;
690 available = priv->end - priv->pos;
692 if (count <= available)
698 /* Full request not available, skip all currently available and
699 * request refill for more
704 bytes_skipped = available;
707 if (bytes_skipped > 0)
708 error = NULL; /* Ignore further errors if we already read some data */
710 if (count > priv->len)
712 /* Large request, shortcut buffer */
714 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
716 nread = g_input_stream_skip (base_stream,
721 if (nread < 0 && bytes_skipped == 0)
725 bytes_skipped += nread;
727 return bytes_skipped;
730 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
731 nread = class->fill (bstream, priv->len, cancellable, error);
735 if (bytes_skipped == 0)
738 return bytes_skipped;
741 available = priv->end - priv->pos;
742 count = MIN (count, available);
744 bytes_skipped += count;
747 return bytes_skipped;
751 g_buffered_input_stream_read (GInputStream *stream,
754 GCancellable *cancellable,
757 GBufferedInputStream *bstream;
758 GBufferedInputStreamPrivate *priv;
759 GBufferedInputStreamClass *class;
760 GInputStream *base_stream;
761 gsize available, bytes_read;
764 bstream = G_BUFFERED_INPUT_STREAM (stream);
765 priv = bstream->priv;
767 available = priv->end - priv->pos;
769 if (count <= available)
771 memcpy (buffer, priv->buffer + priv->pos, count);
776 /* Full request not available, read all currently availbile and request refill for more */
778 memcpy (buffer, priv->buffer + priv->pos, available);
781 bytes_read = available;
785 error = NULL; /* Ignore further errors if we already read some data */
787 if (count > priv->len)
789 /* Large request, shortcut buffer */
791 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
793 nread = g_input_stream_read (base_stream,
794 (char *)buffer + bytes_read,
799 if (nread < 0 && bytes_read == 0)
808 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
809 nread = class->fill (bstream, priv->len, cancellable, error);
818 available = priv->end - priv->pos;
819 count = MIN (count, available);
821 memcpy ((char *)buffer + bytes_read, (char *)priv->buffer + priv->pos, count);
829 * g_buffered_input_stream_read_byte:
830 * @stream: #GBufferedInputStream.
831 * @cancellable: optional #GCancellable object, %NULL to ignore.
832 * @error: location to store the error occuring, or %NULL to ignore.
834 * Tries to read a single byte from the stream or the buffer. Will block
837 * On success, the byte read from the stream is returned. On end of stream
838 * -1 is returned but it's not an exceptional error and @error is not set.
840 * If @cancellable is not %NULL, then the operation can be cancelled by
841 * triggering the cancellable object from another thread. If the operation
842 * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
843 * operation was partially finished when the operation was cancelled the
844 * partial result will be returned, without an error.
846 * On error -1 is returned and @error is set accordingly.
848 * Returns: the byte read from the @stream, or -1 on end of stream or error.
851 g_buffered_input_stream_read_byte (GBufferedInputStream *stream,
852 GCancellable *cancellable,
855 GBufferedInputStreamPrivate *priv;
856 GBufferedInputStreamClass *class;
857 GInputStream *input_stream;
861 g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
864 input_stream = G_INPUT_STREAM (stream);
866 if (g_input_stream_is_closed (input_stream))
868 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
869 _("Stream is already closed"));
873 if (!g_input_stream_set_pending (input_stream, error))
876 available = priv->end - priv->pos;
880 g_input_stream_clear_pending (input_stream);
881 return priv->buffer[priv->pos++];
884 /* Byte not available, request refill for more */
887 g_cancellable_push_current (cancellable);
892 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
893 nread = class->fill (stream, priv->len, cancellable, error);
896 g_cancellable_pop_current (cancellable);
898 g_input_stream_clear_pending (input_stream);
901 return -1; /* error or end of stream */
903 return priv->buffer[priv->pos++];
906 /* ************************** */
907 /* Async stuff implementation */
908 /* ************************** */
911 fill_async_callback (GObject *source_object,
912 GAsyncResult *result,
917 GSimpleAsyncResult *simple;
922 res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
925 g_simple_async_result_set_op_res_gssize (simple, res);
928 g_simple_async_result_set_from_error (simple, error);
929 g_error_free (error);
932 /* Complete immediately, not in idle, since we're already in a mainloop callout */
933 g_simple_async_result_complete (simple);
934 g_object_unref (simple);
938 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
941 GCancellable *cancellable,
942 GAsyncReadyCallback callback,
945 GBufferedInputStreamPrivate *priv;
946 GInputStream *base_stream;
947 GSimpleAsyncResult *simple;
955 in_buffer = priv->end - priv->pos;
957 /* Never fill more than can fit in the buffer */
958 count = MIN (count, priv->len - in_buffer);
960 /* If requested length does not fit at end, compact */
961 if (priv->len - priv->end < count)
962 compact_buffer (stream);
964 simple = g_simple_async_result_new (G_OBJECT (stream),
966 g_buffered_input_stream_real_fill_async);
968 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
969 g_input_stream_read_async (base_stream,
970 priv->buffer + priv->end,
979 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
980 GAsyncResult *result,
983 GSimpleAsyncResult *simple;
986 simple = G_SIMPLE_ASYNC_RESULT (result);
987 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_real_fill_async);
989 nread = g_simple_async_result_get_op_res_gssize (simple);
1000 free_read_async_data (gpointer _data)
1002 ReadAsyncData *data = _data;
1003 g_slice_free (ReadAsyncData, data);
1007 large_read_callback (GObject *source_object,
1008 GAsyncResult *result,
1011 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1012 ReadAsyncData *data;
1016 data = g_simple_async_result_get_op_res_gpointer (simple);
1019 nread = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
1022 /* Only report the error if we've not already read some data */
1023 if (nread < 0 && data->bytes_read == 0)
1024 g_simple_async_result_set_from_error (simple, error);
1027 data->bytes_read += nread;
1030 g_error_free (error);
1032 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1033 g_simple_async_result_complete (simple);
1034 g_object_unref (simple);
1038 read_fill_buffer_callback (GObject *source_object,
1039 GAsyncResult *result,
1042 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1043 GBufferedInputStream *bstream;
1044 GBufferedInputStreamPrivate *priv;
1045 ReadAsyncData *data;
1050 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1051 priv = bstream->priv;
1053 data = g_simple_async_result_get_op_res_gpointer (simple);
1056 nread = g_buffered_input_stream_fill_finish (bstream,
1059 if (nread < 0 && data->bytes_read == 0)
1060 g_simple_async_result_set_from_error (simple, error);
1065 available = priv->end - priv->pos;
1066 data->count = MIN (data->count, available);
1068 memcpy ((char *)data->buffer + data->bytes_read, (char *)priv->buffer + priv->pos, data->count);
1069 data->bytes_read += data->count;
1070 priv->pos += data->count;
1074 g_error_free (error);
1076 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1077 g_simple_async_result_complete (simple);
1078 g_object_unref (simple);
1082 g_buffered_input_stream_read_async (GInputStream *stream,
1086 GCancellable *cancellable,
1087 GAsyncReadyCallback callback,
1090 GBufferedInputStream *bstream;
1091 GBufferedInputStreamPrivate *priv;
1092 GBufferedInputStreamClass *class;
1093 GInputStream *base_stream;
1095 GSimpleAsyncResult *simple;
1096 ReadAsyncData *data;
1098 bstream = G_BUFFERED_INPUT_STREAM (stream);
1099 priv = bstream->priv;
1101 data = g_slice_new (ReadAsyncData);
1102 data->buffer = buffer;
1103 data->bytes_read = 0;
1104 simple = g_simple_async_result_new (G_OBJECT (stream),
1105 callback, user_data,
1106 g_buffered_input_stream_read_async);
1107 g_simple_async_result_set_op_res_gpointer (simple, data, free_read_async_data);
1109 available = priv->end - priv->pos;
1111 if (count <= available)
1113 memcpy (buffer, priv->buffer + priv->pos, count);
1115 data->bytes_read = count;
1117 g_simple_async_result_complete_in_idle (simple);
1118 g_object_unref (simple);
1123 /* Full request not available, read all currently availbile and request refill for more */
1125 memcpy (buffer, priv->buffer + priv->pos, available);
1131 data->bytes_read = available;
1132 data->count = count;
1134 if (count > priv->len)
1136 /* Large request, shortcut buffer */
1138 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1140 g_input_stream_read_async (base_stream,
1141 (char *)buffer + data->bytes_read,
1143 io_priority, cancellable,
1144 large_read_callback,
1149 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1150 class->fill_async (bstream, priv->len, io_priority, cancellable,
1151 read_fill_buffer_callback, simple);
1156 g_buffered_input_stream_read_finish (GInputStream *stream,
1157 GAsyncResult *result,
1160 GSimpleAsyncResult *simple;
1161 ReadAsyncData *data;
1163 simple = G_SIMPLE_ASYNC_RESULT (result);
1165 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_read_async);
1167 data = g_simple_async_result_get_op_res_gpointer (simple);
1169 return data->bytes_read;
1173 gssize bytes_skipped;
1178 free_skip_async_data (gpointer _data)
1180 SkipAsyncData *data = _data;
1181 g_slice_free (SkipAsyncData, data);
1185 large_skip_callback (GObject *source_object,
1186 GAsyncResult *result,
1189 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1190 SkipAsyncData *data;
1194 data = g_simple_async_result_get_op_res_gpointer (simple);
1197 nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1200 /* Only report the error if we've not already read some data */
1201 if (nread < 0 && data->bytes_skipped == 0)
1202 g_simple_async_result_set_from_error (simple, error);
1205 data->bytes_skipped += nread;
1208 g_error_free (error);
1210 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1211 g_simple_async_result_complete (simple);
1212 g_object_unref (simple);
1216 skip_fill_buffer_callback (GObject *source_object,
1217 GAsyncResult *result,
1220 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (user_data);
1221 GBufferedInputStream *bstream;
1222 GBufferedInputStreamPrivate *priv;
1223 SkipAsyncData *data;
1228 bstream = G_BUFFERED_INPUT_STREAM (source_object);
1229 priv = bstream->priv;
1231 data = g_simple_async_result_get_op_res_gpointer (simple);
1234 nread = g_buffered_input_stream_fill_finish (bstream,
1237 if (nread < 0 && data->bytes_skipped == 0)
1238 g_simple_async_result_set_from_error (simple, error);
1243 available = priv->end - priv->pos;
1244 data->count = MIN (data->count, available);
1246 data->bytes_skipped += data->count;
1247 priv->pos += data->count;
1251 g_error_free (error);
1253 /* Complete immediately, not in idle, since we're already in a mainloop callout */
1254 g_simple_async_result_complete (simple);
1255 g_object_unref (simple);
1259 g_buffered_input_stream_skip_async (GInputStream *stream,
1262 GCancellable *cancellable,
1263 GAsyncReadyCallback callback,
1266 GBufferedInputStream *bstream;
1267 GBufferedInputStreamPrivate *priv;
1268 GBufferedInputStreamClass *class;
1269 GInputStream *base_stream;
1271 GSimpleAsyncResult *simple;
1272 SkipAsyncData *data;
1274 bstream = G_BUFFERED_INPUT_STREAM (stream);
1275 priv = bstream->priv;
1277 data = g_slice_new (SkipAsyncData);
1278 data->bytes_skipped = 0;
1279 simple = g_simple_async_result_new (G_OBJECT (stream),
1280 callback, user_data,
1281 g_buffered_input_stream_skip_async);
1282 g_simple_async_result_set_op_res_gpointer (simple, data, free_skip_async_data);
1284 available = priv->end - priv->pos;
1286 if (count <= available)
1289 data->bytes_skipped = count;
1291 g_simple_async_result_complete_in_idle (simple);
1292 g_object_unref (simple);
1297 /* Full request not available, skip all currently availbile and request refill for more */
1304 data->bytes_skipped = available;
1305 data->count = count;
1307 if (count > priv->len)
1309 /* Large request, shortcut buffer */
1311 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1313 g_input_stream_skip_async (base_stream,
1315 io_priority, cancellable,
1316 large_skip_callback,
1321 class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1322 class->fill_async (bstream, priv->len, io_priority, cancellable,
1323 skip_fill_buffer_callback, simple);
1328 g_buffered_input_stream_skip_finish (GInputStream *stream,
1329 GAsyncResult *result,
1332 GSimpleAsyncResult *simple;
1333 SkipAsyncData *data;
1335 simple = G_SIMPLE_ASYNC_RESULT (result);
1337 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_buffered_input_stream_skip_async);
1339 data = g_simple_async_result_get_op_res_gpointer (simple);
1341 return data->bytes_skipped;
1345 #define __G_BUFFERED_INPUT_STREAM_C__
1346 #include "gioaliasdef.c"