GIOStream

GIOStream — Base class for implementing read/write streams

Synopsis

#include <gio/gio.h>

enum                GIOStreamSpliceFlags;
                    GIOStream;
GInputStream *      g_io_stream_get_input_stream        (GIOStream *stream);
GOutputStream *     g_io_stream_get_output_stream       (GIOStream *stream);
void                g_io_stream_splice_async            (GIOStream *stream1,
                                                         GIOStream *stream2,
                                                         GIOStreamSpliceFlags flags,
                                                         int io_priority,
                                                         GCancellable *cancellable,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data);
gboolean            g_io_stream_splice_finish           (GAsyncResult *result,
                                                         GError **error);
gboolean            g_io_stream_close                   (GIOStream *stream,
                                                         GCancellable *cancellable,
                                                         GError **error);
void                g_io_stream_close_async             (GIOStream *stream,
                                                         int io_priority,
                                                         GCancellable *cancellable,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data);
gboolean            g_io_stream_close_finish            (GIOStream *stream,
                                                         GAsyncResult *result,
                                                         GError **error);
gboolean            g_io_stream_is_closed               (GIOStream *stream);
gboolean            g_io_stream_has_pending             (GIOStream *stream);
gboolean            g_io_stream_set_pending             (GIOStream *stream,
                                                         GError **error);
void                g_io_stream_clear_pending           (GIOStream *stream);

Object Hierarchy

  GObject
   +----GIOStream
         +----GFileIOStream
         +----GSocketConnection
         +----GTlsConnection

Properties

  "closed"                   gboolean              : Read / Write
  "input-stream"             GInputStream*         : Read
  "output-stream"            GOutputStream*        : Read

Description

GIOStream represents an object that has both read and write streams. Generally the two streams acts as separate input and output streams, but they share some common resources and state. For instance, for seekable streams they may use the same position in both streams.

Examples of GIOStream objects are GSocketConnection which represents a two-way network connection, and GFileIOStream which represent a file handle opened in read-write mode.

To do the actual reading and writing you need to get the substreams with g_io_stream_get_input_stream() and g_io_stream_get_output_stream().

The GIOStream object owns the input and the output streams, not the other way around, so keeping the substreams alive will not keep the GIOStream object alive. If the GIOStream object is freed it will be closed, thus closing the substream, so even if the substreams stay alive they will always just return a G_IO_ERROR_CLOSED for all operations.

To close a stream use g_io_stream_close() which will close the common stream object and also the individual substreams. You can also close the substreams themselves. In most cases this only marks the substream as closed, so further I/O on it fails. However, some streams may support "half-closed" states where one direction of the stream is actually shut down.

Details

enum GIOStreamSpliceFlags

typedef enum {
  G_IO_STREAM_SPLICE_NONE          = 0,
  G_IO_STREAM_SPLICE_CLOSE_STREAM1 = (1 << 0),
  G_IO_STREAM_SPLICE_CLOSE_STREAM2 = (1 << 1),
  G_IO_STREAM_SPLICE_WAIT_FOR_BOTH = (1 << 2)
} GIOStreamSpliceFlags;

GIOStreamSpliceFlags determine how streams should be spliced.

G_IO_STREAM_SPLICE_NONE

Do not close either stream.

G_IO_STREAM_SPLICE_CLOSE_STREAM1

Close the first stream after the splice.

G_IO_STREAM_SPLICE_CLOSE_STREAM2

Close the second stream after the splice.

G_IO_STREAM_SPLICE_WAIT_FOR_BOTH

Wait for both splice operations to finish before calling the callback.

Since 2.28


GIOStream

typedef struct _GIOStream GIOStream;

Base class for read-write streams.


g_io_stream_get_input_stream ()

GInputStream *      g_io_stream_get_input_stream        (GIOStream *stream);

Gets the input stream for this object. This is used for reading.

stream :

a GIOStream

Returns :

a GInputStream, owned by the GIOStream. Do not free. [transfer none]

Since 2.22


g_io_stream_get_output_stream ()

GOutputStream *     g_io_stream_get_output_stream       (GIOStream *stream);

Gets the output stream for this object. This is used for writing.

stream :

a GIOStream

Returns :

a GOutputStream, owned by the GIOStream. Do not free. [transfer none]

Since 2.22


g_io_stream_splice_async ()

void                g_io_stream_splice_async            (GIOStream *stream1,
                                                         GIOStream *stream2,
                                                         GIOStreamSpliceFlags flags,
                                                         int io_priority,
                                                         GCancellable *cancellable,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data);

Asyncronously splice the output stream of stream1 to the input stream of stream2, and splice the output stream of stream2 to the input stream of stream1.

When the operation is finished callback will be called. You can then call g_io_stream_splice_finish() to get the result of the operation.

stream1 :

a GIOStream.

stream2 :

a GIOStream.

flags :

a set of GIOStreamSpliceFlags.

io_priority :

the io priority of the request.

cancellable :

optional GCancellable object, NULL to ignore. [allow-none]

callback :

a GAsyncReadyCallback. [scope async]

user_data :

user data passed to callback. [closure]

Since 2.28


g_io_stream_splice_finish ()

gboolean            g_io_stream_splice_finish           (GAsyncResult *result,
                                                         GError **error);

Finishes an asynchronous io stream splice operation.

result :

a GAsyncResult.

error :

a GError location to store the error occuring, or NULL to ignore.

Returns :

TRUE on success, FALSE otherwise.

Since 2.28


g_io_stream_close ()

gboolean            g_io_stream_close                   (GIOStream *stream,
                                                         GCancellable *cancellable,
                                                         GError **error);

Closes the stream, releasing resources related to it. This will also closes the individual input and output streams, if they are not already closed.

Once the stream is closed, all other operations will return G_IO_ERROR_CLOSED. Closing a stream multiple times will not return an error.

Closing a stream will automatically flush any outstanding buffers in the stream.

Streams will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.

Some streams might keep the backing store of the stream (e.g. a file descriptor) open after the stream is closed. See the documentation for the individual stream for details.

On failure the first error that happened will be reported, but the close operation will finish as much as possible. A stream that failed to close will still return G_IO_ERROR_CLOSED for all operations. Still, it is important to check and report the error to the user, otherwise there might be a loss of data as all data might not be written.

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. Cancelling a close will still leave the stream closed, but some streams can use a faster close that doesn't block to e.g. check errors.

The default implementation of this method just calls close on the individual input/output streams.

stream :

a GIOStream

cancellable :

optional GCancellable object, NULL to ignore. [allow-none]

error :

location to store the error occuring, or NULL to ignore

Returns :

TRUE on success, FALSE on failure

Since 2.22


g_io_stream_close_async ()

void                g_io_stream_close_async             (GIOStream *stream,
                                                         int io_priority,
                                                         GCancellable *cancellable,
                                                         GAsyncReadyCallback callback,
                                                         gpointer user_data);

Requests an asynchronous close of the stream, releasing resources related to it. When the operation is finished callback will be called. You can then call g_io_stream_close_finish() to get the result of the operation.

For behaviour details see g_io_stream_close().

The asynchronous methods have a default fallback that uses threads to implement asynchronicity, so they are optional for inheriting classes. However, if you override one you must override all.

stream :

a GIOStream

io_priority :

the io priority of the request

cancellable :

optional cancellable object. [allow-none]

callback :

callback to call when the request is satisfied. [scope async]

user_data :

the data to pass to callback function. [closure]

Since 2.22


g_io_stream_close_finish ()

gboolean            g_io_stream_close_finish            (GIOStream *stream,
                                                         GAsyncResult *result,
                                                         GError **error);

Closes a stream.

stream :

a GIOStream

result :

a GAsyncResult

error :

a GError location to store the error occuring, or NULL to ignore

Returns :

TRUE if stream was successfully closed, FALSE otherwise.

Since 2.22


g_io_stream_is_closed ()

gboolean            g_io_stream_is_closed               (GIOStream *stream);

Checks if a stream is closed.

stream :

a GIOStream

Returns :

TRUE if the stream is closed.

Since 2.22


g_io_stream_has_pending ()

gboolean            g_io_stream_has_pending             (GIOStream *stream);

Checks if a stream has pending actions.

stream :

a GIOStream

Returns :

TRUE if stream has pending actions.

Since 2.22


g_io_stream_set_pending ()

gboolean            g_io_stream_set_pending             (GIOStream *stream,
                                                         GError **error);

Sets stream to have actions pending. If the pending flag is already set or stream is closed, it will return FALSE and set error.

stream :

a GIOStream

error :

a GError location to store the error occuring, or NULL to ignore

Returns :

TRUE if pending was previously unset and is now set.

Since 2.22


g_io_stream_clear_pending ()

void                g_io_stream_clear_pending           (GIOStream *stream);

Clears the pending flag on stream.

stream :

a GIOStream

Since 2.22

Property Details

The "closed" property

  "closed"                   gboolean              : Read / Write

Is the stream closed.

Default value: FALSE


The "input-stream" property

  "input-stream"             GInputStream*         : Read

The GInputStream to read from.


The "output-stream" property

  "output-stream"            GOutputStream*        : Read

The GOutputStream to write to.

See Also

GInputStream, GOutputStream