GLib.IOChannel
Description: Reference for GLib.IOChannel

### Struct

# GLibIOChannel

since: 2.0

#### Description

    struct GIOChannel {
      /* No available fields */
    }

The `GIOChannel` data type aims to provide a portable method for using file descriptors, pipes, and sockets, and integrating them into the main event loop (see `GMainContext`). Currently, full support is available on UNIX platforms; support for Windows is only partially complete.

To create a new `GIOChannel` on UNIX systems use `g_io_channel_unix_new()`. This works for plain file descriptors, pipes and sockets. Alternatively, a channel can be created for a file in a system independent manner using `g_io_channel_new_file()`.

Once a `GIOChannel` has been created, it can be used in a generic manner with the functions `g_io_channel_read_chars()`, `g_io_channel_write_chars()`, `g_io_channel_seek_position()`, and `g_io_channel_shutdown()`.

To add a `GIOChannel` to the main event loop, use `g_io_add_watch()` or `g_io_add_watch_full()`. Here you specify which events you are interested in on the `GIOChannel`, and provide a function to be called whenever these events occur.

`GIOChannel` instances are created with an initial reference count of 1. `g_io_channel_ref()` and `g_io_channel_unref()` can be used to increment or decrement the reference count respectively. When the reference count falls to 0, the `GIOChannel` is freed. (Though it isn’t closed automatically, unless it was created using `g_io_channel_new_file()`.) Using `g_io_add_watch()` or `g_io_add_watch_full()` increments a channel’s reference count.

The new functions `g_io_channel_read_chars()`, `g_io_channel_read_line()`, `g_io_channel_read_line_string()`, `g_io_channel_read_to_end()`, `g_io_channel_write_chars()`, `g_io_channel_seek_position()`, and `g_io_channel_flush()` should not be mixed with the deprecated functions `g_io_channel_read()`, `g_io_channel_write()`, and `g_io_channel_seek()` on the same channel.

Available since: 2.0

#### Constructors

###### g_io_channel_new_file

Open a file `filename` as a `GIOChannel` using mode `mode`. This channel will be closed when the last reference to it is dropped, so there is no need to call `g_io_channel_close()` (though doing so will not cause problems, as long as no attempt is made to access the channel after it is closed).

since: 2.0

###### g_io_channel_unix_new

Creates a new `GIOChannel` given a file descriptor. On UNIX systems this works for plain files, pipes, and sockets.

since: 2.0

#### Functions

###### g_io_channel_error_from_errno

Converts an `errno` error number to a `GIOChannelError`.

since: 2.0

###### g_io_channel_error_quark

No description available.

since: 2.0

#### Instance methods

###### g_io_channel_close

Close an IO channel. Any pending data to be written will be flushed, ignoring errors. The channel will not be freed until the last reference is dropped using g_io_channel_unref().

deprecated: 2.2 since: 2.0

###### g_io_channel_flush

Flushes the write buffer for the GIOChannel.

since: 2.0

###### g_io_channel_get_buffer_condition

This function returns a `GIOCondition` depending on whether there is data to be read/space to write data in the internal buffers in the `GIOChannel`. Only the flags `G_IO_IN` and `G_IO_OUT` may be set.

since: 2.0

###### g_io_channel_get_buffer_size

Gets the buffer size.

since: 2.0

###### g_io_channel_get_buffered

Returns whether `channel` is buffered.

since: 2.0

###### g_io_channel_get_close_on_unref

Returns whether the file/socket/whatever associated with `channel` will be closed when `channel` receives its final unref and is destroyed. The default value of this is `TRUE` for channels created by g_io_channel_new_file (), and `FALSE` for all other channels.

since: 2.0

###### g_io_channel_get_encoding

Gets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The encoding `NULL` makes the channel safe for binary data.

since: 2.0

###### g_io_channel_get_flags

Gets the current flags for a `GIOChannel`, including read-only flags such as `G_IO_FLAG_IS_READABLE`.

since: 2.0

###### g_io_channel_get_line_term

This returns the string that `GIOChannel` uses to determine where in the file a line break occurs. A value of `NULL` indicates autodetection.

since: 2.0

###### g_io_channel_init

Initializes a `GIOChannel` struct.

since: 2.0

###### g_io_channel_read

Reads data from a `GIOChannel`.

deprecated: 2.2 since: 2.0

###### g_io_channel_read_chars

Replacement for `g_io_channel_read()` with the new API.

since: 2.0

###### g_io_channel_read_line

Reads a line, including the terminating character(s), from a `GIOChannel` into a newly-allocated string. `str_return` will contain allocated memory if the return is `G_IO_STATUS_NORMAL`.

since: 2.0

###### g_io_channel_read_line_string

Reads a line from a `GIOChannel`, using a `GString` as a buffer.

since: 2.0

###### g_io_channel_read_to_end

Reads all the remaining data from the file.

since: 2.0

###### g_io_channel_read_unichar

Reads a Unicode character from `channel`. This function cannot be called on a channel with `NULL` encoding.

since: 2.0

###### g_io_channel_ref

Increments the reference count of a `GIOChannel`.

since: 2.0

###### g_io_channel_seek

Sets the current position in the `GIOChannel`, similar to the standard library function fseek().

deprecated: 2.2 since: 2.0

###### g_io_channel_seek_position

Replacement for `g_io_channel_seek()` with the new API.

since: 2.0

###### g_io_channel_set_buffer_size

Sets the buffer size.

since: 2.0

###### g_io_channel_set_buffered

The buffering state can only be set if the channel’s encoding is `NULL`. For any other encoding, the channel must be buffered.

since: 2.0

###### g_io_channel_set_close_on_unref

Whether to close the channel on the final unref of the `GIOChannel` data structure. The default value of this is `TRUE` for channels created by g_io_channel_new_file (), and `FALSE` for all other channels.

since: 2.0

###### g_io_channel_set_encoding

Sets the encoding for the input/output of the channel. The internal encoding is always UTF-8. The default encoding for the external file is UTF-8.

since: 2.0

###### g_io_channel_set_flags

Sets the (writeable) flags in `channel` to (`flags` & `G_IO_FLAG_SET_MASK`).

since: 2.0

###### g_io_channel_set_line_term

This sets the string that `GIOChannel` uses to determine where in the file a line break occurs.

since: 2.0

###### g_io_channel_shutdown

Close an IO channel. Any pending data to be written will be flushed if `flush` is `TRUE`. The channel will not be freed until the last reference is dropped using g_io_channel_unref().

since: 2.0

###### g_io_channel_unix_get_fd

Returns the file descriptor of the `GIOChannel`.

since: 2.0

###### g_io_channel_unref

Decrements the reference count of a `GIOChannel`.

since: 2.0

###### g_io_channel_write

Writes data to a `GIOChannel`.

deprecated: 2.2 since: 2.0

###### g_io_channel_write_chars

Replacement for `g_io_channel_write()` with the new API.

since: 2.0

###### g_io_channel_write_unichar

Writes a Unicode character to `channel`. This function cannot be called on a channel with `NULL` encoding.

since: 2.0