DataInputStream#
Superclasses: BufferedInputStream
, FilterInputStream
, InputStream
, Object
Implemented Interfaces: Seekable
Data input stream implements InputStream
and includes functions
for reading structured data directly from a binary input stream.
Constructors#
- class DataInputStream
- classmethod new(base_stream: InputStream) DataInputStream #
Creates a new data input stream for the
base_stream
.- Parameters:
base_stream – a
InputStream
.
Methods#
- class DataInputStream
- get_byte_order() DataStreamByteOrder #
Gets the byte order for the data input stream.
- get_newline_type() DataStreamNewlineType #
Gets the current newline type for the
stream
.
- read_byte(cancellable: Cancellable | None = None) int #
Reads an unsigned 8-bit/1-byte value from
stream
.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_int16(cancellable: Cancellable | None = None) int #
Reads a 16-bit/2-byte value from
stream
.In order to get the correct byte order for this read operation, see
get_byte_order()
andset_byte_order()
.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_int32(cancellable: Cancellable | None = None) int #
Reads a signed 32-bit/4-byte value from
stream
.In order to get the correct byte order for this read operation, see
get_byte_order()
andset_byte_order()
.If
cancellable
is notNone
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLED
will be returned.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_int64(cancellable: Cancellable | None = None) int #
Reads a 64-bit/8-byte value from
stream
.In order to get the correct byte order for this read operation, see
get_byte_order()
andset_byte_order()
.If
cancellable
is notNone
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLED
will be returned.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_line(cancellable: Cancellable | None = None) Tuple[list[int] | None, int] #
Reads a line from the data input stream. Note that no encoding checks or conversion is performed; the input is not guaranteed to be UTF-8, and may in fact have embedded NUL characters.
If
cancellable
is notNone
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLED
will be returned.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_line_async(io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
The asynchronous version of
read_line()
. It is an error to have two outstanding calls to this function.When the operation is finished,
callback
will be called. You can then callread_line_finish()
to get the result of the operation.Added in version 2.20.
- Parameters:
io_priority – the I/O priority of the request
cancellable – optional
Cancellable
object,None
to ignore.callback – callback to call when the request is satisfied.
user_data – the data to pass to callback function.
- read_line_finish(result: AsyncResult) Tuple[list[int] | None, int] #
Finish an asynchronous call started by
read_line_async()
. Note the warning about string encoding inread_line()
applies here as well.Added in version 2.20.
- Parameters:
result – the
AsyncResult
that was provided to the callback.
- read_line_finish_utf8(result: AsyncResult) Tuple[str | None, int] #
Finish an asynchronous call started by
read_line_async()
.Added in version 2.30.
- Parameters:
result – the
AsyncResult
that was provided to the callback.
- read_line_utf8(cancellable: Cancellable | None = None) Tuple[str | None, int] #
Reads a UTF-8 encoded line from the data input stream.
If
cancellable
is notNone
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLED
will be returned.Added in version 2.30.
- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_uint16(cancellable: Cancellable | None = None) int #
Reads an unsigned 16-bit/2-byte value from
stream
.In order to get the correct byte order for this read operation, see
get_byte_order()
andset_byte_order()
.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_uint32(cancellable: Cancellable | None = None) int #
Reads an unsigned 32-bit/4-byte value from
stream
.In order to get the correct byte order for this read operation, see
get_byte_order()
andset_byte_order()
.If
cancellable
is notNone
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLED
will be returned.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_uint64(cancellable: Cancellable | None = None) int #
Reads an unsigned 64-bit/8-byte value from
stream
.In order to get the correct byte order for this read operation, see
get_byte_order()
.If
cancellable
is notNone
, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the errorCANCELLED
will be returned.- Parameters:
cancellable – optional
Cancellable
object,None
to ignore.
- read_until(stop_chars: str, cancellable: Cancellable | None = None) Tuple[str, int] #
Reads a string from the data input stream, up to the first occurrence of any of the stop characters.
Note that, in contrast to
read_until_async()
, this function consumes the stop character that it finds.Don’t use this function in new code. Its functionality is inconsistent with
read_until_async()
. Both functions will be marked as deprecated in a future release. Useread_upto()
instead, but note that that function does not consume the stop character.Deprecated since version 2.56:
- Use
read_upto()
instead, which has more consistent behaviour regarding the stop character.
- Parameters:
stop_chars – characters to terminate the read.
cancellable – optional
Cancellable
object,None
to ignore.
- Use
- read_until_async(stop_chars: str, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
The asynchronous version of
read_until()
. It is an error to have two outstanding calls to this function.Note that, in contrast to
read_until()
, this function does not consume the stop character that it finds. You must read it for yourself.When the operation is finished,
callback
will be called. You can then callread_until_finish()
to get the result of the operation.Don’t use this function in new code. Its functionality is inconsistent with
read_until()
. Both functions will be marked as deprecated in a future release. Useread_upto_async()
instead.Added in version 2.20.
Deprecated since version 2.56:
- Use
read_upto_async()
instead, which has more consistent behaviour regarding the stop character.
- Parameters:
stop_chars – characters to terminate the read.
io_priority – the I/O priority of the request
cancellable – optional
Cancellable
object,None
to ignore.callback – callback to call when the request is satisfied.
user_data – the data to pass to callback function.
- Use
- read_until_finish(result: AsyncResult) Tuple[str, int] #
Finish an asynchronous call started by
read_until_async()
.Added in version 2.20.
Deprecated since version 2.56:
- Use
read_upto_finish()
instead, which has more consistent behaviour regarding the stop character.
- Parameters:
result – the
AsyncResult
that was provided to the callback.
- Use
- read_upto(stop_chars: str, stop_chars_len: int, cancellable: Cancellable | None = None) Tuple[str, int] #
Reads a string from the data input stream, up to the first occurrence of any of the stop characters.
In contrast to
read_until()
, this function does not consume the stop character. You have to useread_byte()
to get it before callingread_upto()
again.Note that
stop_chars
may contain ‘0’ ifstop_chars_len
is specified.The returned string will always be nul-terminated on success.
Added in version 2.26.
- Parameters:
stop_chars – characters to terminate the read
stop_chars_len – length of
stop_chars
. May be -1 ifstop_chars
is nul-terminatedcancellable – optional
Cancellable
object,None
to ignore
- read_upto_async(stop_chars: str, stop_chars_len: int, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None #
The asynchronous version of
read_upto()
. It is an error to have two outstanding calls to this function.In contrast to
read_until()
, this function does not consume the stop character. You have to useread_byte()
to get it before callingread_upto()
again.Note that
stop_chars
may contain ‘0’ ifstop_chars_len
is specified.When the operation is finished,
callback
will be called. You can then callread_upto_finish()
to get the result of the operation.Added in version 2.26.
- Parameters:
stop_chars – characters to terminate the read
stop_chars_len – length of
stop_chars
. May be -1 ifstop_chars
is nul-terminatedio_priority – the I/O priority of the request
cancellable – optional
Cancellable
object,None
to ignorecallback – callback to call when the request is satisfied
user_data – the data to pass to callback function
- read_upto_finish(result: AsyncResult) Tuple[str, int] #
Finish an asynchronous call started by
read_upto_async()
.Note that this function does not consume the stop character. You have to use
read_byte()
to get it before callingread_upto_async()
again.The returned string will always be nul-terminated on success.
Added in version 2.24.
- Parameters:
result – the
AsyncResult
that was provided to the callback
- set_byte_order(order: DataStreamByteOrder) None #
This function sets the byte order for the given
stream
. All subsequent reads from thestream
will be read in the givenorder
.- Parameters:
order – a
DataStreamByteOrder
to set.
- set_newline_type(type: DataStreamNewlineType) None #
Sets the newline type for the
stream
.Note that using G_DATA_STREAM_NEWLINE_TYPE_ANY is slightly unsafe. If a read chunk ends in “CR” we must read an additional byte to know if this is “CR” or “CR LF”, and this might block if there is no more data available.
- Parameters:
type – the type of new line return as
DataStreamNewlineType
.
Properties#
- class DataInputStream
- props.byte_order: DataStreamByteOrder#
The type of the None singleton.
- props.newline_type: DataStreamNewlineType#
The type of the None singleton.