1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 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: Christian Kellner <gicmo@gnome.org>
24 #include "gmemoryinputstream.h"
25 #include "gpollableinputstream.h"
26 #include "ginputstream.h"
27 #include "gseekable.h"
29 #include "gsimpleasyncresult.h"
35 * SECTION:gmemoryinputstream
36 * @short_description: Streaming input operations on memory chunks
38 * @see_also: #GMemoryOutputStream
40 * #GMemoryInputStream is a class for using arbitrary
41 * memory chunks as input for GIO streaming input operations.
43 * As of GLib 2.34, #GMemoryInputStream implements
44 * #GPollableInputStream.
47 typedef struct _Chunk Chunk;
52 GDestroyNotify destroy;
55 struct _GMemoryInputStreamPrivate {
61 static gssize g_memory_input_stream_read (GInputStream *stream,
64 GCancellable *cancellable,
66 static gssize g_memory_input_stream_skip (GInputStream *stream,
68 GCancellable *cancellable,
70 static gboolean g_memory_input_stream_close (GInputStream *stream,
71 GCancellable *cancellable,
73 static void g_memory_input_stream_skip_async (GInputStream *stream,
76 GCancellable *cancellabl,
77 GAsyncReadyCallback callback,
79 static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
82 static void g_memory_input_stream_close_async (GInputStream *stream,
84 GCancellable *cancellabl,
85 GAsyncReadyCallback callback,
87 static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
91 static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
92 static goffset g_memory_input_stream_tell (GSeekable *seekable);
93 static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
94 static gboolean g_memory_input_stream_seek (GSeekable *seekable,
97 GCancellable *cancellable,
99 static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
100 static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
102 GCancellable *cancellable,
105 static void g_memory_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
106 static gboolean g_memory_input_stream_is_readable (GPollableInputStream *stream);
107 static GSource *g_memory_input_stream_create_source (GPollableInputStream *stream,
108 GCancellable *cancellable);
110 static void g_memory_input_stream_finalize (GObject *object);
112 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
113 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
114 g_memory_input_stream_seekable_iface_init);
115 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
116 g_memory_input_stream_pollable_iface_init);
121 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
123 GObjectClass *object_class;
124 GInputStreamClass *istream_class;
126 g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
128 object_class = G_OBJECT_CLASS (klass);
129 object_class->finalize = g_memory_input_stream_finalize;
131 istream_class = G_INPUT_STREAM_CLASS (klass);
132 istream_class->read_fn = g_memory_input_stream_read;
133 istream_class->skip = g_memory_input_stream_skip;
134 istream_class->close_fn = g_memory_input_stream_close;
136 istream_class->skip_async = g_memory_input_stream_skip_async;
137 istream_class->skip_finish = g_memory_input_stream_skip_finish;
138 istream_class->close_async = g_memory_input_stream_close_async;
139 istream_class->close_finish = g_memory_input_stream_close_finish;
143 free_chunk (gpointer data)
148 chunk->destroy (chunk->data);
150 g_slice_free (Chunk, chunk);
154 g_memory_input_stream_finalize (GObject *object)
156 GMemoryInputStream *stream;
157 GMemoryInputStreamPrivate *priv;
159 stream = G_MEMORY_INPUT_STREAM (object);
162 g_slist_free_full (priv->chunks, free_chunk);
164 G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize (object);
168 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
170 iface->tell = g_memory_input_stream_tell;
171 iface->can_seek = g_memory_input_stream_can_seek;
172 iface->seek = g_memory_input_stream_seek;
173 iface->can_truncate = g_memory_input_stream_can_truncate;
174 iface->truncate_fn = g_memory_input_stream_truncate;
178 g_memory_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
180 iface->is_readable = g_memory_input_stream_is_readable;
181 iface->create_source = g_memory_input_stream_create_source;
185 g_memory_input_stream_init (GMemoryInputStream *stream)
187 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
188 G_TYPE_MEMORY_INPUT_STREAM,
189 GMemoryInputStreamPrivate);
193 * g_memory_input_stream_new:
195 * Creates a new empty #GMemoryInputStream.
197 * Returns: a new #GInputStream
200 g_memory_input_stream_new (void)
202 GInputStream *stream;
204 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
210 * g_memory_input_stream_new_from_data:
211 * @data: (array length=len) (element-type guint8) (transfer full): input data
212 * @len: length of the data, may be -1 if @data is a nul-terminated string
213 * @destroy: (allow-none): function that is called to free @data, or %NULL
215 * Creates a new #GMemoryInputStream with data in memory of a given size.
217 * Returns: new #GInputStream read from @data of @len bytes.
220 g_memory_input_stream_new_from_data (const void *data,
222 GDestroyNotify destroy)
224 GInputStream *stream;
226 stream = g_memory_input_stream_new ();
228 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
235 * g_memory_input_stream_add_data:
236 * @stream: a #GMemoryInputStream
237 * @data: (array length=len) (element-type guint8) (transfer full): input data
238 * @len: length of the data, may be -1 if @data is a nul-terminated string
239 * @destroy: (allow-none): function that is called to free @data, or %NULL
241 * Appends @data to data that can be read from the input stream
244 g_memory_input_stream_add_data (GMemoryInputStream *stream,
247 GDestroyNotify destroy)
249 GMemoryInputStreamPrivate *priv;
252 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
253 g_return_if_fail (data != NULL);
260 chunk = g_slice_new (Chunk);
261 chunk->data = (guint8 *)data;
263 chunk->destroy = destroy;
265 priv->chunks = g_slist_append (priv->chunks, chunk);
266 priv->len += chunk->len;
270 g_memory_input_stream_read (GInputStream *stream,
273 GCancellable *cancellable,
276 GMemoryInputStream *memory_stream;
277 GMemoryInputStreamPrivate *priv;
280 gsize offset, start, rest, size;
282 memory_stream = G_MEMORY_INPUT_STREAM (stream);
283 priv = memory_stream->priv;
285 count = MIN (count, priv->len - priv->pos);
288 for (l = priv->chunks; l; l = l->next)
290 chunk = (Chunk *)l->data;
292 if (offset + chunk->len > priv->pos)
295 offset += chunk->len;
298 start = priv->pos - offset;
301 for (; l && rest > 0; l = l->next)
303 chunk = (Chunk *)l->data;
304 size = MIN (rest, chunk->len - start);
306 memcpy ((guint8 *)buffer + (count - rest), chunk->data + start, size);
318 g_memory_input_stream_skip (GInputStream *stream,
320 GCancellable *cancellable,
323 GMemoryInputStream *memory_stream;
324 GMemoryInputStreamPrivate *priv;
326 memory_stream = G_MEMORY_INPUT_STREAM (stream);
327 priv = memory_stream->priv;
329 count = MIN (count, priv->len - priv->pos);
336 g_memory_input_stream_close (GInputStream *stream,
337 GCancellable *cancellable,
344 g_memory_input_stream_skip_async (GInputStream *stream,
347 GCancellable *cancellable,
348 GAsyncReadyCallback callback,
351 GSimpleAsyncResult *simple;
354 nskipped = g_input_stream_skip (stream, count, cancellable, NULL);
355 simple = g_simple_async_result_new (G_OBJECT (stream),
358 g_memory_input_stream_skip_async);
359 g_simple_async_result_set_op_res_gssize (simple, nskipped);
360 g_simple_async_result_complete_in_idle (simple);
361 g_object_unref (simple);
365 g_memory_input_stream_skip_finish (GInputStream *stream,
366 GAsyncResult *result,
369 GSimpleAsyncResult *simple;
372 simple = G_SIMPLE_ASYNC_RESULT (result);
373 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
375 nskipped = g_simple_async_result_get_op_res_gssize (simple);
380 g_memory_input_stream_close_async (GInputStream *stream,
382 GCancellable *cancellable,
383 GAsyncReadyCallback callback,
386 GSimpleAsyncResult *simple;
388 simple = g_simple_async_result_new (G_OBJECT (stream),
391 g_memory_input_stream_close_async);
392 g_simple_async_result_complete_in_idle (simple);
393 g_object_unref (simple);
397 g_memory_input_stream_close_finish (GInputStream *stream,
398 GAsyncResult *result,
405 g_memory_input_stream_tell (GSeekable *seekable)
407 GMemoryInputStream *memory_stream;
408 GMemoryInputStreamPrivate *priv;
410 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
411 priv = memory_stream->priv;
417 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
423 g_memory_input_stream_seek (GSeekable *seekable,
426 GCancellable *cancellable,
429 GMemoryInputStream *memory_stream;
430 GMemoryInputStreamPrivate *priv;
433 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
434 priv = memory_stream->priv;
439 absolute = priv->pos + offset;
447 absolute = priv->len + offset;
451 g_set_error_literal (error,
453 G_IO_ERROR_INVALID_ARGUMENT,
454 _("Invalid GSeekType supplied"));
459 if (absolute < 0 || absolute > priv->len)
461 g_set_error_literal (error,
463 G_IO_ERROR_INVALID_ARGUMENT,
464 _("Invalid seek request"));
468 priv->pos = absolute;
474 g_memory_input_stream_can_truncate (GSeekable *seekable)
480 g_memory_input_stream_truncate (GSeekable *seekable,
482 GCancellable *cancellable,
485 g_set_error_literal (error,
487 G_IO_ERROR_NOT_SUPPORTED,
488 _("Cannot truncate GMemoryInputStream"));
493 g_memory_input_stream_is_readable (GPollableInputStream *stream)
499 g_memory_input_stream_create_source (GPollableInputStream *stream,
500 GCancellable *cancellable)
502 GSource *base_source, *pollable_source;
504 base_source = g_timeout_source_new (0);
505 pollable_source = g_pollable_source_new_full (stream, base_source,
507 g_source_unref (base_source);
509 return pollable_source;