1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
27 #include "gconverterinputstream.h"
28 #include "gsimpleasyncresult.h"
29 #include "gcancellable.h"
30 #include "gioenumtypes.h"
37 * SECTION:gconverterinputstream
38 * @short_description: Converter Input Stream
40 * @see_also: #GInputStream, #GConverter
42 * Converter input stream implements #GInputStream and allows
43 * conversion of data of various types during reading.
47 #define INITIAL_BUFFER_SIZE 4096
56 struct _GConverterInputStreamPrivate {
57 gboolean at_input_end;
59 GConverter *converter;
61 Buffer converted_buffer;
69 static void g_converter_input_stream_set_property (GObject *object,
73 static void g_converter_input_stream_get_property (GObject *object,
77 static void g_converter_input_stream_finalize (GObject *object);
78 static gssize g_converter_input_stream_read (GInputStream *stream,
81 GCancellable *cancellable,
84 G_DEFINE_TYPE (GConverterInputStream,
85 g_converter_input_stream,
86 G_TYPE_FILTER_INPUT_STREAM)
89 g_converter_input_stream_class_init (GConverterInputStreamClass *klass)
91 GObjectClass *object_class;
92 GInputStreamClass *istream_class;
94 g_type_class_add_private (klass, sizeof (GConverterInputStreamPrivate));
96 object_class = G_OBJECT_CLASS (klass);
97 object_class->get_property = g_converter_input_stream_get_property;
98 object_class->set_property = g_converter_input_stream_set_property;
99 object_class->finalize = g_converter_input_stream_finalize;
101 istream_class = G_INPUT_STREAM_CLASS (klass);
102 istream_class->read_fn = g_converter_input_stream_read;
104 g_object_class_install_property (object_class,
106 g_param_spec_object ("converter",
108 P_("The converter object"),
111 G_PARAM_CONSTRUCT_ONLY|
112 G_PARAM_STATIC_STRINGS));
117 g_converter_input_stream_finalize (GObject *object)
119 GConverterInputStreamPrivate *priv;
120 GConverterInputStream *stream;
122 stream = G_CONVERTER_INPUT_STREAM (object);
125 g_free (priv->input_buffer.data);
126 g_free (priv->converted_buffer.data);
128 g_object_unref (priv->converter);
130 G_OBJECT_CLASS (g_converter_input_stream_parent_class)->finalize (object);
134 g_converter_input_stream_set_property (GObject *object,
139 GConverterInputStream *cstream;
141 cstream = G_CONVERTER_INPUT_STREAM (object);
146 cstream->priv->converter = g_value_dup_object (value);
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
157 g_converter_input_stream_get_property (GObject *object,
162 GConverterInputStreamPrivate *priv;
163 GConverterInputStream *cstream;
165 cstream = G_CONVERTER_INPUT_STREAM (object);
166 priv = cstream->priv;
171 g_value_set_object (value, priv->converter);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
181 g_converter_input_stream_init (GConverterInputStream *stream)
183 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
184 G_TYPE_CONVERTER_INPUT_STREAM,
185 GConverterInputStreamPrivate);
189 * g_converter_input_stream_new:
190 * @base_stream: a #GInputStream
191 * @converter: a #GConverter
193 * Creates a new converter input stream for the @base_stream.
195 * Returns: a new #GInputStream.
198 g_converter_input_stream_new (GInputStream *base_stream,
199 GConverter *converter)
201 GInputStream *stream;
203 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
205 stream = g_object_new (G_TYPE_CONVERTER_INPUT_STREAM,
206 "base-stream", base_stream,
207 "converter", converter,
214 buffer_available (Buffer *buffer)
216 return buffer->end - buffer->start;
220 buffer_tailspace (Buffer *buffer)
222 return buffer->size - buffer->end;
226 buffer_data (Buffer *buffer)
228 return buffer->data + buffer->start;
232 buffer_consumed (Buffer *buffer,
235 buffer->start += count;
236 if (buffer->start == buffer->end)
237 buffer->start = buffer->end = 0;
241 buffer_read (Buffer *buffer,
245 memcpy (dest, buffer->data + buffer->start, count);
246 buffer_consumed (buffer, count);
250 compact_buffer (Buffer *buffer)
254 in_buffer = buffer_available (buffer);
255 memmove (buffer->data,
256 buffer->data + buffer->start,
258 buffer->end -= buffer->start;
263 grow_buffer (Buffer *buffer)
266 gsize size, in_buffer;
268 if (buffer->size == 0)
269 size = INITIAL_BUFFER_SIZE;
271 size = buffer->size * 2;
273 data = g_malloc (size);
274 in_buffer = buffer_available (buffer);
277 buffer->data + buffer->start,
279 g_free (buffer->data);
281 buffer->end -= buffer->start;
287 buffer_ensure_space (Buffer *buffer,
290 gsize in_buffer, left_to_fill;
292 in_buffer = buffer_available (buffer);
294 if (in_buffer >= at_least_size)
297 left_to_fill = buffer_tailspace (buffer);
299 if (in_buffer + left_to_fill >= at_least_size)
301 /* We fit in remaining space at end */
302 /* If the copy is small, compact now anyway so we can fill more */
304 compact_buffer (buffer);
306 else if (buffer->size >= at_least_size)
308 /* We fit, but only if we compact */
309 compact_buffer (buffer);
313 /* Need to grow buffer */
314 while (buffer->size < at_least_size)
315 grow_buffer (buffer);
320 fill_input_buffer (GConverterInputStream *stream,
322 GCancellable *cancellable,
325 GConverterInputStreamPrivate *priv;
326 GInputStream *base_stream;
331 buffer_ensure_space (&priv->input_buffer, at_least_size);
333 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
334 nread = g_input_stream_read (base_stream,
335 priv->input_buffer.data + priv->input_buffer.end,
336 buffer_tailspace (&priv->input_buffer),
341 priv->input_buffer.end += nread;
348 g_converter_input_stream_read (GInputStream *stream,
351 GCancellable *cancellable,
354 GConverterInputStream *cstream;
355 GConverterInputStreamPrivate *priv;
356 gsize available, total_bytes_read;
358 GConverterResult res;
364 cstream = G_CONVERTER_INPUT_STREAM (stream);
365 priv = cstream->priv;
367 available = buffer_available (&priv->converted_buffer);
372 /* Converted data available, return that */
373 buffer_read (&priv->converted_buffer, buffer, count);
377 /* Full request not available, read all currently available and request
378 refill/conversion for more */
380 buffer_read (&priv->converted_buffer, buffer, available);
382 total_bytes_read = available;
385 /* If there is no data to convert, and no pre-converted data,
386 do some i/o for more input */
387 if (buffer_available (&priv->input_buffer) == 0 &&
388 total_bytes_read == 0 &&
391 nread = fill_input_buffer (cstream, count, cancellable, error);
395 priv->at_input_end = TRUE;
398 /* First try to convert any available data (or state) directly to the user buffer: */
402 res = g_converter_convert (priv->converter,
403 buffer_data (&priv->input_buffer),
404 buffer_available (&priv->input_buffer),
406 priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
410 if (res != G_CONVERTER_ERROR)
412 total_bytes_read += bytes_written;
413 buffer_consumed (&priv->input_buffer, bytes_read);
414 if (res == G_CONVERTER_FINISHED)
415 priv->finished = TRUE; /* We're done converting */
417 else if (total_bytes_read == 0 &&
418 !g_error_matches (my_error,
420 G_IO_ERROR_PARTIAL_INPUT) &&
421 !g_error_matches (my_error,
423 G_IO_ERROR_NO_SPACE))
425 /* No previously read data and no "special" error, return error */
426 g_propagate_error (error, my_error);
430 g_error_free (my_error);
433 /* We had some pre-converted data and/or we converted directly to the
435 if (total_bytes_read > 0)
436 return total_bytes_read;
438 /* If there is no more to convert, return EOF */
441 g_assert (buffer_available (&priv->converted_buffer) == 0);
445 /* There was "complexity" in the straight-to-buffer conversion,
446 * convert to our own buffer and write from that.
447 * At this point we didn't produce any data into @buffer.
450 /* Ensure we have *some* initial target space */
451 buffer_ensure_space (&priv->converted_buffer, count);
455 g_assert (!priv->finished);
457 /* Try to convert to our buffer */
459 res = g_converter_convert (priv->converter,
460 buffer_data (&priv->input_buffer),
461 buffer_available (&priv->input_buffer),
462 buffer_data (&priv->converted_buffer),
463 buffer_tailspace (&priv->converted_buffer),
464 priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
468 if (res != G_CONVERTER_ERROR)
470 priv->converted_buffer.end += bytes_written;
471 buffer_consumed (&priv->input_buffer, bytes_read);
473 /* Maybe we consumed without producing any output */
474 if (buffer_available (&priv->converted_buffer) == 0 && res != G_CONVERTER_FINISHED)
475 continue; /* Convert more */
477 if (res == G_CONVERTER_FINISHED)
478 priv->finished = TRUE;
480 total_bytes_read = MIN (count, buffer_available (&priv->converted_buffer));
481 buffer_read (&priv->converted_buffer, buffer, total_bytes_read);
483 g_assert (priv->finished || total_bytes_read > 0);
485 return total_bytes_read;
488 /* There was some kind of error filling our buffer */
490 if (g_error_matches (my_error,
492 G_IO_ERROR_PARTIAL_INPUT) &&
497 res = fill_input_buffer (cstream,
498 buffer_available (&priv->input_buffer) + 4096,
503 /* Can't read any more data, return that error */
504 g_error_free (my_error);
505 g_propagate_error (error, my_error2);
510 /* End of file, try INPUT_AT_END */
511 priv->at_input_end = TRUE;
513 g_error_free (my_error);
517 if (g_error_matches (my_error,
519 G_IO_ERROR_NO_SPACE))
521 /* Need more destination space, grow it
522 * Note: if we actually grow the buffer (as opposed to compacting it),
523 * this will double the size, not just add one byte. */
524 buffer_ensure_space (&priv->converted_buffer,
525 priv->converted_buffer.size + 1);
526 g_error_free (my_error);
530 /* Any other random error, return it */
531 g_propagate_error (error, my_error);
535 g_assert_not_reached ();
539 * g_converter_input_stream_get_converter:
540 * @converter_stream: a #GConverterInputStream
542 * Gets the #GConverter that is used by @converter_stream.
544 * Returns: the converter of the converter input stream
549 g_converter_input_stream_get_converter (GConverterInputStream *converter_stream)
551 return converter_stream->priv->converter;
554 #define __G_CONVERTER_INPUT_STREAM_C__
555 #include "gioaliasdef.c"