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: Christian Kellner <gicmo@gnome.org>
24 #include "gfilteroutputstream.h"
25 #include "goutputstream.h"
30 * SECTION:gfilteroutputstream
31 * @short_description: Filter Output Stream
34 * Base class for output stream implementations that perform some
35 * kind of filtering operation on a base stream. Typical examples
36 * of filtering operations are character set conversion, compression
37 * and byte order flipping.
46 static void g_filter_output_stream_set_property (GObject *object,
51 static void g_filter_output_stream_get_property (GObject *object,
55 static void g_filter_output_stream_dispose (GObject *object);
58 static gssize g_filter_output_stream_write (GOutputStream *stream,
61 GCancellable *cancellable,
63 static gboolean g_filter_output_stream_flush (GOutputStream *stream,
64 GCancellable *cancellable,
66 static gboolean g_filter_output_stream_close (GOutputStream *stream,
67 GCancellable *cancellable,
73 } GFilterOutputStreamPrivate;
75 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
78 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
80 GObjectClass *object_class;
81 GOutputStreamClass *ostream_class;
83 object_class = G_OBJECT_CLASS (klass);
84 object_class->get_property = g_filter_output_stream_get_property;
85 object_class->set_property = g_filter_output_stream_set_property;
86 object_class->dispose = g_filter_output_stream_dispose;
88 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
89 ostream_class->write_fn = g_filter_output_stream_write;
90 ostream_class->flush = g_filter_output_stream_flush;
91 ostream_class->close_fn = g_filter_output_stream_close;
93 g_object_class_install_property (object_class,
95 g_param_spec_object ("base-stream",
96 P_("The Filter Base Stream"),
97 P_("The underlying base stream on which the io ops will be done."),
99 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
100 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
102 g_object_class_install_property (object_class,
104 g_param_spec_boolean ("close-base-stream",
105 P_("Close Base Stream"),
106 P_("If the base stream should be closed when the filter stream is closed."),
107 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
108 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
112 g_filter_output_stream_set_property (GObject *object,
117 GFilterOutputStream *filter_stream;
120 filter_stream = G_FILTER_OUTPUT_STREAM (object);
124 case PROP_BASE_STREAM:
125 obj = g_value_dup_object (value);
126 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
129 case PROP_CLOSE_BASE:
130 g_filter_output_stream_set_close_base_stream (filter_stream,
131 g_value_get_boolean (value));
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 g_filter_output_stream_get_property (GObject *object,
147 GFilterOutputStream *filter_stream;
148 GFilterOutputStreamPrivate *priv;
150 filter_stream = G_FILTER_OUTPUT_STREAM (object);
151 priv = g_filter_output_stream_get_private (filter_stream);
155 case PROP_BASE_STREAM:
156 g_value_set_object (value, filter_stream->base_stream);
159 case PROP_CLOSE_BASE:
160 g_value_set_boolean (value, priv->close_base);
164 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 g_filter_output_stream_dispose (GObject *object)
173 GFilterOutputStream *stream;
175 stream = G_FILTER_OUTPUT_STREAM (object);
177 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
179 if (stream->base_stream)
181 g_object_unref (stream->base_stream);
182 stream->base_stream = NULL;
188 g_filter_output_stream_init (GFilterOutputStream *stream)
193 * g_filter_output_stream_get_base_stream:
194 * @stream: a #GFilterOutputStream.
196 * Gets the base stream for the filter stream.
198 * Returns: (transfer none): a #GOutputStream.
201 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
203 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
205 return stream->base_stream;
209 * g_filter_output_stream_get_close_base_stream:
210 * @stream: a #GFilterOutputStream.
212 * Returns whether the base stream will be closed when @stream is
215 * Return value: %TRUE if the base stream will be closed.
218 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
220 GFilterOutputStreamPrivate *priv;
222 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
224 priv = g_filter_output_stream_get_private (stream);
226 return priv->close_base;
230 * g_filter_output_stream_set_close_base_stream:
231 * @stream: a #GFilterOutputStream.
232 * @close_base: %TRUE to close the base stream.
234 * Sets whether the base stream will be closed when @stream is closed.
237 g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
240 GFilterOutputStreamPrivate *priv;
242 g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
244 close_base = !!close_base;
246 priv = g_filter_output_stream_get_private (stream);
248 if (priv->close_base != close_base)
250 priv->close_base = close_base;
251 g_object_notify (G_OBJECT (stream), "close-base-stream");
256 g_filter_output_stream_write (GOutputStream *stream,
259 GCancellable *cancellable,
262 GFilterOutputStream *filter_stream;
265 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
267 nwritten = g_output_stream_write (filter_stream->base_stream,
277 g_filter_output_stream_flush (GOutputStream *stream,
278 GCancellable *cancellable,
281 GFilterOutputStream *filter_stream;
284 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
286 res = g_output_stream_flush (filter_stream->base_stream,
294 g_filter_output_stream_close (GOutputStream *stream,
295 GCancellable *cancellable,
298 GFilterOutputStream *filter_stream = G_FILTER_OUTPUT_STREAM (stream);
299 GFilterOutputStreamPrivate *priv = g_filter_output_stream_get_private (filter_stream);
302 if (priv->close_base)
304 res = g_output_stream_close (filter_stream->base_stream,