summaryrefslogtreecommitdiff
path: root/camel/camel-null-output-stream.c
blob: aff36a310acef2a948db9b0835835ce4553ef8be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * camel-null-output-stream.c
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * SECTION: camel-null-output-stream
 * @short_description: Null output stream
 * @include: camel/camel.h
 * @see_also: #GOutputStream
 *
 * #CamelNullOutputStream is analogous to writing to /dev/null, except it
 * counts the total number of bytes written to it.  This is primarily useful
 * for determining the final size of some outgoing data, especially if using
 * filters on the output stream.
 **/

#include "camel-null-output-stream.h"

#define CAMEL_NULL_OUTPUT_STREAM_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_NULL_OUTPUT_STREAM, CamelNullOutputStreamPrivate))

struct _CamelNullOutputStreamPrivate {
	gsize bytes_written;
};

G_DEFINE_TYPE (
	CamelNullOutputStream,
	camel_null_output_stream,
	G_TYPE_OUTPUT_STREAM)

static gssize
null_output_stream_write (GOutputStream *stream,
                          gconstpointer buffer,
                          gsize count,
                          GCancellable *cancellable,
                          GError **error)
{
	CamelNullOutputStreamPrivate *priv;

	priv = CAMEL_NULL_OUTPUT_STREAM_GET_PRIVATE (stream);

	priv->bytes_written += count;

	return count;
}

static void
camel_null_output_stream_class_init (CamelNullOutputStreamClass *class)
{
	GOutputStreamClass *stream_class;

	g_type_class_add_private (
		class, sizeof (CamelNullOutputStreamPrivate));

	stream_class = G_OUTPUT_STREAM_CLASS (class);
	stream_class->write_fn = null_output_stream_write;
}

static void
camel_null_output_stream_init (CamelNullOutputStream *null_stream)
{
	null_stream->priv = CAMEL_NULL_OUTPUT_STREAM_GET_PRIVATE (null_stream);
}

/**
 * camel_null_output_stream_new:
 *
 * Creates a new "null" output stream.
 *
 * Returns: a new #GOutputStream
 *
 * Since: 3.12
 **/
GOutputStream *
camel_null_output_stream_new (void)
{
	return g_object_new (CAMEL_TYPE_NULL_OUTPUT_STREAM, NULL);
}

/**
 * camel_null_output_stream_get_bytes_written:
 * @null_stream: a #CamelNullOutputStream
 *
 * Gets the total number of bytes written to @null_stream.
 *
 * Returns: total byte count
 *
 * Since: 3.12
 **/
gsize
camel_null_output_stream_get_bytes_written (CamelNullOutputStream *null_stream)
{
	g_return_val_if_fail (CAMEL_IS_NULL_OUTPUT_STREAM (null_stream), 0);

	return null_stream->priv->bytes_written;
}