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
225 g_memory_input_stream_new_from_bytes (GBytes *bytes)
228 GInputStream *stream;
230 stream = g_memory_input_stream_new ();
232 g_memory_input_stream_add_bytes (G_MEMORY_INPUT_STREAM (stream),
239 * g_memory_input_stream_add_data:
240 * @stream: a #GMemoryInputStream
241 * @data: (array length=len) (element-type guint8) (transfer full): input data
242 * @len: length of the data, may be -1 if @data is a nul-terminated string
243 * @destroy: (allow-none): function that is called to free @data, or %NULL
245 * Appends @data to data that can be read from the input stream
248 g_memory_input_stream_add_data (GMemoryInputStream *stream,
251 GDestroyNotify destroy)
258 /* It's safe to discard the const here because we're chaining the
261 bytes = g_bytes_new_with_free_func (data, len, destroy, (void*)data);
263 g_memory_input_stream_add_bytes (stream, bytes);
265 g_bytes_unref (bytes);
269 * g_memory_input_stream_add_bytes:
270 * @stream: a #GMemoryInputStream
273 * Appends @bytes to data that can be read from the input stream.
278 g_memory_input_stream_add_bytes (GMemoryInputStream *stream,
281 GMemoryInputStreamPrivate *priv;
283 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
284 g_return_if_fail (bytes != NULL);
288 priv->chunks = g_slist_append (priv->chunks, g_bytes_ref (bytes));
289 priv->len += g_bytes_get_size (bytes);
293 g_memory_input_stream_read (GInputStream *stream,
296 GCancellable *cancellable,
299 GMemoryInputStream *memory_stream;
300 GMemoryInputStreamPrivate *priv;
304 gsize offset, start, rest, size;
306 memory_stream = G_MEMORY_INPUT_STREAM (stream);
307 priv = memory_stream->priv;
309 count = MIN (count, priv->len - priv->pos);
312 for (l = priv->chunks; l; l = l->next)
314 chunk = (GBytes *)l->data;
315 len = g_bytes_get_size (chunk);
317 if (offset + len > priv->pos)
323 start = priv->pos - offset;
326 for (; l && rest > 0; l = l->next)
328 const guint8* chunk_data;
329 chunk = (GBytes *)l->data;
331 chunk_data = g_bytes_get_data (chunk, &len);
333 size = MIN (rest, len - start);
335 memcpy ((guint8 *)buffer + (count - rest), chunk_data + start, size);
347 g_memory_input_stream_skip (GInputStream *stream,
349 GCancellable *cancellable,
352 GMemoryInputStream *memory_stream;
353 GMemoryInputStreamPrivate *priv;
355 memory_stream = G_MEMORY_INPUT_STREAM (stream);
356 priv = memory_stream->priv;
358 count = MIN (count, priv->len - priv->pos);
365 g_memory_input_stream_close (GInputStream *stream,
366 GCancellable *cancellable,
373 g_memory_input_stream_skip_async (GInputStream *stream,
376 GCancellable *cancellable,
377 GAsyncReadyCallback callback,
383 nskipped = g_input_stream_skip (stream, count, cancellable, NULL);
384 task = g_task_new (stream, cancellable, callback, user_data);
385 g_task_return_int (task, nskipped);
386 g_object_unref (task);
390 g_memory_input_stream_skip_finish (GInputStream *stream,
391 GAsyncResult *result,
394 g_return_val_if_fail (g_task_is_valid (result, stream), -1);
396 return g_task_propagate_int (G_TASK (result), error);
400 g_memory_input_stream_close_async (GInputStream *stream,
402 GCancellable *cancellable,
403 GAsyncReadyCallback callback,
408 task = g_task_new (stream, cancellable, callback, user_data);
409 g_task_return_boolean (task, TRUE);
410 g_object_unref (task);
414 g_memory_input_stream_close_finish (GInputStream *stream,
415 GAsyncResult *result,
422 g_memory_input_stream_tell (GSeekable *seekable)
424 GMemoryInputStream *memory_stream;
425 GMemoryInputStreamPrivate *priv;
427 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
428 priv = memory_stream->priv;
434 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
440 g_memory_input_stream_seek (GSeekable *seekable,
443 GCancellable *cancellable,
446 GMemoryInputStream *memory_stream;
447 GMemoryInputStreamPrivate *priv;
450 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
451 priv = memory_stream->priv;
456 absolute = priv->pos + offset;
464 absolute = priv->len + offset;
468 g_set_error_literal (error,
470 G_IO_ERROR_INVALID_ARGUMENT,
471 _("Invalid GSeekType supplied"));
476 if (absolute < 0 || absolute > priv->len)
478 g_set_error_literal (error,
480 G_IO_ERROR_INVALID_ARGUMENT,
481 _("Invalid seek request"));
485 priv->pos = absolute;
491 g_memory_input_stream_can_truncate (GSeekable *seekable)
497 g_memory_input_stream_truncate (GSeekable *seekable,
499 GCancellable *cancellable,
502 g_set_error_literal (error,
504 G_IO_ERROR_NOT_SUPPORTED,
505 _("Cannot truncate GMemoryInputStream"));
510 g_memory_input_stream_is_readable (GPollableInputStream *stream)
516 g_memory_input_stream_create_source (GPollableInputStream *stream,
517 GCancellable *cancellable)
519 GSource *base_source, *pollable_source;
521 base_source = g_timeout_source_new (0);
522 pollable_source = g_pollable_source_new_full (stream, base_source,
524 g_source_unref (base_source);
526 return pollable_source;