gio: port basic I/O classes from GSimpleAsyncResult to GTask
[platform/upstream/glib.git] / gio / gbufferedinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  * Copyright (C) 2007 Jürg Billeter
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Author: Christian Kellner <gicmo@gnome.org>
22  */
23
24 #include "config.h"
25 #include "gbufferedinputstream.h"
26 #include "ginputstream.h"
27 #include "gcancellable.h"
28 #include "gasyncresult.h"
29 #include "gtask.h"
30 #include "gseekable.h"
31 #include "gioerror.h"
32 #include <string.h>
33 #include "glibintl.h"
34
35
36 /**
37  * SECTION:gbufferedinputstream
38  * @short_description: Buffered Input Stream
39  * @include: gio/gio.h
40  * @see_also: #GFilterInputStream, #GInputStream
41  *
42  * Buffered input stream implements #GFilterInputStream and provides
43  * for buffered reads.
44  *
45  * By default, #GBufferedInputStream's buffer size is set at 4 kilobytes.
46  *
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
49  * construction.
50  *
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.
56  */
57
58
59 #define DEFAULT_BUFFER_SIZE 4096
60
61 struct _GBufferedInputStreamPrivate {
62   guint8 *buffer;
63   gsize   len;
64   gsize   pos;
65   gsize   end;
66   GAsyncReadyCallback outstanding_callback;
67 };
68
69 enum {
70   PROP_0,
71   PROP_BUFSIZE
72 };
73
74 static void g_buffered_input_stream_set_property  (GObject      *object,
75                                                    guint         prop_id,
76                                                    const GValue *value,
77                                                    GParamSpec   *pspec);
78
79 static void g_buffered_input_stream_get_property  (GObject      *object,
80                                                    guint         prop_id,
81                                                    GValue       *value,
82                                                    GParamSpec   *pspec);
83 static void g_buffered_input_stream_finalize      (GObject *object);
84
85
86 static gssize g_buffered_input_stream_skip             (GInputStream          *stream,
87                                                         gsize                  count,
88                                                         GCancellable          *cancellable,
89                                                         GError               **error);
90 static void   g_buffered_input_stream_skip_async       (GInputStream          *stream,
91                                                         gsize                  count,
92                                                         int                    io_priority,
93                                                         GCancellable          *cancellable,
94                                                         GAsyncReadyCallback    callback,
95                                                         gpointer               user_data);
96 static gssize g_buffered_input_stream_skip_finish      (GInputStream          *stream,
97                                                         GAsyncResult          *result,
98                                                         GError               **error);
99 static gssize g_buffered_input_stream_read             (GInputStream          *stream,
100                                                         void                  *buffer,
101                                                         gsize                  count,
102                                                         GCancellable          *cancellable,
103                                                         GError               **error);
104 static gssize g_buffered_input_stream_real_fill        (GBufferedInputStream  *stream,
105                                                         gssize                 count,
106                                                         GCancellable          *cancellable,
107                                                         GError               **error);
108 static void   g_buffered_input_stream_real_fill_async  (GBufferedInputStream  *stream,
109                                                         gssize                 count,
110                                                         int                    io_priority,
111                                                         GCancellable          *cancellable,
112                                                         GAsyncReadyCallback    callback,
113                                                         gpointer               user_data);
114 static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream  *stream,
115                                                         GAsyncResult          *result,
116                                                         GError               **error);
117
118 static void     g_buffered_input_stream_seekable_iface_init (GSeekableIface  *iface);
119 static goffset  g_buffered_input_stream_tell                (GSeekable       *seekable);
120 static gboolean g_buffered_input_stream_can_seek            (GSeekable       *seekable);
121 static gboolean g_buffered_input_stream_seek                (GSeekable       *seekable,
122                                                              goffset          offset,
123                                                              GSeekType        type,
124                                                              GCancellable    *cancellable,
125                                                              GError         **error);
126 static gboolean g_buffered_input_stream_can_truncate        (GSeekable       *seekable);
127 static gboolean g_buffered_input_stream_truncate            (GSeekable       *seekable,
128                                                              goffset          offset,
129                                                              GCancellable    *cancellable,
130                                                              GError         **error);
131
132 static void     g_buffered_input_stream_finalize            (GObject         *object);
133
134 static void compact_buffer (GBufferedInputStream *stream);
135
136 G_DEFINE_TYPE_WITH_CODE (GBufferedInputStream,
137                          g_buffered_input_stream,
138                          G_TYPE_FILTER_INPUT_STREAM,
139                          G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
140                                                 g_buffered_input_stream_seekable_iface_init))
141
142 static void
143 g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
144 {
145   GObjectClass *object_class;
146   GInputStreamClass *istream_class;
147   GBufferedInputStreamClass *bstream_class;
148
149   g_type_class_add_private (klass, sizeof (GBufferedInputStreamPrivate));
150
151   object_class = G_OBJECT_CLASS (klass);
152   object_class->get_property = g_buffered_input_stream_get_property;
153   object_class->set_property = g_buffered_input_stream_set_property;
154   object_class->finalize     = g_buffered_input_stream_finalize;
155
156   istream_class = G_INPUT_STREAM_CLASS (klass);
157   istream_class->skip = g_buffered_input_stream_skip;
158   istream_class->skip_async  = g_buffered_input_stream_skip_async;
159   istream_class->skip_finish = g_buffered_input_stream_skip_finish;
160   istream_class->read_fn = g_buffered_input_stream_read;
161
162   bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
163   bstream_class->fill = g_buffered_input_stream_real_fill;
164   bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
165   bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
166
167   g_object_class_install_property (object_class,
168                                    PROP_BUFSIZE,
169                                    g_param_spec_uint ("buffer-size",
170                                                       P_("Buffer Size"),
171                                                       P_("The size of the backend buffer"),
172                                                       1,
173                                                       G_MAXUINT,
174                                                       DEFAULT_BUFFER_SIZE,
175                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
176                                                       G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
177
178
179 }
180
181 /**
182  * g_buffered_input_stream_get_buffer_size:
183  * @stream: a #GBufferedInputStream
184  *
185  * Gets the size of the input buffer.
186  *
187  * Returns: the current buffer size.
188  */
189 gsize
190 g_buffered_input_stream_get_buffer_size (GBufferedInputStream  *stream)
191 {
192   g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), 0);
193
194   return stream->priv->len;
195 }
196
197 /**
198  * g_buffered_input_stream_set_buffer_size:
199  * @stream: a #GBufferedInputStream
200  * @size: a #gsize
201  *
202  * Sets the size of the internal buffer of @stream to @size, or to the
203  * size of the contents of the buffer. The buffer can never be resized
204  * smaller than its current contents.
205  */
206 void
207 g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
208                                          gsize                 size)
209 {
210   GBufferedInputStreamPrivate *priv;
211   gsize in_buffer;
212   guint8 *buffer;
213
214   g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
215
216   priv = stream->priv;
217
218   if (priv->len == size)
219     return;
220
221   if (priv->buffer)
222     {
223       in_buffer = priv->end - priv->pos;
224
225       /* Never resize smaller than current buffer contents */
226       size = MAX (size, in_buffer);
227
228       buffer = g_malloc (size);
229       memcpy (buffer, priv->buffer + priv->pos, in_buffer);
230       priv->len = size;
231       priv->pos = 0;
232       priv->end = in_buffer;
233       g_free (priv->buffer);
234       priv->buffer = buffer;
235     }
236   else
237     {
238       priv->len = size;
239       priv->pos = 0;
240       priv->end = 0;
241       priv->buffer = g_malloc (size);
242     }
243
244   g_object_notify (G_OBJECT (stream), "buffer-size");
245 }
246
247 static void
248 g_buffered_input_stream_set_property (GObject      *object,
249                                       guint         prop_id,
250                                       const GValue *value,
251                                       GParamSpec   *pspec)
252 {
253   GBufferedInputStream        *bstream;
254
255   bstream = G_BUFFERED_INPUT_STREAM (object);
256
257   switch (prop_id)
258     {
259     case PROP_BUFSIZE:
260       g_buffered_input_stream_set_buffer_size (bstream, g_value_get_uint (value));
261       break;
262
263     default:
264       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265       break;
266     }
267 }
268
269 static void
270 g_buffered_input_stream_get_property (GObject    *object,
271                                       guint       prop_id,
272                                       GValue     *value,
273                                       GParamSpec *pspec)
274 {
275   GBufferedInputStreamPrivate *priv;
276   GBufferedInputStream        *bstream;
277
278   bstream = G_BUFFERED_INPUT_STREAM (object);
279   priv = bstream->priv;
280
281   switch (prop_id)
282     {
283     case PROP_BUFSIZE:
284       g_value_set_uint (value, priv->len);
285       break;
286
287     default:
288       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289       break;
290     }
291 }
292
293 static void
294 g_buffered_input_stream_finalize (GObject *object)
295 {
296   GBufferedInputStreamPrivate *priv;
297   GBufferedInputStream        *stream;
298
299   stream = G_BUFFERED_INPUT_STREAM (object);
300   priv = stream->priv;
301
302   g_free (priv->buffer);
303
304   G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize (object);
305 }
306
307 static void
308 g_buffered_input_stream_seekable_iface_init (GSeekableIface *iface)
309 {
310   iface->tell         = g_buffered_input_stream_tell;
311   iface->can_seek     = g_buffered_input_stream_can_seek;
312   iface->seek         = g_buffered_input_stream_seek;
313   iface->can_truncate = g_buffered_input_stream_can_truncate;
314   iface->truncate_fn  = g_buffered_input_stream_truncate;
315 }
316
317 static void
318 g_buffered_input_stream_init (GBufferedInputStream *stream)
319 {
320   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
321                                               G_TYPE_BUFFERED_INPUT_STREAM,
322                                               GBufferedInputStreamPrivate);
323 }
324
325
326 /**
327  * g_buffered_input_stream_new:
328  * @base_stream: a #GInputStream
329  *
330  * Creates a new #GInputStream from the given @base_stream, with
331  * a buffer set to the default size (4 kilobytes).
332  *
333  * Returns: a #GInputStream for the given @base_stream.
334  */
335 GInputStream *
336 g_buffered_input_stream_new (GInputStream *base_stream)
337 {
338   GInputStream *stream;
339
340   g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
341
342   stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
343                          "base-stream", base_stream,
344                          NULL);
345
346   return stream;
347 }
348
349 /**
350  * g_buffered_input_stream_new_sized:
351  * @base_stream: a #GInputStream
352  * @size: a #gsize
353  *
354  * Creates a new #GBufferedInputStream from the given @base_stream,
355  * with a buffer set to @size.
356  *
357  * Returns: a #GInputStream.
358  */
359 GInputStream *
360 g_buffered_input_stream_new_sized (GInputStream *base_stream,
361                                    gsize         size)
362 {
363   GInputStream *stream;
364
365   g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
366
367   stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
368                          "base-stream", base_stream,
369                          "buffer-size", (guint)size,
370                          NULL);
371
372   return stream;
373 }
374
375 /**
376  * g_buffered_input_stream_fill:
377  * @stream: a #GBufferedInputStream
378  * @count: the number of bytes that will be read from the stream
379  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
380  * @error: location to store the error occurring, or %NULL to ignore
381  *
382  * Tries to read @count bytes from the stream into the buffer.
383  * Will block during this read.
384  *
385  * If @count is zero, returns zero and does nothing. A value of @count
386  * larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
387  *
388  * On success, the number of bytes read into the buffer is returned.
389  * It is not an error if this is not the same as the requested size, as it
390  * can happen e.g. near the end of a file. Zero is returned on end of file
391  * (or if @count is zero),  but never otherwise.
392  *
393  * If @count is -1 then the attempted read size is equal to the number of
394  * bytes that are required to fill the buffer.
395  *
396  * If @cancellable is not %NULL, then the operation can be cancelled by
397  * triggering the cancellable object from another thread. If the operation
398  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
399  * operation was partially finished when the operation was cancelled the
400  * partial result will be returned, without an error.
401  *
402  * On error -1 is returned and @error is set accordingly.
403  *
404  * For the asynchronous, non-blocking, version of this function, see
405  * g_buffered_input_stream_fill_async().
406  *
407  * Returns: the number of bytes read into @stream's buffer, up to @count,
408  *     or -1 on error.
409  */
410 gssize
411 g_buffered_input_stream_fill (GBufferedInputStream  *stream,
412                               gssize                 count,
413                               GCancellable          *cancellable,
414                               GError               **error)
415 {
416   GBufferedInputStreamClass *class;
417   GInputStream *input_stream;
418   gssize res;
419
420   g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
421
422   input_stream = G_INPUT_STREAM (stream);
423
424   if (count < -1)
425     {
426       g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
427                    _("Too large count value passed to %s"), G_STRFUNC);
428       return -1;
429     }
430
431   if (!g_input_stream_set_pending (input_stream, error))
432     return -1;
433
434   if (cancellable)
435     g_cancellable_push_current (cancellable);
436
437   class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
438   res = class->fill (stream, count, cancellable, error);
439
440   if (cancellable)
441     g_cancellable_pop_current (cancellable);
442
443   g_input_stream_clear_pending (input_stream);
444
445   return res;
446 }
447
448 static void
449 async_fill_callback_wrapper (GObject      *source_object,
450                              GAsyncResult *res,
451                              gpointer      user_data)
452 {
453   GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
454
455   g_input_stream_clear_pending (G_INPUT_STREAM (stream));
456   (*stream->priv->outstanding_callback) (source_object, res, user_data);
457   g_object_unref (stream);
458 }
459
460 /**
461  * g_buffered_input_stream_fill_async:
462  * @stream: a #GBufferedInputStream
463  * @count: the number of bytes that will be read from the stream
464  * @io_priority: the <link linkend="io-priority">I/O priority</link>
465  *     of the request
466  * @cancellable: (allow-none): optional #GCancellable object
467  * @callback: (scope async): a #GAsyncReadyCallback
468  * @user_data: (closure): a #gpointer
469  *
470  * Reads data into @stream's buffer asynchronously, up to @count size.
471  * @io_priority can be used to prioritize reads. For the synchronous
472  * version of this function, see g_buffered_input_stream_fill().
473  *
474  * If @count is -1 then the attempted read size is equal to the number
475  * of bytes that are required to fill the buffer.
476  */
477 void
478 g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
479                                     gssize                count,
480                                     int                   io_priority,
481                                     GCancellable         *cancellable,
482                                     GAsyncReadyCallback   callback,
483                                     gpointer              user_data)
484 {
485   GBufferedInputStreamClass *class;
486   GError *error = NULL;
487
488   g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
489
490   if (count == 0)
491     {
492       GTask *task;
493
494       task = g_task_new (stream, cancellable, callback, user_data);
495       g_task_set_source_tag (task, g_buffered_input_stream_fill_async);
496       g_task_return_int (task, 0);
497       g_object_unref (task);
498       return;
499     }
500
501   if (count < -1)
502     {
503       g_task_report_new_error (stream, callback, user_data,
504                                g_buffered_input_stream_fill_async,
505                                G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
506                                _("Too large count value passed to %s"),
507                                G_STRFUNC);
508       return;
509     }
510
511   if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
512     {
513       g_task_report_error (stream, callback, user_data,
514                            g_buffered_input_stream_fill_async,
515                            error);
516       return;
517     }
518
519   class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
520
521   stream->priv->outstanding_callback = callback;
522   g_object_ref (stream);
523   class->fill_async (stream, count, io_priority, cancellable,
524                      async_fill_callback_wrapper, user_data);
525 }
526
527 /**
528  * g_buffered_input_stream_fill_finish:
529  * @stream: a #GBufferedInputStream
530  * @result: a #GAsyncResult
531  * @error: a #GError
532  *
533  * Finishes an asynchronous read.
534  *
535  * Returns: a #gssize of the read stream, or %-1 on an error.
536  */
537 gssize
538 g_buffered_input_stream_fill_finish (GBufferedInputStream  *stream,
539                                      GAsyncResult          *result,
540                                      GError               **error)
541 {
542   GBufferedInputStreamClass *class;
543
544   g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
545   g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
546
547   if (g_async_result_legacy_propagate_error (result, error))
548     return -1;
549   else if (g_async_result_is_tagged (result, g_buffered_input_stream_fill_async))
550     return g_task_propagate_int (G_TASK (result), error);
551
552   class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
553   return class->fill_finish (stream, result, error);
554 }
555
556 /**
557  * g_buffered_input_stream_get_available:
558  * @stream: #GBufferedInputStream
559  *
560  * Gets the size of the available data within the stream.
561  *
562  * Returns: size of the available stream.
563  */
564 gsize
565 g_buffered_input_stream_get_available (GBufferedInputStream *stream)
566 {
567   g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
568
569   return stream->priv->end - stream->priv->pos;
570 }
571
572 /**
573  * g_buffered_input_stream_peek:
574  * @stream: a #GBufferedInputStream
575  * @buffer: (array length=count) (element-type guint8): a pointer to
576  *   an allocated chunk of memory
577  * @offset: a #gsize
578  * @count: a #gsize
579  *
580  * Peeks in the buffer, copying data of size @count into @buffer,
581  * offset @offset bytes.
582  *
583  * Returns: a #gsize of the number of bytes peeked, or -1 on error.
584  */
585 gsize
586 g_buffered_input_stream_peek (GBufferedInputStream *stream,
587                               void                 *buffer,
588                               gsize                 offset,
589                               gsize                 count)
590 {
591   gsize available;
592   gsize end;
593
594   g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
595   g_return_val_if_fail (buffer != NULL, -1);
596
597   available = g_buffered_input_stream_get_available (stream);
598
599   if (offset > available)
600     return 0;
601
602   end = MIN (offset + count, available);
603   count = end - offset;
604
605   memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
606   return count;
607 }
608
609 /**
610  * g_buffered_input_stream_peek_buffer:
611  * @stream: a #GBufferedInputStream
612  * @count: (out): a #gsize to get the number of bytes available in the buffer
613  *
614  * Returns the buffer with the currently available bytes. The returned
615  * buffer must not be modified and will become invalid when reading from
616  * the stream or filling the buffer.
617  *
618  * Returns: (array length=count) (element-type guint8) (transfer none):
619  *          read-only buffer
620  */
621 const void*
622 g_buffered_input_stream_peek_buffer (GBufferedInputStream *stream,
623                                      gsize                *count)
624 {
625   GBufferedInputStreamPrivate *priv;
626
627   g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), NULL);
628
629   priv = stream->priv;
630
631   if (count)
632     *count = priv->end - priv->pos;
633
634   return priv->buffer + priv->pos;
635 }
636
637 static void
638 compact_buffer (GBufferedInputStream *stream)
639 {
640   GBufferedInputStreamPrivate *priv;
641   gsize current_size;
642
643   priv = stream->priv;
644
645   current_size = priv->end - priv->pos;
646
647   g_memmove (priv->buffer, priv->buffer + priv->pos, current_size);
648
649   priv->pos = 0;
650   priv->end = current_size;
651 }
652
653 static gssize
654 g_buffered_input_stream_real_fill (GBufferedInputStream  *stream,
655                                    gssize                 count,
656                                    GCancellable          *cancellable,
657                                    GError               **error)
658 {
659   GBufferedInputStreamPrivate *priv;
660   GInputStream *base_stream;
661   gssize nread;
662   gsize in_buffer;
663
664   priv = stream->priv;
665
666   if (count == -1)
667     count = priv->len;
668
669   in_buffer = priv->end - priv->pos;
670
671   /* Never fill more than can fit in the buffer */
672   count = MIN (count, priv->len - in_buffer);
673
674   /* If requested length does not fit at end, compact */
675   if (priv->len - priv->end < count)
676     compact_buffer (stream);
677
678   base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
679   nread = g_input_stream_read (base_stream,
680                                priv->buffer + priv->end,
681                                count,
682                                cancellable,
683                                error);
684
685   if (nread > 0)
686     priv->end += nread;
687
688   return nread;
689 }
690
691 static gssize
692 g_buffered_input_stream_skip (GInputStream  *stream,
693                               gsize          count,
694                               GCancellable  *cancellable,
695                               GError       **error)
696 {
697   GBufferedInputStream        *bstream;
698   GBufferedInputStreamPrivate *priv;
699   GBufferedInputStreamClass *class;
700   GInputStream *base_stream;
701   gsize available, bytes_skipped;
702   gssize nread;
703
704   bstream = G_BUFFERED_INPUT_STREAM (stream);
705   priv = bstream->priv;
706
707   available = priv->end - priv->pos;
708
709   if (count <= available)
710     {
711       priv->pos += count;
712       return count;
713     }
714
715   /* Full request not available, skip all currently available and
716    * request refill for more
717    */
718
719   priv->pos = 0;
720   priv->end = 0;
721   bytes_skipped = available;
722   count -= available;
723
724   if (bytes_skipped > 0)
725     error = NULL; /* Ignore further errors if we already read some data */
726
727   if (count > priv->len)
728     {
729       /* Large request, shortcut buffer */
730
731       base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
732
733       nread = g_input_stream_skip (base_stream,
734                                    count,
735                                    cancellable,
736                                    error);
737
738       if (nread < 0 && bytes_skipped == 0)
739         return -1;
740
741       if (nread > 0)
742         bytes_skipped += nread;
743
744       return bytes_skipped;
745     }
746
747   class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
748   nread = class->fill (bstream, priv->len, cancellable, error);
749
750   if (nread < 0)
751     {
752       if (bytes_skipped == 0)
753         return -1;
754       else
755         return bytes_skipped;
756     }
757
758   available = priv->end - priv->pos;
759   count = MIN (count, available);
760
761   bytes_skipped += count;
762   priv->pos += count;
763
764   return bytes_skipped;
765 }
766
767 static gssize
768 g_buffered_input_stream_read (GInputStream *stream,
769                               void         *buffer,
770                               gsize         count,
771                               GCancellable *cancellable,
772                               GError      **error)
773 {
774   GBufferedInputStream        *bstream;
775   GBufferedInputStreamPrivate *priv;
776   GBufferedInputStreamClass *class;
777   GInputStream *base_stream;
778   gsize available, bytes_read;
779   gssize nread;
780
781   bstream = G_BUFFERED_INPUT_STREAM (stream);
782   priv = bstream->priv;
783
784   available = priv->end - priv->pos;
785
786   if (count <= available)
787     {
788       memcpy (buffer, priv->buffer + priv->pos, count);
789       priv->pos += count;
790       return count;
791     }
792
793   /* Full request not available, read all currently available and
794    * request refill for more
795    */
796
797   memcpy (buffer, priv->buffer + priv->pos, available);
798   priv->pos = 0;
799   priv->end = 0;
800   bytes_read = available;
801   count -= available;
802
803   if (bytes_read > 0)
804     error = NULL; /* Ignore further errors if we already read some data */
805
806   if (count > priv->len)
807     {
808       /* Large request, shortcut buffer */
809
810       base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
811
812       nread = g_input_stream_read (base_stream,
813                                    (char *)buffer + bytes_read,
814                                    count,
815                                    cancellable,
816                                    error);
817
818       if (nread < 0 && bytes_read == 0)
819         return -1;
820
821       if (nread > 0)
822         bytes_read += nread;
823
824       return bytes_read;
825     }
826
827   class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
828   nread = class->fill (bstream, priv->len, cancellable, error);
829   if (nread < 0)
830     {
831       if (bytes_read == 0)
832         return -1;
833       else
834         return bytes_read;
835     }
836
837   available = priv->end - priv->pos;
838   count = MIN (count, available);
839
840   memcpy ((char *)buffer + bytes_read, (char *)priv->buffer + priv->pos, count);
841   bytes_read += count;
842   priv->pos += count;
843
844   return bytes_read;
845 }
846
847 static goffset
848 g_buffered_input_stream_tell (GSeekable *seekable)
849 {
850   GBufferedInputStream        *bstream;
851   GBufferedInputStreamPrivate *priv;
852   GInputStream *base_stream;
853   GSeekable    *base_stream_seekable;
854   gsize available;
855   goffset base_offset;
856   
857   bstream = G_BUFFERED_INPUT_STREAM (seekable);
858   priv = bstream->priv;
859
860   base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
861   if (!G_IS_SEEKABLE (base_stream))
862     return 0;
863   base_stream_seekable = G_SEEKABLE (base_stream);
864   
865   available = priv->end - priv->pos;
866   base_offset = g_seekable_tell (base_stream_seekable);
867
868   return base_offset - available;
869 }
870
871 static gboolean
872 g_buffered_input_stream_can_seek (GSeekable *seekable)
873 {
874   GInputStream *base_stream;
875   
876   base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
877   return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
878 }
879
880 static gboolean
881 g_buffered_input_stream_seek (GSeekable     *seekable,
882                               goffset        offset,
883                               GSeekType      type,
884                               GCancellable  *cancellable,
885                               GError       **error)
886 {
887   GBufferedInputStream        *bstream;
888   GBufferedInputStreamPrivate *priv;
889   GInputStream *base_stream;
890   GSeekable *base_stream_seekable;
891
892   bstream = G_BUFFERED_INPUT_STREAM (seekable);
893   priv = bstream->priv;
894
895   base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
896   if (!G_IS_SEEKABLE (base_stream))
897     {
898       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
899                            _("Seek not supported on base stream"));
900       return FALSE;
901     }
902
903   base_stream_seekable = G_SEEKABLE (base_stream);
904   
905   if (type == G_SEEK_CUR)
906     {
907       if (offset <= priv->end - priv->pos && offset >= -priv->pos)
908         {
909           priv->pos += offset;
910           return TRUE;
911         }
912       else
913         {
914           offset -= priv->end - priv->pos;
915         }
916     }
917
918   if (g_seekable_seek (base_stream_seekable, offset, type, cancellable, error))
919     {
920       priv->pos = 0;
921       priv->end = 0;
922       return TRUE;
923     }
924   else
925     {
926       return FALSE;
927     }
928 }
929
930 static gboolean
931 g_buffered_input_stream_can_truncate (GSeekable *seekable)
932 {
933   return FALSE;
934 }
935
936 static gboolean
937 g_buffered_input_stream_truncate (GSeekable     *seekable,
938                                   goffset        offset,
939                                   GCancellable  *cancellable,
940                                   GError       **error)
941 {
942   g_set_error_literal (error,
943                        G_IO_ERROR,
944                        G_IO_ERROR_NOT_SUPPORTED,
945                        _("Cannot truncate GBufferedInputStream"));
946   return FALSE;
947 }
948
949 /**
950  * g_buffered_input_stream_read_byte:
951  * @stream: a #GBufferedInputStream
952  * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
953  * @error: location to store the error occurring, or %NULL to ignore
954  *
955  * Tries to read a single byte from the stream or the buffer. Will block
956  * during this read.
957  *
958  * On success, the byte read from the stream is returned. On end of stream
959  * -1 is returned but it's not an exceptional error and @error is not set.
960  *
961  * If @cancellable is not %NULL, then the operation can be cancelled by
962  * triggering the cancellable object from another thread. If the operation
963  * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
964  * operation was partially finished when the operation was cancelled the
965  * partial result will be returned, without an error.
966  *
967  * On error -1 is returned and @error is set accordingly.
968  *
969  * Returns: the byte read from the @stream, or -1 on end of stream or error.
970  */
971 int
972 g_buffered_input_stream_read_byte (GBufferedInputStream  *stream,
973                                    GCancellable          *cancellable,
974                                    GError               **error)
975 {
976   GBufferedInputStreamPrivate *priv;
977   GBufferedInputStreamClass *class;
978   GInputStream *input_stream;
979   gsize available;
980   gssize nread;
981
982   g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
983
984   priv = stream->priv;
985   input_stream = G_INPUT_STREAM (stream);
986
987   if (g_input_stream_is_closed (input_stream))
988     {
989       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
990                            _("Stream is already closed"));
991       return -1;
992     }
993
994   if (!g_input_stream_set_pending (input_stream, error))
995     return -1;
996
997   available = priv->end - priv->pos;
998
999   if (available != 0)
1000     {
1001       g_input_stream_clear_pending (input_stream);
1002       return priv->buffer[priv->pos++];
1003     }
1004
1005   /* Byte not available, request refill for more */
1006
1007   if (cancellable)
1008     g_cancellable_push_current (cancellable);
1009
1010   priv->pos = 0;
1011   priv->end = 0;
1012
1013   class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1014   nread = class->fill (stream, priv->len, cancellable, error);
1015
1016   if (cancellable)
1017     g_cancellable_pop_current (cancellable);
1018
1019   g_input_stream_clear_pending (input_stream);
1020
1021   if (nread <= 0)
1022     return -1; /* error or end of stream */
1023
1024   return priv->buffer[priv->pos++];
1025 }
1026
1027 /* ************************** */
1028 /* Async stuff implementation */
1029 /* ************************** */
1030
1031 static void
1032 fill_async_callback (GObject      *source_object,
1033                      GAsyncResult *result,
1034                      gpointer      user_data)
1035 {
1036   GError *error;
1037   gssize res;
1038   GTask *task = user_data;
1039
1040   error = NULL;
1041   res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
1042                                     result, &error);
1043   if (res == -1)
1044     g_task_return_error (task, error);
1045   else
1046     {
1047       GBufferedInputStream *stream;
1048       GBufferedInputStreamPrivate *priv;
1049
1050       stream = g_task_get_source_object (task);
1051       priv = G_BUFFERED_INPUT_STREAM (stream)->priv;
1052
1053       g_assert_cmpint (priv->end + res, <=, priv->len);
1054       priv->end += res;
1055
1056       g_task_return_int (task, res);
1057     }
1058
1059   g_object_unref (task);
1060 }
1061
1062 static void
1063 g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
1064                                          gssize                count,
1065                                          int                   io_priority,
1066                                          GCancellable         *cancellable,
1067                                          GAsyncReadyCallback   callback,
1068                                          gpointer              user_data)
1069 {
1070   GBufferedInputStreamPrivate *priv;
1071   GInputStream *base_stream;
1072   GTask *task;
1073   gsize in_buffer;
1074
1075   priv = stream->priv;
1076
1077   if (count == -1)
1078     count = priv->len;
1079
1080   in_buffer = priv->end - priv->pos;
1081
1082   /* Never fill more than can fit in the buffer */
1083   count = MIN (count, priv->len - in_buffer);
1084
1085   /* If requested length does not fit at end, compact */
1086   if (priv->len - priv->end < count)
1087     compact_buffer (stream);
1088
1089   task = g_task_new (stream, cancellable, callback, user_data);
1090
1091   base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1092   g_input_stream_read_async (base_stream,
1093                              priv->buffer + priv->end,
1094                              count,
1095                              io_priority,
1096                              cancellable,
1097                              fill_async_callback,
1098                              task);
1099 }
1100
1101 static gssize
1102 g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
1103                                           GAsyncResult         *result,
1104                                           GError              **error)
1105 {
1106   g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1107
1108   return g_task_propagate_int (G_TASK (result), error);
1109 }
1110
1111 typedef struct
1112 {
1113   gssize bytes_skipped;
1114   gssize count;
1115 } SkipAsyncData;
1116
1117 static void
1118 free_skip_async_data (gpointer _data)
1119 {
1120   SkipAsyncData *data = _data;
1121   g_slice_free (SkipAsyncData, data);
1122 }
1123
1124 static void
1125 large_skip_callback (GObject      *source_object,
1126                      GAsyncResult *result,
1127                      gpointer      user_data)
1128 {
1129   GTask *task = G_TASK (user_data);
1130   SkipAsyncData *data;
1131   GError *error;
1132   gssize nread;
1133
1134   data = g_task_get_task_data (task);
1135
1136   error = NULL;
1137   nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
1138                                       result, &error);
1139
1140   /* Only report the error if we've not already read some data */
1141   if (nread < 0 && data->bytes_skipped == 0)
1142     g_task_return_error (task, error);
1143   else
1144     {
1145       if (error)
1146         g_error_free (error);
1147
1148       if (nread > 0)
1149         data->bytes_skipped += nread;
1150
1151       g_task_return_int (task, data->bytes_skipped);
1152     }
1153
1154   g_object_unref (task);
1155 }
1156
1157 static void
1158 skip_fill_buffer_callback (GObject      *source_object,
1159                            GAsyncResult *result,
1160                            gpointer      user_data)
1161 {
1162   GTask *task = G_TASK (user_data);
1163   GBufferedInputStream *bstream;
1164   GBufferedInputStreamPrivate *priv;
1165   SkipAsyncData *data;
1166   GError *error;
1167   gssize nread;
1168   gsize available;
1169
1170   bstream = G_BUFFERED_INPUT_STREAM (source_object);
1171   priv = bstream->priv;
1172
1173   data = g_task_get_task_data (task);
1174
1175   error = NULL;
1176   nread = g_buffered_input_stream_fill_finish (bstream,
1177                                                result, &error);
1178
1179   if (nread < 0 && data->bytes_skipped == 0)
1180     g_task_return_error (task, error);
1181   else
1182     {
1183       if (error)
1184         g_error_free (error);
1185
1186       if (nread > 0)
1187         {
1188           available = priv->end - priv->pos;
1189           data->count = MIN (data->count, available);
1190
1191           data->bytes_skipped += data->count;
1192           priv->pos += data->count;
1193         }
1194
1195       g_task_return_int (task, data->bytes_skipped);
1196     }
1197
1198   g_object_unref (task);
1199 }
1200
1201 static void
1202 g_buffered_input_stream_skip_async (GInputStream        *stream,
1203                                     gsize                count,
1204                                     int                  io_priority,
1205                                     GCancellable        *cancellable,
1206                                     GAsyncReadyCallback  callback,
1207                                     gpointer             user_data)
1208 {
1209   GBufferedInputStream *bstream;
1210   GBufferedInputStreamPrivate *priv;
1211   GBufferedInputStreamClass *class;
1212   GInputStream *base_stream;
1213   gsize available;
1214   GTask *task;
1215   SkipAsyncData *data;
1216
1217   bstream = G_BUFFERED_INPUT_STREAM (stream);
1218   priv = bstream->priv;
1219
1220   data = g_slice_new (SkipAsyncData);
1221   data->bytes_skipped = 0;
1222   task = g_task_new (stream, cancellable, callback, user_data);
1223   g_task_set_task_data (task, data, free_skip_async_data);
1224
1225   available = priv->end - priv->pos;
1226
1227   if (count <= available)
1228     {
1229       priv->pos += count;
1230
1231       g_task_return_int (task, count);
1232       g_object_unref (task);
1233       return;
1234     }
1235
1236   /* Full request not available, skip all currently available
1237    * and request refill for more
1238    */
1239
1240   priv->pos = 0;
1241   priv->end = 0;
1242
1243   count -= available;
1244
1245   data->bytes_skipped = available;
1246   data->count = count;
1247
1248   if (count > priv->len)
1249     {
1250       /* Large request, shortcut buffer */
1251
1252       base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
1253
1254       g_input_stream_skip_async (base_stream,
1255                                  count,
1256                                  io_priority, cancellable,
1257                                  large_skip_callback,
1258                                  task);
1259     }
1260   else
1261     {
1262       class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
1263       class->fill_async (bstream, priv->len, io_priority, cancellable,
1264                          skip_fill_buffer_callback, task);
1265     }
1266 }
1267
1268 static gssize
1269 g_buffered_input_stream_skip_finish (GInputStream   *stream,
1270                                      GAsyncResult   *result,
1271                                      GError        **error)
1272 {
1273   g_return_val_if_fail (g_task_is_valid (result, stream), -1);
1274
1275   return g_task_propagate_int (G_TASK (result), error);
1276 }