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"
29 * SECTION:gdataoutputstream
30 * @short_description: Data Output Stream
31 * @see_also: #GOutputStream
33 * Data output stream implements #GOutputStream and includes functions for
34 * writing data directly to an output stream.
40 struct _GDataOutputStreamPrivate {
41 GDataStreamByteOrder byte_order;
48 static void g_data_output_stream_set_property (GObject *object,
52 static void g_data_output_stream_get_property (GObject *object,
57 G_DEFINE_TYPE (GDataOutputStream,
59 G_TYPE_FILTER_OUTPUT_STREAM)
63 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
65 GObjectClass *object_class;
67 g_type_class_add_private (klass, sizeof (GDataOutputStreamPrivate));
69 object_class = G_OBJECT_CLASS (klass);
70 object_class->get_property = g_data_output_stream_get_property;
71 object_class->set_property = g_data_output_stream_set_property;
75 g_data_output_stream_set_property (GObject *object,
80 GDataOutputStreamPrivate *priv;
81 GDataOutputStream *dstream;
83 dstream = G_DATA_OUTPUT_STREAM (object);
90 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;
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
118 g_data_output_stream_init (GDataOutputStream *stream)
120 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
121 G_TYPE_DATA_OUTPUT_STREAM,
122 GDataOutputStreamPrivate);
124 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
128 * g_data_output_stream_new:
129 * @base_stream: a #GOutputStream.
131 * Creates a new data output stream for @base_stream.
133 * Returns: #GDataOutputStream.
136 g_data_output_stream_new (GOutputStream *base_stream)
138 GDataOutputStream *stream;
140 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
142 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
143 "base-stream", base_stream,
150 * g_data_output_stream_set_byte_order:
151 * @stream: a #GDataOutputStream.
152 * @order: a %GDataStreamByteOrder.
154 * Sets the byte order of the data output stream to @order.
158 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
159 GDataStreamByteOrder order)
161 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
163 stream->priv->byte_order = order;
167 * g_data_output_stream_get_byte_order:
168 * @stream: a #GDataOutputStream.
170 * Gets the byte order for the stream.
172 * Returns: the #GDataStreamByteOrder for the @stream.
175 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
177 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
179 return stream->priv->byte_order;
183 * g_data_output_stream_put_byte:
184 * @stream: a #GDataOutputStream.
186 * @cancellable: optional #GCancellable object, %NULL to ignore.
187 * @error: a #GError, %NULL to ignore.
189 * Puts a byte into the output stream.
191 * Returns: %TRUE if @data was successfully added to the @stream.
194 g_data_output_stream_put_byte (GDataOutputStream *stream,
196 GCancellable *cancellable,
201 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
203 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
210 * g_data_output_stream_put_int16:
211 * @stream: a #GDataOutputStream.
213 * @cancellable: optional #GCancellable object, %NULL to ignore.
214 * @error: a #GError, %NULL to ignore.
216 * Puts a signed 16-bit integer into the output stream.
218 * Returns: %TRUE if @data was successfully added to the @stream.
221 g_data_output_stream_put_int16 (GDataOutputStream *stream,
223 GCancellable *cancellable,
228 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
230 switch (stream->priv->byte_order)
232 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
233 data = GINT16_TO_BE (data);
235 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
236 data = GINT16_TO_LE (data);
238 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
243 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
250 * g_data_output_stream_put_uint16:
251 * @stream: a #GDataOutputStream.
253 * @cancellable: optional #GCancellable object, %NULL to ignore.
254 * @error: a #GError, %NULL to ignore.
256 * Puts an unsigned 16-bit integer into the output stream.
258 * Returns: %TRUE if @data was successfully added to the @stream.
261 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
263 GCancellable *cancellable,
268 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
270 switch (stream->priv->byte_order)
272 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
273 data = GUINT16_TO_BE (data);
275 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
276 data = GUINT16_TO_LE (data);
278 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
283 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
290 * g_data_output_stream_put_int32:
291 * @stream: a #GDataOutputStream.
293 * @cancellable: optional #GCancellable object, %NULL to ignore.
294 * @error: a #GError, %NULL to ignore.
296 * Puts a signed 32-bit integer into the output stream.
298 * Returns: %TRUE if @data was successfully added to the @stream.
301 g_data_output_stream_put_int32 (GDataOutputStream *stream,
303 GCancellable *cancellable,
308 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
310 switch (stream->priv->byte_order)
312 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
313 data = GINT32_TO_BE (data);
315 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
316 data = GINT32_TO_LE (data);
318 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
323 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
330 * g_data_output_stream_put_uint32:
331 * @stream: a #GDataOutputStream.
333 * @cancellable: optional #GCancellable object, %NULL to ignore.
334 * @error: a #GError, %NULL to ignore.
336 * Puts an unsigned 32-bit integer into the stream.
338 * Returns: %TRUE if @data was successfully added to the @stream.
341 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
343 GCancellable *cancellable,
348 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
350 switch (stream->priv->byte_order)
352 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
353 data = GUINT32_TO_BE (data);
355 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
356 data = GUINT32_TO_LE (data);
358 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
363 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
370 * g_data_output_stream_put_int64:
371 * @stream: a #GDataOutputStream.
373 * @cancellable: optional #GCancellable object, %NULL to ignore.
374 * @error: a #GError, %NULL to ignore.
376 * Puts a signed 64-bit integer into the stream.
378 * Returns: %TRUE if @data was successfully added to the @stream.
381 g_data_output_stream_put_int64 (GDataOutputStream *stream,
383 GCancellable *cancellable,
388 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
390 switch (stream->priv->byte_order)
392 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
393 data = GINT64_TO_BE (data);
395 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
396 data = GINT64_TO_LE (data);
398 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
403 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
410 * g_data_output_stream_put_uint64:
411 * @stream: a #GDataOutputStream.
413 * @cancellable: optional #GCancellable object, %NULL to ignore.
414 * @error: a #GError, %NULL to ignore.
416 * Puts an unsigned 64-bit integer into the stream.
418 * Returns: %TRUE if @data was successfully added to the @stream.
421 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
423 GCancellable *cancellable,
428 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
430 switch (stream->priv->byte_order)
432 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
433 data = GUINT64_TO_BE (data);
435 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
436 data = GUINT64_TO_LE (data);
438 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
443 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
450 * g_data_output_stream_put_string:
451 * @stream: a #GDataOutputStream.
453 * @cancellable: optional #GCancellable object, %NULL to ignore.
454 * @error: a #GError, %NULL to ignore.
456 * Puts a string into the output stream.
458 * Returns: %TRUE if @string was successfully added to the @stream.
461 g_data_output_stream_put_string (GDataOutputStream *stream,
463 GCancellable *cancellable,
468 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
469 g_return_val_if_fail (str != NULL, FALSE);
471 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),