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,
149 chunk->destroy (chunk->data);
151 g_slice_free (Chunk, chunk);
155 g_memory_input_stream_finalize (GObject *object)
157 GMemoryInputStream *stream;
158 GMemoryInputStreamPrivate *priv;
160 stream = G_MEMORY_INPUT_STREAM (object);
163 g_slist_free_full (priv->chunks, free_chunk);
165 G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize (object);
169 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
171 iface->tell = g_memory_input_stream_tell;
172 iface->can_seek = g_memory_input_stream_can_seek;
173 iface->seek = g_memory_input_stream_seek;
174 iface->can_truncate = g_memory_input_stream_can_truncate;
175 iface->truncate_fn = g_memory_input_stream_truncate;
179 g_memory_input_stream_init (GMemoryInputStream *stream)
181 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
182 G_TYPE_MEMORY_INPUT_STREAM,
183 GMemoryInputStreamPrivate);
187 * g_memory_input_stream_new:
189 * Creates a new empty #GMemoryInputStream.
191 * Returns: a new #GInputStream
194 g_memory_input_stream_new (void)
196 GInputStream *stream;
198 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
204 * g_memory_input_stream_new_from_data:
205 * @data: (array length=len) (element-type guint8) (transfer full): input data
206 * @len: length of the data, may be -1 if @data is a nul-terminated string
207 * @destroy: (allow-none): function that is called to free @data, or %NULL
209 * Creates a new #GMemoryInputStream with data in memory of a given size.
211 * Returns: new #GInputStream read from @data of @len bytes.
214 g_memory_input_stream_new_from_data (const void *data,
216 GDestroyNotify destroy)
218 GInputStream *stream;
220 stream = g_memory_input_stream_new ();
222 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
229 * g_memory_input_stream_add_data:
230 * @stream: a #GMemoryInputStream
231 * @data: (array length=len) (element-type guint8) (transfer full): input data
232 * @len: length of the data, may be -1 if @data is a nul-terminated string
233 * @destroy: (allow-none): function that is called to free @data, or %NULL
235 * Appends @data to data that can be read from the input stream
238 g_memory_input_stream_add_data (GMemoryInputStream *stream,
241 GDestroyNotify destroy)
243 GMemoryInputStreamPrivate *priv;
246 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
247 g_return_if_fail (data != NULL);
254 chunk = g_slice_new (Chunk);
255 chunk->data = (guint8 *)data;
257 chunk->destroy = destroy;
259 priv->chunks = g_slist_append (priv->chunks, chunk);
260 priv->len += chunk->len;
264 g_memory_input_stream_read (GInputStream *stream,
267 GCancellable *cancellable,
270 GMemoryInputStream *memory_stream;
271 GMemoryInputStreamPrivate *priv;
274 gsize offset, start, rest, size;
276 memory_stream = G_MEMORY_INPUT_STREAM (stream);
277 priv = memory_stream->priv;
279 count = MIN (count, priv->len - priv->pos);
282 for (l = priv->chunks; l; l = l->next)
284 chunk = (Chunk *)l->data;
286 if (offset + chunk->len > priv->pos)
289 offset += chunk->len;
292 start = priv->pos - offset;
295 for (; l && rest > 0; l = l->next)
297 chunk = (Chunk *)l->data;
298 size = MIN (rest, chunk->len - start);
300 memcpy ((guint8 *)buffer + (count - rest), chunk->data + start, size);
312 g_memory_input_stream_skip (GInputStream *stream,
314 GCancellable *cancellable,
317 GMemoryInputStream *memory_stream;
318 GMemoryInputStreamPrivate *priv;
320 memory_stream = G_MEMORY_INPUT_STREAM (stream);
321 priv = memory_stream->priv;
323 count = MIN (count, priv->len - priv->pos);
330 g_memory_input_stream_close (GInputStream *stream,
331 GCancellable *cancellable,
338 g_memory_input_stream_read_async (GInputStream *stream,
342 GCancellable *cancellable,
343 GAsyncReadyCallback callback,
346 GSimpleAsyncResult *simple;
347 GError *error = NULL;
350 nread = G_INPUT_STREAM_GET_CLASS (stream)->read_fn (stream,
355 simple = g_simple_async_result_new (G_OBJECT (stream),
358 g_memory_input_stream_read_async);
360 g_simple_async_result_take_error (simple, error);
362 g_simple_async_result_set_op_res_gssize (simple, nread);
363 g_simple_async_result_complete_in_idle (simple);
364 g_object_unref (simple);
368 g_memory_input_stream_read_finish (GInputStream *stream,
369 GAsyncResult *result,
372 GSimpleAsyncResult *simple;
375 simple = G_SIMPLE_ASYNC_RESULT (result);
376 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
378 nread = g_simple_async_result_get_op_res_gssize (simple);
383 g_memory_input_stream_skip_async (GInputStream *stream,
386 GCancellable *cancellable,
387 GAsyncReadyCallback callback,
390 GSimpleAsyncResult *simple;
393 nskipped = g_input_stream_skip (stream, count, cancellable, NULL);
394 simple = g_simple_async_result_new (G_OBJECT (stream),
397 g_memory_input_stream_skip_async);
398 g_simple_async_result_set_op_res_gssize (simple, nskipped);
399 g_simple_async_result_complete_in_idle (simple);
400 g_object_unref (simple);
404 g_memory_input_stream_skip_finish (GInputStream *stream,
405 GAsyncResult *result,
408 GSimpleAsyncResult *simple;
411 simple = G_SIMPLE_ASYNC_RESULT (result);
412 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
414 nskipped = g_simple_async_result_get_op_res_gssize (simple);
419 g_memory_input_stream_close_async (GInputStream *stream,
421 GCancellable *cancellable,
422 GAsyncReadyCallback callback,
425 GSimpleAsyncResult *simple;
427 simple = g_simple_async_result_new (G_OBJECT (stream),
430 g_memory_input_stream_close_async);
431 g_simple_async_result_complete_in_idle (simple);
432 g_object_unref (simple);
436 g_memory_input_stream_close_finish (GInputStream *stream,
437 GAsyncResult *result,
444 g_memory_input_stream_tell (GSeekable *seekable)
446 GMemoryInputStream *memory_stream;
447 GMemoryInputStreamPrivate *priv;
449 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
450 priv = memory_stream->priv;
456 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
462 g_memory_input_stream_seek (GSeekable *seekable,
465 GCancellable *cancellable,
468 GMemoryInputStream *memory_stream;
469 GMemoryInputStreamPrivate *priv;
472 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
473 priv = memory_stream->priv;
478 absolute = priv->pos + offset;
486 absolute = priv->len + offset;
490 g_set_error_literal (error,
492 G_IO_ERROR_INVALID_ARGUMENT,
493 _("Invalid GSeekType supplied"));
498 if (absolute < 0 || absolute > priv->len)
500 g_set_error_literal (error,
502 G_IO_ERROR_INVALID_ARGUMENT,
503 _("Invalid seek request"));
507 priv->pos = absolute;
513 g_memory_input_stream_can_truncate (GSeekable *seekable)
519 g_memory_input_stream_truncate (GSeekable *seekable,
521 GCancellable *cancellable,
524 g_set_error_literal (error,
526 G_IO_ERROR_NOT_SUPPORTED,
527 _("Cannot truncate GMemoryInputStream"));