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 "gioenumtypes.h"
32 * SECTION:gdataoutputstream
33 * @short_description: Data Output Stream
35 * @see_also: #GOutputStream
37 * Data output stream implements #GOutputStream and includes functions for
38 * writing data directly to an output stream.
44 struct _GDataOutputStreamPrivate {
45 GDataStreamByteOrder byte_order;
53 static void g_data_output_stream_set_property (GObject *object,
57 static void g_data_output_stream_get_property (GObject *object,
62 G_DEFINE_TYPE (GDataOutputStream,
64 G_TYPE_FILTER_OUTPUT_STREAM)
68 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
70 GObjectClass *object_class;
72 g_type_class_add_private (klass, sizeof (GDataOutputStreamPrivate));
74 object_class = G_OBJECT_CLASS (klass);
75 object_class->get_property = g_data_output_stream_get_property;
76 object_class->set_property = g_data_output_stream_set_property;
79 * GDataOutputStream:byte-order:
81 * Determines the byte ordering that is used when writing
82 * multi-byte entities (such as integers) to the stream.
84 g_object_class_install_property (object_class,
86 g_param_spec_enum ("byte-order",
89 G_TYPE_DATA_STREAM_BYTE_ORDER,
90 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
91 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
96 g_data_output_stream_set_property (GObject *object,
101 GDataOutputStream *dstream;
103 dstream = G_DATA_OUTPUT_STREAM (object);
107 case PROP_BYTE_ORDER:
108 g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 g_data_output_stream_get_property (GObject *object,
123 GDataOutputStreamPrivate *priv;
124 GDataOutputStream *dstream;
126 dstream = G_DATA_OUTPUT_STREAM (object);
127 priv = dstream->priv;
131 case PROP_BYTE_ORDER:
132 g_value_set_enum (value, priv->byte_order);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 g_data_output_stream_init (GDataOutputStream *stream)
144 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
145 G_TYPE_DATA_OUTPUT_STREAM,
146 GDataOutputStreamPrivate);
148 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
152 * g_data_output_stream_new:
153 * @base_stream: a #GOutputStream.
155 * Creates a new data output stream for @base_stream.
157 * Returns: #GDataOutputStream.
160 g_data_output_stream_new (GOutputStream *base_stream)
162 GDataOutputStream *stream;
164 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
166 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
167 "base-stream", base_stream,
174 * g_data_output_stream_set_byte_order:
175 * @stream: a #GDataOutputStream.
176 * @order: a %GDataStreamByteOrder.
178 * Sets the byte order of the data output stream to @order.
181 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
182 GDataStreamByteOrder order)
184 GDataOutputStreamPrivate *priv;
185 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
187 if (priv->byte_order != order)
189 priv->byte_order = order;
190 g_object_notify (G_OBJECT (stream), "byte-order");
195 * g_data_output_stream_get_byte_order:
196 * @stream: a #GDataOutputStream.
198 * Gets the byte order for the stream.
200 * Returns: the #GDataStreamByteOrder for the @stream.
203 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
205 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
207 return stream->priv->byte_order;
211 * g_data_output_stream_put_byte:
212 * @stream: a #GDataOutputStream.
214 * @cancellable: optional #GCancellable object, %NULL to ignore.
215 * @error: a #GError, %NULL to ignore.
217 * Puts a byte into the output stream.
219 * Returns: %TRUE if @data was successfully added to the @stream.
222 g_data_output_stream_put_byte (GDataOutputStream *stream,
224 GCancellable *cancellable,
229 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
231 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
238 * g_data_output_stream_put_int16:
239 * @stream: a #GDataOutputStream.
241 * @cancellable: optional #GCancellable object, %NULL to ignore.
242 * @error: a #GError, %NULL to ignore.
244 * Puts a signed 16-bit integer into the output stream.
246 * Returns: %TRUE if @data was successfully added to the @stream.
249 g_data_output_stream_put_int16 (GDataOutputStream *stream,
251 GCancellable *cancellable,
256 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
258 switch (stream->priv->byte_order)
260 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
261 data = GINT16_TO_BE (data);
263 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
264 data = GINT16_TO_LE (data);
266 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
271 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
278 * g_data_output_stream_put_uint16:
279 * @stream: a #GDataOutputStream.
281 * @cancellable: optional #GCancellable object, %NULL to ignore.
282 * @error: a #GError, %NULL to ignore.
284 * Puts an unsigned 16-bit integer into the output stream.
286 * Returns: %TRUE if @data was successfully added to the @stream.
289 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
291 GCancellable *cancellable,
296 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
298 switch (stream->priv->byte_order)
300 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
301 data = GUINT16_TO_BE (data);
303 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
304 data = GUINT16_TO_LE (data);
306 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
311 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
318 * g_data_output_stream_put_int32:
319 * @stream: a #GDataOutputStream.
321 * @cancellable: optional #GCancellable object, %NULL to ignore.
322 * @error: a #GError, %NULL to ignore.
324 * Puts a signed 32-bit integer into the output stream.
326 * Returns: %TRUE if @data was successfully added to the @stream.
329 g_data_output_stream_put_int32 (GDataOutputStream *stream,
331 GCancellable *cancellable,
336 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
338 switch (stream->priv->byte_order)
340 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
341 data = GINT32_TO_BE (data);
343 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
344 data = GINT32_TO_LE (data);
346 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
351 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
358 * g_data_output_stream_put_uint32:
359 * @stream: a #GDataOutputStream.
361 * @cancellable: optional #GCancellable object, %NULL to ignore.
362 * @error: a #GError, %NULL to ignore.
364 * Puts an unsigned 32-bit integer into the stream.
366 * Returns: %TRUE if @data was successfully added to the @stream.
369 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
371 GCancellable *cancellable,
376 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
378 switch (stream->priv->byte_order)
380 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
381 data = GUINT32_TO_BE (data);
383 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
384 data = GUINT32_TO_LE (data);
386 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
391 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
398 * g_data_output_stream_put_int64:
399 * @stream: a #GDataOutputStream.
401 * @cancellable: optional #GCancellable object, %NULL to ignore.
402 * @error: a #GError, %NULL to ignore.
404 * Puts a signed 64-bit integer into the stream.
406 * Returns: %TRUE if @data was successfully added to the @stream.
409 g_data_output_stream_put_int64 (GDataOutputStream *stream,
411 GCancellable *cancellable,
416 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
418 switch (stream->priv->byte_order)
420 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
421 data = GINT64_TO_BE (data);
423 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
424 data = GINT64_TO_LE (data);
426 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
431 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
438 * g_data_output_stream_put_uint64:
439 * @stream: a #GDataOutputStream.
441 * @cancellable: optional #GCancellable object, %NULL to ignore.
442 * @error: a #GError, %NULL to ignore.
444 * Puts an unsigned 64-bit integer into the stream.
446 * Returns: %TRUE if @data was successfully added to the @stream.
449 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
451 GCancellable *cancellable,
456 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
458 switch (stream->priv->byte_order)
460 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
461 data = GUINT64_TO_BE (data);
463 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
464 data = GUINT64_TO_LE (data);
466 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
471 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
478 * g_data_output_stream_put_string:
479 * @stream: a #GDataOutputStream.
481 * @cancellable: optional #GCancellable object, %NULL to ignore.
482 * @error: a #GError, %NULL to ignore.
484 * Puts a string into the output stream.
486 * Returns: %TRUE if @string was successfully added to the @stream.
489 g_data_output_stream_put_string (GDataOutputStream *stream,
491 GCancellable *cancellable,
496 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
497 g_return_val_if_fail (str != NULL, FALSE);
499 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
505 #define __G_DATA_OUTPUT_STREAM_C__
506 #include "gioaliasdef.c"