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 struct _GMemoryInputStreamPrivate {
51 static gssize g_memory_input_stream_read (GInputStream *stream,
54 GCancellable *cancellable,
56 static gssize g_memory_input_stream_skip (GInputStream *stream,
58 GCancellable *cancellable,
60 static gboolean g_memory_input_stream_close (GInputStream *stream,
61 GCancellable *cancellable,
63 static void g_memory_input_stream_read_async (GInputStream *stream,
67 GCancellable *cancellable,
68 GAsyncReadyCallback callback,
70 static gssize g_memory_input_stream_read_finish (GInputStream *stream,
73 static void g_memory_input_stream_skip_async (GInputStream *stream,
76 GCancellable *cancellabl,
77 GAsyncReadyCallback callback,
79 static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
82 static void g_memory_input_stream_close_async (GInputStream *stream,
84 GCancellable *cancellabl,
85 GAsyncReadyCallback callback,
87 static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
91 static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
92 static goffset g_memory_input_stream_tell (GSeekable *seekable);
93 static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
94 static gboolean g_memory_input_stream_seek (GSeekable *seekable,
97 GCancellable *cancellable,
99 static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
100 static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
102 GCancellable *cancellable,
104 static void g_memory_input_stream_finalize (GObject *object);
106 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
107 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
108 g_memory_input_stream_seekable_iface_init))
112 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
114 GObjectClass *object_class;
115 GInputStreamClass *istream_class;
117 g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
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->read_async = g_memory_input_stream_read_async;
128 istream_class->read_finish = g_memory_input_stream_read_finish;
129 istream_class->skip_async = g_memory_input_stream_skip_async;
130 istream_class->skip_finish = g_memory_input_stream_skip_finish;
131 istream_class->close_async = g_memory_input_stream_close_async;
132 istream_class->close_finish = g_memory_input_stream_close_finish;
136 g_memory_input_stream_finalize (GObject *object)
138 GMemoryInputStream *stream;
140 stream = G_MEMORY_INPUT_STREAM (object);
142 if (stream->priv->free_data)
143 g_free (stream->priv->buffer);
145 if (G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize)
146 (*G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize) (object);
150 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
152 iface->tell = g_memory_input_stream_tell;
153 iface->can_seek = g_memory_input_stream_can_seek;
154 iface->seek = g_memory_input_stream_seek;
155 iface->can_truncate = g_memory_input_stream_can_truncate;
156 iface->truncate_fn = g_memory_input_stream_truncate;
160 g_memory_input_stream_init (GMemoryInputStream *stream)
162 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
163 G_TYPE_MEMORY_INPUT_STREAM,
164 GMemoryInputStreamPrivate);
168 * g_memory_input_stream_set_free_data:
169 * @stream: a #GMemoryInputStream.
170 * @free_data: a #gboolean. If %TRUE, frees the data within @stream.
172 * Sets if the data within the @stream should be freed when the stream
176 g_memory_input_stream_set_free_data (GMemoryInputStream *stream,
179 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
181 stream->priv->free_data = free_data;
185 * g_memory_input_stream_from_data:
187 * @len: length of the data.
189 * Creates a new #GMemoryInputStream with data in memory of a given size.
191 * Returns: new #GInputStream read from @data of @len bytes.
194 g_memory_input_stream_from_data (const void *data,
197 GInputStream *stream;
198 GMemoryInputStream *memory_stream;
200 g_return_val_if_fail (data != NULL, NULL);
202 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
203 memory_stream = G_MEMORY_INPUT_STREAM (stream);
208 memory_stream->priv->buffer = (guint8 *)data;
209 memory_stream->priv->len = len;
215 g_memory_input_stream_read (GInputStream *stream,
218 GCancellable *cancellable,
221 GMemoryInputStream *memory_stream;
222 GMemoryInputStreamPrivate * priv;
224 memory_stream = G_MEMORY_INPUT_STREAM (stream);
225 priv = memory_stream->priv;
227 count = MIN (count, priv->len - priv->pos);
228 memcpy (buffer, priv->buffer + priv->pos, count);
235 * g_memory_input_stream_get_data:
236 * @stream: a #GMemoryInputStream
238 * Gets a pointer to the data within the #GMemoryInputStream.
240 * Returns: a pointer to the memory in the @stream.
243 g_memory_input_stream_get_data (GMemoryInputStream *stream)
245 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), NULL);
247 return stream->priv->buffer;
251 * g_memory_input_stream_get_data_size:
252 * @stream: a #GMemoryInputStream
254 * Gets the size of the data within the #GMemoryInputStream.
256 * Returns: a gsize with the size of the data in @stream.
259 g_memory_input_stream_get_data_size (GMemoryInputStream *stream)
261 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), -1);
263 return stream->priv->len;
267 g_memory_input_stream_skip (GInputStream *stream,
269 GCancellable *cancellable,
272 GMemoryInputStream *memory_stream;
273 GMemoryInputStreamPrivate *priv;
275 memory_stream = G_MEMORY_INPUT_STREAM (stream);
276 priv = memory_stream->priv;
278 count = MIN (count, priv->len - priv->pos);
287 g_memory_input_stream_close (GInputStream *stream,
288 GCancellable *cancellable,
295 g_memory_input_stream_read_async (GInputStream *stream,
299 GCancellable *cancellable,
300 GAsyncReadyCallback callback,
303 GSimpleAsyncResult *simple;
306 nread = g_memory_input_stream_read (stream, buffer, count, cancellable, NULL);
307 simple = g_simple_async_result_new (G_OBJECT (stream),
310 g_memory_input_stream_read_async);
311 g_simple_async_result_set_op_res_gssize (simple, nread);
312 g_simple_async_result_complete_in_idle (simple);
313 g_object_unref (simple);
317 g_memory_input_stream_read_finish (GInputStream *stream,
318 GAsyncResult *result,
321 GSimpleAsyncResult *simple;
324 simple = G_SIMPLE_ASYNC_RESULT (result);
325 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
327 nread = g_simple_async_result_get_op_res_gssize (simple);
332 g_memory_input_stream_skip_async (GInputStream *stream,
335 GCancellable *cancellable,
336 GAsyncReadyCallback callback,
339 GSimpleAsyncResult *simple;
342 nskipped = g_memory_input_stream_skip (stream, count, cancellable, NULL);
343 simple = g_simple_async_result_new (G_OBJECT (stream),
346 g_memory_input_stream_skip_async);
347 g_simple_async_result_set_op_res_gssize (simple, nskipped);
348 g_simple_async_result_complete_in_idle (simple);
349 g_object_unref (simple);
353 g_memory_input_stream_skip_finish (GInputStream *stream,
354 GAsyncResult *result,
357 GSimpleAsyncResult *simple;
360 simple = G_SIMPLE_ASYNC_RESULT (result);
361 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
363 nskipped = g_simple_async_result_get_op_res_gssize (simple);
368 g_memory_input_stream_close_async (GInputStream *stream,
370 GCancellable *cancellable,
371 GAsyncReadyCallback callback,
374 GSimpleAsyncResult *simple;
376 simple = g_simple_async_result_new (G_OBJECT (stream),
379 g_memory_input_stream_close_async);
380 g_simple_async_result_complete_in_idle (simple);
381 g_object_unref (simple);
385 g_memory_input_stream_close_finish (GInputStream *stream,
386 GAsyncResult *result,
393 g_memory_input_stream_tell (GSeekable *seekable)
395 GMemoryInputStream *memory_stream;
396 GMemoryInputStreamPrivate * priv;
398 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
399 priv = memory_stream->priv;
405 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
411 g_memory_input_stream_seek (GSeekable *seekable,
414 GCancellable *cancellable,
417 GMemoryInputStream *memory_stream;
418 GMemoryInputStreamPrivate * priv;
421 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
422 priv = memory_stream->priv;
427 absolute = priv->pos + offset;
435 absolute = priv->len + offset;
441 G_IO_ERROR_INVALID_ARGUMENT,
442 "Invalid GSeekType supplied");
447 if (absolute < 0 || absolute > priv->len)
451 G_IO_ERROR_INVALID_ARGUMENT,
452 "Invalid seek request");
456 priv->pos = absolute;
462 g_memory_input_stream_can_truncate (GSeekable *seekable)
468 g_memory_input_stream_truncate (GSeekable *seekable,
470 GCancellable *cancellable,
475 G_IO_ERROR_NOT_SUPPORTED,
476 "Cannot seek on GMemoryInputStream");
480 #define __G_MEMORY_INPUT_STREAM_C__
481 #include "gioaliasdef.c"