class Gio::IOStream

Overview

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

Examples of #GIOStream objects are #GSocketConnection, which represents a two-way network connection; and #GFileIOStream, which represents 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 substreams, so even if the substreams stay alive they will always return %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 but common state in the #GIOStream may still be open. However, some streams may support "half-closed" states where one direction of the stream is actually shut down.

Operations on #GIOStreams cannot be started while another operation on the #GIOStream or its substreams is in progress. Specifically, an application can read from the #GInputStream and write to the #GOutputStream simultaneously (either in separate threads, or as asynchronous operations in the same thread), but an application cannot start any #GIOStream operation while there is a #GIOStream, #GInputStream or #GOutputStream operation in progress, and an application can’t start any #GInputStream or #GOutputStream operation while there is a #GIOStream operation in progress.

This is a product of individual stream operations being associated with a given #GMainContext (the thread-default context at the time the operation was started), rather than entire streams being associated with a single #GMainContext.

GIO may run operations on #GIOStreams from other (worker) threads, and this may be exposed to application code in the behaviour of wrapper streams, such as #GBufferedInputStream or #GTlsConnection. With such wrapper APIs, application code may only run operations on the base (wrapped) stream when the wrapper stream is idle. Note that the semantics of such operations may not be well-defined due to the state the wrapper stream leaves the base stream in (though they are guaranteed not to crash).

Direct Known Subclasses

Defined in:

lib/gi-crystal/src/auto/gio-2.0/io_stream.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new #

Initialize a new IOStream.


[View source]
def self.new(*, closed : Bool | Nil = nil, input_stream : Gio::InputStream | Nil = nil, output_stream : Gio::OutputStream | Nil = nil) #

[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def clear_pending : Nil #

Clears the pending flag on stream.


[View source]
def close(cancellable : Gio::Cancellable | Nil) : Bool #

Closes the stream, releasing resources related to it. This will also close 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.


[View source]
def close_async(io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

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.


[View source]
def close_finish(result : Gio::AsyncResult) : Bool #

Closes a stream.


[View source]
def closed? : Bool #

[View source]
def has_pending : Bool #

Checks if a stream has pending actions.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def input_stream : Gio::InputStream #

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


[View source]
def is_closed : Bool #

Checks if a stream is closed.


[View source]
def output_stream : Gio::OutputStream #

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


[View source]
def set_pending : Bool #

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


[View source]
def splice_async(stream2 : Gio::IOStream, flags : Gio::IOStreamSpliceFlags, io_priority : Int32, cancellable : Gio::Cancellable | Nil, &callback : Gio::AsyncReadyCallback) : Nil #

Asynchronously 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.


[View source]
def splice_finish(result : Gio::AsyncResult) : Bool #

[View source]