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 "gmemoryoutputstream.h"
25 #include "goutputstream.h"
26 #include "gseekable.h"
27 #include "gsimpleasyncresult.h"
35 * SECTION:gmemoryoutputstream
36 * @short_description: Streaming output operations on memory chunks
38 * @see_also: #GMemoryInputStream
40 * #GMemoryOutputStream is a class for using arbitrary
41 * memory chunks as output for GIO streaming output operations.
45 #define MIN_ARRAY_SIZE 16
47 struct _GMemoryOutputStreamPrivate {
51 gsize valid_len; /* The part of data that has been written to */
55 GReallocFunc realloc_fn;
56 GDestroyNotify destroy;
59 static void g_memory_output_stream_finalize (GObject *object);
61 static gssize g_memory_output_stream_write (GOutputStream *stream,
64 GCancellable *cancellable,
67 static gboolean g_memory_output_stream_close (GOutputStream *stream,
68 GCancellable *cancellable,
71 static void g_memory_output_stream_write_async (GOutputStream *stream,
75 GCancellable *cancellable,
76 GAsyncReadyCallback callback,
78 static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
81 static void g_memory_output_stream_close_async (GOutputStream *stream,
83 GCancellable *cancellable,
84 GAsyncReadyCallback callback,
86 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
90 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
91 static goffset g_memory_output_stream_tell (GSeekable *seekable);
92 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
93 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
96 GCancellable *cancellable,
98 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
99 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
101 GCancellable *cancellable,
104 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
105 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
106 g_memory_output_stream_seekable_iface_init))
110 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
112 GOutputStreamClass *ostream_class;
113 GObjectClass *gobject_class;
115 g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
117 gobject_class = G_OBJECT_CLASS (klass);
118 gobject_class->finalize = g_memory_output_stream_finalize;
120 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
122 ostream_class->write_fn = g_memory_output_stream_write;
123 ostream_class->close_fn = g_memory_output_stream_close;
124 ostream_class->write_async = g_memory_output_stream_write_async;
125 ostream_class->write_finish = g_memory_output_stream_write_finish;
126 ostream_class->close_async = g_memory_output_stream_close_async;
127 ostream_class->close_finish = g_memory_output_stream_close_finish;
131 g_memory_output_stream_finalize (GObject *object)
133 GMemoryOutputStream *stream;
134 GMemoryOutputStreamPrivate *priv;
136 stream = G_MEMORY_OUTPUT_STREAM (object);
140 priv->destroy (priv->data);
142 G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
146 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
148 iface->tell = g_memory_output_stream_tell;
149 iface->can_seek = g_memory_output_stream_can_seek;
150 iface->seek = g_memory_output_stream_seek;
151 iface->can_truncate = g_memory_output_stream_can_truncate;
152 iface->truncate_fn = g_memory_output_stream_truncate;
157 g_memory_output_stream_init (GMemoryOutputStream *stream)
159 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
160 G_TYPE_MEMORY_OUTPUT_STREAM,
161 GMemoryOutputStreamPrivate);
165 * g_memory_output_stream_new:
166 * @data: pointer to a chunk of memory to use, or %NULL
167 * @len: the size of @data
168 * @realloc_fn: a function with realloc() semantics to be called when
169 * @data needs to be grown, or %NULL
170 * @destroy: a function to be called on @data when the stream is finalized,
173 * Creates a new #GMemoryOutputStream.
175 * If @data is non-%NULL, the stream will use that for its internal storage.
176 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
177 * storage when necessary. To construct a fixed-size output stream,
178 * pass %NULL as @realloc_fn.
180 * /* a stream that can grow */
181 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
183 * /* a fixed-size stream */
184 * data = malloc (200);
185 * stream2 = g_memory_output_stream_new (data, 200, NULL, free);
188 * Return value: A newly created #GMemoryOutputStream object.
191 g_memory_output_stream_new (gpointer data,
193 GReallocFunc realloc_fn,
194 GDestroyNotify destroy)
196 GOutputStream *stream;
197 GMemoryOutputStreamPrivate *priv;
199 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM, NULL);
201 priv = G_MEMORY_OUTPUT_STREAM (stream)->priv;
205 priv->realloc_fn = realloc_fn;
206 priv->destroy = destroy;
215 * g_memory_output_stream_get_data:
216 * @ostream: a #GMemoryOutputStream
218 * Gets any loaded data from the @ostream.
220 * Note that the returned pointer may become invalid on the next
221 * write or truncate operation on the stream.
223 * Returns: pointer to the stream's data
226 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
228 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
230 return ostream->priv->data;
234 * g_memory_output_stream_get_size:
235 * @ostream: a #GMemoryOutputStream
237 * Gets the size of the currently allocated data area (availible from
238 * g_memory_output_stream_get_data()). If the stream isn't
239 * growable (no realloc was passed to g_memory_output_stream_new()) then
240 * this is the maximum size of the stream and further writes
241 * will return %G_IO_ERROR_NO_SPACE.
243 * Note that for growable streams the returned size may become invalid on
244 * the next write or truncate operation on the stream.
246 * If you want the number of bytes currently written to the stream, use
247 * g_memory_output_stream_get_data_size().
249 * Returns: the number of bytes allocated for the data buffer
252 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
254 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
256 return ostream->priv->len;
260 * g_memory_output_stream_get_data_size:
261 * @ostream: a #GMemoryOutputStream
263 * Returns the number of bytes from the start up
264 * to including the last byte written in the stream
265 * that has not been truncated away.
267 * Returns: the number of bytes written to the stream
272 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
274 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
276 return ostream->priv->valid_len;
281 array_check_boundary (GMemoryOutputStream *stream,
285 if (size > G_MAXUINT)
287 g_set_error_literal (error,
290 _("Reached maximum data array limit"));
299 array_resize (GMemoryOutputStream *ostream,
301 gboolean allow_partial,
304 GMemoryOutputStreamPrivate *priv;
308 priv = ostream->priv;
310 if (!array_check_boundary (ostream, size, error))
313 if (priv->len == size)
316 if (!priv->realloc_fn)
319 priv->pos < priv->len)
320 return TRUE; /* Short write */
322 g_set_error_literal (error,
325 _("Memory output stream not resizable"));
330 data = priv->realloc_fn (priv->data, size);
332 if (size > 0 && !data)
335 priv->pos < priv->len)
336 return TRUE; /* Short write */
338 g_set_error_literal (error,
341 _("Failed to resize memory output stream"));
346 memset ((guint8 *)data + len, 0, size - len);
351 if (priv->len < priv->valid_len)
352 priv->valid_len = priv->len;
358 g_nearest_pow (gint num)
369 g_memory_output_stream_write (GOutputStream *stream,
372 GCancellable *cancellable,
375 GMemoryOutputStream *ostream;
376 GMemoryOutputStreamPrivate *priv;
380 ostream = G_MEMORY_OUTPUT_STREAM (stream);
381 priv = ostream->priv;
386 if (priv->pos + count > priv->len)
388 /* At least enought to fit the write, rounded up
389 for greater than linear growth */
390 new_size = g_nearest_pow (priv->pos + count);
391 new_size = MAX (new_size, MIN_ARRAY_SIZE);
393 if (!array_resize (ostream, new_size, TRUE, error))
397 /* Make sure we handle short writes if the array_resize
398 only added part of the required memory */
399 count = MIN (count, priv->len - priv->pos);
401 dest = (guint8 *)priv->data + priv->pos;
402 memcpy (dest, buffer, count);
405 if (priv->pos > priv->valid_len)
406 priv->valid_len = priv->pos;
412 g_memory_output_stream_close (GOutputStream *stream,
413 GCancellable *cancellable,
420 g_memory_output_stream_write_async (GOutputStream *stream,
424 GCancellable *cancellable,
425 GAsyncReadyCallback callback,
428 GSimpleAsyncResult *simple;
431 nwritten = g_memory_output_stream_write (stream,
438 simple = g_simple_async_result_new (G_OBJECT (stream),
441 g_memory_output_stream_write_async);
443 g_simple_async_result_set_op_res_gssize (simple, nwritten);
444 g_simple_async_result_complete_in_idle (simple);
445 g_object_unref (simple);
449 g_memory_output_stream_write_finish (GOutputStream *stream,
450 GAsyncResult *result,
453 GSimpleAsyncResult *simple;
456 simple = G_SIMPLE_ASYNC_RESULT (result);
458 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
459 g_memory_output_stream_write_async);
461 nwritten = g_simple_async_result_get_op_res_gssize (simple);
467 g_memory_output_stream_close_async (GOutputStream *stream,
469 GCancellable *cancellable,
470 GAsyncReadyCallback callback,
473 GSimpleAsyncResult *simple;
475 simple = g_simple_async_result_new (G_OBJECT (stream),
478 g_memory_output_stream_close_async);
481 /* will always return TRUE */
482 g_memory_output_stream_close (stream, cancellable, NULL);
484 g_simple_async_result_complete_in_idle (simple);
485 g_object_unref (simple);
489 g_memory_output_stream_close_finish (GOutputStream *stream,
490 GAsyncResult *result,
493 GSimpleAsyncResult *simple;
495 simple = G_SIMPLE_ASYNC_RESULT (result);
497 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
498 g_memory_output_stream_close_async);
504 g_memory_output_stream_tell (GSeekable *seekable)
506 GMemoryOutputStream *stream;
507 GMemoryOutputStreamPrivate *priv;
509 stream = G_MEMORY_OUTPUT_STREAM (seekable);
516 g_memory_output_stream_can_seek (GSeekable *seekable)
522 g_memory_output_stream_seek (GSeekable *seekable,
525 GCancellable *cancellable,
528 GMemoryOutputStream *stream;
529 GMemoryOutputStreamPrivate *priv;
532 stream = G_MEMORY_OUTPUT_STREAM (seekable);
538 absolute = priv->pos + offset;
546 absolute = priv->len + offset;
550 g_set_error_literal (error,
552 G_IO_ERROR_INVALID_ARGUMENT,
553 _("Invalid GSeekType supplied"));
560 g_set_error_literal (error,
562 G_IO_ERROR_INVALID_ARGUMENT,
563 _("Invalid seek request"));
567 if (!array_check_boundary (stream, absolute, error))
570 priv->pos = absolute;
576 g_memory_output_stream_can_truncate (GSeekable *seekable)
578 GMemoryOutputStream *ostream;
579 GMemoryOutputStreamPrivate *priv;
581 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
582 priv = ostream->priv;
584 return priv->realloc_fn != NULL;
588 g_memory_output_stream_truncate (GSeekable *seekable,
590 GCancellable *cancellable,
593 GMemoryOutputStream *ostream;
594 GMemoryOutputStreamPrivate *priv;
596 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
597 priv = ostream->priv;
599 if (!array_resize (ostream, offset, FALSE, error))
605 #define __G_MEMORY_OUTPUT_STREAM_C__
606 #include "gioaliasdef.c"