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_read_async (GInputStream *stream,
77 GCancellable *cancellable,
78 GAsyncReadyCallback callback,
80 static gssize g_memory_input_stream_read_finish (GInputStream *stream,
83 static void g_memory_input_stream_skip_async (GInputStream *stream,
86 GCancellable *cancellabl,
87 GAsyncReadyCallback callback,
89 static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
92 static void g_memory_input_stream_close_async (GInputStream *stream,
94 GCancellable *cancellabl,
95 GAsyncReadyCallback callback,
97 static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
101 static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
102 static goffset g_memory_input_stream_tell (GSeekable *seekable);
103 static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
104 static gboolean g_memory_input_stream_seek (GSeekable *seekable,
107 GCancellable *cancellable,
109 static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
110 static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
112 GCancellable *cancellable,
115 static void g_memory_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
116 static gboolean g_memory_input_stream_is_readable (GPollableInputStream *stream);
117 static GSource *g_memory_input_stream_create_source (GPollableInputStream *stream,
118 GCancellable *cancellable);
120 static void g_memory_input_stream_finalize (GObject *object);
122 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
123 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
124 g_memory_input_stream_seekable_iface_init);
125 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
126 g_memory_input_stream_pollable_iface_init);
131 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
133 GObjectClass *object_class;
134 GInputStreamClass *istream_class;
136 g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
138 object_class = G_OBJECT_CLASS (klass);
139 object_class->finalize = g_memory_input_stream_finalize;
141 istream_class = G_INPUT_STREAM_CLASS (klass);
142 istream_class->read_fn = g_memory_input_stream_read;
143 istream_class->skip = g_memory_input_stream_skip;
144 istream_class->close_fn = g_memory_input_stream_close;
146 istream_class->read_async = g_memory_input_stream_read_async;
147 istream_class->read_finish = g_memory_input_stream_read_finish;
148 istream_class->skip_async = g_memory_input_stream_skip_async;
149 istream_class->skip_finish = g_memory_input_stream_skip_finish;
150 istream_class->close_async = g_memory_input_stream_close_async;
151 istream_class->close_finish = g_memory_input_stream_close_finish;
155 free_chunk (gpointer data)
160 chunk->destroy (chunk->data);
162 g_slice_free (Chunk, chunk);
166 g_memory_input_stream_finalize (GObject *object)
168 GMemoryInputStream *stream;
169 GMemoryInputStreamPrivate *priv;
171 stream = G_MEMORY_INPUT_STREAM (object);
174 g_slist_free_full (priv->chunks, free_chunk);
176 G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize (object);
180 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
182 iface->tell = g_memory_input_stream_tell;
183 iface->can_seek = g_memory_input_stream_can_seek;
184 iface->seek = g_memory_input_stream_seek;
185 iface->can_truncate = g_memory_input_stream_can_truncate;
186 iface->truncate_fn = g_memory_input_stream_truncate;
190 g_memory_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
192 iface->is_readable = g_memory_input_stream_is_readable;
193 iface->create_source = g_memory_input_stream_create_source;
197 g_memory_input_stream_init (GMemoryInputStream *stream)
199 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
200 G_TYPE_MEMORY_INPUT_STREAM,
201 GMemoryInputStreamPrivate);
205 * g_memory_input_stream_new:
207 * Creates a new empty #GMemoryInputStream.
209 * Returns: a new #GInputStream
212 g_memory_input_stream_new (void)
214 GInputStream *stream;
216 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
222 * g_memory_input_stream_new_from_data:
223 * @data: (array length=len) (element-type guint8) (transfer full): input data
224 * @len: length of the data, may be -1 if @data is a nul-terminated string
225 * @destroy: (allow-none): function that is called to free @data, or %NULL
227 * Creates a new #GMemoryInputStream with data in memory of a given size.
229 * Returns: new #GInputStream read from @data of @len bytes.
232 g_memory_input_stream_new_from_data (const void *data,
234 GDestroyNotify destroy)
236 GInputStream *stream;
238 stream = g_memory_input_stream_new ();
240 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
247 * g_memory_input_stream_add_data:
248 * @stream: a #GMemoryInputStream
249 * @data: (array length=len) (element-type guint8) (transfer full): input data
250 * @len: length of the data, may be -1 if @data is a nul-terminated string
251 * @destroy: (allow-none): function that is called to free @data, or %NULL
253 * Appends @data to data that can be read from the input stream
256 g_memory_input_stream_add_data (GMemoryInputStream *stream,
259 GDestroyNotify destroy)
261 GMemoryInputStreamPrivate *priv;
264 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
265 g_return_if_fail (data != NULL);
272 chunk = g_slice_new (Chunk);
273 chunk->data = (guint8 *)data;
275 chunk->destroy = destroy;
277 priv->chunks = g_slist_append (priv->chunks, chunk);
278 priv->len += chunk->len;
282 g_memory_input_stream_read (GInputStream *stream,
285 GCancellable *cancellable,
288 GMemoryInputStream *memory_stream;
289 GMemoryInputStreamPrivate *priv;
292 gsize offset, start, rest, size;
294 memory_stream = G_MEMORY_INPUT_STREAM (stream);
295 priv = memory_stream->priv;
297 count = MIN (count, priv->len - priv->pos);
300 for (l = priv->chunks; l; l = l->next)
302 chunk = (Chunk *)l->data;
304 if (offset + chunk->len > priv->pos)
307 offset += chunk->len;
310 start = priv->pos - offset;
313 for (; l && rest > 0; l = l->next)
315 chunk = (Chunk *)l->data;
316 size = MIN (rest, chunk->len - start);
318 memcpy ((guint8 *)buffer + (count - rest), chunk->data + start, size);
330 g_memory_input_stream_skip (GInputStream *stream,
332 GCancellable *cancellable,
335 GMemoryInputStream *memory_stream;
336 GMemoryInputStreamPrivate *priv;
338 memory_stream = G_MEMORY_INPUT_STREAM (stream);
339 priv = memory_stream->priv;
341 count = MIN (count, priv->len - priv->pos);
348 g_memory_input_stream_close (GInputStream *stream,
349 GCancellable *cancellable,
356 g_memory_input_stream_read_async (GInputStream *stream,
360 GCancellable *cancellable,
361 GAsyncReadyCallback callback,
364 GSimpleAsyncResult *simple;
365 GError *error = NULL;
368 nread = G_INPUT_STREAM_GET_CLASS (stream)->read_fn (stream,
373 simple = g_simple_async_result_new (G_OBJECT (stream),
376 g_memory_input_stream_read_async);
378 g_simple_async_result_take_error (simple, error);
380 g_simple_async_result_set_op_res_gssize (simple, nread);
381 g_simple_async_result_complete_in_idle (simple);
382 g_object_unref (simple);
386 g_memory_input_stream_read_finish (GInputStream *stream,
387 GAsyncResult *result,
390 GSimpleAsyncResult *simple;
393 simple = G_SIMPLE_ASYNC_RESULT (result);
394 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
396 nread = g_simple_async_result_get_op_res_gssize (simple);
401 g_memory_input_stream_skip_async (GInputStream *stream,
404 GCancellable *cancellable,
405 GAsyncReadyCallback callback,
408 GSimpleAsyncResult *simple;
411 nskipped = g_input_stream_skip (stream, count, cancellable, NULL);
412 simple = g_simple_async_result_new (G_OBJECT (stream),
415 g_memory_input_stream_skip_async);
416 g_simple_async_result_set_op_res_gssize (simple, nskipped);
417 g_simple_async_result_complete_in_idle (simple);
418 g_object_unref (simple);
422 g_memory_input_stream_skip_finish (GInputStream *stream,
423 GAsyncResult *result,
426 GSimpleAsyncResult *simple;
429 simple = G_SIMPLE_ASYNC_RESULT (result);
430 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
432 nskipped = g_simple_async_result_get_op_res_gssize (simple);
437 g_memory_input_stream_close_async (GInputStream *stream,
439 GCancellable *cancellable,
440 GAsyncReadyCallback callback,
443 GSimpleAsyncResult *simple;
445 simple = g_simple_async_result_new (G_OBJECT (stream),
448 g_memory_input_stream_close_async);
449 g_simple_async_result_complete_in_idle (simple);
450 g_object_unref (simple);
454 g_memory_input_stream_close_finish (GInputStream *stream,
455 GAsyncResult *result,
462 g_memory_input_stream_tell (GSeekable *seekable)
464 GMemoryInputStream *memory_stream;
465 GMemoryInputStreamPrivate *priv;
467 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
468 priv = memory_stream->priv;
474 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
480 g_memory_input_stream_seek (GSeekable *seekable,
483 GCancellable *cancellable,
486 GMemoryInputStream *memory_stream;
487 GMemoryInputStreamPrivate *priv;
490 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
491 priv = memory_stream->priv;
496 absolute = priv->pos + offset;
504 absolute = priv->len + offset;
508 g_set_error_literal (error,
510 G_IO_ERROR_INVALID_ARGUMENT,
511 _("Invalid GSeekType supplied"));
516 if (absolute < 0 || absolute > priv->len)
518 g_set_error_literal (error,
520 G_IO_ERROR_INVALID_ARGUMENT,
521 _("Invalid seek request"));
525 priv->pos = absolute;
531 g_memory_input_stream_can_truncate (GSeekable *seekable)
537 g_memory_input_stream_truncate (GSeekable *seekable,
539 GCancellable *cancellable,
542 g_set_error_literal (error,
544 G_IO_ERROR_NOT_SUPPORTED,
545 _("Cannot truncate GMemoryInputStream"));
550 g_memory_input_stream_is_readable (GPollableInputStream *stream)
556 g_memory_input_stream_create_source (GPollableInputStream *stream,
557 GCancellable *cancellable)
559 GSource *base_source, *pollable_source;
561 base_source = g_timeout_source_new (0);
562 pollable_source = g_pollable_source_new_full (stream, base_source,
564 g_source_unref (base_source);
566 return pollable_source;