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 "gfilterinputstream.h"
23 #include "ginputstream.h"
28 * SECTION:gfilterinputstream
29 * @short_description: Filter Input Stream
32 * Base class for input 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_input_stream_set_property (GObject *object,
49 static void g_filter_input_stream_get_property (GObject *object,
53 static void g_filter_input_stream_finalize (GObject *object);
56 static gssize g_filter_input_stream_read (GInputStream *stream,
59 GCancellable *cancellable,
61 static gssize g_filter_input_stream_skip (GInputStream *stream,
63 GCancellable *cancellable,
65 static gboolean g_filter_input_stream_close (GInputStream *stream,
66 GCancellable *cancellable,
72 } GFilterInputStreamPrivate;
74 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (GFilterInputStream, g_filter_input_stream, G_TYPE_INPUT_STREAM)
77 g_filter_input_stream_class_init (GFilterInputStreamClass *klass)
79 GObjectClass *object_class;
80 GInputStreamClass *istream_class;
82 object_class = G_OBJECT_CLASS (klass);
83 object_class->get_property = g_filter_input_stream_get_property;
84 object_class->set_property = g_filter_input_stream_set_property;
85 object_class->finalize = g_filter_input_stream_finalize;
87 istream_class = G_INPUT_STREAM_CLASS (klass);
88 istream_class->read_fn = g_filter_input_stream_read;
89 istream_class->skip = g_filter_input_stream_skip;
90 istream_class->close_fn = g_filter_input_stream_close;
92 g_object_class_install_property (object_class,
94 g_param_spec_object ("base-stream",
95 P_("The Filter Base Stream"),
96 P_("The underlying base stream on which the io ops will be done."),
98 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
99 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
101 g_object_class_install_property (object_class,
103 g_param_spec_boolean ("close-base-stream",
104 P_("Close Base Stream"),
105 P_("If the base stream should be closed when the filter stream is closed."),
106 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
107 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
111 g_filter_input_stream_set_property (GObject *object,
116 GFilterInputStream *filter_stream;
119 filter_stream = G_FILTER_INPUT_STREAM (object);
123 case PROP_BASE_STREAM:
124 obj = g_value_dup_object (value);
125 filter_stream->base_stream = G_INPUT_STREAM (obj);
128 case PROP_CLOSE_BASE:
129 g_filter_input_stream_set_close_base_stream (filter_stream,
130 g_value_get_boolean (value));
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
141 g_filter_input_stream_get_property (GObject *object,
146 GFilterInputStream *filter_stream;
147 GFilterInputStreamPrivate *priv;
149 filter_stream = G_FILTER_INPUT_STREAM (object);
150 priv = g_filter_input_stream_get_instance_private (filter_stream);
154 case PROP_BASE_STREAM:
155 g_value_set_object (value, filter_stream->base_stream);
158 case PROP_CLOSE_BASE:
159 g_value_set_boolean (value, priv->close_base);
163 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170 g_filter_input_stream_finalize (GObject *object)
172 GFilterInputStream *stream;
174 stream = G_FILTER_INPUT_STREAM (object);
176 g_object_unref (stream->base_stream);
178 G_OBJECT_CLASS (g_filter_input_stream_parent_class)->finalize (object);
182 g_filter_input_stream_init (GFilterInputStream *stream)
187 * g_filter_input_stream_get_base_stream:
188 * @stream: a #GFilterInputStream.
190 * Gets the base stream for the filter stream.
192 * Returns: (transfer none): a #GInputStream.
195 g_filter_input_stream_get_base_stream (GFilterInputStream *stream)
197 g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), NULL);
199 return stream->base_stream;
203 * g_filter_input_stream_get_close_base_stream:
204 * @stream: a #GFilterInputStream.
206 * Returns whether the base stream will be closed when @stream is
209 * Returns: %TRUE if the base stream will be closed.
212 g_filter_input_stream_get_close_base_stream (GFilterInputStream *stream)
214 GFilterInputStreamPrivate *priv;
216 g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), FALSE);
218 priv = g_filter_input_stream_get_instance_private (stream);
220 return priv->close_base;
224 * g_filter_input_stream_set_close_base_stream:
225 * @stream: a #GFilterInputStream.
226 * @close_base: %TRUE to close the base stream.
228 * Sets whether the base stream will be closed when @stream is closed.
231 g_filter_input_stream_set_close_base_stream (GFilterInputStream *stream,
234 GFilterInputStreamPrivate *priv;
236 g_return_if_fail (G_IS_FILTER_INPUT_STREAM (stream));
238 close_base = !!close_base;
240 priv = g_filter_input_stream_get_instance_private (stream);
242 if (priv->close_base != close_base)
244 priv->close_base = close_base;
245 g_object_notify (G_OBJECT (stream), "close-base-stream");
250 g_filter_input_stream_read (GInputStream *stream,
253 GCancellable *cancellable,
256 GFilterInputStream *filter_stream;
257 GInputStream *base_stream;
260 filter_stream = G_FILTER_INPUT_STREAM (stream);
261 base_stream = filter_stream->base_stream;
263 nread = g_input_stream_read (base_stream,
273 g_filter_input_stream_skip (GInputStream *stream,
275 GCancellable *cancellable,
278 GFilterInputStream *filter_stream;
279 GInputStream *base_stream;
282 filter_stream = G_FILTER_INPUT_STREAM (stream);
283 base_stream = filter_stream->base_stream;
285 nskipped = g_input_stream_skip (base_stream,
293 g_filter_input_stream_close (GInputStream *stream,
294 GCancellable *cancellable,
297 GFilterInputStream *filter_stream = G_FILTER_INPUT_STREAM (stream);
298 GFilterInputStreamPrivate *priv = g_filter_input_stream_get_instance_private (filter_stream);
301 if (priv->close_base)
303 res = g_input_stream_close (filter_stream->base_stream,