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>
25 #include "gmemoryinputstream.h"
26 #include "ginputstream.h"
27 #include "gseekable.h"
29 #include "gsimpleasyncresult.h"
33 struct _GMemoryInputStreamPrivate {
40 static gssize g_memory_input_stream_read (GInputStream *stream,
43 GCancellable *cancellable,
45 static gssize g_memory_input_stream_skip (GInputStream *stream,
47 GCancellable *cancellable,
49 static gboolean g_memory_input_stream_close (GInputStream *stream,
50 GCancellable *cancellable,
52 static void g_memory_input_stream_read_async (GInputStream *stream,
56 GCancellable *cancellable,
57 GAsyncReadyCallback callback,
59 static gssize g_memory_input_stream_read_finish (GInputStream *stream,
62 static void g_memory_input_stream_skip_async (GInputStream *stream,
65 GCancellable *cancellabl,
66 GAsyncReadyCallback callback,
68 static gssize g_memory_input_stream_skip_finish (GInputStream *stream,
71 static void g_memory_input_stream_close_async (GInputStream *stream,
73 GCancellable *cancellabl,
74 GAsyncReadyCallback callback,
76 static gboolean g_memory_input_stream_close_finish (GInputStream *stream,
80 static void g_memory_input_stream_seekable_iface_init (GSeekableIface *iface);
81 static goffset g_memory_input_stream_tell (GSeekable *seekable);
82 static gboolean g_memory_input_stream_can_seek (GSeekable *seekable);
83 static gboolean g_memory_input_stream_seek (GSeekable *seekable,
86 GCancellable *cancellable,
88 static gboolean g_memory_input_stream_can_truncate (GSeekable *seekable);
89 static gboolean g_memory_input_stream_truncate (GSeekable *seekable,
91 GCancellable *cancellable,
93 static void g_memory_input_stream_finalize (GObject *object);
95 G_DEFINE_TYPE_WITH_CODE (GMemoryInputStream, g_memory_input_stream, G_TYPE_INPUT_STREAM,
96 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
97 g_memory_input_stream_seekable_iface_init))
101 g_memory_input_stream_class_init (GMemoryInputStreamClass *klass)
103 GObjectClass *object_class;
104 GInputStreamClass *istream_class;
106 g_type_class_add_private (klass, sizeof (GMemoryInputStreamPrivate));
108 object_class = G_OBJECT_CLASS (klass);
109 object_class->finalize = g_memory_input_stream_finalize;
111 istream_class = G_INPUT_STREAM_CLASS (klass);
112 istream_class->read = g_memory_input_stream_read;
113 istream_class->skip = g_memory_input_stream_skip;
114 istream_class->close = g_memory_input_stream_close;
116 istream_class->read_async = g_memory_input_stream_read_async;
117 istream_class->read_finish = g_memory_input_stream_read_finish;
118 istream_class->skip_async = g_memory_input_stream_skip_async;
119 istream_class->skip_finish = g_memory_input_stream_skip_finish;
120 istream_class->close_async = g_memory_input_stream_close_async;
121 istream_class->close_finish = g_memory_input_stream_close_finish;
125 g_memory_input_stream_finalize (GObject *object)
127 GMemoryInputStream *stream;
129 stream = G_MEMORY_INPUT_STREAM (object);
131 if (stream->priv->free_data)
132 g_free (stream->priv->buffer);
134 if (G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize)
135 (*G_OBJECT_CLASS (g_memory_input_stream_parent_class)->finalize) (object);
139 g_memory_input_stream_seekable_iface_init (GSeekableIface *iface)
141 iface->tell = g_memory_input_stream_tell;
142 iface->can_seek = g_memory_input_stream_can_seek;
143 iface->seek = g_memory_input_stream_seek;
144 iface->can_truncate = g_memory_input_stream_can_truncate;
145 iface->truncate = g_memory_input_stream_truncate;
149 g_memory_input_stream_init (GMemoryInputStream *stream)
151 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
152 G_TYPE_MEMORY_INPUT_STREAM,
153 GMemoryInputStreamPrivate);
157 * g_memory_input_stream_set_free_data:
163 g_memory_input_stream_set_free_data (GMemoryInputStream *stream,
166 g_return_if_fail (G_IS_MEMORY_INPUT_STREAM (stream));
168 stream->priv->free_data = free_data;
172 * g_memory_input_stream_from_data:
174 * @len: length of the data.
176 * Returns: new #GInputStream read from @data of @len bytes.
179 g_memory_input_stream_from_data (const void *data, gssize len)
181 GInputStream *stream;
182 GMemoryInputStream *memory_stream;
184 g_return_val_if_fail (data != NULL, NULL);
186 stream = g_object_new (G_TYPE_MEMORY_INPUT_STREAM, NULL);
187 memory_stream = G_MEMORY_INPUT_STREAM (stream);
192 memory_stream->priv->buffer = (guint8 *)data;
193 memory_stream->priv->len = len;
199 g_memory_input_stream_read (GInputStream *stream,
202 GCancellable *cancellable,
205 GMemoryInputStream *memory_stream;
206 GMemoryInputStreamPrivate * priv;
208 memory_stream = G_MEMORY_INPUT_STREAM (stream);
209 priv = memory_stream->priv;
211 count = MIN (count, priv->len - priv->pos);
212 memcpy (buffer, priv->buffer + priv->pos, count);
219 * g_memory_input_stream_get_data:
225 g_memory_input_stream_get_data (GMemoryInputStream *stream)
227 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), NULL);
229 return stream->priv->buffer;
233 * g_memory_input_stream_get_data_size:
239 g_memory_input_stream_get_data_size (GMemoryInputStream *stream)
241 g_return_val_if_fail (G_IS_MEMORY_INPUT_STREAM (stream), -1);
243 return stream->priv->len;
247 g_memory_input_stream_skip (GInputStream *stream,
249 GCancellable *cancellable,
252 GMemoryInputStream *memory_stream;
253 GMemoryInputStreamPrivate *priv;
255 memory_stream = G_MEMORY_INPUT_STREAM (stream);
256 priv = memory_stream->priv;
258 count = MIN (count, priv->len - priv->pos);
267 g_memory_input_stream_close (GInputStream *stream,
268 GCancellable *cancellable,
275 g_memory_input_stream_read_async (GInputStream *stream,
279 GCancellable *cancellable,
280 GAsyncReadyCallback callback,
283 GSimpleAsyncResult *simple;
286 nread = g_memory_input_stream_read (stream, buffer, count, cancellable, NULL);
287 simple = g_simple_async_result_new (G_OBJECT (stream),
290 g_memory_input_stream_read_async);
291 g_simple_async_result_set_op_res_gssize (simple, nread);
292 g_simple_async_result_complete_in_idle (simple);
293 g_object_unref (simple);
297 g_memory_input_stream_read_finish (GInputStream *stream,
298 GAsyncResult *result,
301 GSimpleAsyncResult *simple;
304 simple = G_SIMPLE_ASYNC_RESULT (result);
305 g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_read_async);
307 nread = g_simple_async_result_get_op_res_gssize (simple);
312 g_memory_input_stream_skip_async (GInputStream *stream,
315 GCancellable *cancellable,
316 GAsyncReadyCallback callback,
319 GSimpleAsyncResult *simple;
322 nskipped = g_memory_input_stream_skip (stream, count, cancellable, NULL);
323 simple = g_simple_async_result_new (G_OBJECT (stream),
326 g_memory_input_stream_skip_async);
327 g_simple_async_result_set_op_res_gssize (simple, nskipped);
328 g_simple_async_result_complete_in_idle (simple);
329 g_object_unref (simple);
333 g_memory_input_stream_skip_finish (GInputStream *stream,
334 GAsyncResult *result,
337 GSimpleAsyncResult *simple;
340 simple = G_SIMPLE_ASYNC_RESULT (result);
341 g_assert (g_simple_async_result_get_source_tag (simple) == g_memory_input_stream_skip_async);
343 nskipped = g_simple_async_result_get_op_res_gssize (simple);
348 g_memory_input_stream_close_async (GInputStream *stream,
350 GCancellable *cancellable,
351 GAsyncReadyCallback callback,
354 GSimpleAsyncResult *simple;
356 simple = g_simple_async_result_new (G_OBJECT (stream),
359 g_memory_input_stream_close_async);
360 g_simple_async_result_complete_in_idle (simple);
361 g_object_unref (simple);
365 g_memory_input_stream_close_finish (GInputStream *stream,
366 GAsyncResult *result,
373 g_memory_input_stream_tell (GSeekable *seekable)
375 GMemoryInputStream *memory_stream;
376 GMemoryInputStreamPrivate * priv;
378 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
379 priv = memory_stream->priv;
385 gboolean g_memory_input_stream_can_seek (GSeekable *seekable)
391 g_memory_input_stream_seek (GSeekable *seekable,
394 GCancellable *cancellable,
397 GMemoryInputStream *memory_stream;
398 GMemoryInputStreamPrivate * priv;
401 memory_stream = G_MEMORY_INPUT_STREAM (seekable);
402 priv = memory_stream->priv;
407 absolute = priv->pos + offset;
415 absolute = priv->len + offset;
421 G_IO_ERROR_INVALID_ARGUMENT,
422 "Invalid GSeekType supplied");
427 if (absolute < 0 || absolute > priv->len)
431 G_IO_ERROR_INVALID_ARGUMENT,
432 "Invalid seek request");
436 priv->pos = absolute;
442 g_memory_input_stream_can_truncate (GSeekable *seekable)
448 g_memory_input_stream_truncate (GSeekable *seekable,
450 GCancellable *cancellable,
455 G_IO_ERROR_NOT_SUPPORTED,
456 "Cannot seek on GMemoryInputStream");
460 /* vim: ts=2 sw=2 et */