class Gio::OutputStream

Overview

#GOutputStream has functions to write to a stream (g_output_stream_write()), to close a stream (g_output_stream_close()) and to flush pending writes (g_output_stream_flush()).

To copy the content of an input stream to an output stream without manually handling the reads and writes, use g_output_stream_splice().

See the documentation for #GIOStream for details of thread safety of streaming APIs.

All of these functions have async variants too.

Direct Known Subclasses

Defined in:

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

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class GObject::Object

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)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, 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)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? 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, 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

compat_control(what : UInt64, data : Pointer(Void)?) : 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

Constructor Detail

def self.new #

Initialize a new OutputStream.


Class Method Detail

def self.g_type : UInt64 #

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


Instance Method Detail

def clear_pending : Nil #

Clears the pending flag on @stream.


def close(cancellable : Gio::Cancellable?) : Bool #

Closes the stream, releasing resources related to it.

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 nil, 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 there some streams can use a faster close that doesn't block to e.g. check errors. On cancellation (as with any error) there is no guarantee that all written data will reach the target.


def close_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : 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_output_stream_close_finish() to get the result of the operation.

For behaviour details see g_output_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.


def close_finish(result : Gio::AsyncResult) : Bool #

Closes an output stream.


def flush(cancellable : Gio::Cancellable?) : Bool #

Forces a write of all user-space buffered data for the given @stream. Will block during the operation. Closing the stream will implicitly cause a flush.

This function is optional for inherited classes.

If @cancellable is not nil, 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.


def flush_async(io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

Forces an asynchronous write of all user-space buffered data for the given @stream. For behaviour details see g_output_stream_flush().

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


def flush_finish(result : Gio::AsyncResult) : Bool #

Finishes flushing an output stream.


def has_pending : Bool #

Checks if an output stream has pending actions.


def is_closed : Bool #

Checks if an output stream has already been closed.


def is_closing : Bool #

Checks if an output stream is being closed. This can be used inside e.g. a flush implementation to see if the flush (or other i/o operation) is called from within the closing operation.


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.


def splice(source : Gio::InputStream, flags : Gio::OutputStreamSpliceFlags, cancellable : Gio::Cancellable?) : Int64 #

Splices an input stream into an output stream.


def splice_async(source : Gio::InputStream, flags : Gio::OutputStreamSpliceFlags, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

Splices a stream asynchronously. When the operation is finished @callback will be called. You can then call g_output_stream_splice_finish() to get the result of the operation.

For the synchronous, blocking version of this function, see g_output_stream_splice().


def splice_finish(result : Gio::AsyncResult) : Int64 #

Finishes an asynchronous stream splice operation.


def write(buffer : Enumerable(UInt8), cancellable : Gio::Cancellable?) : Int64 #

Tries to write @count bytes from @buffer into the stream. Will block during the operation.

If count is 0, returns 0 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 written to the stream is returned. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, or if there is not enough storage in the stream. All writes block until at least one byte is written or an error occurs; 0 is never returned (unless @count is 0).

If @cancellable is not nil, 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.


def write_all(buffer : Enumerable(UInt8), cancellable : Gio::Cancellable?) : Bool #

Tries to write @count bytes from @buffer into the stream. Will block during the operation.

This function is similar to g_output_stream_write(), except it tries to write as many bytes as requested, only stopping on an error.

On a successful write of @count bytes, true is returned, and @bytes_written is set to @count.

If there is an error during the operation false is returned and @error is set to indicate the error status.

As a special exception to the normal conventions for functions that use #GError, if this function returns false (and sets @error) then @bytes_written will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_write().


def write_all_async(buffer : Enumerable(UInt8), io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

Request an asynchronous write of @count bytes from @buffer into the stream. When the operation is finished @callback will be called. You can then call g_output_stream_write_all_finish() to get the result of the operation.

This is the asynchronous version of g_output_stream_write_all().

Call g_output_stream_write_all_finish() to collect the result.

Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is %G_PRIORITY_DEFAULT.

Note that no copy of @buffer will be made, so it must stay valid until @callback is called.


def write_all_finish(result : Gio::AsyncResult) : Bool #

Finishes an asynchronous stream write operation started with g_output_stream_write_all_async().

As a special exception to the normal conventions for functions that use #GError, if this function returns false (and sets @error) then @bytes_written will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_write_async().


def write_async(buffer : Enumerable(UInt8), io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

Request an asynchronous write of @count bytes from @buffer into the stream. When the operation is finished @callback will be called. You can then call g_output_stream_write_finish() to get the result of the operation.

During an async request no other sync and async calls are allowed, and will result in %G_IO_ERROR_PENDING errors.

A value of @count larger than %G_MAXSSIZE will cause a %G_IO_ERROR_INVALID_ARGUMENT error.

On success, the number of bytes written will be passed to the @callback. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, but generally we try to write as many bytes as requested.

You are guaranteed that this method will never fail with %G_IO_ERROR_WOULD_BLOCK - if @stream can't accept more data, the method will just wait until this changes.

Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is %G_PRIORITY_DEFAULT.

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.

For the synchronous, blocking version of this function, see g_output_stream_write().

Note that no copy of @buffer will be made, so it must stay valid until @callback is called. See g_output_stream_write_bytes_async() for a #GBytes version that will automatically hold a reference to the contents (without copying) for the duration of the call.


def write_bytes(bytes : GLib::Bytes, cancellable : Gio::Cancellable?) : Int64 #

A wrapper function for g_output_stream_write() which takes a #GBytes as input. This can be more convenient for use by language bindings or in other cases where the refcounted nature of #GBytes is helpful over a bare pointer interface.

However, note that this function may still perform partial writes, just like g_output_stream_write(). If that occurs, to continue writing, you will need to create a new #GBytes containing just the remaining bytes, using g_bytes_new_from_bytes(). Passing the same #GBytes instance multiple times potentially can result in duplicated data in the output stream.


def write_bytes_async(bytes : GLib::Bytes, io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

This function is similar to g_output_stream_write_async(), but takes a #GBytes as input. Due to the refcounted nature of #GBytes, this allows the stream to avoid taking a copy of the data.

However, note that this function may still perform partial writes, just like g_output_stream_write_async(). If that occurs, to continue writing, you will need to create a new #GBytes containing just the remaining bytes, using g_bytes_new_from_bytes(). Passing the same #GBytes instance multiple times potentially can result in duplicated data in the output stream.

For the synchronous, blocking version of this function, see g_output_stream_write_bytes().


def write_bytes_finish(result : Gio::AsyncResult) : Int64 #

Finishes a stream write-from-#GBytes operation.


def write_finish(result : Gio::AsyncResult) : Int64 #

Finishes a stream write operation.


def writev(vectors : Enumerable(Gio::OutputVector), cancellable : Gio::Cancellable?) : Bool #

Tries to write the bytes contained in the @n_vectors @vectors into the stream. Will block during the operation.

If @n_vectors is 0 or the sum of all bytes in @vectors is 0, returns 0 and does nothing.

On success, the number of bytes written to the stream is returned. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, or if there is not enough storage in the stream. All writes block until at least one byte is written or an error occurs; 0 is never returned (unless @n_vectors is 0 or the sum of all bytes in @vectors is 0).

If @cancellable is not nil, 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.

Some implementations of g_output_stream_writev() may have limitations on the aggregate buffer size, and will return %G_IO_ERROR_INVALID_ARGUMENT if these are exceeded. For example, when writing to a local file on UNIX platforms, the aggregate buffer size must not exceed %G_MAXSSIZE bytes.


def writev_all(vectors : Enumerable(Gio::OutputVector), cancellable : Gio::Cancellable?) : Bool #

Tries to write the bytes contained in the @n_vectors @vectors into the stream. Will block during the operation.

This function is similar to g_output_stream_writev(), except it tries to write as many bytes as requested, only stopping on an error.

On a successful write of all @n_vectors vectors, true is returned, and @bytes_written is set to the sum of all the sizes of @vectors.

If there is an error during the operation false is returned and @error is set to indicate the error status.

As a special exception to the normal conventions for functions that use #GError, if this function returns false (and sets @error) then @bytes_written will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_write().

The content of the individual elements of @vectors might be changed by this function.


def writev_all_async(vectors : Enumerable(Gio::OutputVector), io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

Request an asynchronous write of the bytes contained in the @n_vectors @vectors into the stream. When the operation is finished @callback will be called. You can then call g_output_stream_writev_all_finish() to get the result of the operation.

This is the asynchronous version of g_output_stream_writev_all().

Call g_output_stream_writev_all_finish() to collect the result.

Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is %G_PRIORITY_DEFAULT.

Note that no copy of @vectors will be made, so it must stay valid until @callback is called. The content of the individual elements of @vectors might be changed by this function.


def writev_all_finish(result : Gio::AsyncResult) : Bool #

Finishes an asynchronous stream write operation started with g_output_stream_writev_all_async().

As a special exception to the normal conventions for functions that use #GError, if this function returns false (and sets @error) then @bytes_written will be set to the number of bytes that were successfully written before the error was encountered. This functionality is only available from C. If you need it from another language then you must write your own loop around g_output_stream_writev_async().


def writev_async(vectors : Enumerable(Gio::OutputVector), io_priority : Int32, cancellable : Gio::Cancellable?, callback : Gio::AsyncReadyCallback?, user_data : Pointer(Void)?) : Nil #

Request an asynchronous write of the bytes contained in @n_vectors @vectors into the stream. When the operation is finished @callback will be called. You can then call g_output_stream_writev_finish() to get the result of the operation.

During an async request no other sync and async calls are allowed, and will result in %G_IO_ERROR_PENDING errors.

On success, the number of bytes written will be passed to the @callback. It is not an error if this is not the same as the requested size, as it can happen e.g. on a partial I/O error, but generally we try to write as many bytes as requested.

You are guaranteed that this method will never fail with %G_IO_ERROR_WOULD_BLOCK — if @stream can't accept more data, the method will just wait until this changes.

Any outstanding I/O request with higher priority (lower numerical value) will be executed before an outstanding request with lower priority. Default priority is %G_PRIORITY_DEFAULT.

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.

For the synchronous, blocking version of this function, see g_output_stream_writev().

Note that no copy of @vectors will be made, so it must stay valid until @callback is called.


def writev_finish(result : Gio::AsyncResult) : Bool #

Finishes a stream writev operation.