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"
31 * SECTION:gdataoutputstream
32 * @short_description: Data Output Stream
34 * @see_also: #GOutputStream
36 * Data output stream implements #GOutputStream and includes functions for
37 * writing data directly to an output stream.
43 struct _GDataOutputStreamPrivate {
44 GDataStreamByteOrder byte_order;
52 static void g_data_output_stream_set_property (GObject *object,
56 static void g_data_output_stream_get_property (GObject *object,
61 G_DEFINE_TYPE (GDataOutputStream,
63 G_TYPE_FILTER_OUTPUT_STREAM)
67 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
69 GObjectClass *object_class;
71 g_type_class_add_private (klass, sizeof (GDataOutputStreamPrivate));
73 object_class = G_OBJECT_CLASS (klass);
74 object_class->get_property = g_data_output_stream_get_property;
75 object_class->set_property = g_data_output_stream_set_property;
78 * GDataOutputStream:byte-order:
80 * Determines the byte ordering that is used when writing
81 * multi-byte entities (such as integers) to the stream.
83 g_object_class_install_property (object_class,
85 g_param_spec_enum ("byte-order",
88 G_TYPE_DATA_STREAM_BYTE_ORDER,
89 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
90 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
95 g_data_output_stream_set_property (GObject *object,
100 GDataOutputStream *dstream;
102 dstream = G_DATA_OUTPUT_STREAM (object);
106 case PROP_BYTE_ORDER:
107 g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
117 g_data_output_stream_get_property (GObject *object,
122 GDataOutputStreamPrivate *priv;
123 GDataOutputStream *dstream;
125 dstream = G_DATA_OUTPUT_STREAM (object);
126 priv = dstream->priv;
130 case PROP_BYTE_ORDER:
131 g_value_set_enum (value, priv->byte_order);
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 g_data_output_stream_init (GDataOutputStream *stream)
143 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
144 G_TYPE_DATA_OUTPUT_STREAM,
145 GDataOutputStreamPrivate);
147 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
151 * g_data_output_stream_new:
152 * @base_stream: a #GOutputStream.
154 * Creates a new data output stream for @base_stream.
156 * Returns: #GDataOutputStream.
159 g_data_output_stream_new (GOutputStream *base_stream)
161 GDataOutputStream *stream;
163 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
165 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
166 "base-stream", base_stream,
173 * g_data_output_stream_set_byte_order:
174 * @stream: a #GDataOutputStream.
175 * @order: a %GDataStreamByteOrder.
177 * Sets the byte order of the data output stream to @order.
180 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
181 GDataStreamByteOrder order)
183 GDataOutputStreamPrivate *priv;
184 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
186 if (priv->byte_order != order)
188 priv->byte_order = order;
189 g_object_notify (G_OBJECT (stream), "byte-order");
194 * g_data_output_stream_get_byte_order:
195 * @stream: a #GDataOutputStream.
197 * Gets the byte order for the stream.
199 * Returns: the #GDataStreamByteOrder for the @stream.
202 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
204 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
206 return stream->priv->byte_order;
210 * g_data_output_stream_put_byte:
211 * @stream: a #GDataOutputStream.
213 * @cancellable: optional #GCancellable object, %NULL to ignore.
214 * @error: a #GError, %NULL to ignore.
216 * Puts a byte into the output stream.
218 * Returns: %TRUE if @data was successfully added to the @stream.
221 g_data_output_stream_put_byte (GDataOutputStream *stream,
223 GCancellable *cancellable,
228 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
230 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
237 * g_data_output_stream_put_int16:
238 * @stream: a #GDataOutputStream.
240 * @cancellable: optional #GCancellable object, %NULL to ignore.
241 * @error: a #GError, %NULL to ignore.
243 * Puts a signed 16-bit integer into the output stream.
245 * Returns: %TRUE if @data was successfully added to the @stream.
248 g_data_output_stream_put_int16 (GDataOutputStream *stream,
250 GCancellable *cancellable,
255 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
257 switch (stream->priv->byte_order)
259 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
260 data = GINT16_TO_BE (data);
262 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
263 data = GINT16_TO_LE (data);
265 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
270 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
277 * g_data_output_stream_put_uint16:
278 * @stream: a #GDataOutputStream.
280 * @cancellable: optional #GCancellable object, %NULL to ignore.
281 * @error: a #GError, %NULL to ignore.
283 * Puts an unsigned 16-bit integer into the output stream.
285 * Returns: %TRUE if @data was successfully added to the @stream.
288 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
290 GCancellable *cancellable,
295 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
297 switch (stream->priv->byte_order)
299 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
300 data = GUINT16_TO_BE (data);
302 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
303 data = GUINT16_TO_LE (data);
305 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
310 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
317 * g_data_output_stream_put_int32:
318 * @stream: a #GDataOutputStream.
320 * @cancellable: optional #GCancellable object, %NULL to ignore.
321 * @error: a #GError, %NULL to ignore.
323 * Puts a signed 32-bit integer into the output stream.
325 * Returns: %TRUE if @data was successfully added to the @stream.
328 g_data_output_stream_put_int32 (GDataOutputStream *stream,
330 GCancellable *cancellable,
335 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
337 switch (stream->priv->byte_order)
339 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
340 data = GINT32_TO_BE (data);
342 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
343 data = GINT32_TO_LE (data);
345 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
350 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
357 * g_data_output_stream_put_uint32:
358 * @stream: a #GDataOutputStream.
360 * @cancellable: optional #GCancellable object, %NULL to ignore.
361 * @error: a #GError, %NULL to ignore.
363 * Puts an unsigned 32-bit integer into the stream.
365 * Returns: %TRUE if @data was successfully added to the @stream.
368 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
370 GCancellable *cancellable,
375 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
377 switch (stream->priv->byte_order)
379 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
380 data = GUINT32_TO_BE (data);
382 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
383 data = GUINT32_TO_LE (data);
385 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
390 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
397 * g_data_output_stream_put_int64:
398 * @stream: a #GDataOutputStream.
400 * @cancellable: optional #GCancellable object, %NULL to ignore.
401 * @error: a #GError, %NULL to ignore.
403 * Puts a signed 64-bit integer into the stream.
405 * Returns: %TRUE if @data was successfully added to the @stream.
408 g_data_output_stream_put_int64 (GDataOutputStream *stream,
410 GCancellable *cancellable,
415 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
417 switch (stream->priv->byte_order)
419 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
420 data = GINT64_TO_BE (data);
422 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
423 data = GINT64_TO_LE (data);
425 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
430 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
437 * g_data_output_stream_put_uint64:
438 * @stream: a #GDataOutputStream.
440 * @cancellable: optional #GCancellable object, %NULL to ignore.
441 * @error: a #GError, %NULL to ignore.
443 * Puts an unsigned 64-bit integer into the stream.
445 * Returns: %TRUE if @data was successfully added to the @stream.
448 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
450 GCancellable *cancellable,
455 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
457 switch (stream->priv->byte_order)
459 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
460 data = GUINT64_TO_BE (data);
462 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
463 data = GUINT64_TO_LE (data);
465 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
470 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
477 * g_data_output_stream_put_string:
478 * @stream: a #GDataOutputStream.
480 * @cancellable: optional #GCancellable object, %NULL to ignore.
481 * @error: a #GError, %NULL to ignore.
483 * Puts a string into the output stream.
485 * Returns: %TRUE if @string was successfully added to the @stream.
488 g_data_output_stream_put_string (GDataOutputStream *stream,
490 GCancellable *cancellable,
495 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
496 g_return_val_if_fail (str != NULL, FALSE);
498 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),