1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-08-20 07:38:54 +02:00
Files
.gitlab-ci
.reuse
LICENSES
docs
fuzzing
gio
completion
gdbus-2.0
inotify
kqueue
tests
win32
xdgmime
data-to-c.py
dbus-daemon.xml
gaction.c
gaction.h
gactiongroup.c
gactiongroup.h
gactiongroupexporter.c
gactiongroupexporter.h
gactionmap.c
gactionmap.h
gappinfo.c
gappinfo.h
gappinfoprivate.h
gapplication-tool.c
gapplication.c
gapplication.h
gapplicationcommandline.c
gapplicationcommandline.h
gapplicationimpl-dbus.c
gapplicationimpl.h
gasynchelper.c
gasynchelper.h
gasyncinitable.c
gasyncinitable.h
gasyncresult.c
gasyncresult.h
gbufferedinputstream.c
gbufferedinputstream.h
gbufferedoutputstream.c
gbufferedoutputstream.h
gbytesicon.c
gbytesicon.h
gcancellable.c
gcancellable.h
gcharsetconverter.c
gcharsetconverter.h
gcocoanotificationbackend.m
gcontenttype-win32.c
gcontenttype.c
gcontenttype.h
gcontenttypeprivate.h
gcontextspecificgroup.c
gcontextspecificgroup.h
gconverter.c
gconverter.h
gconverterinputstream.c
gconverterinputstream.h
gconverteroutputstream.c
gconverteroutputstream.h
gcredentials.c
gcredentials.h
gcredentialsprivate.h
gdatagrambased.c
gdatagrambased.h
gdatainputstream.c
gdatainputstream.h
gdataoutputstream.c
gdataoutputstream.h
gdbus-tool.c
gdbusactiongroup-private.h
gdbusactiongroup.c
gdbusactiongroup.h
gdbusaddress.c
gdbusaddress.h
gdbusauth.c
gdbusauth.h
gdbusauthmechanism.c
gdbusauthmechanism.h
gdbusauthmechanismanon.c
gdbusauthmechanismanon.h
gdbusauthmechanismexternal.c
gdbusauthmechanismexternal.h
gdbusauthmechanismsha1.c
gdbusauthmechanismsha1.h
gdbusauthobserver.c
gdbusauthobserver.h
gdbusconnection.c
gdbusconnection.h
gdbusdaemon.c
gdbusdaemon.h
gdbuserror.c
gdbuserror.h
gdbusinterface.c
gdbusinterface.h
gdbusinterfaceskeleton.c
gdbusinterfaceskeleton.h
gdbusintrospection.c
gdbusintrospection.h
gdbusmenumodel.c
gdbusmenumodel.h
gdbusmessage.c
gdbusmessage.h
gdbusmethodinvocation.c
gdbusmethodinvocation.h
gdbusnameowning.c
gdbusnameowning.h
gdbusnamewatching.c
gdbusnamewatching.h
gdbusobject.c
gdbusobject.h
gdbusobjectmanager.c
gdbusobjectmanager.h
gdbusobjectmanagerclient.c
gdbusobjectmanagerclient.h
gdbusobjectmanagerserver.c
gdbusobjectmanagerserver.h
gdbusobjectproxy.c
gdbusobjectproxy.h
gdbusobjectskeleton.c
gdbusobjectskeleton.h
gdbusprivate.c
gdbusprivate.h
gdbusproxy.c
gdbusproxy.h
gdbusserver.c
gdbusserver.h
gdbusutils.c
gdbusutils.h
gdebugcontroller.c
gdebugcontroller.h
gdebugcontrollerdbus.c
gdebugcontrollerdbus.h
gdelayedsettingsbackend.c
gdelayedsettingsbackend.h
gdesktopappinfo.c
gdesktopappinfo.h
gdocumentportal.c
gdocumentportal.h
gdrive.c
gdrive.h
gdtlsclientconnection.c
gdtlsclientconnection.h
gdtlsconnection.c
gdtlsconnection.h
gdtlsserverconnection.c
gdtlsserverconnection.h
gdummyfile.c
gdummyfile.h
gdummyproxyresolver.c
gdummyproxyresolver.h
gdummytlsbackend.c
gdummytlsbackend.h
gemblem.c
gemblem.h
gemblemedicon.c
gemblemedicon.h
gfdonotificationbackend.c
gfile.c
gfile.h
gfileattribute-priv.h
gfileattribute.c
gfileattribute.h
gfiledescriptorbased.c
gfiledescriptorbased.h
gfileenumerator.c
gfileenumerator.h
gfileicon.c
gfileicon.h
gfileinfo-priv.h
gfileinfo.c
gfileinfo.h
gfileinputstream.c
gfileinputstream.h
gfileiostream.c
gfileiostream.h
gfilemonitor.c
gfilemonitor.h
gfilenamecompleter.c
gfilenamecompleter.h
gfileoutputstream.c
gfileoutputstream.h
gfilterinputstream.c
gfilterinputstream.h
gfilteroutputstream.c
gfilteroutputstream.h
ggtknotificationbackend.c
ghttpproxy.c
ghttpproxy.h
gicon.c
gicon.h
ginetaddress.c
ginetaddress.h
ginetaddressmask.c
ginetaddressmask.h
ginetsocketaddress.c
ginetsocketaddress.h
ginitable.c
ginitable.h
ginputstream.c
ginputstream.h
gio-autocleanups.h
gio-launch-desktop.c
gio-querymodules.c
gio-tool-cat.c
gio-tool-copy.c
gio-tool-info.c
gio-tool-launch.c
gio-tool-list.c
gio-tool-mime.c
gio-tool-mkdir.c
gio-tool-monitor.c
gio-tool-mount.c
gio-tool-move.c
gio-tool-open.c
gio-tool-remove.c
gio-tool-rename.c
gio-tool-save.c
gio-tool-set.c
gio-tool-trash.c
gio-tool-tree.c
gio-tool.c
gio-tool.h
gio.h
gio.rc.in
gio.stp.in
gio_probes.d
gio_trace.h
gioenums.h
gioenumtypes.c.template
gioenumtypes.h.template
gioerror.c
gioerror.h
giomodule-priv.c
giomodule-priv.h
giomodule.c
giomodule.h
gioprivate.h
gioscheduler.c
gioscheduler.h
giostream.c
giostream.h
giotypes.h
giounix-private.c
giounix-private.h
giowin32-afunix.h
giowin32-priv.h
giowin32-private.c
gkeyfilesettingsbackend.c
glib-compile-resources.c
glib-compile-schemas.c
glistmodel.c
glistmodel.h
gliststore.c
gliststore.h
gloadableicon.c
gloadableicon.h
glocalfile.c
glocalfile.h
glocalfileenumerator.c
glocalfileenumerator.h
glocalfileinfo.c
glocalfileinfo.h
glocalfileinputstream.c
glocalfileinputstream.h
glocalfileiostream.c
glocalfileiostream.h
glocalfilemonitor.c
glocalfilemonitor.h
glocalfileoutputstream.c
glocalfileoutputstream.h
glocalvfs.c
glocalvfs.h
gmarshal-internal.c
gmarshal-internal.h
gmarshal-internal.list
gmemoryinputstream.c
gmemoryinputstream.h
gmemorymonitor.c
gmemorymonitor.h
gmemorymonitordbus.c
gmemorymonitordbus.h
gmemorymonitorportal.c
gmemorymonitorportal.h
gmemorymonitorwin32.c
gmemoryoutputstream.c
gmemoryoutputstream.h
gmemorysettingsbackend.c
gmenu.c
gmenu.h
gmenuexporter.c
gmenuexporter.h
gmenumodel.c
gmenumodel.h
gmount.c
gmount.h
gmountoperation.c
gmountoperation.h
gmountprivate.h
gnativesocketaddress.c
gnativesocketaddress.h
gnativevolumemonitor.c
gnativevolumemonitor.h
gnetworkaddress.c
gnetworkaddress.h
gnetworking.c
gnetworking.h.in
gnetworkingprivate.h
gnetworkmonitor.c
gnetworkmonitor.h
gnetworkmonitorbase.c
gnetworkmonitorbase.h
gnetworkmonitornetlink.c
gnetworkmonitornetlink.h
gnetworkmonitornm.c
gnetworkmonitornm.h
gnetworkmonitorportal.c
gnetworkmonitorportal.h
gnetworkservice.c
gnetworkservice.h
gnextstepsettingsbackend.m
gnotification-private.h
gnotification.c
gnotification.h
gnotificationbackend.c
gnotificationbackend.h
gnullsettingsbackend.c
gopenuriportal.c
gopenuriportal.h
gosxappinfo.h
gosxappinfo.m
gosxcontenttype.m
goutputstream.c
goutputstream.h
gpermission.c
gpermission.h
gpollableinputstream.c
gpollableinputstream.h
gpollableoutputstream.c
gpollableoutputstream.h
gpollableutils.c
gpollableutils.h
gpollfilemonitor.c
gpollfilemonitor.h
gportalnotificationbackend.c
gportalsupport.c
gportalsupport.h
gpowerprofilemonitor.c
gpowerprofilemonitor.h
gpowerprofilemonitordbus.c
gpowerprofilemonitordbus.h
gpowerprofilemonitorportal.c
gpowerprofilemonitorportal.h
gpropertyaction.c
gpropertyaction.h
gproxy.c
gproxy.h
gproxyaddress.c
gproxyaddress.h
gproxyaddressenumerator.c
gproxyaddressenumerator.h
gproxyresolver.c
gproxyresolver.h
gproxyresolverportal.c
gproxyresolverportal.h
gregistrysettingsbackend.c
gregistrysettingsbackend.h
gremoteactiongroup.c
gremoteactiongroup.h
gresolver.c
gresolver.h
gresource-tool.c
gresource.c
gresource.h
gresourcefile.c
gresourcefile.h
gsandbox.c
gsandbox.h
gschema.dtd
gschema.its
gschema.loc
gseekable.c
gseekable.h
gsettings-mapping.c
gsettings-mapping.h
gsettings-tool.c
gsettings.c
gsettings.h
gsettingsbackend.c
gsettingsbackend.h
gsettingsbackendinternal.h
gsettingsschema-internal.h
gsettingsschema.c
gsettingsschema.h
gsimpleaction.c
gsimpleaction.h
gsimpleactiongroup.c
gsimpleactiongroup.h
gsimpleasyncresult.c
gsimpleasyncresult.h
gsimpleiostream.c
gsimpleiostream.h
gsimplepermission.c
gsimplepermission.h
gsimpleproxyresolver.c
gsimpleproxyresolver.h
gsocket.c
gsocket.h
gsocketaddress.c
gsocketaddress.h
gsocketaddressenumerator.c
gsocketaddressenumerator.h
gsocketclient.c
gsocketclient.h
gsocketconnectable.c
gsocketconnectable.h
gsocketconnection.c
gsocketconnection.h
gsocketcontrolmessage.c
gsocketcontrolmessage.h
gsocketinputstream.c
gsocketinputstream.h
gsocketlistener.c
gsocketlistener.h
gsocketoutputstream.c
gsocketoutputstream.h
gsocketservice.c
gsocketservice.h
gsocks4aproxy.c
gsocks4aproxy.h
gsocks4proxy.c
gsocks4proxy.h
gsocks5proxy.c
gsocks5proxy.h
gsrvtarget.c
gsrvtarget.h
gsubprocess.c
gsubprocess.h
gsubprocesslauncher-private.h
gsubprocesslauncher.c
gsubprocesslauncher.h
gtask.c
gtask.h
gtcpconnection.c
gtcpconnection.h
gtcpwrapperconnection.c
gtcpwrapperconnection.h
gtestdbus.c
gtestdbus.h
gthemedicon.c
gthemedicon.h
gthreadedresolver.c
gthreadedresolver.h
gthreadedsocketservice.c
gthreadedsocketservice.h
gtlsbackend.c
gtlsbackend.h
gtlscertificate.c
gtlscertificate.h
gtlsclientconnection.c
gtlsclientconnection.h
gtlsconnection.c
gtlsconnection.h
gtlsdatabase.c
gtlsdatabase.h
gtlsfiledatabase.c
gtlsfiledatabase.h
gtlsinteraction.c
gtlsinteraction.h
gtlspassword.c
gtlspassword.h
gtlsserverconnection.c
gtlsserverconnection.h
gtrashportal.c
gtrashportal.h
gunionvolumemonitor.c
gunionvolumemonitor.h
gunixconnection.c
gunixconnection.h
gunixcredentialsmessage.c
gunixcredentialsmessage.h
gunixfdlist.c
gunixfdlist.h
gunixfdmessage.c
gunixfdmessage.h
gunixinputstream.c
gunixinputstream.h
gunixmount.c
gunixmount.h
gunixmounts.c
gunixmounts.h
gunixoutputstream.c
gunixoutputstream.h
gunixsocketaddress.c
gunixsocketaddress.h
gunixvolume.c
gunixvolume.h
gunixvolumemonitor.c
gunixvolumemonitor.h
gvfs.c
gvfs.h
gvolume.c
gvolume.h
gvolumemonitor.c
gvolumemonitor.h
gwin32api-application-activation-manager.h
gwin32api-iterator.h
gwin32api-misc.h
gwin32api-package.h
gwin32api-storage.h
gwin32appinfo.c
gwin32appinfo.h
gwin32file-sync-stream.c
gwin32file-sync-stream.h
gwin32inputstream.c
gwin32inputstream.h
gwin32mount.c
gwin32mount.h
gwin32networkmonitor.c
gwin32networkmonitor.h
gwin32notificationbackend.c
gwin32outputstream.c
gwin32outputstream.h
gwin32packageparser.c
gwin32packageparser.h
gwin32registrykey.c
gwin32registrykey.h
gwin32sid.c
gwin32sid.h
gwin32volumemonitor.c
gwin32volumemonitor.h
gzlibcompressor.c
gzlibcompressor.h
gzlibdecompressor.c
gzlibdecompressor.h
meson.build
org.freedesktop.portal.Documents.xml
org.freedesktop.portal.OpenURI.xml
org.freedesktop.portal.ProxyResolver.xml
org.freedesktop.portal.Trash.xml
strinfo.c
thumbnail-verify.c
thumbnail-verify.h
glib
gmodule
gobject
gthread
m4macros
po
subprojects
tests
tools
.clang-format
.dir-locals.el
.editorconfig
.gitignore
.gitlab-ci.yml
.gitmodules
.lcovrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
INSTALL.md
NEWS
README.md
README.win32.md
SECURITY.md
glib.doap
meson.build
meson_options.txt
msvc_recommended_pragmas.h
glib/gio/gbufferedinputstream.c

1278 lines
38 KiB
C
Raw Normal View History

/* GIO - GLib Input, Output and Streaming Library
2010-07-05 07:40:38 -04:00
*
* Copyright (C) 2006-2007 Red Hat, Inc.
* Copyright (C) 2007 Jürg Billeter
*
* 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
2014-01-23 12:58:29 +01:00
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
2010-07-05 07:40:38 -04:00
* Author: Christian Kellner <gicmo@gnome.org>
*/
#include "config.h"
#include "gbufferedinputstream.h"
#include "ginputstream.h"
#include "gcancellable.h"
#include "gasyncresult.h"
#include "gtask.h"
#include "gseekable.h"
#include "gioerror.h"
#include <string.h>
#include "glibintl.h"
/**
* SECTION:gbufferedinputstream
* @short_description: Buffered Input Stream
* @include: gio/gio.h
* @see_also: #GFilterInputStream, #GInputStream
2010-07-05 07:40:38 -04:00
*
* Buffered input stream implements #GFilterInputStream and provides
* for buffered reads.
*
* By default, #GBufferedInputStream's buffer size is set at 4 kilobytes.
2010-07-05 07:40:38 -04:00
*
* To create a buffered input stream, use g_buffered_input_stream_new(),
* or g_buffered_input_stream_new_sized() to specify the buffer's size at
* construction.
2010-07-05 07:40:38 -04:00
*
* To get the size of a buffer within a buffered input stream, use
* g_buffered_input_stream_get_buffer_size(). To change the size of a
* buffered input stream's buffer, use
2010-07-05 07:40:38 -04:00
* g_buffered_input_stream_set_buffer_size(). Note that the buffer's size
* cannot be reduced below the size of the data within the buffer.
2010-07-05 07:40:38 -04:00
*/
#define DEFAULT_BUFFER_SIZE 4096
struct _GBufferedInputStreamPrivate {
guint8 *buffer;
gsize len;
gsize pos;
gsize end;
GAsyncReadyCallback outstanding_callback;
};
enum {
PROP_0,
PROP_BUFSIZE
};
static void g_buffered_input_stream_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void g_buffered_input_stream_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void g_buffered_input_stream_finalize (GObject *object);
static gssize g_buffered_input_stream_skip (GInputStream *stream,
2010-07-05 07:40:38 -04:00
gsize count,
GCancellable *cancellable,
GError **error);
static void g_buffered_input_stream_skip_async (GInputStream *stream,
2010-07-05 07:40:38 -04:00
gsize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gssize g_buffered_input_stream_skip_finish (GInputStream *stream,
2010-07-05 07:40:38 -04:00
GAsyncResult *result,
GError **error);
static gssize g_buffered_input_stream_read (GInputStream *stream,
2010-07-05 07:40:38 -04:00
void *buffer,
gsize count,
GCancellable *cancellable,
GError **error);
static gssize g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
2010-07-05 07:40:38 -04:00
gssize count,
GCancellable *cancellable,
GError **error);
static void g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
2010-07-05 07:40:38 -04:00
gssize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data);
static gssize g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
2010-07-05 07:40:38 -04:00
GAsyncResult *result,
GError **error);
static void g_buffered_input_stream_seekable_iface_init (GSeekableIface *iface);
static goffset g_buffered_input_stream_tell (GSeekable *seekable);
static gboolean g_buffered_input_stream_can_seek (GSeekable *seekable);
static gboolean g_buffered_input_stream_seek (GSeekable *seekable,
goffset offset,
GSeekType type,
GCancellable *cancellable,
GError **error);
static gboolean g_buffered_input_stream_can_truncate (GSeekable *seekable);
static gboolean g_buffered_input_stream_truncate (GSeekable *seekable,
goffset offset,
GCancellable *cancellable,
GError **error);
static void compact_buffer (GBufferedInputStream *stream);
G_DEFINE_TYPE_WITH_CODE (GBufferedInputStream,
g_buffered_input_stream,
G_TYPE_FILTER_INPUT_STREAM,
G_ADD_PRIVATE (GBufferedInputStream)
G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
g_buffered_input_stream_seekable_iface_init))
static void
g_buffered_input_stream_class_init (GBufferedInputStreamClass *klass)
{
GObjectClass *object_class;
GInputStreamClass *istream_class;
GBufferedInputStreamClass *bstream_class;
object_class = G_OBJECT_CLASS (klass);
object_class->get_property = g_buffered_input_stream_get_property;
object_class->set_property = g_buffered_input_stream_set_property;
object_class->finalize = g_buffered_input_stream_finalize;
istream_class = G_INPUT_STREAM_CLASS (klass);
istream_class->skip = g_buffered_input_stream_skip;
istream_class->skip_async = g_buffered_input_stream_skip_async;
istream_class->skip_finish = g_buffered_input_stream_skip_finish;
istream_class->read_fn = g_buffered_input_stream_read;
bstream_class = G_BUFFERED_INPUT_STREAM_CLASS (klass);
bstream_class->fill = g_buffered_input_stream_real_fill;
bstream_class->fill_async = g_buffered_input_stream_real_fill_async;
bstream_class->fill_finish = g_buffered_input_stream_real_fill_finish;
2010-07-05 07:40:38 -04:00
g_object_class_install_property (object_class,
PROP_BUFSIZE,
g_param_spec_uint ("buffer-size",
P_("Buffer Size"),
P_("The size of the backend buffer"),
1,
G_MAXUINT,
DEFAULT_BUFFER_SIZE,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT |
G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB));
}
/**
* g_buffered_input_stream_get_buffer_size:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
*
* Gets the size of the input buffer.
2010-07-05 07:40:38 -04:00
*
* Returns: the current buffer size.
2010-07-05 07:40:38 -04:00
*/
gsize
g_buffered_input_stream_get_buffer_size (GBufferedInputStream *stream)
{
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), 0);
return stream->priv->len;
}
/**
* g_buffered_input_stream_set_buffer_size:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
* @size: a #gsize
*
2010-07-05 07:40:38 -04:00
* Sets the size of the internal buffer of @stream to @size, or to the
* size of the contents of the buffer. The buffer can never be resized
* smaller than its current contents.
2010-07-05 07:40:38 -04:00
*/
void
2010-07-05 07:40:38 -04:00
g_buffered_input_stream_set_buffer_size (GBufferedInputStream *stream,
gsize size)
{
GBufferedInputStreamPrivate *priv;
gsize in_buffer;
guint8 *buffer;
2010-07-05 07:40:38 -04:00
g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
priv = stream->priv;
if (priv->len == size)
return;
if (priv->buffer)
{
in_buffer = priv->end - priv->pos;
/* Never resize smaller than current buffer contents */
size = MAX (size, in_buffer);
buffer = g_malloc (size);
memcpy (buffer, priv->buffer + priv->pos, in_buffer);
priv->len = size;
priv->pos = 0;
priv->end = in_buffer;
g_free (priv->buffer);
priv->buffer = buffer;
}
else
{
priv->len = size;
priv->pos = 0;
priv->end = 0;
priv->buffer = g_malloc (size);
}
g_object_notify (G_OBJECT (stream), "buffer-size");
}
static void
g_buffered_input_stream_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GBufferedInputStream *bstream;
bstream = G_BUFFERED_INPUT_STREAM (object);
switch (prop_id)
{
case PROP_BUFSIZE:
g_buffered_input_stream_set_buffer_size (bstream, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
g_buffered_input_stream_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GBufferedInputStreamPrivate *priv;
GBufferedInputStream *bstream;
bstream = G_BUFFERED_INPUT_STREAM (object);
priv = bstream->priv;
switch (prop_id)
2010-07-05 07:40:38 -04:00
{
case PROP_BUFSIZE:
g_value_set_uint (value, priv->len);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
g_buffered_input_stream_finalize (GObject *object)
{
GBufferedInputStreamPrivate *priv;
GBufferedInputStream *stream;
stream = G_BUFFERED_INPUT_STREAM (object);
priv = stream->priv;
g_free (priv->buffer);
G_OBJECT_CLASS (g_buffered_input_stream_parent_class)->finalize (object);
}
static void
g_buffered_input_stream_seekable_iface_init (GSeekableIface *iface)
{
iface->tell = g_buffered_input_stream_tell;
iface->can_seek = g_buffered_input_stream_can_seek;
iface->seek = g_buffered_input_stream_seek;
iface->can_truncate = g_buffered_input_stream_can_truncate;
iface->truncate_fn = g_buffered_input_stream_truncate;
}
static void
g_buffered_input_stream_init (GBufferedInputStream *stream)
{
stream->priv = g_buffered_input_stream_get_instance_private (stream);
}
/**
* g_buffered_input_stream_new:
2010-07-05 07:40:38 -04:00
* @base_stream: a #GInputStream
*
* Creates a new #GInputStream from the given @base_stream, with
* a buffer set to the default size (4 kilobytes).
*
* Returns: a #GInputStream for the given @base_stream.
2010-07-05 07:40:38 -04:00
*/
GInputStream *
g_buffered_input_stream_new (GInputStream *base_stream)
{
GInputStream *stream;
g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
"base-stream", base_stream,
NULL);
return stream;
}
/**
* g_buffered_input_stream_new_sized:
2010-07-05 07:40:38 -04:00
* @base_stream: a #GInputStream
* @size: a #gsize
*
* Creates a new #GBufferedInputStream from the given @base_stream,
* with a buffer set to @size.
*
* Returns: a #GInputStream.
2010-07-05 07:40:38 -04:00
*/
GInputStream *
g_buffered_input_stream_new_sized (GInputStream *base_stream,
gsize size)
{
GInputStream *stream;
g_return_val_if_fail (G_IS_INPUT_STREAM (base_stream), NULL);
stream = g_object_new (G_TYPE_BUFFERED_INPUT_STREAM,
"base-stream", base_stream,
"buffer-size", (guint)size,
NULL);
return stream;
}
/**
* g_buffered_input_stream_fill:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
* @count: the number of bytes that will be read from the stream
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
* @error: location to store the error occurring, or %NULL to ignore
*
2010-07-05 07:40:38 -04:00
* Tries to read @count bytes from the stream into the buffer.
* Will block during this read.
2010-07-05 07:40:38 -04:00
*
* If @count is zero, returns zero and does nothing. A value of @count
* larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.
*
* On success, the number of bytes read into the buffer is returned.
* It is not an error if this is not the same as the requested size, as it
* can happen e.g. near the end of a file. Zero is returned on end of file
* (or if @count is zero), but never otherwise.
*
* If @count is -1 then the attempted read size is equal to the number of
* bytes that are required to fill the buffer.
*
* If @cancellable is not %NULL, then the operation can be cancelled by
* triggering the cancellable object from another thread. If the operation
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
* operation was partially finished when the operation was cancelled the
* partial result will be returned, without an error.
*
* On error -1 is returned and @error is set accordingly.
2010-07-05 07:40:38 -04:00
*
* For the asynchronous, non-blocking, version of this function, see
* g_buffered_input_stream_fill_async().
*
2010-07-05 07:40:38 -04:00
* Returns: the number of bytes read into @stream's buffer, up to @count,
* or -1 on error.
2010-07-05 07:40:38 -04:00
*/
gssize
g_buffered_input_stream_fill (GBufferedInputStream *stream,
gssize count,
GCancellable *cancellable,
GError **error)
{
GBufferedInputStreamClass *class;
GInputStream *input_stream;
gssize res;
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
2010-07-05 07:40:38 -04:00
input_stream = G_INPUT_STREAM (stream);
2010-07-05 07:40:38 -04:00
if (count < -1)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("Too large count value passed to %s"), G_STRFUNC);
return -1;
}
if (!g_input_stream_set_pending (input_stream, error))
return -1;
if (cancellable)
g_cancellable_push_current (cancellable);
2010-07-05 07:40:38 -04:00
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
res = class->fill (stream, count, cancellable, error);
if (cancellable)
g_cancellable_pop_current (cancellable);
2010-07-05 07:40:38 -04:00
g_input_stream_clear_pending (input_stream);
2010-07-05 07:40:38 -04:00
return res;
}
static void
async_fill_callback_wrapper (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GBufferedInputStream *stream = G_BUFFERED_INPUT_STREAM (source_object);
g_input_stream_clear_pending (G_INPUT_STREAM (stream));
(*stream->priv->outstanding_callback) (source_object, res, user_data);
g_object_unref (stream);
}
/**
* g_buffered_input_stream_fill_async:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
* @count: the number of bytes that will be read from the stream
* @io_priority: the [I/O priority][io-priority] of the request
* @cancellable: (nullable): optional #GCancellable object
* @callback: (scope async): a #GAsyncReadyCallback
* @user_data: (closure): a #gpointer
*
* Reads data into @stream's buffer asynchronously, up to @count size.
* @io_priority can be used to prioritize reads. For the synchronous
* version of this function, see g_buffered_input_stream_fill().
*
* If @count is -1 then the attempted read size is equal to the number
* of bytes that are required to fill the buffer.
2010-07-05 07:40:38 -04:00
*/
void
g_buffered_input_stream_fill_async (GBufferedInputStream *stream,
gssize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GBufferedInputStreamClass *class;
GError *error = NULL;
g_return_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream));
if (count == 0)
{
GTask *task;
task = g_task_new (stream, cancellable, callback, user_data);
g_task_set_source_tag (task, g_buffered_input_stream_fill_async);
g_task_return_int (task, 0);
g_object_unref (task);
return;
}
2010-07-05 07:40:38 -04:00
if (count < -1)
{
g_task_report_new_error (stream, callback, user_data,
g_buffered_input_stream_fill_async,
G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
_("Too large count value passed to %s"),
G_STRFUNC);
return;
}
2010-07-05 07:40:38 -04:00
if (!g_input_stream_set_pending (G_INPUT_STREAM (stream), &error))
{
g_task_report_error (stream, callback, user_data,
g_buffered_input_stream_fill_async,
error);
return;
}
2010-07-05 07:40:38 -04:00
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
2010-07-05 07:40:38 -04:00
stream->priv->outstanding_callback = callback;
g_object_ref (stream);
class->fill_async (stream, count, io_priority, cancellable,
async_fill_callback_wrapper, user_data);
}
/**
* g_buffered_input_stream_fill_finish:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
* @result: a #GAsyncResult
* @error: a #GError
*
* Finishes an asynchronous read.
2010-07-05 07:40:38 -04:00
*
* Returns: a #gssize of the read stream, or `-1` on an error.
2010-07-05 07:40:38 -04:00
*/
gssize
g_buffered_input_stream_fill_finish (GBufferedInputStream *stream,
GAsyncResult *result,
GError **error)
{
GBufferedInputStreamClass *class;
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
g_return_val_if_fail (G_IS_ASYNC_RESULT (result), -1);
if (g_async_result_legacy_propagate_error (result, error))
return -1;
else if (g_async_result_is_tagged (result, g_buffered_input_stream_fill_async))
return g_task_propagate_int (G_TASK (result), error);
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
return class->fill_finish (stream, result, error);
}
/**
* g_buffered_input_stream_get_available:
2010-07-05 07:40:38 -04:00
* @stream: #GBufferedInputStream
*
* Gets the size of the available data within the stream.
2010-07-05 07:40:38 -04:00
*
* Returns: size of the available stream.
*/
gsize
g_buffered_input_stream_get_available (GBufferedInputStream *stream)
{
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
return stream->priv->end - stream->priv->pos;
}
/**
* g_buffered_input_stream_peek:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
* @buffer: (array length=count) (element-type guint8): a pointer to
* an allocated chunk of memory
2010-07-05 07:40:38 -04:00
* @offset: a #gsize
* @count: a #gsize
*
* Peeks in the buffer, copying data of size @count into @buffer,
* offset @offset bytes.
2010-07-05 07:40:38 -04:00
*
* Returns: a #gsize of the number of bytes peeked, or -1 on error.
*/
gsize
g_buffered_input_stream_peek (GBufferedInputStream *stream,
void *buffer,
gsize offset,
gsize count)
{
gsize available;
gsize end;
2010-07-05 07:40:38 -04:00
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
g_return_val_if_fail (buffer != NULL, -1);
available = g_buffered_input_stream_get_available (stream);
if (offset > available)
return 0;
2010-07-05 07:40:38 -04:00
end = MIN (offset + count, available);
count = end - offset;
2010-07-05 07:40:38 -04:00
memcpy (buffer, stream->priv->buffer + stream->priv->pos + offset, count);
return count;
}
/**
* g_buffered_input_stream_peek_buffer:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
* @count: (out): a #gsize to get the number of bytes available in the buffer
*
* Returns the buffer with the currently available bytes. The returned
* buffer must not be modified and will become invalid when reading from
* the stream or filling the buffer.
*
* Returns: (array length=count) (element-type guint8) (transfer none):
* read-only buffer
2010-07-05 07:40:38 -04:00
*/
const void*
g_buffered_input_stream_peek_buffer (GBufferedInputStream *stream,
gsize *count)
{
GBufferedInputStreamPrivate *priv;
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), NULL);
priv = stream->priv;
2010-07-05 07:40:38 -04:00
if (count)
*count = priv->end - priv->pos;
return priv->buffer + priv->pos;
}
static void
compact_buffer (GBufferedInputStream *stream)
{
GBufferedInputStreamPrivate *priv;
gsize current_size;
priv = stream->priv;
current_size = priv->end - priv->pos;
2010-07-05 07:40:38 -04:00
memmove (priv->buffer, priv->buffer + priv->pos, current_size);
2010-07-05 07:40:38 -04:00
priv->pos = 0;
priv->end = current_size;
}
static gssize
g_buffered_input_stream_real_fill (GBufferedInputStream *stream,
gssize count,
GCancellable *cancellable,
GError **error)
{
GBufferedInputStreamPrivate *priv;
GInputStream *base_stream;
gssize nread;
gsize in_buffer;
priv = stream->priv;
if (count == -1)
count = priv->len;
2010-07-05 07:40:38 -04:00
in_buffer = priv->end - priv->pos;
/* Never fill more than can fit in the buffer */
Fix signedness warning in gio/gbufferedoutputstream.c:g_buffered_output_stream_set_buffer_size() gio/gbufferedoutputstream.c: In function ‘g_buffered_output_stream_set_buffer_size’: glib/gmacros.h:806:26: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘goffset’ {aka ‘long int’} 806 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ^ gio/gbufferedoutputstream.c:211:14: note: in expansion of macro ‘MAX’ 211 | size = MAX (size, priv->pos); | ^~~ Fix signedness warning in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’}K [-Werror=sign-compare] 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:664:11: note: in expansion of macro ‘MIN’ 664 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:667:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 667 | if (priv->len - priv->end < count) | ^ Fix signedness warnings in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill_async() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill_async’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:1075:11: note: in expansion of macro ‘MIN’ 1075 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:1078:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 1078 | if (priv->len - priv->end < count) | ^
2020-11-16 21:51:34 +01:00
count = MIN ((gsize) count, priv->len - in_buffer);
/* If requested length does not fit at end, compact */
Fix signedness warning in gio/gbufferedoutputstream.c:g_buffered_output_stream_set_buffer_size() gio/gbufferedoutputstream.c: In function ‘g_buffered_output_stream_set_buffer_size’: glib/gmacros.h:806:26: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘goffset’ {aka ‘long int’} 806 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ^ gio/gbufferedoutputstream.c:211:14: note: in expansion of macro ‘MAX’ 211 | size = MAX (size, priv->pos); | ^~~ Fix signedness warning in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’}K [-Werror=sign-compare] 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:664:11: note: in expansion of macro ‘MIN’ 664 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:667:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 667 | if (priv->len - priv->end < count) | ^ Fix signedness warnings in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill_async() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill_async’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:1075:11: note: in expansion of macro ‘MIN’ 1075 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:1078:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 1078 | if (priv->len - priv->end < count) | ^
2020-11-16 21:51:34 +01:00
if (priv->len - priv->end < (gsize) count)
compact_buffer (stream);
base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
nread = g_input_stream_read (base_stream,
priv->buffer + priv->end,
count,
cancellable,
error);
if (nread > 0)
priv->end += nread;
2010-07-05 07:40:38 -04:00
return nread;
}
static gssize
g_buffered_input_stream_skip (GInputStream *stream,
gsize count,
GCancellable *cancellable,
GError **error)
{
GBufferedInputStream *bstream;
GBufferedInputStreamPrivate *priv;
GBufferedInputStreamClass *class;
GInputStream *base_stream;
gsize available, bytes_skipped;
gssize nread;
bstream = G_BUFFERED_INPUT_STREAM (stream);
priv = bstream->priv;
available = priv->end - priv->pos;
if (count <= available)
{
priv->pos += count;
return count;
}
2010-07-05 07:40:38 -04:00
/* Full request not available, skip all currently available and
* request refill for more
*/
2010-07-05 07:40:38 -04:00
priv->pos = 0;
priv->end = 0;
bytes_skipped = available;
count -= available;
if (bytes_skipped > 0)
error = NULL; /* Ignore further errors if we already read some data */
2010-07-05 07:40:38 -04:00
if (count > priv->len)
{
/* Large request, shortcut buffer */
2010-07-05 07:40:38 -04:00
base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
nread = g_input_stream_skip (base_stream,
count,
cancellable,
error);
2010-07-05 07:40:38 -04:00
if (nread < 0 && bytes_skipped == 0)
return -1;
2010-07-05 07:40:38 -04:00
if (nread > 0)
bytes_skipped += nread;
2010-07-05 07:40:38 -04:00
return bytes_skipped;
}
2010-07-05 07:40:38 -04:00
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
nread = class->fill (bstream, priv->len, cancellable, error);
2010-07-05 07:40:38 -04:00
if (nread < 0)
{
if (bytes_skipped == 0)
return -1;
else
return bytes_skipped;
}
2010-07-05 07:40:38 -04:00
available = priv->end - priv->pos;
count = MIN (count, available);
2010-07-05 07:40:38 -04:00
bytes_skipped += count;
priv->pos += count;
2010-07-05 07:40:38 -04:00
return bytes_skipped;
}
static gssize
g_buffered_input_stream_read (GInputStream *stream,
void *buffer,
gsize count,
GCancellable *cancellable,
GError **error)
{
GBufferedInputStream *bstream;
GBufferedInputStreamPrivate *priv;
GBufferedInputStreamClass *class;
GInputStream *base_stream;
gsize available, bytes_read;
gssize nread;
bstream = G_BUFFERED_INPUT_STREAM (stream);
priv = bstream->priv;
available = priv->end - priv->pos;
if (count <= available)
{
memcpy (buffer, priv->buffer + priv->pos, count);
priv->pos += count;
return count;
}
2010-07-05 07:40:38 -04:00
/* Full request not available, read all currently available and
* request refill for more
*/
memcpy (buffer, priv->buffer + priv->pos, available);
priv->pos = 0;
priv->end = 0;
bytes_read = available;
count -= available;
if (bytes_read > 0)
error = NULL; /* Ignore further errors if we already read some data */
2010-07-05 07:40:38 -04:00
if (count > priv->len)
{
/* Large request, shortcut buffer */
2010-07-05 07:40:38 -04:00
base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
nread = g_input_stream_read (base_stream,
2010-07-05 07:40:38 -04:00
(char *)buffer + bytes_read,
count,
cancellable,
error);
if (nread < 0 && bytes_read == 0)
return -1;
2010-07-05 07:40:38 -04:00
if (nread > 0)
bytes_read += nread;
2010-07-05 07:40:38 -04:00
return bytes_read;
}
2010-07-05 07:40:38 -04:00
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
nread = class->fill (bstream, priv->len, cancellable, error);
if (nread < 0)
{
if (bytes_read == 0)
return -1;
else
return bytes_read;
}
2010-07-05 07:40:38 -04:00
available = priv->end - priv->pos;
count = MIN (count, available);
2010-07-05 07:40:38 -04:00
memcpy ((char *)buffer + bytes_read, (char *)priv->buffer + priv->pos, count);
bytes_read += count;
priv->pos += count;
2010-07-05 07:40:38 -04:00
return bytes_read;
}
static goffset
g_buffered_input_stream_tell (GSeekable *seekable)
{
GBufferedInputStream *bstream;
GBufferedInputStreamPrivate *priv;
GInputStream *base_stream;
GSeekable *base_stream_seekable;
gsize available;
goffset base_offset;
bstream = G_BUFFERED_INPUT_STREAM (seekable);
priv = bstream->priv;
base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
if (!G_IS_SEEKABLE (base_stream))
return 0;
base_stream_seekable = G_SEEKABLE (base_stream);
available = priv->end - priv->pos;
base_offset = g_seekable_tell (base_stream_seekable);
return base_offset - available;
}
static gboolean
g_buffered_input_stream_can_seek (GSeekable *seekable)
{
GInputStream *base_stream;
base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
return G_IS_SEEKABLE (base_stream) && g_seekable_can_seek (G_SEEKABLE (base_stream));
}
static gboolean
g_buffered_input_stream_seek (GSeekable *seekable,
goffset offset,
GSeekType type,
GCancellable *cancellable,
GError **error)
{
GBufferedInputStream *bstream;
GBufferedInputStreamPrivate *priv;
GInputStream *base_stream;
GSeekable *base_stream_seekable;
bstream = G_BUFFERED_INPUT_STREAM (seekable);
priv = bstream->priv;
base_stream = G_FILTER_INPUT_STREAM (seekable)->base_stream;
if (!G_IS_SEEKABLE (base_stream))
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
_("Seek not supported on base stream"));
return FALSE;
}
base_stream_seekable = G_SEEKABLE (base_stream);
if (type == G_SEEK_CUR)
{
if (offset <= (goffset) (priv->end - priv->pos) &&
offset >= (goffset) -priv->pos)
{
priv->pos += offset;
return TRUE;
}
else
{
offset -= priv->end - priv->pos;
}
}
if (g_seekable_seek (base_stream_seekable, offset, type, cancellable, error))
{
priv->pos = 0;
priv->end = 0;
return TRUE;
}
else
{
return FALSE;
}
}
static gboolean
g_buffered_input_stream_can_truncate (GSeekable *seekable)
{
return FALSE;
}
static gboolean
g_buffered_input_stream_truncate (GSeekable *seekable,
goffset offset,
GCancellable *cancellable,
GError **error)
{
g_set_error_literal (error,
G_IO_ERROR,
G_IO_ERROR_NOT_SUPPORTED,
_("Cannot truncate GBufferedInputStream"));
return FALSE;
}
/**
* g_buffered_input_stream_read_byte:
2010-07-05 07:40:38 -04:00
* @stream: a #GBufferedInputStream
* @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
* @error: location to store the error occurring, or %NULL to ignore
*
* Tries to read a single byte from the stream or the buffer. Will block
* during this read.
*
* On success, the byte read from the stream is returned. On end of stream
* -1 is returned but it's not an exceptional error and @error is not set.
2010-07-05 07:40:38 -04:00
*
* If @cancellable is not %NULL, then the operation can be cancelled by
* triggering the cancellable object from another thread. If the operation
* was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. If an
* operation was partially finished when the operation was cancelled the
* partial result will be returned, without an error.
*
* On error -1 is returned and @error is set accordingly.
2010-07-05 07:40:38 -04:00
*
* Returns: the byte read from the @stream, or -1 on end of stream or error.
2010-07-05 07:40:38 -04:00
*/
int
g_buffered_input_stream_read_byte (GBufferedInputStream *stream,
GCancellable *cancellable,
GError **error)
{
GBufferedInputStreamPrivate *priv;
GBufferedInputStreamClass *class;
GInputStream *input_stream;
gsize available;
gssize nread;
g_return_val_if_fail (G_IS_BUFFERED_INPUT_STREAM (stream), -1);
priv = stream->priv;
input_stream = G_INPUT_STREAM (stream);
if (g_input_stream_is_closed (input_stream))
{
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
_("Stream is already closed"));
return -1;
}
if (!g_input_stream_set_pending (input_stream, error))
return -1;
available = priv->end - priv->pos;
if (available != 0)
{
g_input_stream_clear_pending (input_stream);
return priv->buffer[priv->pos++];
}
/* Byte not available, request refill for more */
if (cancellable)
g_cancellable_push_current (cancellable);
priv->pos = 0;
priv->end = 0;
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
nread = class->fill (stream, priv->len, cancellable, error);
if (cancellable)
g_cancellable_pop_current (cancellable);
g_input_stream_clear_pending (input_stream);
if (nread <= 0)
return -1; /* error or end of stream */
return priv->buffer[priv->pos++];
}
/* ************************** */
/* Async stuff implementation */
/* ************************** */
static void
fill_async_callback (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GError *error;
gssize res;
GTask *task = user_data;
2010-07-05 07:40:38 -04:00
error = NULL;
res = g_input_stream_read_finish (G_INPUT_STREAM (source_object),
2010-07-05 07:40:38 -04:00
result, &error);
if (res == -1)
g_task_return_error (task, error);
else
{
GBufferedInputStream *stream;
GBufferedInputStreamPrivate *priv;
stream = g_task_get_source_object (task);
priv = G_BUFFERED_INPUT_STREAM (stream)->priv;
g_assert_cmpint (priv->end + res, <=, priv->len);
priv->end += res;
g_task_return_int (task, res);
}
2010-07-05 07:40:38 -04:00
g_object_unref (task);
}
static void
g_buffered_input_stream_real_fill_async (GBufferedInputStream *stream,
gssize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GBufferedInputStreamPrivate *priv;
GInputStream *base_stream;
GTask *task;
gsize in_buffer;
priv = stream->priv;
if (count == -1)
count = priv->len;
2010-07-05 07:40:38 -04:00
in_buffer = priv->end - priv->pos;
/* Never fill more than can fit in the buffer */
Fix signedness warning in gio/gbufferedoutputstream.c:g_buffered_output_stream_set_buffer_size() gio/gbufferedoutputstream.c: In function ‘g_buffered_output_stream_set_buffer_size’: glib/gmacros.h:806:26: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘goffset’ {aka ‘long int’} 806 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ^ gio/gbufferedoutputstream.c:211:14: note: in expansion of macro ‘MAX’ 211 | size = MAX (size, priv->pos); | ^~~ Fix signedness warning in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’}K [-Werror=sign-compare] 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:664:11: note: in expansion of macro ‘MIN’ 664 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:667:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 667 | if (priv->len - priv->end < count) | ^ Fix signedness warnings in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill_async() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill_async’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:1075:11: note: in expansion of macro ‘MIN’ 1075 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:1078:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 1078 | if (priv->len - priv->end < count) | ^
2020-11-16 21:51:34 +01:00
count = MIN ((gsize) count, priv->len - in_buffer);
/* If requested length does not fit at end, compact */
Fix signedness warning in gio/gbufferedoutputstream.c:g_buffered_output_stream_set_buffer_size() gio/gbufferedoutputstream.c: In function ‘g_buffered_output_stream_set_buffer_size’: glib/gmacros.h:806:26: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘goffset’ {aka ‘long int’} 806 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ^ gio/gbufferedoutputstream.c:211:14: note: in expansion of macro ‘MAX’ 211 | size = MAX (size, priv->pos); | ^~~ Fix signedness warning in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’}K [-Werror=sign-compare] 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:664:11: note: in expansion of macro ‘MIN’ 664 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:667:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 667 | if (priv->len - priv->end < count) | ^ Fix signedness warnings in gio/gbufferedinputstream.c:g_buffered_input_stream_real_fill_async() gio/gbufferedinputstream.c: In function ‘g_buffered_input_stream_real_fill_async’: glib/gmacros.h:809:26: error: comparison of integer expressions of different signedness: ‘gssize’ {aka ‘long int’} and ‘gsize’ {aka ‘long unsigned int’} 809 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ^ gio/gbufferedinputstream.c:1075:11: note: in expansion of macro ‘MIN’ 1075 | count = MIN (count, priv->len - in_buffer); | ^~~ gio/gbufferedinputstream.c:1078:29: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘gssize’ {aka ‘long int’} 1078 | if (priv->len - priv->end < count) | ^
2020-11-16 21:51:34 +01:00
if (priv->len - priv->end < (gsize) count)
compact_buffer (stream);
task = g_task_new (stream, cancellable, callback, user_data);
g_task_set_source_tag (task, g_buffered_input_stream_real_fill_async);
2010-07-05 07:40:38 -04:00
base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
g_input_stream_read_async (base_stream,
2010-07-05 07:40:38 -04:00
priv->buffer + priv->end,
count,
io_priority,
cancellable,
fill_async_callback,
task);
}
static gssize
g_buffered_input_stream_real_fill_finish (GBufferedInputStream *stream,
2010-07-05 07:40:38 -04:00
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (g_task_is_valid (result, stream), -1);
return g_task_propagate_int (G_TASK (result), error);
}
2010-07-05 07:40:38 -04:00
typedef struct
{
gsize bytes_skipped;
gsize count;
} SkipAsyncData;
2010-07-05 07:40:38 -04:00
static void
free_skip_async_data (gpointer _data)
{
SkipAsyncData *data = _data;
g_slice_free (SkipAsyncData, data);
}
static void
2010-07-05 07:40:38 -04:00
large_skip_callback (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GTask *task = G_TASK (user_data);
SkipAsyncData *data;
GError *error;
gssize nread;
data = g_task_get_task_data (task);
2010-07-05 07:40:38 -04:00
error = NULL;
nread = g_input_stream_skip_finish (G_INPUT_STREAM (source_object),
2010-07-05 07:40:38 -04:00
result, &error);
/* Only report the error if we've not already read some data */
if (nread < 0 && data->bytes_skipped == 0)
g_task_return_error (task, error);
else
{
if (error)
g_error_free (error);
2010-07-05 07:40:38 -04:00
if (nread > 0)
data->bytes_skipped += nread;
2010-07-05 07:40:38 -04:00
g_task_return_int (task, data->bytes_skipped);
}
g_object_unref (task);
}
static void
2010-07-05 07:40:38 -04:00
skip_fill_buffer_callback (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GTask *task = G_TASK (user_data);
GBufferedInputStream *bstream;
GBufferedInputStreamPrivate *priv;
SkipAsyncData *data;
GError *error;
gssize nread;
gsize available;
bstream = G_BUFFERED_INPUT_STREAM (source_object);
priv = bstream->priv;
2010-07-05 07:40:38 -04:00
data = g_task_get_task_data (task);
2010-07-05 07:40:38 -04:00
error = NULL;
nread = g_buffered_input_stream_fill_finish (bstream,
2010-07-05 07:40:38 -04:00
result, &error);
if (nread < 0 && data->bytes_skipped == 0)
g_task_return_error (task, error);
else
{
if (error)
g_error_free (error);
if (nread > 0)
{
available = priv->end - priv->pos;
data->count = MIN (data->count, available);
data->bytes_skipped += data->count;
priv->pos += data->count;
}
2010-07-05 07:40:38 -04:00
g_assert (data->bytes_skipped <= G_MAXSSIZE);
g_task_return_int (task, data->bytes_skipped);
}
g_object_unref (task);
}
static void
2010-07-05 07:40:38 -04:00
g_buffered_input_stream_skip_async (GInputStream *stream,
gsize count,
int io_priority,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GBufferedInputStream *bstream;
GBufferedInputStreamPrivate *priv;
GBufferedInputStreamClass *class;
GInputStream *base_stream;
gsize available;
GTask *task;
SkipAsyncData *data;
bstream = G_BUFFERED_INPUT_STREAM (stream);
priv = bstream->priv;
data = g_slice_new (SkipAsyncData);
data->bytes_skipped = 0;
task = g_task_new (stream, cancellable, callback, user_data);
g_task_set_source_tag (task, g_buffered_input_stream_skip_async);
g_task_set_task_data (task, data, free_skip_async_data);
2010-07-05 07:40:38 -04:00
available = priv->end - priv->pos;
2010-07-05 07:40:38 -04:00
if (count <= available)
{
priv->pos += count;
2010-07-05 07:40:38 -04:00
g_task_return_int (task, count);
g_object_unref (task);
return;
}
2010-07-05 07:40:38 -04:00
/* Full request not available, skip all currently available
* and request refill for more
*/
priv->pos = 0;
priv->end = 0;
2010-07-05 07:40:38 -04:00
count -= available;
2010-07-05 07:40:38 -04:00
data->bytes_skipped = available;
data->count = count;
if (count > priv->len)
{
/* Large request, shortcut buffer */
base_stream = G_FILTER_INPUT_STREAM (stream)->base_stream;
2010-07-05 07:40:38 -04:00
/* If 'count > G_MAXSSIZE then 'g_input_stream_skip_async()'
* will return an error anyway before calling this.
* Assert that this is never called for too big `count` for clarity. */
g_assert ((gssize) count >= 0);
g_input_stream_skip_async (base_stream,
2010-07-05 07:40:38 -04:00
count,
io_priority, cancellable,
large_skip_callback,
task);
}
else
{
class = G_BUFFERED_INPUT_STREAM_GET_CLASS (stream);
class->fill_async (bstream, priv->len, io_priority, cancellable,
skip_fill_buffer_callback, task);
}
}
static gssize
g_buffered_input_stream_skip_finish (GInputStream *stream,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (g_task_is_valid (result, stream), -1);
2010-07-05 07:40:38 -04:00
return g_task_propagate_int (G_TASK (result), error);
}