Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright (C) 2009 Red Hat, Inc.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Alexander Larsson <alexl@redhat.com>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include <string.h>
26 : :
27 : : #include "gconverteroutputstream.h"
28 : : #include "gpollableoutputstream.h"
29 : : #include "gcancellable.h"
30 : : #include "gioenumtypes.h"
31 : : #include "gioerror.h"
32 : : #include "glibintl.h"
33 : :
34 : :
35 : : /**
36 : : * GConverterOutputStream:
37 : : *
38 : : * Converter output stream implements [class@Gio.OutputStream] and allows
39 : : * conversion of data of various types during reading.
40 : : *
41 : : * As of GLib 2.34, `GConverterOutputStream` implements
42 : : * [iface@Gio.PollableOutputStream].
43 : : */
44 : :
45 : : #define INITIAL_BUFFER_SIZE 4096
46 : :
47 : : typedef struct {
48 : : char *data;
49 : : gsize start;
50 : : gsize end;
51 : : gsize size;
52 : : } Buffer;
53 : :
54 : : struct _GConverterOutputStreamPrivate {
55 : : gboolean at_output_end;
56 : : gboolean finished;
57 : : GConverter *converter;
58 : : Buffer output_buffer; /* To be converted and written */
59 : : Buffer converted_buffer; /* Already converted */
60 : : };
61 : :
62 : : /* Buffering strategy:
63 : : *
64 : : * Each time we write we must at least consume some input, or
65 : : * return an error. Thus we start with writing all already
66 : : * converted data and *then* we start converting (reporting
67 : : * an error at any point in this).
68 : : *
69 : : * Its possible that what the user wrote is not enough data
70 : : * for the converter, so we must then buffer it in output_buffer
71 : : * and ask for more data, but we want to avoid this as much as
72 : : * possible, converting directly from the users buffer.
73 : : */
74 : :
75 : : enum {
76 : : PROP_0,
77 : : PROP_CONVERTER
78 : : };
79 : :
80 : : static void g_converter_output_stream_set_property (GObject *object,
81 : : guint prop_id,
82 : : const GValue *value,
83 : : GParamSpec *pspec);
84 : : static void g_converter_output_stream_get_property (GObject *object,
85 : : guint prop_id,
86 : : GValue *value,
87 : : GParamSpec *pspec);
88 : : static void g_converter_output_stream_finalize (GObject *object);
89 : : static gssize g_converter_output_stream_write (GOutputStream *stream,
90 : : const void *buffer,
91 : : gsize count,
92 : : GCancellable *cancellable,
93 : : GError **error);
94 : : static gboolean g_converter_output_stream_flush (GOutputStream *stream,
95 : : GCancellable *cancellable,
96 : : GError **error);
97 : :
98 : : static gboolean g_converter_output_stream_can_poll (GPollableOutputStream *stream);
99 : : static gboolean g_converter_output_stream_is_writable (GPollableOutputStream *stream);
100 : : static gssize g_converter_output_stream_write_nonblocking (GPollableOutputStream *stream,
101 : : const void *buffer,
102 : : gsize size,
103 : : GError **error);
104 : :
105 : : static GSource *g_converter_output_stream_create_source (GPollableOutputStream *stream,
106 : : GCancellable *cancellable);
107 : :
108 : : static void g_converter_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
109 : :
110 : 164787 : G_DEFINE_TYPE_WITH_CODE (GConverterOutputStream,
111 : : g_converter_output_stream,
112 : 6 : G_TYPE_FILTER_OUTPUT_STREAM,
113 : 6 : G_ADD_PRIVATE (GConverterOutputStream)
114 : 6 : G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
115 : : g_converter_output_stream_pollable_iface_init))
116 : :
117 : : static void
118 : 12 : g_converter_output_stream_class_init (GConverterOutputStreamClass *klass)
119 : : {
120 : : GObjectClass *object_class;
121 : : GOutputStreamClass *istream_class;
122 : :
123 : 12 : object_class = G_OBJECT_CLASS (klass);
124 : 12 : object_class->get_property = g_converter_output_stream_get_property;
125 : 12 : object_class->set_property = g_converter_output_stream_set_property;
126 : 12 : object_class->finalize = g_converter_output_stream_finalize;
127 : :
128 : 12 : istream_class = G_OUTPUT_STREAM_CLASS (klass);
129 : 12 : istream_class->write_fn = g_converter_output_stream_write;
130 : 12 : istream_class->flush = g_converter_output_stream_flush;
131 : :
132 : : /**
133 : : * GConverterOutputStream:converter:
134 : : *
135 : : * The converter object.
136 : : */
137 : 18 : g_object_class_install_property (object_class,
138 : : PROP_CONVERTER,
139 : 6 : g_param_spec_object ("converter", NULL, NULL,
140 : 6 : G_TYPE_CONVERTER,
141 : : G_PARAM_READWRITE|
142 : : G_PARAM_CONSTRUCT_ONLY|
143 : : G_PARAM_STATIC_STRINGS));
144 : :
145 : 12 : }
146 : :
147 : : static void
148 : 12 : g_converter_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
149 : : {
150 : 12 : iface->can_poll = g_converter_output_stream_can_poll;
151 : 12 : iface->is_writable = g_converter_output_stream_is_writable;
152 : 12 : iface->write_nonblocking = g_converter_output_stream_write_nonblocking;
153 : 12 : iface->create_source = g_converter_output_stream_create_source;
154 : 12 : }
155 : :
156 : : static void
157 : 51 : g_converter_output_stream_finalize (GObject *object)
158 : : {
159 : : GConverterOutputStreamPrivate *priv;
160 : : GConverterOutputStream *stream;
161 : :
162 : 51 : stream = G_CONVERTER_OUTPUT_STREAM (object);
163 : 51 : priv = stream->priv;
164 : :
165 : 51 : g_free (priv->output_buffer.data);
166 : 51 : g_free (priv->converted_buffer.data);
167 : 51 : if (priv->converter)
168 : 51 : g_object_unref (priv->converter);
169 : :
170 : 51 : G_OBJECT_CLASS (g_converter_output_stream_parent_class)->finalize (object);
171 : 51 : }
172 : :
173 : : static void
174 : 52 : g_converter_output_stream_set_property (GObject *object,
175 : : guint prop_id,
176 : : const GValue *value,
177 : : GParamSpec *pspec)
178 : : {
179 : : GConverterOutputStream *cstream;
180 : :
181 : 52 : cstream = G_CONVERTER_OUTPUT_STREAM (object);
182 : :
183 : 52 : switch (prop_id)
184 : : {
185 : 26 : case PROP_CONVERTER:
186 : 52 : cstream->priv->converter = g_value_dup_object (value);
187 : 52 : break;
188 : :
189 : 0 : default:
190 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191 : 0 : break;
192 : : }
193 : :
194 : 52 : }
195 : :
196 : : static void
197 : 2 : g_converter_output_stream_get_property (GObject *object,
198 : : guint prop_id,
199 : : GValue *value,
200 : : GParamSpec *pspec)
201 : : {
202 : : GConverterOutputStreamPrivate *priv;
203 : : GConverterOutputStream *cstream;
204 : :
205 : 2 : cstream = G_CONVERTER_OUTPUT_STREAM (object);
206 : 2 : priv = cstream->priv;
207 : :
208 : 2 : switch (prop_id)
209 : : {
210 : 1 : case PROP_CONVERTER:
211 : 2 : g_value_set_object (value, priv->converter);
212 : 2 : break;
213 : :
214 : 0 : default:
215 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216 : 0 : break;
217 : : }
218 : 2 : }
219 : :
220 : : static void
221 : 52 : g_converter_output_stream_init (GConverterOutputStream *stream)
222 : : {
223 : 52 : stream->priv = g_converter_output_stream_get_instance_private (stream);
224 : 52 : }
225 : :
226 : : /**
227 : : * g_converter_output_stream_new:
228 : : * @base_stream: a #GOutputStream
229 : : * @converter: a #GConverter
230 : : *
231 : : * Creates a new converter output stream for the @base_stream.
232 : : *
233 : : * Returns: a new #GOutputStream.
234 : : **/
235 : : GOutputStream *
236 : 52 : g_converter_output_stream_new (GOutputStream *base_stream,
237 : : GConverter *converter)
238 : : {
239 : : GOutputStream *stream;
240 : :
241 : 78 : g_return_val_if_fail (G_IS_OUTPUT_STREAM (base_stream), NULL);
242 : :
243 : 78 : stream = g_object_new (G_TYPE_CONVERTER_OUTPUT_STREAM,
244 : 26 : "base-stream", base_stream,
245 : 26 : "converter", converter,
246 : : NULL);
247 : :
248 : 52 : return stream;
249 : 26 : }
250 : :
251 : : static gsize
252 : 1323808 : buffer_data_size (Buffer *buffer)
253 : : {
254 : 1323808 : return buffer->end - buffer->start;
255 : : }
256 : :
257 : : static gsize
258 : 657704 : buffer_tailspace (Buffer *buffer)
259 : : {
260 : 657704 : return buffer->size - buffer->end;
261 : : }
262 : :
263 : : static char *
264 : 329044 : buffer_data (Buffer *buffer)
265 : : {
266 : : /* The buffer is grown on demand, so @data is NULL until something has been
267 : : * put in it. Offsetting it would be undefined behavior, so callers must
268 : : * check buffer_data_size() first. */
269 : 329044 : g_assert (buffer->data != NULL);
270 : :
271 : 329044 : return buffer->data + buffer->start;
272 : : }
273 : :
274 : : static void
275 : 526 : buffer_consumed (Buffer *buffer,
276 : : gsize count)
277 : : {
278 : 526 : buffer->start += count;
279 : 526 : if (buffer->start == buffer->end)
280 : 522 : buffer->start = buffer->end = 0;
281 : 526 : }
282 : :
283 : : static void
284 : 172764 : compact_buffer (Buffer *buffer)
285 : : {
286 : : gsize in_buffer;
287 : :
288 : 172764 : in_buffer = buffer_data_size (buffer);
289 : 345528 : memmove (buffer->data,
290 : 172764 : buffer->data + buffer->start,
291 : 86382 : in_buffer);
292 : 172764 : buffer->end -= buffer->start;
293 : 172764 : buffer->start = 0;
294 : 172764 : }
295 : :
296 : : static void
297 : 190 : grow_buffer (Buffer *buffer)
298 : : {
299 : : char *data;
300 : : gsize size, in_buffer;
301 : :
302 : 190 : if (buffer->size == 0)
303 : 56 : size = INITIAL_BUFFER_SIZE;
304 : : else
305 : 134 : size = buffer->size * 2;
306 : :
307 : 190 : data = g_malloc (size);
308 : 190 : in_buffer = buffer_data_size (buffer);
309 : :
310 : 190 : if (in_buffer != 0)
311 : 20 : memcpy (data,
312 : 10 : buffer->data + buffer->start,
313 : 5 : in_buffer);
314 : :
315 : 190 : g_free (buffer->data);
316 : 190 : buffer->data = data;
317 : 190 : buffer->end -= buffer->start;
318 : 190 : buffer->start = 0;
319 : 190 : buffer->size = size;
320 : 190 : }
321 : :
322 : : /* Ensures that the buffer can fit at_least_size bytes,
323 : : * *including* the current in-buffer data */
324 : : static void
325 : 328640 : buffer_ensure_space (Buffer *buffer,
326 : : gsize at_least_size)
327 : : {
328 : : gsize in_buffer, left_to_fill;
329 : :
330 : 328640 : in_buffer = buffer_data_size (buffer);
331 : :
332 : 328640 : if (in_buffer >= at_least_size)
333 : 0 : return;
334 : :
335 : 328640 : left_to_fill = buffer_tailspace (buffer);
336 : :
337 : 328640 : if (in_buffer + left_to_fill >= at_least_size)
338 : : {
339 : : /* We fit in remaining space at end */
340 : : /* If the copy is small, compact now anyway so we can fill more */
341 : 328564 : if (in_buffer < 256)
342 : 172764 : compact_buffer (buffer);
343 : 164282 : }
344 : 76 : else if (buffer->size >= at_least_size)
345 : : {
346 : : /* We fit, but only if we compact */
347 : 0 : compact_buffer (buffer);
348 : 0 : }
349 : : else
350 : : {
351 : : /* Need to grow buffer */
352 : 264 : while (buffer->size < at_least_size)
353 : 188 : grow_buffer (buffer);
354 : : }
355 : 164320 : }
356 : :
357 : : static void
358 : 164120 : buffer_append (Buffer *buffer,
359 : : const char *data,
360 : : gsize data_size)
361 : : {
362 : 246180 : buffer_ensure_space (buffer,
363 : 164120 : buffer_data_size (buffer) + data_size);
364 : 164120 : memcpy (buffer->data + buffer->end, data, data_size);
365 : 164120 : buffer->end += data_size;
366 : 164120 : }
367 : :
368 : :
369 : : static gboolean
370 : 164946 : flush_buffer (GConverterOutputStream *stream,
371 : : gboolean blocking,
372 : : GCancellable *cancellable,
373 : : GError **error)
374 : : {
375 : : GConverterOutputStreamPrivate *priv;
376 : : GOutputStream *base_stream;
377 : : gsize nwritten;
378 : : gsize available;
379 : : gboolean res;
380 : :
381 : 164946 : priv = stream->priv;
382 : :
383 : 164946 : base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;
384 : :
385 : 164946 : available = buffer_data_size (&priv->converted_buffer);
386 : 164946 : if (available > 0)
387 : : {
388 : 636 : res = g_pollable_stream_write_all (base_stream,
389 : 424 : buffer_data (&priv->converted_buffer),
390 : 212 : available,
391 : 212 : blocking,
392 : : &nwritten,
393 : 212 : cancellable,
394 : 212 : error);
395 : 424 : buffer_consumed (&priv->converted_buffer, nwritten);
396 : 424 : return res;
397 : : }
398 : 164522 : return TRUE;
399 : 82473 : }
400 : :
401 : :
402 : : static gssize
403 : 164458 : write_internal (GOutputStream *stream,
404 : : const void *buffer,
405 : : gsize count,
406 : : gboolean blocking,
407 : : GCancellable *cancellable,
408 : : GError **error)
409 : : {
410 : : GConverterOutputStream *cstream;
411 : : GConverterOutputStreamPrivate *priv;
412 : : gssize retval;
413 : : GConverterResult res;
414 : : gsize bytes_read;
415 : : gsize bytes_written;
416 : : GError *my_error;
417 : : const char *to_convert;
418 : : gsize to_convert_size, converted_bytes;
419 : : gboolean converting_from_buffer;
420 : :
421 : 164458 : cstream = G_CONVERTER_OUTPUT_STREAM (stream);
422 : 164458 : priv = cstream->priv;
423 : :
424 : : /* Write out all available pre-converted data and fail if
425 : : not possible */
426 : 164458 : if (!flush_buffer (cstream, blocking, cancellable, error))
427 : 0 : return -1;
428 : :
429 : 164458 : if (priv->finished)
430 : : {
431 : 3 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_MESSAGE_TOO_LARGE,
432 : 1 : _("Unexpected data after end of conversion"));
433 : 2 : return -1;
434 : : }
435 : :
436 : : /* Convert as much as possible */
437 : 164456 : if (buffer_data_size (&priv->output_buffer) > 0)
438 : : {
439 : 164084 : converting_from_buffer = TRUE;
440 : 164084 : buffer_append (&priv->output_buffer, buffer, count);
441 : 164084 : to_convert = buffer_data (&priv->output_buffer);
442 : 164084 : to_convert_size = buffer_data_size (&priv->output_buffer);
443 : 82042 : }
444 : : else
445 : : {
446 : 372 : converting_from_buffer = FALSE;
447 : 372 : to_convert = buffer;
448 : 372 : to_convert_size = count;
449 : : }
450 : :
451 : : /* Ensure we have *some* initial target space */
452 : 164456 : buffer_ensure_space (&priv->converted_buffer, to_convert_size);
453 : :
454 : 164456 : converted_bytes = 0;
455 : 164830 : while (!priv->finished && converted_bytes < to_convert_size)
456 : : {
457 : : /* Ensure we have *some* target space */
458 : 164464 : if (buffer_tailspace (&priv->converted_buffer) == 0)
459 : 0 : grow_buffer (&priv->converted_buffer);
460 : :
461 : : /* Try to convert to our buffer */
462 : 164464 : my_error = NULL;
463 : 246696 : res = g_converter_convert (priv->converter,
464 : 164464 : to_convert + converted_bytes,
465 : 82232 : to_convert_size - converted_bytes,
466 : 164464 : buffer_data (&priv->converted_buffer) + buffer_data_size (&priv->converted_buffer),
467 : 82232 : buffer_tailspace (&priv->converted_buffer),
468 : : 0,
469 : : &bytes_read,
470 : : &bytes_written,
471 : : &my_error);
472 : :
473 : 164464 : if (res != G_CONVERTER_ERROR)
474 : : {
475 : 370 : priv->converted_buffer.end += bytes_written;
476 : 370 : converted_bytes += bytes_read;
477 : :
478 : 370 : if (res == G_CONVERTER_FINISHED)
479 : 2 : priv->finished = TRUE;
480 : 185 : }
481 : : else
482 : : {
483 : : /* No-space errors can be handled locally: */
484 : 246141 : if (g_error_matches (my_error,
485 : 82047 : G_IO_ERROR,
486 : : G_IO_ERROR_NO_SPACE))
487 : : {
488 : : /* Need more destination space, grow it
489 : : * Note: if we actually grow the buffer (as opposed to compacting it),
490 : : * this will double the size, not just add one byte. */
491 : 6 : buffer_ensure_space (&priv->converted_buffer,
492 : 4 : priv->converted_buffer.size + 1);
493 : 4 : g_error_free (my_error);
494 : 4 : continue;
495 : : }
496 : :
497 : 164090 : if (converted_bytes > 0)
498 : : {
499 : : /* We got a conversion error, but we did convert some bytes before
500 : : that, so handle those before reporting the error */
501 : 4 : g_error_free (my_error);
502 : 4 : break;
503 : : }
504 : :
505 : 246129 : if (g_error_matches (my_error,
506 : 82043 : G_IO_ERROR,
507 : : G_IO_ERROR_PARTIAL_INPUT))
508 : : {
509 : : /* Consume everything to buffer that we append to next time
510 : : we write */
511 : 164084 : if (!converting_from_buffer)
512 : 36 : buffer_append (&priv->output_buffer, buffer, count);
513 : : /* in the converting_from_buffer case we already appended this */
514 : :
515 : 164084 : g_error_free (my_error);
516 : 164084 : return count; /* consume everything */
517 : : }
518 : :
519 : : /* Converted no data and got a normal error, return it */
520 : 2 : g_propagate_error (error, my_error);
521 : 2 : return -1;
522 : : }
523 : : }
524 : :
525 : 370 : if (converting_from_buffer)
526 : : {
527 : 36 : buffer_consumed (&priv->output_buffer, converted_bytes);
528 : 36 : retval = count;
529 : 18 : }
530 : : else
531 : 334 : retval = converted_bytes;
532 : :
533 : : /* We now successfully consumed retval bytes, so we can't return an error,
534 : : even if writing this to the base stream fails. If it does we'll just
535 : : stop early and report this error when we try again on the next
536 : : write call. */
537 : 370 : flush_buffer (cstream, blocking, cancellable, NULL);
538 : :
539 : 370 : return retval;
540 : 82229 : }
541 : :
542 : : static gssize
543 : 82398 : g_converter_output_stream_write (GOutputStream *stream,
544 : : const void *buffer,
545 : : gsize count,
546 : : GCancellable *cancellable,
547 : : GError **error)
548 : : {
549 : 82398 : return write_internal (stream, buffer, count, TRUE, cancellable, error);
550 : : }
551 : :
552 : : static gboolean
553 : 60 : g_converter_output_stream_flush (GOutputStream *stream,
554 : : GCancellable *cancellable,
555 : : GError **error)
556 : : {
557 : : GConverterOutputStream *cstream;
558 : : GConverterOutputStreamPrivate *priv;
559 : : GConverterResult res;
560 : : GError *my_error;
561 : : gboolean is_closing;
562 : : gboolean flushed;
563 : : gsize bytes_read;
564 : : gsize bytes_written;
565 : : gsize to_convert_size;
566 : :
567 : 60 : cstream = G_CONVERTER_OUTPUT_STREAM (stream);
568 : 60 : priv = cstream->priv;
569 : :
570 : 60 : is_closing = g_output_stream_is_closing (stream);
571 : :
572 : : /* Write out all available pre-converted data and fail if
573 : : not possible */
574 : 60 : if (!flush_buffer (cstream, TRUE, cancellable, error))
575 : 0 : return FALSE;
576 : :
577 : : /* Ensure we have *some* initial target space */
578 : 60 : buffer_ensure_space (&priv->converted_buffer, 1);
579 : :
580 : : /* Convert whole buffer */
581 : 60 : flushed = FALSE;
582 : 126 : while (!priv->finished && !flushed)
583 : : {
584 : : /* Ensure we have *some* target space */
585 : 68 : if (buffer_tailspace (&priv->converted_buffer) == 0)
586 : 2 : grow_buffer (&priv->converted_buffer);
587 : :
588 : : /* Try to convert to our buffer */
589 : 68 : to_convert_size = buffer_data_size (&priv->output_buffer);
590 : 68 : my_error = NULL;
591 : 136 : res = g_converter_convert (priv->converter,
592 : 36 : to_convert_size > 0 ? buffer_data (&priv->output_buffer) : NULL,
593 : 34 : to_convert_size,
594 : 68 : buffer_data (&priv->converted_buffer) + buffer_data_size (&priv->converted_buffer),
595 : 34 : buffer_tailspace (&priv->converted_buffer),
596 : 34 : is_closing ? G_CONVERTER_INPUT_AT_END : G_CONVERTER_FLUSH,
597 : : &bytes_read,
598 : : &bytes_written,
599 : : &my_error);
600 : :
601 : 68 : if (res != G_CONVERTER_ERROR)
602 : : {
603 : 66 : priv->converted_buffer.end += bytes_written;
604 : 66 : buffer_consumed (&priv->output_buffer, bytes_read);
605 : :
606 : 66 : if (res == G_CONVERTER_FINISHED)
607 : 48 : priv->finished = TRUE;
608 : 66 : if (!is_closing &&
609 : 8 : res == G_CONVERTER_FLUSHED)
610 : : {
611 : : /* Should not have returned FLUSHED with input left */
612 : 8 : g_assert (buffer_data_size (&priv->output_buffer) == 0);
613 : 8 : flushed = TRUE;
614 : 4 : }
615 : 33 : }
616 : : else
617 : : {
618 : : /* No-space errors can be handled locally: */
619 : 3 : if (g_error_matches (my_error,
620 : 1 : G_IO_ERROR,
621 : : G_IO_ERROR_NO_SPACE))
622 : : {
623 : : /* Need more destination space, grow it
624 : : * Note: if we actually grow the buffer (as opposed to compacting it),
625 : : * this will double the size, not just add one byte. */
626 : 0 : buffer_ensure_space (&priv->converted_buffer,
627 : 0 : priv->converted_buffer.size + 1);
628 : 0 : g_error_free (my_error);
629 : 0 : continue;
630 : : }
631 : :
632 : : /* Any other error, including PARTIAL_INPUT can't be fixed by now
633 : : and is an error */
634 : 2 : g_propagate_error (error, my_error);
635 : 2 : return FALSE;
636 : : }
637 : : }
638 : :
639 : : /* Now write all converted data to base stream */
640 : 58 : if (!flush_buffer (cstream, TRUE, cancellable, error))
641 : 0 : return FALSE;
642 : :
643 : 58 : return TRUE;
644 : 30 : }
645 : :
646 : : static gboolean
647 : 2 : g_converter_output_stream_can_poll (GPollableOutputStream *stream)
648 : : {
649 : 2 : GOutputStream *base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;
650 : :
651 : 4 : return (G_IS_POLLABLE_OUTPUT_STREAM (base_stream) &&
652 : 2 : g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (base_stream)));
653 : : }
654 : :
655 : : static gboolean
656 : 2 : g_converter_output_stream_is_writable (GPollableOutputStream *stream)
657 : : {
658 : 2 : GOutputStream *base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;
659 : :
660 : 2 : return g_pollable_output_stream_is_writable (G_POLLABLE_OUTPUT_STREAM (base_stream));
661 : : }
662 : :
663 : : static gssize
664 : 82060 : g_converter_output_stream_write_nonblocking (GPollableOutputStream *stream,
665 : : const void *buffer,
666 : : gsize count,
667 : : GError **error)
668 : : {
669 : 123090 : return write_internal (G_OUTPUT_STREAM (stream), buffer, count, FALSE,
670 : 41030 : NULL, error);
671 : : }
672 : :
673 : : static GSource *
674 : 0 : g_converter_output_stream_create_source (GPollableOutputStream *stream,
675 : : GCancellable *cancellable)
676 : : {
677 : 0 : GOutputStream *base_stream = G_FILTER_OUTPUT_STREAM (stream)->base_stream;
678 : : GSource *base_source, *pollable_source;
679 : :
680 : 0 : base_source = g_pollable_output_stream_create_source (G_POLLABLE_OUTPUT_STREAM (base_stream), NULL);
681 : 0 : pollable_source = g_pollable_source_new_full (stream, base_source,
682 : 0 : cancellable);
683 : 0 : g_source_unref (base_source);
684 : :
685 : 0 : return pollable_source;
686 : : }
687 : :
688 : : /**
689 : : * g_converter_output_stream_get_converter:
690 : : * @converter_stream: a #GConverterOutputStream
691 : : *
692 : : * Gets the #GConverter that is used by @converter_stream.
693 : : *
694 : : * Returns: (transfer none): the converter of the converter output stream
695 : : *
696 : : * Since: 2.24
697 : : */
698 : : GConverter *
699 : 20 : g_converter_output_stream_get_converter (GConverterOutputStream *converter_stream)
700 : : {
701 : 20 : return converter_stream->priv->converter;
702 : : }
|