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_fn = g_memory_input_stream_read;
123 istream_class->skip = g_memory_input_stream_skip;
124 istream_class->close_fn = 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_fn = 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:
168 * @stream: a #GMemoryInputStream.
169 * @free_data: a #gboolean. If %TRUE, frees the data within @stream.
171 * Sets if the data within the @stream should be freed when the stream
175 g_memory_input_stream_set_free_data (GMemoryInputStream *stream,
178 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
180 stream->priv->free_data = free_data;
184 * g_memory_input_stream_from_data:
186 * @len: length of the data.
188 * Creates a new #GMemoryInputStream with data in memory of a given size.
190 * Returns: new #GInputStream read from @data of @len bytes.
193 g_memory_input_stream_from_data (const void *data,
196 GInputStream *stream;
197 GMemoryInputStream *memory_stream;
199 g_return_val_if_fail (data != NULL, NULL);
201 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
202 memory_stream = G_MEMORY_INPUT_STREAM (stream);
207 memory_stream->priv->buffer = (guint8 *)data;
208 memory_stream->priv->len = len;
214 g_memory_input_stream_read (GInputStream *stream,
217 GCancellable *cancellable,
220 GMemoryInputStream *memory_stream;
221 GMemoryInputStreamPrivate * priv;
223 memory_stream = G_MEMORY_INPUT_STREAM (stream);
224 priv = memory_stream->priv;
226 count = MIN (count, priv->len - priv->pos);
227 memcpy (buffer, priv->buffer + priv->pos, count);
234 * g_memory_input_stream_get_data:
235 * @stream: a #GMemoryInputStream
237 * Gets a pointer to the data within the #GMemoryInputStream.
239 * Returns: a pointer to the memory in the @stream.
242 g_memory_input_stream_get_data (GMemoryInputStream *stream)
244 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), NULL);
246 return stream->priv->buffer;
250 * g_memory_input_stream_get_data_size:
251 * @stream: a #GMemoryInputStream
253 * Gets the size of the data within the #GMemoryInputStream.
255 * Returns: a gsize with the size of the data in @stream, or -1
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_assert (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_assert (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"