mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-24 14:36:13 +01:00
Merge branch 'convert-bytes' into 'main'
Add g_converter_convert_bytes See merge request GNOME/glib!4035
This commit is contained in:
commit
86dd02f487
@ -23,6 +23,9 @@
|
||||
#include "config.h"
|
||||
#include "gconverter.h"
|
||||
#include "glibintl.h"
|
||||
#include "gmemoryinputstream.h"
|
||||
#include "gmemoryoutputstream.h"
|
||||
#include "gconverteroutputstream.h"
|
||||
|
||||
|
||||
/**
|
||||
@ -207,3 +210,48 @@ g_converter_reset (GConverter *converter)
|
||||
|
||||
(* iface->reset) (converter);
|
||||
}
|
||||
|
||||
/**
|
||||
* g_converter_convert_bytes:
|
||||
* @converter: the `GConverter` to use
|
||||
* @bytes: the data to convert
|
||||
* @error: location to store the error occurring
|
||||
*
|
||||
* Applies @converter to the data in @bytes.
|
||||
*
|
||||
* Returns: (transfer full): A newly-allocated
|
||||
* `GBytes` with the converted data, or `NULL` if an error
|
||||
* occurred
|
||||
*
|
||||
* Since: 2.82
|
||||
*/
|
||||
GBytes *
|
||||
g_converter_convert_bytes (GConverter *converter,
|
||||
GBytes *bytes,
|
||||
GError **error)
|
||||
{
|
||||
GInputStream *input;
|
||||
GOutputStream *output;
|
||||
GOutputStream *conv;
|
||||
GOutputStreamSpliceFlags flags;
|
||||
GBytes *result = NULL;
|
||||
|
||||
g_converter_reset (converter);
|
||||
|
||||
input = g_memory_input_stream_new_from_bytes (bytes);
|
||||
output = g_memory_output_stream_new_resizable ();
|
||||
conv = g_converter_output_stream_new (output, converter);
|
||||
|
||||
flags = G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET;
|
||||
|
||||
if (g_output_stream_splice (conv, input, flags, NULL, error) != -1)
|
||||
{
|
||||
result = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (output));
|
||||
}
|
||||
|
||||
g_object_unref (conv);
|
||||
g_object_unref (output);
|
||||
g_object_unref (input);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -84,6 +84,11 @@ GConverterResult g_converter_convert (GConverter *converter,
|
||||
GIO_AVAILABLE_IN_ALL
|
||||
void g_converter_reset (GConverter *converter);
|
||||
|
||||
GIO_AVAILABLE_IN_2_82
|
||||
GBytes * g_converter_convert_bytes (GConverter *converter,
|
||||
GBytes *bytes,
|
||||
GError **error);
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
|
68
gio/tests/converter.c
Normal file
68
gio/tests/converter.c
Normal file
@ -0,0 +1,68 @@
|
||||
/* GLib testing framework examples and tests
|
||||
* Copyright (C) 2024 Red Hat, Inc.
|
||||
* Authors: Matthias Clasen
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
*
|
||||
* 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; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#include <glib/glib.h>
|
||||
#include <gio/gio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static void
|
||||
test_convert_bytes (void)
|
||||
{
|
||||
char data[8192];
|
||||
GBytes *bytes;
|
||||
GConverter *converter;
|
||||
GError *error = NULL;
|
||||
GBytes *result;
|
||||
|
||||
for (gsize i = 0; i < sizeof (data); i++)
|
||||
data[i] = g_test_rand_int_range (0, 256);
|
||||
|
||||
bytes = g_bytes_new_static (data, sizeof (data));
|
||||
|
||||
converter = G_CONVERTER (g_zlib_compressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP, 9));
|
||||
result = g_converter_convert_bytes (converter, bytes, &error);
|
||||
g_assert_no_error (error);
|
||||
g_assert_nonnull (result);
|
||||
|
||||
g_bytes_unref (result);
|
||||
g_object_unref (converter);
|
||||
|
||||
converter = G_CONVERTER (g_zlib_decompressor_new (G_ZLIB_COMPRESSOR_FORMAT_GZIP));
|
||||
result = g_converter_convert_bytes (converter, bytes, &error);
|
||||
g_assert_nonnull (error);
|
||||
g_error_free (error);
|
||||
g_assert_true (result == NULL);
|
||||
|
||||
g_object_unref (converter);
|
||||
g_bytes_unref (bytes);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/converter/bytes", test_convert_bytes);
|
||||
|
||||
return g_test_run();
|
||||
}
|
@ -70,6 +70,7 @@ gio_tests = {
|
||||
# "LC_COLLATE support for collation orders other than simple codepoint order"
|
||||
'can_fail' : linux_libc == 'musl',
|
||||
},
|
||||
'converter' : {},
|
||||
'credentials' : {},
|
||||
'data-input-stream' : {},
|
||||
'data-output-stream' : {},
|
||||
|
Loading…
Reference in New Issue
Block a user