OutputStream

class OutputStream(**properties: Any)

Superclasses: Object

Subclasses: FileOutputStream, FilterOutputStream, MemoryOutputStream, UnixOutputStream

Constructors:

OutputStream(**properties)

Methods

class OutputStream
clear_pending() None

Clears the pending flag on stream.

close(cancellable: Cancellable | None = None) bool

Closes the stream, releasing resources related to it.

Once the stream is closed, all other operations will return 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 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 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(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 close_finish() to get the result of the operation.

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

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

close_finish(result: AsyncResult) bool

Closes an output stream.

Parameters:

result – a AsyncResult.

flush(cancellable: Cancellable | None = None) 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 None, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error CANCELLED will be returned.

Parameters:

cancellable – optional cancellable object

flush_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

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

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

Parameters:
  • io_priority – the io priority of the request.

  • cancellable – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

flush_finish(result: AsyncResult) bool

Finishes flushing an output stream.

Parameters:

result – a GAsyncResult.

has_pending() bool

Checks if an output stream has pending actions.

is_closed() bool

Checks if an output stream has already been closed.

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.

Added in version 2.24.

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.

splice(source: InputStream, flags: OutputStreamSpliceFlags, cancellable: Cancellable | None = None) int

Splices an input stream into an output stream.

Parameters:
splice_async(source: InputStream, flags: OutputStreamSpliceFlags, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

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

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

Parameters:
  • source – a InputStream.

  • flags – a set of OutputStreamSpliceFlags.

  • io_priority – the io priority of the request.

  • cancellable – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

splice_finish(result: AsyncResult) int

Finishes an asynchronous stream splice operation.

Parameters:

result – a AsyncResult.

write(buffer: Sequence[int], cancellable: Cancellable | None = None) int

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 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 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(buffer: Sequence[int], cancellable: Cancellable | None = None) tuple[bool, int]

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

This function is similar to 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 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 write().

Parameters:
  • buffer – the buffer containing the data to write.

  • cancellable – optional Cancellable object, None to ignore.

write_all_async(buffer: Sequence[int], io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 write_all_finish() to get the result of the operation.

This is the asynchronous version of write_all().

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

Added in version 2.44.

Parameters:
  • buffer – the buffer containing the data to write

  • io_priority – the io priority of the request

  • cancellable – optional Cancellable object, None to ignore

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

write_all_finish(result: AsyncResult) tuple[bool, int]

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

As a special exception to the normal conventions for functions that use 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 write_async().

Added in version 2.44.

Parameters:

result – a AsyncResult

write_async(buffer: Sequence[int], io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 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 PENDING errors.

A value of count larger than %G_MAXSSIZE will cause a 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 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 write().

Note that no copy of buffer will be made, so it must stay valid until callback is called. See write_bytes_async() for a 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 – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

write_bytes(bytes: Bytes, cancellable: Cancellable | None = None) int

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

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

Parameters:
  • bytes – the Bytes to write

  • cancellable – optional cancellable object

write_bytes_async(bytes: Bytes, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

This function is similar to write_async(), but takes a Bytes as input. Due to the refcounted nature of 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 write_async(). If that occurs, to continue writing, you will need to create a new Bytes containing just the remaining bytes, using new_from_bytes(). Passing the same Bytes instance multiple times potentially can result in duplicated data in the output stream.

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

Parameters:
  • bytes – The bytes to write

  • io_priority – the io priority of the request.

  • cancellable – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

write_bytes_finish(result: AsyncResult) int

Finishes a stream write-from-Bytes operation.

Parameters:

result – a AsyncResult.

write_finish(result: AsyncResult) int

Finishes a stream write operation.

Parameters:

result – a AsyncResult.

writev(vectors: Sequence[OutputVector], cancellable: Cancellable | None = 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 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 writev() may have limitations on the aggregate buffer size, and will return 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.

Added in version 2.60.

Parameters:
  • vectors – the buffer containing the OutputVector to write.

  • cancellable – optional cancellable object

writev_all(vectors: Sequence[OutputVector], cancellable: Cancellable | None = 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 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 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 write().

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

Added in version 2.60.

Parameters:
  • vectors – the buffer containing the OutputVector to write.

  • cancellable – optional Cancellable object, None to ignore.

writev_all_async(vectors: Sequence[OutputVector], io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 writev_all_finish() to get the result of the operation.

This is the asynchronous version of writev_all().

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

Added in version 2.60.

Parameters:
  • vectors – the buffer containing the OutputVector to write.

  • io_priority – the I/O priority of the request

  • cancellable – optional Cancellable object, None to ignore

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

writev_all_finish(result: AsyncResult) tuple[bool, int]

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

As a special exception to the normal conventions for functions that use 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 writev_async().

Added in version 2.60.

Parameters:

result – a AsyncResult

writev_async(vectors: Sequence[OutputVector], io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 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 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 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 writev().

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

Added in version 2.60.

Parameters:
  • vectors – the buffer containing the OutputVector to write.

  • io_priority – the I/O priority of the request.

  • cancellable – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

writev_finish(result: AsyncResult) tuple[bool, int]

Finishes a stream writev operation.

Added in version 2.60.

Parameters:

result – a AsyncResult.

Virtual Methods

class OutputStream
do_close_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 close_finish() to get the result of the operation.

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

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

do_close_finish(result: AsyncResult) bool

Closes an output stream.

Parameters:

result – a AsyncResult.

do_close_fn(cancellable: Cancellable | None = None) bool

The type of the None singleton.

Parameters:

cancellable

do_flush(cancellable: Cancellable | None = None) 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 None, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error CANCELLED will be returned.

Parameters:

cancellable – optional cancellable object

do_flush_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

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

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

Parameters:
  • io_priority – the io priority of the request.

  • cancellable – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

do_flush_finish(result: AsyncResult) bool

Finishes flushing an output stream.

Parameters:

result – a GAsyncResult.

do_splice(source: InputStream, flags: OutputStreamSpliceFlags, cancellable: Cancellable | None = None) int

Splices an input stream into an output stream.

Parameters:
do_splice_async(source: InputStream, flags: OutputStreamSpliceFlags, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

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

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

Parameters:
  • source – a InputStream.

  • flags – a set of OutputStreamSpliceFlags.

  • io_priority – the io priority of the request.

  • cancellable – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

do_splice_finish(result: AsyncResult) int

Finishes an asynchronous stream splice operation.

Parameters:

result – a AsyncResult.

do_write_async(buffer: Sequence[int] | None, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 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 PENDING errors.

A value of count larger than %G_MAXSSIZE will cause a 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 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 write().

Note that no copy of buffer will be made, so it must stay valid until callback is called. See write_bytes_async() for a 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 – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

do_write_finish(result: AsyncResult) int

Finishes a stream write operation.

Parameters:

result – a AsyncResult.

do_write_fn(buffer: Sequence[int] | None = None, cancellable: Cancellable | None = None) int

The type of the None singleton.

Parameters:
  • buffer – the buffer containing the data to write.

  • cancellable – optional cancellable object

do_writev_async(vectors: Sequence[OutputVector], io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) 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 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 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 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 writev().

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

Added in version 2.60.

Parameters:
  • vectors – the buffer containing the OutputVector to write.

  • io_priority – the I/O priority of the request.

  • cancellable – optional Cancellable object, None to ignore.

  • callback – a AsyncReadyCallback to call when the request is satisfied

  • user_data – the data to pass to callback function

do_writev_finish(result: AsyncResult) tuple[bool, int]

Finishes a stream writev operation.

Added in version 2.60.

Parameters:

result – a AsyncResult.

do_writev_fn(vectors: Sequence[OutputVector], cancellable: Cancellable | None = None) tuple[bool, int]

The type of the None singleton.

Added in version 2.60.

Parameters:
  • vectors – the buffer containing the OutputVector to write.

  • cancellable – optional cancellable object

Fields

class OutputStream
parent_instance
priv