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 "gsimpleasyncresult.h"
26 #include "goutputstream.h"
31 * SECTION:gfilteroutputstream
32 * @short_description: Filter Output Stream
35 * Base class for output stream implementations that perform some
36 * kind of filtering operation on a base stream. Typical examples
37 * of filtering operations are character set conversion, compression
38 * and byte order flipping.
47 static void g_filter_output_stream_set_property (GObject *object,
52 static void g_filter_output_stream_get_property (GObject *object,
56 static void g_filter_output_stream_dispose (GObject *object);
59 static gssize g_filter_output_stream_write (GOutputStream *stream,
62 GCancellable *cancellable,
64 static gboolean g_filter_output_stream_flush (GOutputStream *stream,
65 GCancellable *cancellable,
67 static gboolean g_filter_output_stream_close (GOutputStream *stream,
68 GCancellable *cancellable,
71 G_DEFINE_ABSTRACT_TYPE (GFilterOutputStream, g_filter_output_stream, G_TYPE_OUTPUT_STREAM)
73 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
74 G_TYPE_FILTER_OUTPUT_STREAM, GFilterOutputStreamPrivate)
79 } GFilterOutputStreamPrivate;
82 g_filter_output_stream_class_init (GFilterOutputStreamClass *klass)
84 GObjectClass *object_class;
85 GOutputStreamClass *ostream_class;
87 object_class = G_OBJECT_CLASS (klass);
88 object_class->get_property = g_filter_output_stream_get_property;
89 object_class->set_property = g_filter_output_stream_set_property;
90 object_class->dispose = g_filter_output_stream_dispose;
92 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
93 ostream_class->write_fn = g_filter_output_stream_write;
94 ostream_class->flush = g_filter_output_stream_flush;
95 ostream_class->close_fn = g_filter_output_stream_close;
97 g_type_class_add_private (klass, sizeof (GFilterOutputStreamPrivate));
99 g_object_class_install_property (object_class,
101 g_param_spec_object ("base-stream",
102 P_("The Filter Base Stream"),
103 P_("The underlying base stream on which the io ops will be done."),
104 G_TYPE_OUTPUT_STREAM,
105 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
106 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
108 g_object_class_install_property (object_class,
110 g_param_spec_boolean ("close-base-stream",
111 P_("Close Base Stream"),
112 P_("If the base stream should be closed when the filter stream is closed."),
113 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
114 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
118 g_filter_output_stream_set_property (GObject *object,
123 GFilterOutputStream *filter_stream;
126 filter_stream = G_FILTER_OUTPUT_STREAM (object);
130 case PROP_BASE_STREAM:
131 obj = g_value_dup_object (value);
132 filter_stream->base_stream = G_OUTPUT_STREAM (obj);
135 case PROP_CLOSE_BASE:
136 g_filter_output_stream_set_close_base_stream (filter_stream,
137 g_value_get_boolean (value));
141 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 g_filter_output_stream_get_property (GObject *object,
153 GFilterOutputStream *filter_stream;
155 filter_stream = G_FILTER_OUTPUT_STREAM (object);
159 case PROP_BASE_STREAM:
160 g_value_set_object (value, filter_stream->base_stream);
163 case PROP_CLOSE_BASE:
164 g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 g_filter_output_stream_dispose (GObject *object)
177 GFilterOutputStream *stream;
179 stream = G_FILTER_OUTPUT_STREAM (object);
181 G_OBJECT_CLASS (g_filter_output_stream_parent_class)->dispose (object);
183 if (stream->base_stream)
185 g_object_unref (stream->base_stream);
186 stream->base_stream = NULL;
192 g_filter_output_stream_init (GFilterOutputStream *stream)
197 * g_filter_output_stream_get_base_stream:
198 * @stream: a #GFilterOutputStream.
200 * Gets the base stream for the filter stream.
202 * Returns: (transfer none): a #GOutputStream.
205 g_filter_output_stream_get_base_stream (GFilterOutputStream *stream)
207 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), NULL);
209 return stream->base_stream;
213 * g_filter_output_stream_get_close_base_stream:
214 * @stream: a #GFilterOutputStream.
216 * Returns whether the base stream will be closed when @stream is
219 * Return value: %TRUE if the base stream will be closed.
222 g_filter_output_stream_get_close_base_stream (GFilterOutputStream *stream)
224 g_return_val_if_fail (G_IS_FILTER_OUTPUT_STREAM (stream), FALSE);
226 return GET_PRIVATE (stream)->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 = 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,
300 if (GET_PRIVATE (stream)->close_base)
302 GFilterOutputStream *filter_stream;
304 filter_stream = G_FILTER_OUTPUT_STREAM (stream);
306 res = g_output_stream_close (filter_stream->base_stream,