InputStream#
Superclasses: Object
Subclasses: FileInputStream, FilterInputStream, MemoryInputStream, UnixInputStream
GInputStream is a base class for implementing streaming input.
It has functions to read from a stream (read),
to close a stream (close) and to skip some content
(skip).
To copy the content of an input stream to an output stream without
manually handling the reads and writes, use splice.
See the documentation for IOStream for details of thread safety
of streaming APIs.
All of these functions have async variants too.
Methods#
- class InputStream
-
- 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.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
CLOSEDfor all operations. Still, it is important to check and report the error to the user.If
cancellableis notNone, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLEDwill 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.- Parameters:
cancellable – optional
Cancellableobject,Noneto ignore.
- async close_async(self, io_priority: int) bool#
This is the awaitable version of
close_async().- Parameters:
io_priority – the I/O priority of the request
- close_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
Requests an asynchronous closes of the stream, releasing resources related to it. When the operation is finished
callbackwill be called. You can then callclose_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 I/O priority of the request
cancellable – optional cancellable object
callback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- close_finish(result: AsyncResult) bool#
Finishes closing a stream asynchronously, started from
close_async().- Parameters:
result – a
AsyncResult.
- do_close_async(self, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
- Parameters:
io_priority
cancellable
callback
user_data
- do_close_finish(self, result: AsyncResult) bool#
- Parameters:
result
- do_close_fn(self, cancellable: Cancellable | None = None) bool#
- Parameters:
cancellable
- do_read_async(self, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) list[int] | None#
- Parameters:
io_priority
cancellable
callback
user_data
- do_read_finish(self, result: AsyncResult) int#
- Parameters:
result
- do_read_fn(self, buffer: Any, count: int, cancellable: Cancellable | None = None) int#
- Parameters:
buffer
count
cancellable
- do_skip(self, count: int, cancellable: Cancellable | None = None) int#
- Parameters:
count
cancellable
- do_skip_async(self, count: int, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
- Parameters:
count
io_priority
cancellable
callback
user_data
- do_skip_finish(self, result: AsyncResult) int#
- Parameters:
result
- read(cancellable: Cancellable | None = None) tuple[int, list[int]]#
Tries to read
countbytes from the stream into the buffer starting atbuffer. Will block during this read.If count is zero returns zero and does nothing. A value of
countlarger than %G_MAXSSIZE will cause aINVALID_ARGUMENTerror.On success, the number of bytes read into the buffer is returned. It is not an error if this is not the same as the requested size, as it can happen e.g. near the end of a file. Zero is returned on end of file (or if
countis zero), but never otherwise.The returned
bufferis not a nul-terminated string, it can contain nul bytes at any position, and this function doesn’t nul-terminate thebuffer.If
cancellableis notNone, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLEDwill 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
erroris set accordingly.- Parameters:
cancellable – optional
Cancellableobject,Noneto ignore.
- read_all(cancellable: Cancellable | None = None) tuple[bool, list[int], int]#
Tries to read
countbytes from the stream into the buffer starting atbuffer. Will block during this read.This function is similar to
read(), except it tries to read as many bytes as requested, only stopping on an error or end of stream.On a successful read of
countbytes, or if we reached the end of the stream,Trueis returned, andbytes_readis set to the number of bytes read intobuffer.If there is an error during the operation
Falseis returned anderroris set to indicate the error status.As a special exception to the normal conventions for functions that use
Error, if this function returnsFalse(and setserror) thenbytes_readwill be set to the number of bytes that were successfully read 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 aroundread().- Parameters:
cancellable – optional
Cancellableobject,Noneto ignore.
- read_all_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) list[int]#
Request an asynchronous read of
countbytes from the stream into the buffer starting atbuffer.This is the asynchronous equivalent of
read_all().Call
read_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.
Added in version 2.44.
- Parameters:
io_priority – the I/O priority of the request
cancellable – optional
Cancellableobject,Noneto ignorecallback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- read_all_finish(result: AsyncResult) tuple[bool, int]#
Finishes an asynchronous stream read operation started with
read_all_async().As a special exception to the normal conventions for functions that use
Error, if this function returnsFalse(and setserror) thenbytes_readwill be set to the number of bytes that were successfully read 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 aroundread_async().Added in version 2.44.
- Parameters:
result – a
AsyncResult
- read_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) list[int]#
Request an asynchronous read of
countbytes from the stream into the buffer starting atbuffer. When the operation is finishedcallbackwill be called. You can then callread_finish()to get the result of the operation.During an async request no other sync and async calls are allowed on
stream, and will result inPENDINGerrors.A value of
countlarger than %G_MAXSSIZE will cause aINVALID_ARGUMENTerror.On success, the number of bytes read into the buffer 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. near the end of a file, but generally we try to read as many bytes as requested. Zero is returned on end of file (or if
countis zero), but never otherwise.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.
- Parameters:
io_priority – the I/O priority of the request.
cancellable – optional
Cancellableobject,Noneto ignore.callback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- read_bytes(count: int, cancellable: Cancellable | None = None) Bytes#
Like
read(), this tries to readcountbytes from the stream in a blocking fashion. However, rather than reading into a user-supplied buffer, this will create a newBytescontaining the data that was read. This may be easier to use from language bindings.If count is zero, returns a zero-length
Bytesand does nothing. A value ofcountlarger than %G_MAXSSIZE will cause aINVALID_ARGUMENTerror.On success, a new
Bytesis returned. It is not an error if the size of this object is not the same as the requested size, as it can happen e.g. near the end of a file. A zero-lengthBytesis returned on end of file (or ifcountis zero), but never otherwise.If
cancellableis notNone, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLEDwill be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.On error
Noneis returned anderroris set accordingly.Added in version 2.34.
- Parameters:
count – maximum number of bytes that will be read from the stream. Common values include 4096 and 8192.
cancellable – optional
Cancellableobject,Noneto ignore.
- async read_bytes_async(self, count: int, io_priority: int) Bytes#
This is the awaitable version of
read_bytes_async().Added in version 2.34.
- Parameters:
count – the number of bytes that will be read from the stream
io_priority – the I/O priority of the request
- read_bytes_async(count: int, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
Request an asynchronous read of
countbytes from the stream into a newBytes. When the operation is finishedcallbackwill be called. You can then callread_bytes_finish()to get the result of the operation.During an async request no other sync and async calls are allowed on
stream, and will result inPENDINGerrors.A value of
countlarger than %G_MAXSSIZE will cause aINVALID_ARGUMENTerror.On success, the new
Byteswill be passed to the callback. It is not an error if this is smaller than the requested size, as it can happen e.g. near the end of a file, but generally we try to read as many bytes as requested. Zero is returned on end of file (or ifcountis zero), but never otherwise.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.
Added in version 2.34.
- Parameters:
count – the number of bytes that will be read from the stream
io_priority – the I/O priority of the request
cancellable – optional
Cancellableobject,Noneto ignore.callback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- read_bytes_finish(result: AsyncResult) Bytes#
Finishes an asynchronous stream read-into-
Bytesoperation.Added in version 2.34.
- Parameters:
result – a
AsyncResult.
- read_finish(result: AsyncResult) int#
Finishes an asynchronous stream read operation.
- Parameters:
result – a
AsyncResult.
- set_pending() bool#
Sets
streamto have actions pending. If the pending flag is already set orstreamis closed, it will returnFalseand seterror.
- skip(count: int, cancellable: Cancellable | None = None) int#
Tries to skip
countbytes from the stream. Will block during the operation.This is identical to
read(), from a behaviour standpoint, but the bytes that are skipped are not returned to the user. Some streams have an implementation that is more efficient than reading the data.This function is optional for inherited classes, as the default implementation emulates it using read.
If
cancellableis notNone, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLEDwill be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.- Parameters:
count – the number of bytes that will be skipped from the stream
cancellable – optional
Cancellableobject,Noneto ignore.
- async skip_async(self, count: int, io_priority: int) int#
This is the awaitable version of
skip_async().- Parameters:
count – the number of bytes that will be skipped from the stream
io_priority – the I/O priority of the request
- skip_async(count: int, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
Request an asynchronous skip of
countbytes from the stream. When the operation is finishedcallbackwill be called. You can then callskip_finish()to get the result of the operation.During an async request no other sync and async calls are allowed, and will result in
PENDINGerrors.A value of
countlarger than %G_MAXSSIZE will cause aINVALID_ARGUMENTerror.On success, the number of bytes skipped 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. near the end of a file, but generally we try to skip as many bytes as requested. Zero is returned on end of file (or if
countis zero), but never otherwise.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.
- Parameters:
count – the number of bytes that will be skipped from the stream
io_priority – the I/O priority of the request
cancellable – optional
Cancellableobject,Noneto ignore.callback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- skip_finish(result: AsyncResult) int#
Finishes a stream skip operation.
- Parameters:
result – a
AsyncResult.
Virtual Methods#
- class InputStream
- do_close_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
Requests an asynchronous closes of the stream, releasing resources related to it. When the operation is finished
callbackwill be called. You can then callclose_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 I/O priority of the request
cancellable – optional cancellable object
callback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- do_close_finish(result: AsyncResult) bool#
Finishes closing a stream asynchronously, started from
close_async().- Parameters:
result – a
AsyncResult.
- do_close_fn(cancellable: Cancellable | None = None) bool#
The type of the None singleton.
- Parameters:
cancellable
- do_read_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) list[int] | None#
Request an asynchronous read of
countbytes from the stream into the buffer starting atbuffer. When the operation is finishedcallbackwill be called. You can then callread_finish()to get the result of the operation.During an async request no other sync and async calls are allowed on
stream, and will result inPENDINGerrors.A value of
countlarger than %G_MAXSSIZE will cause aINVALID_ARGUMENTerror.On success, the number of bytes read into the buffer 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. near the end of a file, but generally we try to read as many bytes as requested. Zero is returned on end of file (or if
countis zero), but never otherwise.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.
- Parameters:
io_priority – the I/O priority of the request.
cancellable – optional
Cancellableobject,Noneto ignore.callback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- do_read_finish(result: AsyncResult) int#
Finishes an asynchronous stream read operation.
- Parameters:
result – a
AsyncResult.
- do_read_fn(buffer: Any, count: int, cancellable: Cancellable | None = None) int#
The type of the None singleton.
- Parameters:
buffer
count
cancellable
- do_skip(count: int, cancellable: Cancellable | None = None) int#
Tries to skip
countbytes from the stream. Will block during the operation.This is identical to
read(), from a behaviour standpoint, but the bytes that are skipped are not returned to the user. Some streams have an implementation that is more efficient than reading the data.This function is optional for inherited classes, as the default implementation emulates it using read.
If
cancellableis notNone, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLEDwill be returned. If an operation was partially finished when the operation was cancelled the partial result will be returned, without an error.- Parameters:
count – the number of bytes that will be skipped from the stream
cancellable – optional
Cancellableobject,Noneto ignore.
- do_skip_async(count: int, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
Request an asynchronous skip of
countbytes from the stream. When the operation is finishedcallbackwill be called. You can then callskip_finish()to get the result of the operation.During an async request no other sync and async calls are allowed, and will result in
PENDINGerrors.A value of
countlarger than %G_MAXSSIZE will cause aINVALID_ARGUMENTerror.On success, the number of bytes skipped 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. near the end of a file, but generally we try to skip as many bytes as requested. Zero is returned on end of file (or if
countis zero), but never otherwise.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.
- Parameters:
count – the number of bytes that will be skipped from the stream
io_priority – the I/O priority of the request
cancellable – optional
Cancellableobject,Noneto ignore.callback – a
AsyncReadyCallbackto call when the request is satisfieduser_data – the data to pass to callback function
- do_skip_finish(result: AsyncResult) int#
Finishes a stream skip operation.
- Parameters:
result – a
AsyncResult.