1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include "gdataoutputstream.h"
26 #include "gseekable.h"
27 #include "gioenumtypes.h"
35 * Data output stream implements [class@Gio.OutputStream] and includes functions
36 * for writing data directly to an output stream.
40 struct _GDataOutputStreamPrivate {
41 GDataStreamByteOrder byte_order;
49 static void g_data_output_stream_set_property (GObject *object,
53 static void g_data_output_stream_get_property (GObject *object,
58 static void g_data_output_stream_seekable_iface_init (GSeekableIface *iface);
59 static goffset g_data_output_stream_tell (GSeekable *seekable);
60 static gboolean g_data_output_stream_can_seek (GSeekable *seekable);
61 static gboolean g_data_output_stream_seek (GSeekable *seekable,
64 GCancellable *cancellable,
66 static gboolean g_data_output_stream_can_truncate (GSeekable *seekable);
67 static gboolean g_data_output_stream_truncate (GSeekable *seekable,
69 GCancellable *cancellable,
72 G_DEFINE_TYPE_WITH_CODE (GDataOutputStream,
74 G_TYPE_FILTER_OUTPUT_STREAM,
75 G_ADD_PRIVATE (GDataOutputStream)
76 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
77 g_data_output_stream_seekable_iface_init))
81 g_data_output_stream_class_init (GDataOutputStreamClass *klass)
83 GObjectClass *object_class;
85 object_class = G_OBJECT_CLASS (klass);
86 object_class->get_property = g_data_output_stream_get_property;
87 object_class->set_property = g_data_output_stream_set_property;
90 * GDataOutputStream:byte-order:
92 * Determines the byte ordering that is used when writing
93 * multi-byte entities (such as integers) to the stream.
95 g_object_class_install_property (object_class,
97 g_param_spec_enum ("byte-order", NULL, NULL,
98 G_TYPE_DATA_STREAM_BYTE_ORDER,
99 G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN,
100 G_PARAM_READWRITE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_BLURB));
105 g_data_output_stream_set_property (GObject *object,
110 GDataOutputStream *dstream;
112 dstream = G_DATA_OUTPUT_STREAM (object);
116 case PROP_BYTE_ORDER:
117 g_data_output_stream_set_byte_order (dstream, g_value_get_enum (value));
121 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
127 g_data_output_stream_get_property (GObject *object,
132 GDataOutputStreamPrivate *priv;
133 GDataOutputStream *dstream;
135 dstream = G_DATA_OUTPUT_STREAM (object);
136 priv = dstream->priv;
140 case PROP_BYTE_ORDER:
141 g_value_set_enum (value, priv->byte_order);
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 g_data_output_stream_init (GDataOutputStream *stream)
153 stream->priv = g_data_output_stream_get_instance_private (stream);
154 stream->priv->byte_order = G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN;
158 g_data_output_stream_seekable_iface_init (GSeekableIface *iface)
160 iface->tell = g_data_output_stream_tell;
161 iface->can_seek = g_data_output_stream_can_seek;
162 iface->seek = g_data_output_stream_seek;
163 iface->can_truncate = g_data_output_stream_can_truncate;
164 iface->truncate_fn = g_data_output_stream_truncate;
168 * g_data_output_stream_new:
169 * @base_stream: a #GOutputStream.
171 * Creates a new data output stream for @base_stream.
173 * Returns: #GDataOutputStream.
176 g_data_output_stream_new (GOutputStream *base_stream)
178 GDataOutputStream *stream;
180 g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
182 stream = g_object_new (G_TYPE_DATA_OUTPUT_STREAM,
183 "base-stream", base_stream,
190 * g_data_output_stream_set_byte_order:
191 * @stream: a #GDataOutputStream.
192 * @order: a %GDataStreamByteOrder.
194 * Sets the byte order of the data output stream to @order.
197 g_data_output_stream_set_byte_order (GDataOutputStream *stream,
198 GDataStreamByteOrder order)
200 GDataOutputStreamPrivate *priv;
201 g_return_if_fail (G_IS_DATA_OUTPUT_STREAM (stream));
203 if (priv->byte_order != order)
205 priv->byte_order = order;
206 g_object_notify (G_OBJECT (stream), "byte-order");
211 * g_data_output_stream_get_byte_order:
212 * @stream: a #GDataOutputStream.
214 * Gets the byte order for the stream.
216 * Returns: the #GDataStreamByteOrder for the @stream.
219 g_data_output_stream_get_byte_order (GDataOutputStream *stream)
221 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN);
223 return stream->priv->byte_order;
227 * g_data_output_stream_put_byte:
228 * @stream: a #GDataOutputStream.
230 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
231 * @error: a #GError, %NULL to ignore.
233 * Puts a byte into the output stream.
235 * Returns: %TRUE if @data was successfully added to the @stream.
238 g_data_output_stream_put_byte (GDataOutputStream *stream,
240 GCancellable *cancellable,
245 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
247 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
254 * g_data_output_stream_put_int16:
255 * @stream: a #GDataOutputStream.
257 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
258 * @error: a #GError, %NULL to ignore.
260 * Puts a signed 16-bit integer into the output stream.
262 * Returns: %TRUE if @data was successfully added to the @stream.
265 g_data_output_stream_put_int16 (GDataOutputStream *stream,
267 GCancellable *cancellable,
272 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
274 switch (stream->priv->byte_order)
276 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
277 data = GINT16_TO_BE (data);
279 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
280 data = GINT16_TO_LE (data);
282 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
287 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
294 * g_data_output_stream_put_uint16:
295 * @stream: a #GDataOutputStream.
297 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
298 * @error: a #GError, %NULL to ignore.
300 * Puts an unsigned 16-bit integer into the output stream.
302 * Returns: %TRUE if @data was successfully added to the @stream.
305 g_data_output_stream_put_uint16 (GDataOutputStream *stream,
307 GCancellable *cancellable,
312 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
314 switch (stream->priv->byte_order)
316 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
317 data = GUINT16_TO_BE (data);
319 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
320 data = GUINT16_TO_LE (data);
322 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
327 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
334 * g_data_output_stream_put_int32:
335 * @stream: a #GDataOutputStream.
337 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
338 * @error: a #GError, %NULL to ignore.
340 * Puts a signed 32-bit integer into the output stream.
342 * Returns: %TRUE if @data was successfully added to the @stream.
345 g_data_output_stream_put_int32 (GDataOutputStream *stream,
347 GCancellable *cancellable,
352 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
354 switch (stream->priv->byte_order)
356 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
357 data = GINT32_TO_BE (data);
359 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
360 data = GINT32_TO_LE (data);
362 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
367 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
374 * g_data_output_stream_put_uint32:
375 * @stream: a #GDataOutputStream.
377 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
378 * @error: a #GError, %NULL to ignore.
380 * Puts an unsigned 32-bit integer into the stream.
382 * Returns: %TRUE if @data was successfully added to the @stream.
385 g_data_output_stream_put_uint32 (GDataOutputStream *stream,
387 GCancellable *cancellable,
392 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
394 switch (stream->priv->byte_order)
396 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
397 data = GUINT32_TO_BE (data);
399 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
400 data = GUINT32_TO_LE (data);
402 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
407 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
414 * g_data_output_stream_put_int64:
415 * @stream: a #GDataOutputStream.
417 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
418 * @error: a #GError, %NULL to ignore.
420 * Puts a signed 64-bit integer into the stream.
422 * Returns: %TRUE if @data was successfully added to the @stream.
425 g_data_output_stream_put_int64 (GDataOutputStream *stream,
427 GCancellable *cancellable,
432 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
434 switch (stream->priv->byte_order)
436 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
437 data = GINT64_TO_BE (data);
439 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
440 data = GINT64_TO_LE (data);
442 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
447 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
454 * g_data_output_stream_put_uint64:
455 * @stream: a #GDataOutputStream.
457 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
458 * @error: a #GError, %NULL to ignore.
460 * Puts an unsigned 64-bit integer into the stream.
462 * Returns: %TRUE if @data was successfully added to the @stream.
465 g_data_output_stream_put_uint64 (GDataOutputStream *stream,
467 GCancellable *cancellable,
472 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
474 switch (stream->priv->byte_order)
476 case G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN:
477 data = GUINT64_TO_BE (data);
479 case G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN:
480 data = GUINT64_TO_LE (data);
482 case G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN:
487 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
494 * g_data_output_stream_put_string:
495 * @stream: a #GDataOutputStream.
497 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
498 * @error: a #GError, %NULL to ignore.
500 * Puts a string into the output stream.
502 * Returns: %TRUE if @string was successfully added to the @stream.
505 g_data_output_stream_put_string (GDataOutputStream *stream,
507 GCancellable *cancellable,
512 g_return_val_if_fail (G_IS_DATA_OUTPUT_STREAM (stream), FALSE);
513 g_return_val_if_fail (str != NULL, FALSE);
515 return g_output_stream_write_all (G_OUTPUT_STREAM (stream),
522 g_data_output_stream_tell (GSeekable *seekable)
524 GOutputStream *base_stream;
525 GSeekable *base_stream_seekable;
527 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
528 if (!G_IS_SEEKABLE (base_stream))
530 base_stream_seekable = G_SEEKABLE (base_stream);
531 return g_seekable_tell (base_stream_seekable);
535 g_data_output_stream_can_seek (GSeekable *seekable)
537 GOutputStream *base_stream;
539 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
540 return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
544 g_data_output_stream_seek (GSeekable *seekable,
547 GCancellable *cancellable,
550 GOutputStream *base_stream;
551 GSeekable *base_stream_seekable;
553 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
554 if (!G_IS_SEEKABLE (base_stream))
556 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
557 _("Seek not supported on base stream"));
561 base_stream_seekable = G_SEEKABLE (base_stream);
562 return g_seekable_seek (base_stream_seekable, offset, type, cancellable, error);
566 g_data_output_stream_can_truncate (GSeekable *seekable)
568 GOutputStream *base_stream;
570 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
571 return G_IS_SEEKABLE (base_stream) && g_seekable_can_truncate (G_SEEKABLE (base_stream));
575 g_data_output_stream_truncate (GSeekable *seekable,
577 GCancellable *cancellable,
580 GOutputStream *base_stream;
581 GSeekable *base_stream_seekable;
583 base_stream = G_FILTER_OUTPUT_STREAM (seekable)->base_stream;
584 if (!G_IS_SEEKABLE (base_stream))
586 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
587 _("Truncate not supported on base stream"));
591 base_stream_seekable = G_SEEKABLE (base_stream);
592 return g_seekable_truncate (base_stream_seekable, offset, cancellable, error);