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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "gdataoutputstream.h"
24 #include "gseekable.h"
25 #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 static void g_data_output_stream_seekable_iface_init (GSeekableIface *iface);
62 static goffset g_data_output_stream_tell (GSeekable *seekable);
63 static gboolean g_data_output_stream_can_seek (GSeekable *seekable);
64 static gboolean g_data_output_stream_seek (GSeekable *seekable,
67 GCancellable *cancellable,
69 static gboolean g_data_output_stream_can_truncate (GSeekable *seekable);
70 static gboolean g_data_output_stream_truncate (GSeekable *seekable,
72 GCancellable *cancellable,
75 G_DEFINE_TYPE_WITH_CODE (GDataOutputStream,
77 G_TYPE_FILTER_OUTPUT_STREAM,
78 G_ADD_PRIVATE (GDataOutputStream)
79 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
80 g_data_output_stream_seekable_iface_init))
84 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
86 GObjectClass *object_class;
88 object_class = G_OBJECT_CLASS (klass);
89 object_class->get_property = g_data_output_stream_get_property;
90 object_class->set_property = g_data_output_stream_set_property;
93 * GDataOutputStream:byte-order:
95 * Determines the byte ordering that is used when writing
96 * multi-byte entities (such as integers) to the stream.
98 g_object_class_install_property (object_class,
100 g_param_spec_enum ("byte-order",
102 P_("The byte order"),
103 G_TYPE_DATA_STREAM_BYTE_ORDER,
104 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
105 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
110 g_data_output_stream_set_property (GObject *object,
115 GDataOutputStream *dstream;
117 dstream = G_DATA_OUTPUT_STREAM (object);
121 case PROP_BYTE_ORDER:
122 g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
126 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
132 g_data_output_stream_get_property (GObject *object,
137 GDataOutputStreamPrivate *priv;
138 GDataOutputStream *dstream;
140 dstream = G_DATA_OUTPUT_STREAM (object);
141 priv = dstream->priv;
145 case PROP_BYTE_ORDER:
146 g_value_set_enum (value, priv->byte_order);
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
156 g_data_output_stream_init (GDataOutputStream *stream)
158 stream->priv = g_data_output_stream_get_instance_private (stream);
159 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
163 g_data_output_stream_seekable_iface_init (GSeekableIface *iface)
165 iface->tell = g_data_output_stream_tell;
166 iface->can_seek = g_data_output_stream_can_seek;
167 iface->seek = g_data_output_stream_seek;
168 iface->can_truncate = g_data_output_stream_can_truncate;
169 iface->truncate_fn = g_data_output_stream_truncate;
173 * g_data_output_stream_new:
174 * @base_stream: a #GOutputStream.
176 * Creates a new data output stream for @base_stream.
178 * Returns: #GDataOutputStream.
181 g_data_output_stream_new (GOutputStream *base_stream)
183 GDataOutputStream *stream;
185 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
187 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
188 "base-stream", base_stream,
195 * g_data_output_stream_set_byte_order:
196 * @stream: a #GDataOutputStream.
197 * @order: a %GDataStreamByteOrder.
199 * Sets the byte order of the data output stream to @order.
202 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
203 GDataStreamByteOrder order)
205 GDataOutputStreamPrivate *priv;
206 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
208 if (priv->byte_order != order)
210 priv->byte_order = order;
211 g_object_notify (G_OBJECT (stream), "byte-order");
216 * g_data_output_stream_get_byte_order:
217 * @stream: a #GDataOutputStream.
219 * Gets the byte order for the stream.
221 * Returns: the #GDataStreamByteOrder for the @stream.
224 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
226 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
228 return stream->priv->byte_order;
232 * g_data_output_stream_put_byte:
233 * @stream: a #GDataOutputStream.
235 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
236 * @error: a #GError, %NULL to ignore.
238 * Puts a byte into the output stream.
240 * Returns: %TRUE if @data was successfully added to the @stream.
243 g_data_output_stream_put_byte (GDataOutputStream *stream,
245 GCancellable *cancellable,
250 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
252 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
259 * g_data_output_stream_put_int16:
260 * @stream: a #GDataOutputStream.
262 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
263 * @error: a #GError, %NULL to ignore.
265 * Puts a signed 16-bit integer into the output stream.
267 * Returns: %TRUE if @data was successfully added to the @stream.
270 g_data_output_stream_put_int16 (GDataOutputStream *stream,
272 GCancellable *cancellable,
277 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
279 switch (stream->priv->byte_order)
281 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
282 data = GINT16_TO_BE (data);
284 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
285 data = GINT16_TO_LE (data);
287 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
292 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
299 * g_data_output_stream_put_uint16:
300 * @stream: a #GDataOutputStream.
302 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
303 * @error: a #GError, %NULL to ignore.
305 * Puts an unsigned 16-bit integer into the output stream.
307 * Returns: %TRUE if @data was successfully added to the @stream.
310 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
312 GCancellable *cancellable,
317 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
319 switch (stream->priv->byte_order)
321 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
322 data = GUINT16_TO_BE (data);
324 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
325 data = GUINT16_TO_LE (data);
327 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
332 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
339 * g_data_output_stream_put_int32:
340 * @stream: a #GDataOutputStream.
342 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
343 * @error: a #GError, %NULL to ignore.
345 * Puts a signed 32-bit integer into the output stream.
347 * Returns: %TRUE if @data was successfully added to the @stream.
350 g_data_output_stream_put_int32 (GDataOutputStream *stream,
352 GCancellable *cancellable,
357 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
359 switch (stream->priv->byte_order)
361 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
362 data = GINT32_TO_BE (data);
364 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
365 data = GINT32_TO_LE (data);
367 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
372 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
379 * g_data_output_stream_put_uint32:
380 * @stream: a #GDataOutputStream.
382 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
383 * @error: a #GError, %NULL to ignore.
385 * Puts an unsigned 32-bit integer into the stream.
387 * Returns: %TRUE if @data was successfully added to the @stream.
390 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
392 GCancellable *cancellable,
397 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
399 switch (stream->priv->byte_order)
401 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
402 data = GUINT32_TO_BE (data);
404 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
405 data = GUINT32_TO_LE (data);
407 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
412 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
419 * g_data_output_stream_put_int64:
420 * @stream: a #GDataOutputStream.
422 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
423 * @error: a #GError, %NULL to ignore.
425 * Puts a signed 64-bit integer into the stream.
427 * Returns: %TRUE if @data was successfully added to the @stream.
430 g_data_output_stream_put_int64 (GDataOutputStream *stream,
432 GCancellable *cancellable,
437 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
439 switch (stream->priv->byte_order)
441 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
442 data = GINT64_TO_BE (data);
444 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
445 data = GINT64_TO_LE (data);
447 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
452 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
459 * g_data_output_stream_put_uint64:
460 * @stream: a #GDataOutputStream.
462 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
463 * @error: a #GError, %NULL to ignore.
465 * Puts an unsigned 64-bit integer into the stream.
467 * Returns: %TRUE if @data was successfully added to the @stream.
470 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
472 GCancellable *cancellable,
477 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
479 switch (stream->priv->byte_order)
481 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
482 data = GUINT64_TO_BE (data);
484 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
485 data = GUINT64_TO_LE (data);
487 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
492 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
499 * g_data_output_stream_put_string:
500 * @stream: a #GDataOutputStream.
502 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
503 * @error: a #GError, %NULL to ignore.
505 * Puts a string into the output stream.
507 * Returns: %TRUE if @string was successfully added to the @stream.
510 g_data_output_stream_put_string (GDataOutputStream *stream,
512 GCancellable *cancellable,
517 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
518 g_return_val_if_fail (str != NULL, FALSE);
520 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
527 g_data_output_stream_tell (GSeekable *seekable)
529 GOutputStream *base_stream;
530 GSeekable *base_stream_seekable;
532 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
533 if (!G_IS_SEEKABLE (base_stream))
535 base_stream_seekable = G_SEEKABLE (base_stream);
536 return g_seekable_tell (base_stream_seekable);
540 g_data_output_stream_can_seek (GSeekable *seekable)
542 GOutputStream *base_stream;
544 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
545 return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
549 g_data_output_stream_seek (GSeekable *seekable,
552 GCancellable *cancellable,
555 GOutputStream *base_stream;
556 GSeekable *base_stream_seekable;
558 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
559 if (!G_IS_SEEKABLE (base_stream))
561 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
562 _("Seek not supported on base stream"));
566 base_stream_seekable = G_SEEKABLE (base_stream);
567 return g_seekable_seek (base_stream_seekable, offset, type, cancellable, error);
571 g_data_output_stream_can_truncate (GSeekable *seekable)
573 GOutputStream *base_stream;
575 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
576 return G_IS_SEEKABLE (base_stream) && g_seekable_can_truncate (G_SEEKABLE (base_stream));
580 g_data_output_stream_truncate (GSeekable *seekable,
582 GCancellable *cancellable,
585 GOutputStream *base_stream;
586 GSeekable *base_stream_seekable;
588 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
589 if (!G_IS_SEEKABLE (base_stream))
591 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
592 _("Truncate not supported on base stream"));
596 base_stream_seekable = G_SEEKABLE (base_stream);
597 return g_seekable_truncate (base_stream_seekable, offset, cancellable, error);