Gio.OutputStream¶
class — extends GObject.Object
GOutputStream is a base class for implementing streaming output.
It has functions to write to a stream (OutputStream.write),
to close a stream (OutputStream.close) and to flush pending
writes (OutputStream.flush).
To copy the content of an input stream to an output stream without
manually handling the reads and writes, use OutputStream.splice.
See the documentation for IOStream for details of thread safety
of streaming APIs.
All of these functions have async variants too.
All classes derived from GOutputStream should implement synchronous
writing, splicing, flushing and closing streams, but may implement
asynchronous versions.
Methods¶
clear_pending¶
Clears the pending flag on stream.
close¶
Closes the stream, releasing resources related to it.
Once the stream is closed, all other operations will return IOErrorEnum.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 IOErrorEnum.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 None, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error IOErrorEnum.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.
Parameters:
cancellable— optional cancellable object
close_async¶
def close_async(self, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.close_finish to get
the result of the operation.
For behaviour details see OutputStream.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.
Parameters:
io_priority— the io priority of the request.cancellable— optional cancellable objectcallback— aGAsyncReadyCallbackto call when the request is satisfied
close_finish¶
Closes an output stream.
Parameters:
result— aAsyncResult.
flush¶
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 None, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error IOErrorEnum.CANCELLED will be returned.
Parameters:
cancellable— optional cancellable object
flush_async¶
def flush_async(self, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
Forces an asynchronous write of all user-space buffered data for
the given stream.
For behaviour details see OutputStream.flush.
When the operation is finished callback will be
called. You can then call OutputStream.flush_finish to get the
result of the operation.
Parameters:
io_priority— the io priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
flush_finish¶
Finishes flushing an output stream.
Parameters:
result— a GAsyncResult.
has_pending¶
Checks if an output stream has pending actions.
is_closed¶
Checks if an output stream has already been closed.
is_closing¶
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.
set_pending¶
Sets stream to have actions pending. If the pending flag is
already set or stream is closed, it will return False and set
error.
splice¶
def splice(self, source: InputStream, flags: OutputStreamSpliceFlags | int, cancellable: Cancellable | None = ...) -> int
Splices an input stream into an output stream.
Parameters:
source— aInputStream.flags— a set ofOutputStreamSpliceFlags.cancellable— optionalCancellableobject,Noneto ignore.
splice_async¶
def splice_async(self, source: InputStream, flags: OutputStreamSpliceFlags | int, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
Splices a stream asynchronously.
When the operation is finished callback will be called.
You can then call OutputStream.splice_finish to get the
result of the operation.
For the synchronous, blocking version of this function, see
OutputStream.splice.
Parameters:
source— aInputStream.flags— a set ofOutputStreamSpliceFlags.io_priority— the io priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
splice_finish¶
Finishes an asynchronous stream splice operation.
Parameters:
result— aAsyncResult.
write¶
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 IOErrorEnum.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 None, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error IOErrorEnum.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.
Parameters:
buffer— the buffer containing the data to write.cancellable— optional cancellable object
write_all¶
Tries to write count bytes from buffer into the stream. Will block
during the operation.
This function is similar to OutputStream.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 GLib.Error, 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
OutputStream.write.
Parameters:
buffer— the buffer containing the data to write.cancellable— optionalCancellableobject,Noneto ignore.
write_all_async¶
def write_all_async(self, buffer: list[int], io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.write_all_finish to get the result of the
operation.
This is the asynchronous version of OutputStream.write_all.
Call OutputStream.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 GLib.PRIORITY_DEFAULT.
Note that no copy of buffer will be made, so it must stay valid
until callback is called.
Parameters:
buffer— the buffer containing the data to writeio_priority— the io priority of the requestcancellable— optionalCancellableobject,Noneto ignorecallback— aGAsyncReadyCallbackto call when the request is satisfied
write_all_finish¶
Finishes an asynchronous stream write operation started with
OutputStream.write_all_async.
As a special exception to the normal conventions for functions that
use GLib.Error, 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
OutputStream.write_async.
Parameters:
result— aAsyncResult
write_async¶
def write_async(self, buffer: list[int], io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.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 IOErrorEnum.PENDING errors.
A value of count larger than G_MAXSSIZE will cause a
IOErrorEnum.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
IOErrorEnum.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 GLib.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
OutputStream.write.
Note that no copy of buffer will be made, so it must stay valid
until callback is called. See OutputStream.write_bytes_async
for a GLib.Bytes version that will automatically hold a reference to
the contents (without copying) for the duration of the call.
Parameters:
buffer— the buffer containing the data to write.io_priority— the io priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
write_bytes¶
A wrapper function for OutputStream.write which takes a
GLib.Bytes as input. This can be more convenient for use by language
bindings or in other cases where the refcounted nature of GLib.Bytes
is helpful over a bare pointer interface.
However, note that this function may still perform partial writes,
just like OutputStream.write. If that occurs, to continue
writing, you will need to create a new GLib.Bytes containing just the
remaining bytes, using GLib.Bytes.new_from_bytes. Passing the same
GLib.Bytes instance multiple times potentially can result in duplicated
data in the output stream.
Parameters:
bytes— theGLib.Bytesto writecancellable— optional cancellable object
write_bytes_async¶
def write_bytes_async(self, bytes: bytes, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
This function is similar to OutputStream.write_async, but
takes a GLib.Bytes as input. Due to the refcounted nature of GLib.Bytes,
this allows the stream to avoid taking a copy of the data.
However, note that this function may still perform partial writes,
just like OutputStream.write_async. If that occurs, to continue
writing, you will need to create a new GLib.Bytes containing just the
remaining bytes, using GLib.Bytes.new_from_bytes. Passing the same
GLib.Bytes instance multiple times potentially can result in duplicated
data in the output stream.
For the synchronous, blocking version of this function, see
OutputStream.write_bytes.
Parameters:
bytes— The bytes to writeio_priority— the io priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
write_bytes_finish¶
Finishes a stream write-from-GLib.Bytes operation.
Parameters:
result— aAsyncResult.
write_finish¶
Finishes a stream write operation.
Parameters:
result— aAsyncResult.
writev¶
def writev(self, vectors: list[OutputVector], cancellable: Cancellable | None = ...) -> tuple[bool, int]
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 None, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error IOErrorEnum.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 OutputStream.writev may have limitations on the
aggregate buffer size, and will return IOErrorEnum.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.
Parameters:
vectors— the buffer containing theGOutputVectorsto write.cancellable— optional cancellable object
writev_all¶
def writev_all(self, vectors: list[OutputVector], cancellable: Cancellable | None = ...) -> tuple[bool, int]
Tries to write the bytes contained in the n_vectors vectors into the
stream. Will block during the operation.
This function is similar to OutputStream.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 GLib.Error, 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
OutputStream.write.
The content of the individual elements of vectors might be changed by this
function.
Parameters:
vectors— the buffer containing theGOutputVectorsto write.cancellable— optionalCancellableobject,Noneto ignore.
writev_all_async¶
def writev_all_async(self, vectors: list[OutputVector], io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.writev_all_finish to get the result of the
operation.
This is the asynchronous version of OutputStream.writev_all.
Call OutputStream.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 GLib.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.
Parameters:
vectors— the buffer containing theGOutputVectorsto write.io_priority— the I/O priority of the requestcancellable— optionalCancellableobject,Noneto ignorecallback— aGAsyncReadyCallbackto call when the request is satisfied
writev_all_finish¶
Finishes an asynchronous stream write operation started with
OutputStream.writev_all_async.
As a special exception to the normal conventions for functions that
use GLib.Error, 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
OutputStream.writev_async.
Parameters:
result— aAsyncResult
writev_async¶
def writev_async(self, vectors: list[OutputVector], io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.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 IOErrorEnum.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
IOErrorEnum.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 GLib.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
OutputStream.writev.
Note that no copy of vectors will be made, so it must stay valid
until callback is called.
Parameters:
vectors— the buffer containing theGOutputVectorsto write.io_priority— the I/O priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
writev_finish¶
Finishes a stream writev operation.
Parameters:
result— aAsyncResult.
Virtual methods¶
do_close_async¶
def do_close_async(self, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.close_finish to get
the result of the operation.
For behaviour details see OutputStream.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.
Parameters:
io_priority— the io priority of the request.cancellable— optional cancellable objectcallback— aGAsyncReadyCallbackto call when the request is satisfied
do_close_finish¶
Closes an output stream.
Parameters:
result— aAsyncResult.
do_close_fn¶
do_flush¶
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 None, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error IOErrorEnum.CANCELLED will be returned.
Parameters:
cancellable— optional cancellable object
do_flush_async¶
def do_flush_async(self, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
Forces an asynchronous write of all user-space buffered data for
the given stream.
For behaviour details see OutputStream.flush.
When the operation is finished callback will be
called. You can then call OutputStream.flush_finish to get the
result of the operation.
Parameters:
io_priority— the io priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
do_flush_finish¶
Finishes flushing an output stream.
Parameters:
result— a GAsyncResult.
do_splice¶
def do_splice(self, source: InputStream, flags: OutputStreamSpliceFlags | int, cancellable: Cancellable | None = ...) -> int
Splices an input stream into an output stream.
Parameters:
source— aInputStream.flags— a set ofOutputStreamSpliceFlags.cancellable— optionalCancellableobject,Noneto ignore.
do_splice_async¶
def do_splice_async(self, source: InputStream, flags: OutputStreamSpliceFlags | int, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
Splices a stream asynchronously.
When the operation is finished callback will be called.
You can then call OutputStream.splice_finish to get the
result of the operation.
For the synchronous, blocking version of this function, see
OutputStream.splice.
Parameters:
source— aInputStream.flags— a set ofOutputStreamSpliceFlags.io_priority— the io priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
do_splice_finish¶
Finishes an asynchronous stream splice operation.
Parameters:
result— aAsyncResult.
do_write_async¶
def do_write_async(self, buffer: list[int] | None, io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.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 IOErrorEnum.PENDING errors.
A value of count larger than G_MAXSSIZE will cause a
IOErrorEnum.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
IOErrorEnum.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 GLib.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
OutputStream.write.
Note that no copy of buffer will be made, so it must stay valid
until callback is called. See OutputStream.write_bytes_async
for a GLib.Bytes version that will automatically hold a reference to
the contents (without copying) for the duration of the call.
Parameters:
buffer— the buffer containing the data to write.io_priority— the io priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
do_write_finish¶
Finishes a stream write operation.
Parameters:
result— aAsyncResult.
do_write_fn¶
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 IOErrorEnum.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 None, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error IOErrorEnum.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.
Parameters:
buffer— the buffer containing the data to write.cancellable— optional cancellable object
do_writev_async¶
def do_writev_async(self, vectors: list[OutputVector], io_priority: int, cancellable: Cancellable | None = ..., callback: Callable[[OutputStream | None, AsyncResult], None] | None = ...) -> None
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 OutputStream.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 IOErrorEnum.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
IOErrorEnum.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 GLib.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
OutputStream.writev.
Note that no copy of vectors will be made, so it must stay valid
until callback is called.
Parameters:
vectors— the buffer containing theGOutputVectorsto write.io_priority— the I/O priority of the request.cancellable— optionalCancellableobject,Noneto ignore.callback— aGAsyncReadyCallbackto call when the request is satisfied
do_writev_finish¶
Finishes a stream writev operation.
Parameters:
result— aAsyncResult.
do_writev_fn¶
def do_writev_fn(self, vectors: list[OutputVector], cancellable: Cancellable | None = ...) -> tuple[bool, int]
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 None, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error IOErrorEnum.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 OutputStream.writev may have limitations on the
aggregate buffer size, and will return IOErrorEnum.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.
Parameters:
vectors— the buffer containing theGOutputVectorsto write.cancellable— optional cancellable object