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: Alexander Larsson <alexl@redhat.com>
25 #include "gdataoutputstream.h"
26 #include "gseekable.h"
27 #include "gioenumtypes.h"
33 * SECTION:gdataoutputstream
34 * @short_description: Data Output Stream
36 * @see_also: #GOutputStream
38 * Data output stream implements #GOutputStream and includes functions for
39 * writing data directly to an output stream.
45 struct _GDataOutputStreamPrivate {
46 GDataStreamByteOrder byte_order;
54 static void g_data_output_stream_set_property (GObject *object,
58 static void g_data_output_stream_get_property (GObject *object,
63 static void g_data_output_stream_seekable_iface_init (GSeekableIface *iface);
64 static goffset g_data_output_stream_tell (GSeekable *seekable);
65 static gboolean g_data_output_stream_can_seek (GSeekable *seekable);
66 static gboolean g_data_output_stream_seek (GSeekable *seekable,
69 GCancellable *cancellable,
71 static gboolean g_data_output_stream_can_truncate (GSeekable *seekable);
72 static gboolean g_data_output_stream_truncate (GSeekable *seekable,
74 GCancellable *cancellable,
77 G_DEFINE_TYPE_WITH_CODE (GDataOutputStream,
79 G_TYPE_FILTER_OUTPUT_STREAM,
80 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
81 g_data_output_stream_seekable_iface_init))
85 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
87 GObjectClass *object_class;
89 g_type_class_add_private (klass, sizeof (GDataOutputStreamPrivate));
91 object_class = G_OBJECT_CLASS (klass);
92 object_class->get_property = g_data_output_stream_get_property;
93 object_class->set_property = g_data_output_stream_set_property;
96 * GDataOutputStream:byte-order:
98 * Determines the byte ordering that is used when writing
99 * multi-byte entities (such as integers) to the stream.
101 g_object_class_install_property (object_class,
103 g_param_spec_enum ("byte-order",
105 P_("The byte order"),
106 G_TYPE_DATA_STREAM_BYTE_ORDER,
107 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
108 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
113 g_data_output_stream_set_property (GObject *object,
118 GDataOutputStream *dstream;
120 dstream = G_DATA_OUTPUT_STREAM (object);
124 case PROP_BYTE_ORDER:
125 g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 g_data_output_stream_get_property (GObject *object,
140 GDataOutputStreamPrivate *priv;
141 GDataOutputStream *dstream;
143 dstream = G_DATA_OUTPUT_STREAM (object);
144 priv = dstream->priv;
148 case PROP_BYTE_ORDER:
149 g_value_set_enum (value, priv->byte_order);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 g_data_output_stream_init (GDataOutputStream *stream)
161 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
162 G_TYPE_DATA_OUTPUT_STREAM,
163 GDataOutputStreamPrivate);
165 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
169 g_data_output_stream_seekable_iface_init (GSeekableIface *iface)
171 iface->tell = g_data_output_stream_tell;
172 iface->can_seek = g_data_output_stream_can_seek;
173 iface->seek = g_data_output_stream_seek;
174 iface->can_truncate = g_data_output_stream_can_truncate;
175 iface->truncate_fn = g_data_output_stream_truncate;
179 * g_data_output_stream_new:
180 * @base_stream: a #GOutputStream.
182 * Creates a new data output stream for @base_stream.
184 * Returns: #GDataOutputStream.
187 g_data_output_stream_new (GOutputStream *base_stream)
189 GDataOutputStream *stream;
191 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
193 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
194 "base-stream", base_stream,
201 * g_data_output_stream_set_byte_order:
202 * @stream: a #GDataOutputStream.
203 * @order: a %GDataStreamByteOrder.
205 * Sets the byte order of the data output stream to @order.
208 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
209 GDataStreamByteOrder order)
211 GDataOutputStreamPrivate *priv;
212 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
214 if (priv->byte_order != order)
216 priv->byte_order = order;
217 g_object_notify (G_OBJECT (stream), "byte-order");
222 * g_data_output_stream_get_byte_order:
223 * @stream: a #GDataOutputStream.
225 * Gets the byte order for the stream.
227 * Returns: the #GDataStreamByteOrder for the @stream.
230 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
232 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
234 return stream->priv->byte_order;
238 * g_data_output_stream_put_byte:
239 * @stream: a #GDataOutputStream.
241 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
242 * @error: a #GError, %NULL to ignore.
244 * Puts a byte into the output stream.
246 * Returns: %TRUE if @data was successfully added to the @stream.
249 g_data_output_stream_put_byte (GDataOutputStream *stream,
251 GCancellable *cancellable,
256 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
258 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
265 * g_data_output_stream_put_int16:
266 * @stream: a #GDataOutputStream.
268 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
269 * @error: a #GError, %NULL to ignore.
271 * Puts a signed 16-bit integer into the output stream.
273 * Returns: %TRUE if @data was successfully added to the @stream.
276 g_data_output_stream_put_int16 (GDataOutputStream *stream,
278 GCancellable *cancellable,
283 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
285 switch (stream->priv->byte_order)
287 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
288 data = GINT16_TO_BE (data);
290 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
291 data = GINT16_TO_LE (data);
293 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
298 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
305 * g_data_output_stream_put_uint16:
306 * @stream: a #GDataOutputStream.
308 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
309 * @error: a #GError, %NULL to ignore.
311 * Puts an unsigned 16-bit integer into the output stream.
313 * Returns: %TRUE if @data was successfully added to the @stream.
316 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
318 GCancellable *cancellable,
323 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
325 switch (stream->priv->byte_order)
327 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
328 data = GUINT16_TO_BE (data);
330 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
331 data = GUINT16_TO_LE (data);
333 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
338 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
345 * g_data_output_stream_put_int32:
346 * @stream: a #GDataOutputStream.
348 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
349 * @error: a #GError, %NULL to ignore.
351 * Puts a signed 32-bit integer into the output stream.
353 * Returns: %TRUE if @data was successfully added to the @stream.
356 g_data_output_stream_put_int32 (GDataOutputStream *stream,
358 GCancellable *cancellable,
363 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
365 switch (stream->priv->byte_order)
367 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
368 data = GINT32_TO_BE (data);
370 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
371 data = GINT32_TO_LE (data);
373 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
378 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
385 * g_data_output_stream_put_uint32:
386 * @stream: a #GDataOutputStream.
388 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
389 * @error: a #GError, %NULL to ignore.
391 * Puts an unsigned 32-bit integer into the stream.
393 * Returns: %TRUE if @data was successfully added to the @stream.
396 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
398 GCancellable *cancellable,
403 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
405 switch (stream->priv->byte_order)
407 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
408 data = GUINT32_TO_BE (data);
410 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
411 data = GUINT32_TO_LE (data);
413 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
418 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
425 * g_data_output_stream_put_int64:
426 * @stream: a #GDataOutputStream.
428 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
429 * @error: a #GError, %NULL to ignore.
431 * Puts a signed 64-bit integer into the stream.
433 * Returns: %TRUE if @data was successfully added to the @stream.
436 g_data_output_stream_put_int64 (GDataOutputStream *stream,
438 GCancellable *cancellable,
443 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
445 switch (stream->priv->byte_order)
447 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
448 data = GINT64_TO_BE (data);
450 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
451 data = GINT64_TO_LE (data);
453 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
458 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
465 * g_data_output_stream_put_uint64:
466 * @stream: a #GDataOutputStream.
468 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
469 * @error: a #GError, %NULL to ignore.
471 * Puts an unsigned 64-bit integer into the stream.
473 * Returns: %TRUE if @data was successfully added to the @stream.
476 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
478 GCancellable *cancellable,
483 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
485 switch (stream->priv->byte_order)
487 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
488 data = GUINT64_TO_BE (data);
490 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
491 data = GUINT64_TO_LE (data);
493 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
498 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
505 * g_data_output_stream_put_string:
506 * @stream: a #GDataOutputStream.
508 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
509 * @error: a #GError, %NULL to ignore.
511 * Puts a string into the output stream.
513 * Returns: %TRUE if @string was successfully added to the @stream.
516 g_data_output_stream_put_string (GDataOutputStream *stream,
518 GCancellable *cancellable,
523 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
524 g_return_val_if_fail (str != NULL, FALSE);
526 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
533 g_data_output_stream_tell (GSeekable *seekable)
535 GOutputStream *base_stream;
536 GSeekable *base_stream_seekable;
538 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
539 if (!G_IS_SEEKABLE (base_stream))
541 base_stream_seekable = G_SEEKABLE (base_stream);
542 return g_seekable_tell (base_stream_seekable);
546 g_data_output_stream_can_seek (GSeekable *seekable)
548 GOutputStream *base_stream;
550 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
551 return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
555 g_data_output_stream_seek (GSeekable *seekable,
558 GCancellable *cancellable,
561 GOutputStream *base_stream;
562 GSeekable *base_stream_seekable;
564 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
565 if (!G_IS_SEEKABLE (base_stream))
567 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
568 _("Seek not supported on base stream"));
572 base_stream_seekable = G_SEEKABLE (base_stream);
573 return g_seekable_seek (base_stream_seekable, offset, type, cancellable, error);
577 g_data_output_stream_can_truncate (GSeekable *seekable)
579 GOutputStream *base_stream;
581 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
582 return G_IS_SEEKABLE (base_stream) && g_seekable_can_truncate (G_SEEKABLE (base_stream));
586 g_data_output_stream_truncate (GSeekable *seekable,
588 GCancellable *cancellable,
591 GOutputStream *base_stream;
592 GSeekable *base_stream_seekable;
594 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
595 if (!G_IS_SEEKABLE (base_stream))
597 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
598 _("Truncate not supported on base stream"));
602 base_stream_seekable = G_SEEKABLE (base_stream);
603 return g_seekable_truncate (base_stream_seekable, offset, cancellable, error);