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
36 * @see_also: #GMemoryOutputStream
38 * #GMemoryInputStream is a class for using arbitrary
39 * memory chunks as input for GIO streaming input operations.
43 struct _GMemoryInputStreamPrivate {
50 static gssize g_memory_input_stream_read (GInputStream *stream,
53 GCancellable *cancellable,
55 static gssize g_memory_input_stream_skip (GInputStream *stream,
57 GCancellable *cancellable,
59 static gboolean g_memory_input_stream_close (GInputStream *stream,
60 GCancellable *cancellable,
62 static void g_memory_input_stream_read_async (GInputStream *stream,
66 GCancellable *cancellable,
67 GAsyncReadyCallback callback,
69 static gssize g_memory_input_stream_read_finish (GInputStream *stream,
72 static void g_memory_input_stream_skip_async (GInputStream *stream,
75 GCancellable *cancellabl,
76 GAsyncReadyCallback callback,
78 static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
81 static void g_memory_input_stream_close_async (GInputStream *stream,
83 GCancellable *cancellabl,
84 GAsyncReadyCallback callback,
86 static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
90 static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
91 static goffset g_memory_input_stream_tell (GSeekable *seekable);
92 static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
93 static gboolean g_memory_input_stream_seek (GSeekable *seekable,
96 GCancellable *cancellable,
98 static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
99 static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
101 GCancellable *cancellable,
103 static void g_memory_input_stream_finalize (GObject *object);
105 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
106 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
107 g_memory_input_stream_seekable_iface_init))
111 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
113 GObjectClass *object_class;
114 GInputStreamClass *istream_class;
116 g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
118 object_class = G_OBJECT_CLASS (klass);
119 object_class->finalize = g_memory_input_stream_finalize;
121 istream_class = G_INPUT_STREAM_CLASS (klass);
122 istream_class->read = g_memory_input_stream_read;
123 istream_class->skip = g_memory_input_stream_skip;
124 istream_class->close = g_memory_input_stream_close;
126 istream_class->read_async = g_memory_input_stream_read_async;
127 istream_class->read_finish = g_memory_input_stream_read_finish;
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;
139 stream = G_MEMORY_INPUT_STREAM (object);
141 if (stream->priv->free_data)
142 g_free (stream->priv->buffer);
144 if (G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize)
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 = g_memory_input_stream_truncate;
159 g_memory_input_stream_init (GMemoryInputStream *stream)
161 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
162 G_TYPE_MEMORY_INPUT_STREAM,
163 GMemoryInputStreamPrivate);
167 * g_memory_input_stream_set_free_data:
173 g_memory_input_stream_set_free_data (GMemoryInputStream *stream,
176 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
178 stream->priv->free_data = free_data;
182 * g_memory_input_stream_from_data:
184 * @len: length of the data.
186 * Returns: new #GInputStream read from @data of @len bytes.
189 g_memory_input_stream_from_data (const void *data,
192 GInputStream *stream;
193 GMemoryInputStream *memory_stream;
195 g_return_val_if_fail (data != NULL, NULL);
197 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
198 memory_stream = G_MEMORY_INPUT_STREAM (stream);
203 memory_stream->priv->buffer = (guint8 *)data;
204 memory_stream->priv->len = len;
210 g_memory_input_stream_read (GInputStream *stream,
213 GCancellable *cancellable,
216 GMemoryInputStream *memory_stream;
217 GMemoryInputStreamPrivate * priv;
219 memory_stream = G_MEMORY_INPUT_STREAM (stream);
220 priv = memory_stream->priv;
222 count = MIN (count, priv->len - priv->pos);
223 memcpy (buffer, priv->buffer + priv->pos, count);
230 * g_memory_input_stream_get_data:
236 g_memory_input_stream_get_data (GMemoryInputStream *stream)
238 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), NULL);
240 return stream->priv->buffer;
244 * g_memory_input_stream_get_data_size:
250 g_memory_input_stream_get_data_size (GMemoryInputStream *stream)
252 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), -1);
254 return stream->priv->len;
258 g_memory_input_stream_skip (GInputStream *stream,
260 GCancellable *cancellable,
263 GMemoryInputStream *memory_stream;
264 GMemoryInputStreamPrivate *priv;
266 memory_stream = G_MEMORY_INPUT_STREAM (stream);
267 priv = memory_stream->priv;
269 count = MIN (count, priv->len - priv->pos);
278 g_memory_input_stream_close (GInputStream *stream,
279 GCancellable *cancellable,
286 g_memory_input_stream_read_async (GInputStream *stream,
290 GCancellable *cancellable,
291 GAsyncReadyCallback callback,
294 GSimpleAsyncResult *simple;
297 nread = g_memory_input_stream_read (stream, buffer, count, cancellable, NULL);
298 simple = g_simple_async_result_new (G_OBJECT (stream),
301 g_memory_input_stream_read_async);
302 g_simple_async_result_set_op_res_gssize (simple, nread);
303 g_simple_async_result_complete_in_idle (simple);
304 g_object_unref (simple);
308 g_memory_input_stream_read_finish (GInputStream *stream,
309 GAsyncResult *result,
312 GSimpleAsyncResult *simple;
315 simple = G_SIMPLE_ASYNC_RESULT (result);
316 g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
318 nread = g_simple_async_result_get_op_res_gssize (simple);
323 g_memory_input_stream_skip_async (GInputStream *stream,
326 GCancellable *cancellable,
327 GAsyncReadyCallback callback,
330 GSimpleAsyncResult *simple;
333 nskipped = g_memory_input_stream_skip (stream, count, cancellable, NULL);
334 simple = g_simple_async_result_new (G_OBJECT (stream),
337 g_memory_input_stream_skip_async);
338 g_simple_async_result_set_op_res_gssize (simple, nskipped);
339 g_simple_async_result_complete_in_idle (simple);
340 g_object_unref (simple);
344 g_memory_input_stream_skip_finish (GInputStream *stream,
345 GAsyncResult *result,
348 GSimpleAsyncResult *simple;
351 simple = G_SIMPLE_ASYNC_RESULT (result);
352 g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
354 nskipped = g_simple_async_result_get_op_res_gssize (simple);
359 g_memory_input_stream_close_async (GInputStream *stream,
361 GCancellable *cancellable,
362 GAsyncReadyCallback callback,
365 GSimpleAsyncResult *simple;
367 simple = g_simple_async_result_new (G_OBJECT (stream),
370 g_memory_input_stream_close_async);
371 g_simple_async_result_complete_in_idle (simple);
372 g_object_unref (simple);
376 g_memory_input_stream_close_finish (GInputStream *stream,
377 GAsyncResult *result,
384 g_memory_input_stream_tell (GSeekable *seekable)
386 GMemoryInputStream *memory_stream;
387 GMemoryInputStreamPrivate * priv;
389 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
390 priv = memory_stream->priv;
396 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
402 g_memory_input_stream_seek (GSeekable *seekable,
405 GCancellable *cancellable,
408 GMemoryInputStream *memory_stream;
409 GMemoryInputStreamPrivate * priv;
412 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
413 priv = memory_stream->priv;
418 absolute = priv->pos + offset;
426 absolute = priv->len + offset;
432 G_IO_ERROR_INVALID_ARGUMENT,
433 "Invalid GSeekType supplied");
438 if (absolute < 0 || absolute > priv->len)
442 G_IO_ERROR_INVALID_ARGUMENT,
443 "Invalid seek request");
447 priv->pos = absolute;
453 g_memory_input_stream_can_truncate (GSeekable *seekable)
459 g_memory_input_stream_truncate (GSeekable *seekable,
461 GCancellable *cancellable,
466 G_IO_ERROR_NOT_SUPPORTED,
467 "Cannot seek on GMemoryInputStream");
471 #define __G_MEMORY_INPUT_STREAM_C__
472 #include "gioaliasdef.c"