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 "gfilterinputstream.h"
25 #include "ginputstream.h"
26 #include "gsimpleasyncresult.h"
31 * SECTION:gfilterinputstream
32 * @short_description: Filter Input Stream
35 * Base class for input 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_input_stream_set_property (GObject *object,
52 static void g_filter_input_stream_get_property (GObject *object,
56 static void g_filter_input_stream_finalize (GObject *object);
59 static gssize g_filter_input_stream_read (GInputStream *stream,
62 GCancellable *cancellable,
64 static gssize g_filter_input_stream_skip (GInputStream *stream,
66 GCancellable *cancellable,
68 static gboolean g_filter_input_stream_close (GInputStream *stream,
69 GCancellable *cancellable,
72 G_DEFINE_TYPE (GFilterInputStream, g_filter_input_stream, G_TYPE_INPUT_STREAM)
74 #define GET_PRIVATE(inst) G_TYPE_INSTANCE_GET_PRIVATE (inst, \
75 G_TYPE_FILTER_INPUT_STREAM, GFilterInputStreamPrivate)
80 GAsyncReadyCallback outstanding_callback;
81 gpointer outstanding_user_data;
82 } GFilterInputStreamPrivate;
85 g_filter_input_stream_class_init (GFilterInputStreamClass *klass)
87 GObjectClass *object_class;
88 GInputStreamClass *istream_class;
90 object_class = G_OBJECT_CLASS (klass);
91 object_class->get_property = g_filter_input_stream_get_property;
92 object_class->set_property = g_filter_input_stream_set_property;
93 object_class->finalize = g_filter_input_stream_finalize;
95 istream_class = G_INPUT_STREAM_CLASS (klass);
96 istream_class->read_fn = g_filter_input_stream_read;
97 istream_class->skip = g_filter_input_stream_skip;
98 istream_class->close_fn = g_filter_input_stream_close;
100 g_type_class_add_private (klass, sizeof (GFilterInputStreamPrivate));
102 g_object_class_install_property (object_class,
104 g_param_spec_object ("base-stream",
105 P_("The Filter Base Stream"),
106 P_("The underlying base stream on which the io ops will be done."),
108 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
109 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
111 g_object_class_install_property (object_class,
113 g_param_spec_boolean ("close-base-stream",
114 P_("Close Base Stream"),
115 P_("If the base stream should be closed when the filter stream is closed."),
116 TRUE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
117 G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
121 g_filter_input_stream_set_property (GObject *object,
126 GFilterInputStream *filter_stream;
129 filter_stream = G_FILTER_INPUT_STREAM (object);
133 case PROP_BASE_STREAM:
134 obj = g_value_dup_object (value);
135 filter_stream->base_stream = G_INPUT_STREAM (obj);
138 case PROP_CLOSE_BASE:
139 g_filter_input_stream_set_close_base_stream (filter_stream,
140 g_value_get_boolean (value));
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 g_filter_input_stream_get_property (GObject *object,
156 GFilterInputStream *filter_stream;
158 filter_stream = G_FILTER_INPUT_STREAM (object);
162 case PROP_BASE_STREAM:
163 g_value_set_object (value, filter_stream->base_stream);
166 case PROP_CLOSE_BASE:
167 g_value_set_boolean (value, GET_PRIVATE (filter_stream)->close_base);
171 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
178 g_filter_input_stream_finalize (GObject *object)
180 GFilterInputStream *stream;
182 stream = G_FILTER_INPUT_STREAM (object);
184 g_object_unref (stream->base_stream);
186 G_OBJECT_CLASS (g_filter_input_stream_parent_class)->finalize (object);
190 g_filter_input_stream_init (GFilterInputStream *stream)
196 * g_filter_input_stream_get_base_stream:
197 * @stream: a #GFilterInputStream.
199 * Gets the base stream for the filter stream.
201 * Returns: (transfer none): a #GInputStream.
204 g_filter_input_stream_get_base_stream (GFilterInputStream *stream)
206 g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), NULL);
208 return stream->base_stream;
212 * g_filter_input_stream_get_close_base_stream:
213 * @stream: a #GFilterInputStream.
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_input_stream_get_close_base_stream (GFilterInputStream *stream)
223 g_return_val_if_fail (G_IS_FILTER_INPUT_STREAM (stream), FALSE);
225 return GET_PRIVATE (stream)->close_base;
229 * g_filter_input_stream_set_close_base_stream:
230 * @stream: a #GFilterInputStream.
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_input_stream_set_close_base_stream (GFilterInputStream *stream,
239 GFilterInputStreamPrivate *priv;
241 g_return_if_fail (G_IS_FILTER_INPUT_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_input_stream_read (GInputStream *stream,
258 GCancellable *cancellable,
261 GFilterInputStream *filter_stream;
262 GInputStream *base_stream;
265 filter_stream = G_FILTER_INPUT_STREAM (stream);
266 base_stream = filter_stream->base_stream;
268 nread = g_input_stream_read (base_stream,
278 g_filter_input_stream_skip (GInputStream *stream,
280 GCancellable *cancellable,
283 GFilterInputStream *filter_stream;
284 GInputStream *base_stream;
287 filter_stream = G_FILTER_INPUT_STREAM (stream);
288 base_stream = filter_stream->base_stream;
290 nskipped = g_input_stream_skip (base_stream,
298 g_filter_input_stream_close (GInputStream *stream,
299 GCancellable *cancellable,
304 if (GET_PRIVATE (stream)->close_base)
306 GFilterInputStream *filter_stream;
307 GInputStream *base_stream;
309 filter_stream = G_FILTER_INPUT_STREAM (stream);
310 base_stream = filter_stream->base_stream;
312 res = g_input_stream_close (base_stream,