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_ADD_PRIVATE (GDataOutputStream)
81 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
82 g_data_output_stream_seekable_iface_init))
86 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
88 GObjectClass *object_class;
90 object_class = G_OBJECT_CLASS (klass);
91 object_class->get_property = g_data_output_stream_get_property;
92 object_class->set_property = g_data_output_stream_set_property;
95 * GDataOutputStream:byte-order:
97 * Determines the byte ordering that is used when writing
98 * multi-byte entities (such as integers) to the stream.
100 g_object_class_install_property (object_class,
102 g_param_spec_enum ("byte-order",
104 P_("The byte order"),
105 G_TYPE_DATA_STREAM_BYTE_ORDER,
106 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
107 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
112 g_data_output_stream_set_property (GObject *object,
117 GDataOutputStream *dstream;
119 dstream = G_DATA_OUTPUT_STREAM (object);
123 case PROP_BYTE_ORDER:
124 g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134 g_data_output_stream_get_property (GObject *object,
139 GDataOutputStreamPrivate *priv;
140 GDataOutputStream *dstream;
142 dstream = G_DATA_OUTPUT_STREAM (object);
143 priv = dstream->priv;
147 case PROP_BYTE_ORDER:
148 g_value_set_enum (value, priv->byte_order);
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 g_data_output_stream_init (GDataOutputStream *stream)
160 stream->priv = g_data_output_stream_get_instance_private (stream);
161 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
165 g_data_output_stream_seekable_iface_init (GSeekableIface *iface)
167 iface->tell = g_data_output_stream_tell;
168 iface->can_seek = g_data_output_stream_can_seek;
169 iface->seek = g_data_output_stream_seek;
170 iface->can_truncate = g_data_output_stream_can_truncate;
171 iface->truncate_fn = g_data_output_stream_truncate;
175 * g_data_output_stream_new:
176 * @base_stream: a #GOutputStream.
178 * Creates a new data output stream for @base_stream.
180 * Returns: #GDataOutputStream.
183 g_data_output_stream_new (GOutputStream *base_stream)
185 GDataOutputStream *stream;
187 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
189 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
190 "base-stream", base_stream,
197 * g_data_output_stream_set_byte_order:
198 * @stream: a #GDataOutputStream.
199 * @order: a %GDataStreamByteOrder.
201 * Sets the byte order of the data output stream to @order.
204 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
205 GDataStreamByteOrder order)
207 GDataOutputStreamPrivate *priv;
208 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
210 if (priv->byte_order != order)
212 priv->byte_order = order;
213 g_object_notify (G_OBJECT (stream), "byte-order");
218 * g_data_output_stream_get_byte_order:
219 * @stream: a #GDataOutputStream.
221 * Gets the byte order for the stream.
223 * Returns: the #GDataStreamByteOrder for the @stream.
226 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
228 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
230 return stream->priv->byte_order;
234 * g_data_output_stream_put_byte:
235 * @stream: a #GDataOutputStream.
237 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
238 * @error: a #GError, %NULL to ignore.
240 * Puts a byte into the output stream.
242 * Returns: %TRUE if @data was successfully added to the @stream.
245 g_data_output_stream_put_byte (GDataOutputStream *stream,
247 GCancellable *cancellable,
252 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
254 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
261 * g_data_output_stream_put_int16:
262 * @stream: a #GDataOutputStream.
264 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
265 * @error: a #GError, %NULL to ignore.
267 * Puts a signed 16-bit integer into the output stream.
269 * Returns: %TRUE if @data was successfully added to the @stream.
272 g_data_output_stream_put_int16 (GDataOutputStream *stream,
274 GCancellable *cancellable,
279 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
281 switch (stream->priv->byte_order)
283 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
284 data = GINT16_TO_BE (data);
286 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
287 data = GINT16_TO_LE (data);
289 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
294 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
301 * g_data_output_stream_put_uint16:
302 * @stream: a #GDataOutputStream.
304 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
305 * @error: a #GError, %NULL to ignore.
307 * Puts an unsigned 16-bit integer into the output stream.
309 * Returns: %TRUE if @data was successfully added to the @stream.
312 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
314 GCancellable *cancellable,
319 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
321 switch (stream->priv->byte_order)
323 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
324 data = GUINT16_TO_BE (data);
326 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
327 data = GUINT16_TO_LE (data);
329 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
334 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
341 * g_data_output_stream_put_int32:
342 * @stream: a #GDataOutputStream.
344 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
345 * @error: a #GError, %NULL to ignore.
347 * Puts a signed 32-bit integer into the output stream.
349 * Returns: %TRUE if @data was successfully added to the @stream.
352 g_data_output_stream_put_int32 (GDataOutputStream *stream,
354 GCancellable *cancellable,
359 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
361 switch (stream->priv->byte_order)
363 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
364 data = GINT32_TO_BE (data);
366 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
367 data = GINT32_TO_LE (data);
369 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
374 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
381 * g_data_output_stream_put_uint32:
382 * @stream: a #GDataOutputStream.
384 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
385 * @error: a #GError, %NULL to ignore.
387 * Puts an unsigned 32-bit integer into the stream.
389 * Returns: %TRUE if @data was successfully added to the @stream.
392 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
394 GCancellable *cancellable,
399 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
401 switch (stream->priv->byte_order)
403 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
404 data = GUINT32_TO_BE (data);
406 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
407 data = GUINT32_TO_LE (data);
409 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
414 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
421 * g_data_output_stream_put_int64:
422 * @stream: a #GDataOutputStream.
424 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
425 * @error: a #GError, %NULL to ignore.
427 * Puts a signed 64-bit integer into the stream.
429 * Returns: %TRUE if @data was successfully added to the @stream.
432 g_data_output_stream_put_int64 (GDataOutputStream *stream,
434 GCancellable *cancellable,
439 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
441 switch (stream->priv->byte_order)
443 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
444 data = GINT64_TO_BE (data);
446 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
447 data = GINT64_TO_LE (data);
449 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
454 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
461 * g_data_output_stream_put_uint64:
462 * @stream: a #GDataOutputStream.
464 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
465 * @error: a #GError, %NULL to ignore.
467 * Puts an unsigned 64-bit integer into the stream.
469 * Returns: %TRUE if @data was successfully added to the @stream.
472 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
474 GCancellable *cancellable,
479 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
481 switch (stream->priv->byte_order)
483 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
484 data = GUINT64_TO_BE (data);
486 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
487 data = GUINT64_TO_LE (data);
489 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
494 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
501 * g_data_output_stream_put_string:
502 * @stream: a #GDataOutputStream.
504 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
505 * @error: a #GError, %NULL to ignore.
507 * Puts a string into the output stream.
509 * Returns: %TRUE if @string was successfully added to the @stream.
512 g_data_output_stream_put_string (GDataOutputStream *stream,
514 GCancellable *cancellable,
519 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
520 g_return_val_if_fail (str != NULL, FALSE);
522 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
529 g_data_output_stream_tell (GSeekable *seekable)
531 GOutputStream *base_stream;
532 GSeekable *base_stream_seekable;
534 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
535 if (!G_IS_SEEKABLE (base_stream))
537 base_stream_seekable = G_SEEKABLE (base_stream);
538 return g_seekable_tell (base_stream_seekable);
542 g_data_output_stream_can_seek (GSeekable *seekable)
544 GOutputStream *base_stream;
546 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
547 return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
551 g_data_output_stream_seek (GSeekable *seekable,
554 GCancellable *cancellable,
557 GOutputStream *base_stream;
558 GSeekable *base_stream_seekable;
560 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
561 if (!G_IS_SEEKABLE (base_stream))
563 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
564 _("Seek not supported on base stream"));
568 base_stream_seekable = G_SEEKABLE (base_stream);
569 return g_seekable_seek (base_stream_seekable, offset, type, cancellable, error);
573 g_data_output_stream_can_truncate (GSeekable *seekable)
575 GOutputStream *base_stream;
577 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
578 return G_IS_SEEKABLE (base_stream) && g_seekable_can_truncate (G_SEEKABLE (base_stream));
582 g_data_output_stream_truncate (GSeekable *seekable,
584 GCancellable *cancellable,
587 GOutputStream *base_stream;
588 GSeekable *base_stream_seekable;
590 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
591 if (!G_IS_SEEKABLE (base_stream))
593 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
594 _("Truncate not supported on base stream"));
598 base_stream_seekable = G_SEEKABLE (base_stream);
599 return g_seekable_truncate (base_stream_seekable, offset, cancellable, error);