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"
36 * SECTION:gconverterinputstream
37 * @short_description: Converter Input Stream
39 * @see_also: #GInputStream, #GConverter
41 * Converter input stream implements #GInputStream and allows
42 * conversion of data of various types during reading.
46 #define INITIAL_BUFFER_SIZE 4096
55 struct _GConverterInputStreamPrivate {
56 gboolean at_input_end;
58 GConverter *converter;
60 Buffer converted_buffer;
68 static void g_converter_input_stream_set_property (GObject *object,
72 static void g_converter_input_stream_get_property (GObject *object,
76 static void g_converter_input_stream_finalize (GObject *object);
77 static gssize g_converter_input_stream_read (GInputStream *stream,
80 GCancellable *cancellable,
83 G_DEFINE_TYPE (GConverterInputStream,
84 g_converter_input_stream,
85 G_TYPE_FILTER_INPUT_STREAM)
88 g_converter_input_stream_class_init (GConverterInputStreamClass *klass)
90 GObjectClass *object_class;
91 GInputStreamClass *istream_class;
93 g_type_class_add_private (klass, sizeof (GConverterInputStreamPrivate));
95 object_class = G_OBJECT_CLASS (klass);
96 object_class->get_property = g_converter_input_stream_get_property;
97 object_class->set_property = g_converter_input_stream_set_property;
98 object_class->finalize = g_converter_input_stream_finalize;
100 istream_class = G_INPUT_STREAM_CLASS (klass);
101 istream_class->read_fn = g_converter_input_stream_read;
103 g_object_class_install_property (object_class,
105 g_param_spec_object ("converter",
107 P_("The converter object"),
110 G_PARAM_CONSTRUCT_ONLY|
111 G_PARAM_STATIC_STRINGS));
116 g_converter_input_stream_finalize (GObject *object)
118 GConverterInputStreamPrivate *priv;
119 GConverterInputStream *stream;
121 stream = G_CONVERTER_INPUT_STREAM (object);
124 g_free (priv->input_buffer.data);
125 g_free (priv->converted_buffer.data);
127 g_object_unref (priv->converter);
129 G_OBJECT_CLASS (g_converter_input_stream_parent_class)->finalize (object);
133 g_converter_input_stream_set_property (GObject *object,
138 GConverterInputStream *cstream;
140 cstream = G_CONVERTER_INPUT_STREAM (object);
145 cstream->priv->converter = g_value_dup_object (value);
149 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156 g_converter_input_stream_get_property (GObject *object,
161 GConverterInputStreamPrivate *priv;
162 GConverterInputStream *cstream;
164 cstream = G_CONVERTER_INPUT_STREAM (object);
165 priv = cstream->priv;
170 g_value_set_object (value, priv->converter);
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180 g_converter_input_stream_init (GConverterInputStream *stream)
182 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
183 G_TYPE_CONVERTER_INPUT_STREAM,
184 GConverterInputStreamPrivate);
188 * g_converter_input_stream_new:
189 * @base_stream: a #GInputStream
190 * @converter: a #GConverter
192 * Creates a new converter input stream for the @base_stream.
194 * Returns: a new #GInputStream.
197 g_converter_input_stream_new (GInputStream *base_stream,
198 GConverter *converter)
200 GInputStream *stream;
202 g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
204 stream = g_object_new (G_TYPE_CONVERTER_INPUT_STREAM,
205 "base-stream", base_stream,
206 "converter", converter,
213 buffer_data_size (Buffer *buffer)
215 return buffer->end - buffer->start;
219 buffer_tailspace (Buffer *buffer)
221 return buffer->size - buffer->end;
225 buffer_data (Buffer *buffer)
227 return buffer->data + buffer->start;
231 buffer_consumed (Buffer *buffer,
234 buffer->start += count;
235 if (buffer->start == buffer->end)
236 buffer->start = buffer->end = 0;
240 buffer_read (Buffer *buffer,
244 memcpy (dest, buffer->data + buffer->start, count);
245 buffer_consumed (buffer, count);
249 compact_buffer (Buffer *buffer)
253 in_buffer = buffer_data_size (buffer);
254 memmove (buffer->data,
255 buffer->data + buffer->start,
257 buffer->end -= buffer->start;
262 grow_buffer (Buffer *buffer)
265 gsize size, in_buffer;
267 if (buffer->size == 0)
268 size = INITIAL_BUFFER_SIZE;
270 size = buffer->size * 2;
272 data = g_malloc (size);
273 in_buffer = buffer_data_size (buffer);
276 buffer->data + buffer->start,
278 g_free (buffer->data);
280 buffer->end -= buffer->start;
285 /* Ensures that the buffer can fit at_least_size bytes,
286 * *including* the current in-buffer data */
288 buffer_ensure_space (Buffer *buffer,
291 gsize in_buffer, left_to_fill;
293 in_buffer = buffer_data_size (buffer);
295 if (in_buffer >= at_least_size)
298 left_to_fill = buffer_tailspace (buffer);
300 if (in_buffer + left_to_fill >= at_least_size)
302 /* We fit in remaining space at end */
303 /* If the copy is small, compact now anyway so we can fill more */
305 compact_buffer (buffer);
307 else if (buffer->size >= at_least_size)
309 /* We fit, but only if we compact */
310 compact_buffer (buffer);
314 /* Need to grow buffer */
315 while (buffer->size < at_least_size)
316 grow_buffer (buffer);
321 fill_input_buffer (GConverterInputStream *stream,
323 GCancellable *cancellable,
326 GConverterInputStreamPrivate *priv;
327 GInputStream *base_stream;
332 buffer_ensure_space (&priv->input_buffer, at_least_size);
334 base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
335 nread = g_input_stream_read (base_stream,
336 priv->input_buffer.data + priv->input_buffer.end,
337 buffer_tailspace (&priv->input_buffer),
342 priv->input_buffer.end += nread;
349 g_converter_input_stream_read (GInputStream *stream,
352 GCancellable *cancellable,
355 GConverterInputStream *cstream;
356 GConverterInputStreamPrivate *priv;
357 gsize available, total_bytes_read;
359 GConverterResult res;
365 cstream = G_CONVERTER_INPUT_STREAM (stream);
366 priv = cstream->priv;
368 available = buffer_data_size (&priv->converted_buffer);
373 /* Converted data available, return that */
374 buffer_read (&priv->converted_buffer, buffer, count);
378 /* Full request not available, read all currently available and request
379 refill/conversion for more */
381 buffer_read (&priv->converted_buffer, buffer, available);
383 total_bytes_read = available;
386 /* If there is no data to convert, and no pre-converted data,
387 do some i/o for more input */
388 if (buffer_data_size (&priv->input_buffer) == 0 &&
389 total_bytes_read == 0 &&
392 nread = fill_input_buffer (cstream, count, cancellable, error);
396 priv->at_input_end = TRUE;
399 /* First try to convert any available data (or state) directly to the user buffer: */
403 res = g_converter_convert (priv->converter,
404 buffer_data (&priv->input_buffer),
405 buffer_data_size (&priv->input_buffer),
407 priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
411 if (res != G_CONVERTER_ERROR)
413 total_bytes_read += bytes_written;
414 buffer_consumed (&priv->input_buffer, bytes_read);
415 if (res == G_CONVERTER_FINISHED)
416 priv->finished = TRUE; /* We're done converting */
418 else if (total_bytes_read == 0 &&
419 !g_error_matches (my_error,
421 G_IO_ERROR_PARTIAL_INPUT) &&
422 !g_error_matches (my_error,
424 G_IO_ERROR_NO_SPACE))
426 /* No previously read data and no "special" error, return error */
427 g_propagate_error (error, my_error);
431 g_error_free (my_error);
434 /* We had some pre-converted data and/or we converted directly to the
436 if (total_bytes_read > 0)
437 return total_bytes_read;
439 /* If there is no more to convert, return EOF */
442 g_assert (buffer_data_size (&priv->converted_buffer) == 0);
446 /* There was "complexity" in the straight-to-buffer conversion,
447 * convert to our own buffer and write from that.
448 * At this point we didn't produce any data into @buffer.
451 /* Ensure we have *some* initial target space */
452 buffer_ensure_space (&priv->converted_buffer, count);
456 g_assert (!priv->finished);
458 /* Try to convert to our buffer */
460 res = g_converter_convert (priv->converter,
461 buffer_data (&priv->input_buffer),
462 buffer_data_size (&priv->input_buffer),
463 buffer_data (&priv->converted_buffer),
464 buffer_tailspace (&priv->converted_buffer),
465 priv->at_input_end ? G_CONVERTER_INPUT_AT_END : 0,
469 if (res != G_CONVERTER_ERROR)
471 priv->converted_buffer.end += bytes_written;
472 buffer_consumed (&priv->input_buffer, bytes_read);
474 /* Maybe we consumed without producing any output */
475 if (buffer_data_size (&priv->converted_buffer) == 0 && res != G_CONVERTER_FINISHED)
476 continue; /* Convert more */
478 if (res == G_CONVERTER_FINISHED)
479 priv->finished = TRUE;
481 total_bytes_read = MIN (count, buffer_data_size (&priv->converted_buffer));
482 buffer_read (&priv->converted_buffer, buffer, total_bytes_read);
484 g_assert (priv->finished || total_bytes_read > 0);
486 return total_bytes_read;
489 /* There was some kind of error filling our buffer */
491 if (g_error_matches (my_error,
493 G_IO_ERROR_PARTIAL_INPUT) &&
498 res = fill_input_buffer (cstream,
499 buffer_data_size (&priv->input_buffer) + 4096,
504 /* Can't read any more data, return that error */
505 g_error_free (my_error);
506 g_propagate_error (error, my_error2);
511 /* End of file, try INPUT_AT_END */
512 priv->at_input_end = TRUE;
514 g_error_free (my_error);
518 if (g_error_matches (my_error,
520 G_IO_ERROR_NO_SPACE))
522 /* Need more destination space, grow it
523 * Note: if we actually grow the buffer (as opposed to compacting it),
524 * this will double the size, not just add one byte. */
525 buffer_ensure_space (&priv->converted_buffer,
526 priv->converted_buffer.size + 1);
527 g_error_free (my_error);
531 /* Any other random error, return it */
532 g_propagate_error (error, my_error);
536 g_assert_not_reached ();
540 * g_converter_input_stream_get_converter:
541 * @converter_stream: a #GConverterInputStream
543 * Gets the #GConverter that is used by @converter_stream.
545 * Returns: (transfer none): the converter of the converter input stream
550 g_converter_input_stream_get_converter (GConverterInputStream *converter_stream)
552 return converter_stream->priv->converter;