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: Christian Kellner <gicmo@gnome.org>
22 #include "gfilteroutputstream.h"
23 #include "goutputstream.h"
28 * SECTION:gfilteroutputstream
29 * @short_description: Filter Output Stream
32 * Base class for output stream implementations that perform some
33 * kind of filtering operation on a base stream. Typical examples
34 * of filtering operations are character set conversion, compression
35 * and byte order flipping.
44 static void g_filter_output_stream_set_property (GObject *object,
49 static void g_filter_output_stream_get_property (GObject *object,
53 static void g_filter_output_stream_dispose (GObject *object);
56 static gssize g_filter_output_stream_write (GOutputStream *stream,
59 GCancellable *cancellable,
61 static gboolean g_filter_output_stream_flush (GOutputStream *stream,
62 GCancellable *cancellable,
64 static gboolean g_filter_output_stream_close (GOutputStream *stream,
65 GCancellable *cancellable,
71 } GFilterOutputStreamPrivate;
73 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
76 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
78 GObjectClass *object_class;
79 GOutputStreamClass *ostream_class;
81 object_class = G_OBJECT_CLASS (klass);
82 object_class->get_property = g_filter_output_stream_get_property;
83 object_class->set_property = g_filter_output_stream_set_property;
84 object_class->dispose = g_filter_output_stream_dispose;
86 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
87 ostream_class->write_fn = g_filter_output_stream_write;
88 ostream_class->flush = g_filter_output_stream_flush;
89 ostream_class->close_fn = g_filter_output_stream_close;
91 g_object_class_install_property (object_class,
93 g_param_spec_object ("base-stream",
94 P_("The Filter Base Stream"),
95 P_("The underlying base stream on which the io ops will be done."),
97 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
98 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
100 g_object_class_install_property (object_class,
102 g_param_spec_boolean ("close-base-stream",
103 P_("Close Base Stream"),
104 P_("If the base stream should be closed when the filter stream is closed."),
105 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
106 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
110 g_filter_output_stream_set_property (GObject *object,
115 GFilterOutputStream *filter_stream;
118 filter_stream = G_FILTER_OUTPUT_STREAM (object);
122 case PROP_BASE_STREAM:
123 obj = g_value_dup_object (value);
124 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
127 case PROP_CLOSE_BASE:
128 g_filter_output_stream_set_close_base_stream (filter_stream,
129 g_value_get_boolean (value));
133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 g_filter_output_stream_get_property (GObject *object,
145 GFilterOutputStream *filter_stream;
146 GFilterOutputStreamPrivate *priv;
148 filter_stream = G_FILTER_OUTPUT_STREAM (object);
149 priv = g_filter_output_stream_get_instance_private (filter_stream);
153 case PROP_BASE_STREAM:
154 g_value_set_object (value, filter_stream->base_stream);
157 case PROP_CLOSE_BASE:
158 g_value_set_boolean (value, priv->close_base);
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
169 g_filter_output_stream_dispose (GObject *object)
171 GFilterOutputStream *stream;
173 stream = G_FILTER_OUTPUT_STREAM (object);
175 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
177 if (stream->base_stream)
179 g_object_unref (stream->base_stream);
180 stream->base_stream = NULL;
186 g_filter_output_stream_init (GFilterOutputStream *stream)
191 * g_filter_output_stream_get_base_stream:
192 * @stream: a #GFilterOutputStream.
194 * Gets the base stream for the filter stream.
196 * Returns: (transfer none): a #GOutputStream.
199 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
201 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
203 return stream->base_stream;
207 * g_filter_output_stream_get_close_base_stream:
208 * @stream: a #GFilterOutputStream.
210 * Returns whether the base stream will be closed when @stream is
213 * Returns: %TRUE if the base stream will be closed.
216 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
218 GFilterOutputStreamPrivate *priv;
220 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
222 priv = g_filter_output_stream_get_instance_private (stream);
224 return priv->close_base;
228 * g_filter_output_stream_set_close_base_stream:
229 * @stream: a #GFilterOutputStream.
230 * @close_base: %TRUE to close the base stream.
232 * Sets whether the base stream will be closed when @stream is closed.
235 g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
238 GFilterOutputStreamPrivate *priv;
240 g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
242 close_base = !!close_base;
244 priv = g_filter_output_stream_get_instance_private (stream);
246 if (priv->close_base != close_base)
248 priv->close_base = close_base;
249 g_object_notify (G_OBJECT (stream), "close-base-stream");
254 g_filter_output_stream_write (GOutputStream *stream,
257 GCancellable *cancellable,
260 GFilterOutputStream *filter_stream;
263 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
265 nwritten = g_output_stream_write (filter_stream->base_stream,
275 g_filter_output_stream_flush (GOutputStream *stream,
276 GCancellable *cancellable,
279 GFilterOutputStream *filter_stream;
282 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
284 res = g_output_stream_flush (filter_stream->base_stream,
292 g_filter_output_stream_close (GOutputStream *stream,
293 GCancellable *cancellable,
296 GFilterOutputStream *filter_stream = G_FILTER_OUTPUT_STREAM (stream);
297 GFilterOutputStreamPrivate *priv = g_filter_output_stream_get_instance_private (filter_stream);
300 if (priv->close_base)
302 res = g_output_stream_close (filter_stream->base_stream,