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"
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 struct _GMemoryInputStreamPrivate {
53 static gssize g_memory_input_stream_read (GInputStream *stream,
56 GCancellable *cancellable,
58 static gssize g_memory_input_stream_skip (GInputStream *stream,
60 GCancellable *cancellable,
62 static gboolean g_memory_input_stream_close (GInputStream *stream,
63 GCancellable *cancellable,
65 static void g_memory_input_stream_skip_async (GInputStream *stream,
68 GCancellable *cancellabl,
69 GAsyncReadyCallback callback,
71 static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
74 static void g_memory_input_stream_close_async (GInputStream *stream,
76 GCancellable *cancellabl,
77 GAsyncReadyCallback callback,
79 static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
83 static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
84 static goffset g_memory_input_stream_tell (GSeekable *seekable);
85 static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
86 static gboolean g_memory_input_stream_seek (GSeekable *seekable,
89 GCancellable *cancellable,
91 static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
92 static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
94 GCancellable *cancellable,
97 static void g_memory_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
98 static gboolean g_memory_input_stream_is_readable (GPollableInputStream *stream);
99 static GSource *g_memory_input_stream_create_source (GPollableInputStream *stream,
100 GCancellable *cancellable);
102 static void g_memory_input_stream_finalize (GObject *object);
104 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
105 G_ADD_PRIVATE (GMemoryInputStream)
106 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
107 g_memory_input_stream_seekable_iface_init);
108 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
109 g_memory_input_stream_pollable_iface_init);
114 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
116 GObjectClass *object_class;
117 GInputStreamClass *istream_class;
119 object_class = G_OBJECT_CLASS (klass);
120 object_class->finalize = g_memory_input_stream_finalize;
122 istream_class = G_INPUT_STREAM_CLASS (klass);
123 istream_class->read_fn = g_memory_input_stream_read;
124 istream_class->skip = g_memory_input_stream_skip;
125 istream_class->close_fn = g_memory_input_stream_close;
127 istream_class->skip_async = g_memory_input_stream_skip_async;
128 istream_class->skip_finish = g_memory_input_stream_skip_finish;
129 istream_class->close_async = g_memory_input_stream_close_async;
130 istream_class->close_finish = g_memory_input_stream_close_finish;
134 g_memory_input_stream_finalize (GObject *object)
136 GMemoryInputStream *stream;
137 GMemoryInputStreamPrivate *priv;
139 stream = G_MEMORY_INPUT_STREAM (object);
142 g_slist_free_full (priv->chunks, (GDestroyNotify)g_bytes_unref);
144 G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize (object);
148 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
150 iface->tell = g_memory_input_stream_tell;
151 iface->can_seek = g_memory_input_stream_can_seek;
152 iface->seek = g_memory_input_stream_seek;
153 iface->can_truncate = g_memory_input_stream_can_truncate;
154 iface->truncate_fn = g_memory_input_stream_truncate;
158 g_memory_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
160 iface->is_readable = g_memory_input_stream_is_readable;
161 iface->create_source = g_memory_input_stream_create_source;
165 g_memory_input_stream_init (GMemoryInputStream *stream)
167 stream->priv = g_memory_input_stream_get_instance_private (stream);
171 * g_memory_input_stream_new:
173 * Creates a new empty #GMemoryInputStream.
175 * Returns: a new #GInputStream
178 g_memory_input_stream_new (void)
180 GInputStream *stream;
182 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
188 * g_memory_input_stream_new_from_data:
189 * @data: (array length=len) (element-type guint8) (transfer full): input data
190 * @len: length of the data, may be -1 if @data is a nul-terminated string
191 * @destroy: (allow-none): function that is called to free @data, or %NULL
193 * Creates a new #GMemoryInputStream with data in memory of a given size.
195 * Returns: new #GInputStream read from @data of @len bytes.
198 g_memory_input_stream_new_from_data (const void *data,
200 GDestroyNotify destroy)
202 GInputStream *stream;
204 stream = g_memory_input_stream_new ();
206 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
213 * g_memory_input_stream_new_from_bytes:
216 * Creates a new #GMemoryInputStream with data from the given @bytes.
218 * Returns: new #GInputStream read from @bytes
223 g_memory_input_stream_new_from_bytes (GBytes *bytes)
226 GInputStream *stream;
228 stream = g_memory_input_stream_new ();
230 g_memory_input_stream_add_bytes (G_MEMORY_INPUT_STREAM (stream),
237 * g_memory_input_stream_add_data:
238 * @stream: a #GMemoryInputStream
239 * @data: (array length=len) (element-type guint8) (transfer full): input data
240 * @len: length of the data, may be -1 if @data is a nul-terminated string
241 * @destroy: (allow-none): function that is called to free @data, or %NULL
243 * Appends @data to data that can be read from the input stream
246 g_memory_input_stream_add_data (GMemoryInputStream *stream,
249 GDestroyNotify destroy)
256 /* It's safe to discard the const here because we're chaining the
259 bytes = g_bytes_new_with_free_func (data, len, destroy, (void*)data);
261 g_memory_input_stream_add_bytes (stream, bytes);
263 g_bytes_unref (bytes);
267 * g_memory_input_stream_add_bytes:
268 * @stream: a #GMemoryInputStream
271 * Appends @bytes to data that can be read from the input stream.
276 g_memory_input_stream_add_bytes (GMemoryInputStream *stream,
279 GMemoryInputStreamPrivate *priv;
281 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
282 g_return_if_fail (bytes != NULL);
286 priv->chunks = g_slist_append (priv->chunks, g_bytes_ref (bytes));
287 priv->len += g_bytes_get_size (bytes);
291 g_memory_input_stream_read (GInputStream *stream,
294 GCancellable *cancellable,
297 GMemoryInputStream *memory_stream;
298 GMemoryInputStreamPrivate *priv;
302 gsize offset, start, rest, size;
304 memory_stream = G_MEMORY_INPUT_STREAM (stream);
305 priv = memory_stream->priv;
307 count = MIN (count, priv->len - priv->pos);
310 for (l = priv->chunks; l; l = l->next)
312 chunk = (GBytes *)l->data;
313 len = g_bytes_get_size (chunk);
315 if (offset + len > priv->pos)
321 start = priv->pos - offset;
324 for (; l && rest > 0; l = l->next)
326 const guint8* chunk_data;
327 chunk = (GBytes *)l->data;
329 chunk_data = g_bytes_get_data (chunk, &len);
331 size = MIN (rest, len - start);
333 memcpy ((guint8 *)buffer + (count - rest), chunk_data + start, size);
345 g_memory_input_stream_skip (GInputStream *stream,
347 GCancellable *cancellable,
350 GMemoryInputStream *memory_stream;
351 GMemoryInputStreamPrivate *priv;
353 memory_stream = G_MEMORY_INPUT_STREAM (stream);
354 priv = memory_stream->priv;
356 count = MIN (count, priv->len - priv->pos);
363 g_memory_input_stream_close (GInputStream *stream,
364 GCancellable *cancellable,
371 g_memory_input_stream_skip_async (GInputStream *stream,
374 GCancellable *cancellable,
375 GAsyncReadyCallback callback,
380 GError *error = NULL;
382 nskipped = G_INPUT_STREAM_GET_CLASS (stream)->skip (stream, count, cancellable, &error);
383 task = g_task_new (stream, cancellable, callback, user_data);
385 g_task_return_error (task, error);
387 g_task_return_int (task, nskipped);
388 g_object_unref (task);
392 g_memory_input_stream_skip_finish (GInputStream *stream,
393 GAsyncResult *result,
396 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
398 return g_task_propagate_int (G_TASK (result), error);
402 g_memory_input_stream_close_async (GInputStream *stream,
404 GCancellable *cancellable,
405 GAsyncReadyCallback callback,
410 task = g_task_new (stream, cancellable, callback, user_data);
411 g_task_return_boolean (task, TRUE);
412 g_object_unref (task);
416 g_memory_input_stream_close_finish (GInputStream *stream,
417 GAsyncResult *result,
424 g_memory_input_stream_tell (GSeekable *seekable)
426 GMemoryInputStream *memory_stream;
427 GMemoryInputStreamPrivate *priv;
429 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
430 priv = memory_stream->priv;
436 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
442 g_memory_input_stream_seek (GSeekable *seekable,
445 GCancellable *cancellable,
448 GMemoryInputStream *memory_stream;
449 GMemoryInputStreamPrivate *priv;
452 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
453 priv = memory_stream->priv;
458 absolute = priv->pos + offset;
466 absolute = priv->len + offset;
470 g_set_error_literal (error,
472 G_IO_ERROR_INVALID_ARGUMENT,
473 _("Invalid GSeekType supplied"));
478 if (absolute < 0 || absolute > priv->len)
480 g_set_error_literal (error,
482 G_IO_ERROR_INVALID_ARGUMENT,
483 _("Invalid seek request"));
487 priv->pos = absolute;
493 g_memory_input_stream_can_truncate (GSeekable *seekable)
499 g_memory_input_stream_truncate (GSeekable *seekable,
501 GCancellable *cancellable,
504 g_set_error_literal (error,
506 G_IO_ERROR_NOT_SUPPORTED,
507 _("Cannot truncate GMemoryInputStream"));
512 g_memory_input_stream_is_readable (GPollableInputStream *stream)
518 g_memory_input_stream_create_source (GPollableInputStream *stream,
519 GCancellable *cancellable)
521 GSource *base_source, *pollable_source;
523 base_source = g_timeout_source_new (0);
524 pollable_source = g_pollable_source_new_full (stream, base_source,
526 g_source_unref (base_source);
528 return pollable_source;