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"
31 * SECTION:gdataoutputstream
32 * @short_description: Data Output Stream
33 * @see_also: #GOutputStream
35 * Data output stream implements #GOutputStream and includes functions for
36 * writing data directly to an output stream.
42 struct _GDataOutputStreamPrivate {
43 GDataStreamByteOrder byte_order;
50 static void g_data_output_stream_set_property (GObject *object,
54 static void g_data_output_stream_get_property (GObject *object,
59 G_DEFINE_TYPE (GDataOutputStream,
61 G_TYPE_FILTER_OUTPUT_STREAM)
65 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
67 GObjectClass *object_class;
69 g_type_class_add_private (klass, sizeof (GDataOutputStreamPrivate));
71 object_class = G_OBJECT_CLASS (klass);
72 object_class->get_property = g_data_output_stream_get_property;
73 object_class->set_property = g_data_output_stream_set_property;
77 g_data_output_stream_set_property (GObject *object,
82 GDataOutputStreamPrivate *priv;
83 GDataOutputStream *dstream;
85 dstream = G_DATA_OUTPUT_STREAM (object);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 g_data_output_stream_get_property (GObject *object,
102 GDataOutputStreamPrivate *priv;
103 GDataOutputStream *dstream;
105 dstream = G_DATA_OUTPUT_STREAM (object);
106 priv = dstream->priv;
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 g_data_output_stream_init (GDataOutputStream *stream)
119 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
120 G_TYPE_DATA_OUTPUT_STREAM,
121 GDataOutputStreamPrivate);
123 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
127 * g_data_output_stream_new:
128 * @base_stream: a #GOutputStream.
130 * Creates a new data output stream for @base_stream.
132 * Returns: #GDataOutputStream.
135 g_data_output_stream_new (GOutputStream *base_stream)
137 GDataOutputStream *stream;
139 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
141 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
142 "base-stream", base_stream,
149 * g_data_output_stream_set_byte_order:
150 * @stream: a #GDataOutputStream.
151 * @order: a %GDataStreamByteOrder.
153 * Sets the byte order of the data output stream to @order.
156 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
157 GDataStreamByteOrder order)
159 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
161 stream->priv->byte_order = order;
165 * g_data_output_stream_get_byte_order:
166 * @stream: a #GDataOutputStream.
168 * Gets the byte order for the stream.
170 * Returns: the #GDataStreamByteOrder for the @stream.
173 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
175 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
177 return stream->priv->byte_order;
181 * g_data_output_stream_put_byte:
182 * @stream: a #GDataOutputStream.
184 * @cancellable: optional #GCancellable object, %NULL to ignore.
185 * @error: a #GError, %NULL to ignore.
187 * Puts a byte into the output stream.
189 * Returns: %TRUE if @data was successfully added to the @stream.
192 g_data_output_stream_put_byte (GDataOutputStream *stream,
194 GCancellable *cancellable,
199 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
201 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
208 * g_data_output_stream_put_int16:
209 * @stream: a #GDataOutputStream.
211 * @cancellable: optional #GCancellable object, %NULL to ignore.
212 * @error: a #GError, %NULL to ignore.
214 * Puts a signed 16-bit integer into the output stream.
216 * Returns: %TRUE if @data was successfully added to the @stream.
219 g_data_output_stream_put_int16 (GDataOutputStream *stream,
221 GCancellable *cancellable,
226 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
228 switch (stream->priv->byte_order)
230 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
231 data = GINT16_TO_BE (data);
233 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
234 data = GINT16_TO_LE (data);
236 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
241 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
248 * g_data_output_stream_put_uint16:
249 * @stream: a #GDataOutputStream.
251 * @cancellable: optional #GCancellable object, %NULL to ignore.
252 * @error: a #GError, %NULL to ignore.
254 * Puts an unsigned 16-bit integer into the output stream.
256 * Returns: %TRUE if @data was successfully added to the @stream.
259 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
261 GCancellable *cancellable,
266 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
268 switch (stream->priv->byte_order)
270 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
271 data = GUINT16_TO_BE (data);
273 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
274 data = GUINT16_TO_LE (data);
276 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
281 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
288 * g_data_output_stream_put_int32:
289 * @stream: a #GDataOutputStream.
291 * @cancellable: optional #GCancellable object, %NULL to ignore.
292 * @error: a #GError, %NULL to ignore.
294 * Puts a signed 32-bit integer into the output stream.
296 * Returns: %TRUE if @data was successfully added to the @stream.
299 g_data_output_stream_put_int32 (GDataOutputStream *stream,
301 GCancellable *cancellable,
306 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
308 switch (stream->priv->byte_order)
310 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
311 data = GINT32_TO_BE (data);
313 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
314 data = GINT32_TO_LE (data);
316 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
321 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
328 * g_data_output_stream_put_uint32:
329 * @stream: a #GDataOutputStream.
331 * @cancellable: optional #GCancellable object, %NULL to ignore.
332 * @error: a #GError, %NULL to ignore.
334 * Puts an unsigned 32-bit integer into the stream.
336 * Returns: %TRUE if @data was successfully added to the @stream.
339 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
341 GCancellable *cancellable,
346 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
348 switch (stream->priv->byte_order)
350 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
351 data = GUINT32_TO_BE (data);
353 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
354 data = GUINT32_TO_LE (data);
356 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
361 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
368 * g_data_output_stream_put_int64:
369 * @stream: a #GDataOutputStream.
371 * @cancellable: optional #GCancellable object, %NULL to ignore.
372 * @error: a #GError, %NULL to ignore.
374 * Puts a signed 64-bit integer into the stream.
376 * Returns: %TRUE if @data was successfully added to the @stream.
379 g_data_output_stream_put_int64 (GDataOutputStream *stream,
381 GCancellable *cancellable,
386 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
388 switch (stream->priv->byte_order)
390 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
391 data = GINT64_TO_BE (data);
393 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
394 data = GINT64_TO_LE (data);
396 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
401 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
408 * g_data_output_stream_put_uint64:
409 * @stream: a #GDataOutputStream.
411 * @cancellable: optional #GCancellable object, %NULL to ignore.
412 * @error: a #GError, %NULL to ignore.
414 * Puts an unsigned 64-bit integer into the stream.
416 * Returns: %TRUE if @data was successfully added to the @stream.
419 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
421 GCancellable *cancellable,
426 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
428 switch (stream->priv->byte_order)
430 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
431 data = GUINT64_TO_BE (data);
433 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
434 data = GUINT64_TO_LE (data);
436 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
441 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
448 * g_data_output_stream_put_string:
449 * @stream: a #GDataOutputStream.
451 * @cancellable: optional #GCancellable object, %NULL to ignore.
452 * @error: a #GError, %NULL to ignore.
454 * Puts a string into the output stream.
456 * Returns: %TRUE if @string was successfully added to the @stream.
459 g_data_output_stream_put_string (GDataOutputStream *stream,
461 GCancellable *cancellable,
466 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
467 g_return_val_if_fail (str != NULL, FALSE);
469 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
475 #define __G_DATA_OUTPUT_STREAM_C__
476 #include "gioaliasdef.c"