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 "ginputstream.h"
26 #include "gseekable.h"
28 #include "gsimpleasyncresult.h"
34 * SECTION:gmemoryinputstream
35 * @short_description: Streaming input operations on memory chunks
37 * @see_also: #GMemoryOutputStream
39 * #GMemoryInputStream is a class for using arbitrary
40 * memory chunks as input for GIO streaming input operations.
44 typedef struct _Chunk Chunk;
49 GDestroyNotify destroy;
52 struct _GMemoryInputStreamPrivate {
58 static gssize g_memory_input_stream_read (GInputStream *stream,
61 GCancellable *cancellable,
63 static gssize g_memory_input_stream_skip (GInputStream *stream,
65 GCancellable *cancellable,
67 static gboolean g_memory_input_stream_close (GInputStream *stream,
68 GCancellable *cancellable,
70 static void g_memory_input_stream_read_async (GInputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gssize g_memory_input_stream_read_finish (GInputStream *stream,
80 static void g_memory_input_stream_skip_async (GInputStream *stream,
83 GCancellable *cancellabl,
84 GAsyncReadyCallback callback,
86 static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
89 static void g_memory_input_stream_close_async (GInputStream *stream,
91 GCancellable *cancellabl,
92 GAsyncReadyCallback callback,
94 static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
98 static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
99 static goffset g_memory_input_stream_tell (GSeekable *seekable);
100 static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
101 static gboolean g_memory_input_stream_seek (GSeekable *seekable,
104 GCancellable *cancellable,
106 static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
107 static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
109 GCancellable *cancellable,
111 static void g_memory_input_stream_finalize (GObject *object);
113 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
114 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
115 g_memory_input_stream_seekable_iface_init))
119 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
121 GObjectClass *object_class;
122 GInputStreamClass *istream_class;
124 g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
126 object_class = G_OBJECT_CLASS (klass);
127 object_class->finalize = g_memory_input_stream_finalize;
129 istream_class = G_INPUT_STREAM_CLASS (klass);
130 istream_class->read_fn = g_memory_input_stream_read;
131 istream_class->skip = g_memory_input_stream_skip;
132 istream_class->close_fn = g_memory_input_stream_close;
134 istream_class->read_async = g_memory_input_stream_read_async;
135 istream_class->read_finish = g_memory_input_stream_read_finish;
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_init (GMemoryInputStream *stream)
180 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
181 G_TYPE_MEMORY_INPUT_STREAM,
182 GMemoryInputStreamPrivate);
186 * g_memory_input_stream_new:
188 * Creates a new empty #GMemoryInputStream.
190 * Returns: a new #GInputStream
193 g_memory_input_stream_new (void)
195 GInputStream *stream;
197 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
203 * g_memory_input_stream_new_from_data:
204 * @data: (array length=len) (element-type guint8) (transfer full): input data
205 * @len: length of the data, may be -1 if @data is a nul-terminated string
206 * @destroy: (allow-none): function that is called to free @data, or %NULL
208 * Creates a new #GMemoryInputStream with data in memory of a given size.
210 * Returns: new #GInputStream read from @data of @len bytes.
213 g_memory_input_stream_new_from_data (const void *data,
215 GDestroyNotify destroy)
217 GInputStream *stream;
219 stream = g_memory_input_stream_new ();
221 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
228 * g_memory_input_stream_add_data:
229 * @stream: a #GMemoryInputStream
230 * @data: (array length=len) (element-type guint8) (transfer full): input data
231 * @len: length of the data, may be -1 if @data is a nul-terminated string
232 * @destroy: (allow-none): function that is called to free @data, or %NULL
234 * Appends @data to data that can be read from the input stream
237 g_memory_input_stream_add_data (GMemoryInputStream *stream,
240 GDestroyNotify destroy)
242 GMemoryInputStreamPrivate *priv;
245 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
246 g_return_if_fail (data != NULL);
253 chunk = g_slice_new (Chunk);
254 chunk->data = (guint8 *)data;
256 chunk->destroy = destroy;
258 priv->chunks = g_slist_append (priv->chunks, chunk);
259 priv->len += chunk->len;
263 g_memory_input_stream_read (GInputStream *stream,
266 GCancellable *cancellable,
269 GMemoryInputStream *memory_stream;
270 GMemoryInputStreamPrivate *priv;
273 gsize offset, start, rest, size;
275 memory_stream = G_MEMORY_INPUT_STREAM (stream);
276 priv = memory_stream->priv;
278 count = MIN (count, priv->len - priv->pos);
281 for (l = priv->chunks; l; l = l->next)
283 chunk = (Chunk *)l->data;
285 if (offset + chunk->len > priv->pos)
288 offset += chunk->len;
291 start = priv->pos - offset;
294 for (; l && rest > 0; l = l->next)
296 chunk = (Chunk *)l->data;
297 size = MIN (rest, chunk->len - start);
299 memcpy ((guint8 *)buffer + (count - rest), chunk->data + start, size);
311 g_memory_input_stream_skip (GInputStream *stream,
313 GCancellable *cancellable,
316 GMemoryInputStream *memory_stream;
317 GMemoryInputStreamPrivate *priv;
319 memory_stream = G_MEMORY_INPUT_STREAM (stream);
320 priv = memory_stream->priv;
322 count = MIN (count, priv->len - priv->pos);
329 g_memory_input_stream_close (GInputStream *stream,
330 GCancellable *cancellable,
337 g_memory_input_stream_read_async (GInputStream *stream,
341 GCancellable *cancellable,
342 GAsyncReadyCallback callback,
345 GSimpleAsyncResult *simple;
346 GError *error = NULL;
349 nread = G_INPUT_STREAM_GET_CLASS (stream)->read_fn (stream,
354 simple = g_simple_async_result_new (G_OBJECT (stream),
357 g_memory_input_stream_read_async);
359 g_simple_async_result_take_error (simple, error);
361 g_simple_async_result_set_op_res_gssize (simple, nread);
362 g_simple_async_result_complete_in_idle (simple);
363 g_object_unref (simple);
367 g_memory_input_stream_read_finish (GInputStream *stream,
368 GAsyncResult *result,
371 GSimpleAsyncResult *simple;
374 simple = G_SIMPLE_ASYNC_RESULT (result);
375 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
377 nread = g_simple_async_result_get_op_res_gssize (simple);
382 g_memory_input_stream_skip_async (GInputStream *stream,
385 GCancellable *cancellable,
386 GAsyncReadyCallback callback,
389 GSimpleAsyncResult *simple;
392 nskipped = g_input_stream_skip (stream, count, cancellable, NULL);
393 simple = g_simple_async_result_new (G_OBJECT (stream),
396 g_memory_input_stream_skip_async);
397 g_simple_async_result_set_op_res_gssize (simple, nskipped);
398 g_simple_async_result_complete_in_idle (simple);
399 g_object_unref (simple);
403 g_memory_input_stream_skip_finish (GInputStream *stream,
404 GAsyncResult *result,
407 GSimpleAsyncResult *simple;
410 simple = G_SIMPLE_ASYNC_RESULT (result);
411 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
413 nskipped = g_simple_async_result_get_op_res_gssize (simple);
418 g_memory_input_stream_close_async (GInputStream *stream,
420 GCancellable *cancellable,
421 GAsyncReadyCallback callback,
424 GSimpleAsyncResult *simple;
426 simple = g_simple_async_result_new (G_OBJECT (stream),
429 g_memory_input_stream_close_async);
430 g_simple_async_result_complete_in_idle (simple);
431 g_object_unref (simple);
435 g_memory_input_stream_close_finish (GInputStream *stream,
436 GAsyncResult *result,
443 g_memory_input_stream_tell (GSeekable *seekable)
445 GMemoryInputStream *memory_stream;
446 GMemoryInputStreamPrivate *priv;
448 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
449 priv = memory_stream->priv;
455 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
461 g_memory_input_stream_seek (GSeekable *seekable,
464 GCancellable *cancellable,
467 GMemoryInputStream *memory_stream;
468 GMemoryInputStreamPrivate *priv;
471 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
472 priv = memory_stream->priv;
477 absolute = priv->pos + offset;
485 absolute = priv->len + offset;
489 g_set_error_literal (error,
491 G_IO_ERROR_INVALID_ARGUMENT,
492 _("Invalid GSeekType supplied"));
497 if (absolute < 0 || absolute > priv->len)
499 g_set_error_literal (error,
501 G_IO_ERROR_INVALID_ARGUMENT,
502 _("Invalid seek request"));
506 priv->pos = absolute;
512 g_memory_input_stream_can_truncate (GSeekable *seekable)
518 g_memory_input_stream_truncate (GSeekable *seekable,
520 GCancellable *cancellable,
523 g_set_error_literal (error,
525 G_IO_ERROR_NOT_SUPPORTED,
526 _("Cannot truncate GMemoryInputStream"));