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, gssize len)
191 GInputStream *stream;
192 GMemoryInputStream *memory_stream;
194 g_return_val_if_fail (data != NULL, NULL);
196 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
197 memory_stream = G_MEMORY_INPUT_STREAM (stream);
202 memory_stream->priv->buffer = (guint8 *)data;
203 memory_stream->priv->len = len;
209 g_memory_input_stream_read (GInputStream *stream,
212 GCancellable *cancellable,
215 GMemoryInputStream *memory_stream;
216 GMemoryInputStreamPrivate * priv;
218 memory_stream = G_MEMORY_INPUT_STREAM (stream);
219 priv = memory_stream->priv;
221 count = MIN (count, priv->len - priv->pos);
222 memcpy (buffer, priv->buffer + priv->pos, count);
229 * g_memory_input_stream_get_data:
235 g_memory_input_stream_get_data (GMemoryInputStream *stream)
237 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), NULL);
239 return stream->priv->buffer;
243 * g_memory_input_stream_get_data_size:
249 g_memory_input_stream_get_data_size (GMemoryInputStream *stream)
251 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), -1);
253 return stream->priv->len;
257 g_memory_input_stream_skip (GInputStream *stream,
259 GCancellable *cancellable,
262 GMemoryInputStream *memory_stream;
263 GMemoryInputStreamPrivate *priv;
265 memory_stream = G_MEMORY_INPUT_STREAM (stream);
266 priv = memory_stream->priv;
268 count = MIN (count, priv->len - priv->pos);
277 g_memory_input_stream_close (GInputStream *stream,
278 GCancellable *cancellable,
285 g_memory_input_stream_read_async (GInputStream *stream,
289 GCancellable *cancellable,
290 GAsyncReadyCallback callback,
293 GSimpleAsyncResult *simple;
296 nread = g_memory_input_stream_read (stream, buffer, count, cancellable, NULL);
297 simple = g_simple_async_result_new (G_OBJECT (stream),
300 g_memory_input_stream_read_async);
301 g_simple_async_result_set_op_res_gssize (simple, nread);
302 g_simple_async_result_complete_in_idle (simple);
303 g_object_unref (simple);
307 g_memory_input_stream_read_finish (GInputStream *stream,
308 GAsyncResult *result,
311 GSimpleAsyncResult *simple;
314 simple = G_SIMPLE_ASYNC_RESULT (result);
315 g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
317 nread = g_simple_async_result_get_op_res_gssize (simple);
322 g_memory_input_stream_skip_async (GInputStream *stream,
325 GCancellable *cancellable,
326 GAsyncReadyCallback callback,
329 GSimpleAsyncResult *simple;
332 nskipped = g_memory_input_stream_skip (stream, count, cancellable, NULL);
333 simple = g_simple_async_result_new (G_OBJECT (stream),
336 g_memory_input_stream_skip_async);
337 g_simple_async_result_set_op_res_gssize (simple, nskipped);
338 g_simple_async_result_complete_in_idle (simple);
339 g_object_unref (simple);
343 g_memory_input_stream_skip_finish (GInputStream *stream,
344 GAsyncResult *result,
347 GSimpleAsyncResult *simple;
350 simple = G_SIMPLE_ASYNC_RESULT (result);
351 g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
353 nskipped = g_simple_async_result_get_op_res_gssize (simple);
358 g_memory_input_stream_close_async (GInputStream *stream,
360 GCancellable *cancellable,
361 GAsyncReadyCallback callback,
364 GSimpleAsyncResult *simple;
366 simple = g_simple_async_result_new (G_OBJECT (stream),
369 g_memory_input_stream_close_async);
370 g_simple_async_result_complete_in_idle (simple);
371 g_object_unref (simple);
375 g_memory_input_stream_close_finish (GInputStream *stream,
376 GAsyncResult *result,
383 g_memory_input_stream_tell (GSeekable *seekable)
385 GMemoryInputStream *memory_stream;
386 GMemoryInputStreamPrivate * priv;
388 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
389 priv = memory_stream->priv;
395 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
401 g_memory_input_stream_seek (GSeekable *seekable,
404 GCancellable *cancellable,
407 GMemoryInputStream *memory_stream;
408 GMemoryInputStreamPrivate * priv;
411 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
412 priv = memory_stream->priv;
417 absolute = priv->pos + offset;
425 absolute = priv->len + offset;
431 G_IO_ERROR_INVALID_ARGUMENT,
432 "Invalid GSeekType supplied");
437 if (absolute < 0 || absolute > priv->len)
441 G_IO_ERROR_INVALID_ARGUMENT,
442 "Invalid seek request");
446 priv->pos = absolute;
452 g_memory_input_stream_can_truncate (GSeekable *seekable)
458 g_memory_input_stream_truncate (GSeekable *seekable,
460 GCancellable *cancellable,
465 G_IO_ERROR_NOT_SUPPORTED,
466 "Cannot seek on GMemoryInputStream");
470 #define __G_MEMORY_INPUT_STREAM_C__
471 #include "gioaliasdef.c"
473 /* vim: ts=2 sw=2 et */