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,
70 G_DEFINE_ABSTRACT_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
72 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
73 G_TYPE_FILTER_OUTPUT_STREAM, GFilterOutputStreamPrivate)
78 } GFilterOutputStreamPrivate;
81 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
83 GObjectClass *object_class;
84 GOutputStreamClass *ostream_class;
86 object_class = G_OBJECT_CLASS (klass);
87 object_class->get_property = g_filter_output_stream_get_property;
88 object_class->set_property = g_filter_output_stream_set_property;
89 object_class->dispose = g_filter_output_stream_dispose;
91 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
92 ostream_class->write_fn = g_filter_output_stream_write;
93 ostream_class->flush = g_filter_output_stream_flush;
94 ostream_class->close_fn = g_filter_output_stream_close;
96 g_type_class_add_private (klass, sizeof (GFilterOutputStreamPrivate));
98 g_object_class_install_property (object_class,
100 g_param_spec_object ("base-stream",
101 P_("The Filter Base Stream"),
102 P_("The underlying base stream on which the io ops will be done."),
103 G_TYPE_OUTPUT_STREAM,
104 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
105 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
107 g_object_class_install_property (object_class,
109 g_param_spec_boolean ("close-base-stream",
110 P_("Close Base Stream"),
111 P_("If the base stream should be closed when the filter stream is closed."),
112 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
113 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
117 g_filter_output_stream_set_property (GObject *object,
122 GFilterOutputStream *filter_stream;
125 filter_stream = G_FILTER_OUTPUT_STREAM (object);
129 case PROP_BASE_STREAM:
130 obj = g_value_dup_object (value);
131 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
134 case PROP_CLOSE_BASE:
135 g_filter_output_stream_set_close_base_stream (filter_stream,
136 g_value_get_boolean (value));
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
147 g_filter_output_stream_get_property (GObject *object,
152 GFilterOutputStream *filter_stream;
154 filter_stream = G_FILTER_OUTPUT_STREAM (object);
158 case PROP_BASE_STREAM:
159 g_value_set_object (value, filter_stream->base_stream);
162 case PROP_CLOSE_BASE:
163 g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 g_filter_output_stream_dispose (GObject *object)
176 GFilterOutputStream *stream;
178 stream = G_FILTER_OUTPUT_STREAM (object);
180 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
182 if (stream->base_stream)
184 g_object_unref (stream->base_stream);
185 stream->base_stream = NULL;
191 g_filter_output_stream_init (GFilterOutputStream *stream)
196 * g_filter_output_stream_get_base_stream:
197 * @stream: a #GFilterOutputStream.
199 * Gets the base stream for the filter stream.
201 * Returns: (transfer none): a #GOutputStream.
204 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
206 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
208 return stream->base_stream;
212 * g_filter_output_stream_get_close_base_stream:
213 * @stream: a #GFilterOutputStream.
215 * Returns whether the base stream will be closed when @stream is
218 * Return value: %TRUE if the base stream will be closed.
221 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
223 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
225 return GET_PRIVATE (stream)->close_base;
229 * g_filter_output_stream_set_close_base_stream:
230 * @stream: a #GFilterOutputStream.
231 * @close_base: %TRUE to close the base stream.
233 * Sets whether the base stream will be closed when @stream is closed.
236 g_filter_output_stream_set_close_base_stream (GFilterOutputStream *stream,
239 GFilterOutputStreamPrivate *priv;
241 g_return_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream));
243 close_base = !!close_base;
245 priv = GET_PRIVATE (stream);
247 if (priv->close_base != close_base)
249 priv->close_base = close_base;
250 g_object_notify (G_OBJECT (stream), "close-base-stream");
255 g_filter_output_stream_write (GOutputStream *stream,
258 GCancellable *cancellable,
261 GFilterOutputStream *filter_stream;
264 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
266 nwritten = g_output_stream_write (filter_stream->base_stream,
276 g_filter_output_stream_flush (GOutputStream *stream,
277 GCancellable *cancellable,
280 GFilterOutputStream *filter_stream;
283 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
285 res = g_output_stream_flush (filter_stream->base_stream,
293 g_filter_output_stream_close (GOutputStream *stream,
294 GCancellable *cancellable,
299 if (GET_PRIVATE (stream)->close_base)
301 GFilterOutputStream *filter_stream;
303 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
305 res = g_output_stream_close (filter_stream->base_stream,