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_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
106 g_memory_input_stream_seekable_iface_init);
107 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
108 g_memory_input_stream_pollable_iface_init);
113 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
115 GObjectClass *object_class;
116 GInputStreamClass *istream_class;
118 g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
120 object_class = G_OBJECT_CLASS (klass);
121 object_class->finalize = g_memory_input_stream_finalize;
123 istream_class = G_INPUT_STREAM_CLASS (klass);
124 istream_class->read_fn = g_memory_input_stream_read;
125 istream_class->skip = g_memory_input_stream_skip;
126 istream_class->close_fn = g_memory_input_stream_close;
128 istream_class->skip_async = g_memory_input_stream_skip_async;
129 istream_class->skip_finish = g_memory_input_stream_skip_finish;
130 istream_class->close_async = g_memory_input_stream_close_async;
131 istream_class->close_finish = g_memory_input_stream_close_finish;
135 g_memory_input_stream_finalize (GObject *object)
137 GMemoryInputStream *stream;
138 GMemoryInputStreamPrivate *priv;
140 stream = G_MEMORY_INPUT_STREAM (object);
143 g_slist_free_full (priv->chunks, (GDestroyNotify)g_bytes_unref);
145 G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize (object);
149 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
151 iface->tell = g_memory_input_stream_tell;
152 iface->can_seek = g_memory_input_stream_can_seek;
153 iface->seek = g_memory_input_stream_seek;
154 iface->can_truncate = g_memory_input_stream_can_truncate;
155 iface->truncate_fn = g_memory_input_stream_truncate;
159 g_memory_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
161 iface->is_readable = g_memory_input_stream_is_readable;
162 iface->create_source = g_memory_input_stream_create_source;
166 g_memory_input_stream_init (GMemoryInputStream *stream)
168 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
169 G_TYPE_MEMORY_INPUT_STREAM,
170 GMemoryInputStreamPrivate);
174 * g_memory_input_stream_new:
176 * Creates a new empty #GMemoryInputStream.
178 * Returns: a new #GInputStream
181 g_memory_input_stream_new (void)
183 GInputStream *stream;
185 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
191 * g_memory_input_stream_new_from_data:
192 * @data: (array length=len) (element-type guint8) (transfer full): input data
193 * @len: length of the data, may be -1 if @data is a nul-terminated string
194 * @destroy: (allow-none): function that is called to free @data, or %NULL
196 * Creates a new #GMemoryInputStream with data in memory of a given size.
198 * Returns: new #GInputStream read from @data of @len bytes.
201 g_memory_input_stream_new_from_data (const void *data,
203 GDestroyNotify destroy)
205 GInputStream *stream;
207 stream = g_memory_input_stream_new ();
209 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
216 * g_memory_input_stream_new_from_bytes:
219 * Creates a new #GMemoryInputStream with data from the given @bytes.
221 * Returns: new #GInputStream read from @bytes
226 g_memory_input_stream_new_from_bytes (GBytes *bytes)
229 GInputStream *stream;
231 stream = g_memory_input_stream_new ();
233 g_memory_input_stream_add_bytes (G_MEMORY_INPUT_STREAM (stream),
240 * g_memory_input_stream_add_data:
241 * @stream: a #GMemoryInputStream
242 * @data: (array length=len) (element-type guint8) (transfer full): input data
243 * @len: length of the data, may be -1 if @data is a nul-terminated string
244 * @destroy: (allow-none): function that is called to free @data, or %NULL
246 * Appends @data to data that can be read from the input stream
249 g_memory_input_stream_add_data (GMemoryInputStream *stream,
252 GDestroyNotify destroy)
259 /* It's safe to discard the const here because we're chaining the
262 bytes = g_bytes_new_with_free_func (data, len, destroy, (void*)data);
264 g_memory_input_stream_add_bytes (stream, bytes);
266 g_bytes_unref (bytes);
270 * g_memory_input_stream_add_bytes:
271 * @stream: a #GMemoryInputStream
274 * Appends @bytes to data that can be read from the input stream.
279 g_memory_input_stream_add_bytes (GMemoryInputStream *stream,
282 GMemoryInputStreamPrivate *priv;
284 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
285 g_return_if_fail (bytes != NULL);
289 priv->chunks = g_slist_append (priv->chunks, g_bytes_ref (bytes));
290 priv->len += g_bytes_get_size (bytes);
294 g_memory_input_stream_read (GInputStream *stream,
297 GCancellable *cancellable,
300 GMemoryInputStream *memory_stream;
301 GMemoryInputStreamPrivate *priv;
305 gsize offset, start, rest, size;
307 memory_stream = G_MEMORY_INPUT_STREAM (stream);
308 priv = memory_stream->priv;
310 count = MIN (count, priv->len - priv->pos);
313 for (l = priv->chunks; l; l = l->next)
315 chunk = (GBytes *)l->data;
316 len = g_bytes_get_size (chunk);
318 if (offset + len > priv->pos)
324 start = priv->pos - offset;
327 for (; l && rest > 0; l = l->next)
329 const guint8* chunk_data;
330 chunk = (GBytes *)l->data;
332 chunk_data = g_bytes_get_data (chunk, &len);
334 size = MIN (rest, len - start);
336 memcpy ((guint8 *)buffer + (count - rest), chunk_data + start, size);
348 g_memory_input_stream_skip (GInputStream *stream,
350 GCancellable *cancellable,
353 GMemoryInputStream *memory_stream;
354 GMemoryInputStreamPrivate *priv;
356 memory_stream = G_MEMORY_INPUT_STREAM (stream);
357 priv = memory_stream->priv;
359 count = MIN (count, priv->len - priv->pos);
366 g_memory_input_stream_close (GInputStream *stream,
367 GCancellable *cancellable,
374 g_memory_input_stream_skip_async (GInputStream *stream,
377 GCancellable *cancellable,
378 GAsyncReadyCallback callback,
383 GError *error = NULL;
385 nskipped = G_INPUT_STREAM_GET_CLASS (stream)->skip (stream, count, cancellable, &error);
386 task = g_task_new (stream, cancellable, callback, user_data);
388 g_task_return_error (task, error);
390 g_task_return_int (task, nskipped);
391 g_object_unref (task);
395 g_memory_input_stream_skip_finish (GInputStream *stream,
396 GAsyncResult *result,
399 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
401 return g_task_propagate_int (G_TASK (result), error);
405 g_memory_input_stream_close_async (GInputStream *stream,
407 GCancellable *cancellable,
408 GAsyncReadyCallback callback,
413 task = g_task_new (stream, cancellable, callback, user_data);
414 g_task_return_boolean (task, TRUE);
415 g_object_unref (task);
419 g_memory_input_stream_close_finish (GInputStream *stream,
420 GAsyncResult *result,
427 g_memory_input_stream_tell (GSeekable *seekable)
429 GMemoryInputStream *memory_stream;
430 GMemoryInputStreamPrivate *priv;
432 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
433 priv = memory_stream->priv;
439 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
445 g_memory_input_stream_seek (GSeekable *seekable,
448 GCancellable *cancellable,
451 GMemoryInputStream *memory_stream;
452 GMemoryInputStreamPrivate *priv;
455 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
456 priv = memory_stream->priv;
461 absolute = priv->pos + offset;
469 absolute = priv->len + offset;
473 g_set_error_literal (error,
475 G_IO_ERROR_INVALID_ARGUMENT,
476 _("Invalid GSeekType supplied"));
481 if (absolute < 0 || absolute > priv->len)
483 g_set_error_literal (error,
485 G_IO_ERROR_INVALID_ARGUMENT,
486 _("Invalid seek request"));
490 priv->pos = absolute;
496 g_memory_input_stream_can_truncate (GSeekable *seekable)
502 g_memory_input_stream_truncate (GSeekable *seekable,
504 GCancellable *cancellable,
507 g_set_error_literal (error,
509 G_IO_ERROR_NOT_SUPPORTED,
510 _("Cannot truncate GMemoryInputStream"));
515 g_memory_input_stream_is_readable (GPollableInputStream *stream)
521 g_memory_input_stream_create_source (GPollableInputStream *stream,
522 GCancellable *cancellable)
524 GSource *base_source, *pollable_source;
526 base_source = g_timeout_source_new (0);
527 pollable_source = g_pollable_source_new_full (stream, base_source,
529 g_source_unref (base_source);
531 return pollable_source;