BufferedInputStream
Superclasses: FilterInputStream
, InputStream
, Object
Subclasses: DataInputStream
Implemented Interfaces: Seekable
Buffered input stream implements FilterInputStream
and provides
for buffered reads.
By default, GBufferedInputStream
’s buffer size is set at 4 kilobytes.
To create a buffered input stream, use new
,
or new_sized
to specify the buffer’s size at
construction.
To get the size of a buffer within a buffered input stream, use
get_buffer_size
. To change the size of a
buffered input stream’s buffer, use set_buffer_size
.
Note that the buffer’s size cannot be reduced below the size of the data within the buffer.
Constructors
- class BufferedInputStream
- classmethod new(base_stream: InputStream) InputStream
Creates a new
InputStream
from the givenbase_stream
, with a buffer set to the default size (4 kilobytes).- Parameters:
base_stream – a
InputStream
- classmethod new_sized(base_stream: InputStream, size: int) InputStream
Creates a new
BufferedInputStream
from the givenbase_stream
, with a buffer set tosize
.- Parameters:
base_stream – a
InputStream
size – a
gsize
Methods
- class BufferedInputStream
- fill(count: int, cancellable: Cancellable | None = None) int
Tries to read
count
bytes from the stream into the buffer. Will block during this read.If
count
is zero, returns zero and does nothing. A value ofcount
larger than%G_MAXSSIZE
will cause aINVALID_ARGUMENT
error.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
count
is zero), but never otherwise.If
count
is -1 then the attempted read size is equal to the number of bytes that are required to fill the buffer.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. 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.For the asynchronous, non-blocking, version of this function, see
fill_async()
.- Parameters:
count – the number of bytes that will be read from the stream
cancellable – optional
Cancellable
object,None
to ignore
- fill_async(count: int, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Reads data into
stream
’s buffer asynchronously, up tocount
size.io_priority
can be used to prioritize reads. For the synchronous version of this function, seefill()
.If
count
is -1 then the attempted read size is equal to the number of bytes that are required to fill the buffer.- Parameters:
count – the number of bytes that will be read from the stream
io_priority – the [I/O priority][io-priority] of the request
cancellable – optional
Cancellable
objectcallback – a
AsyncReadyCallback
user_data – a
gpointer
- fill_finish(result: AsyncResult) int
Finishes an asynchronous read.
- Parameters:
result – a
AsyncResult
- peek(buffer: Sequence[int], offset: int) int
Peeks in the buffer, copying data of size
count
intobuffer
, offsetoffset
bytes.- Parameters:
buffer – a pointer to an allocated chunk of memory
offset – a
gsize
- peek_buffer() bytes
Returns the buffer with the currently available bytes. The returned buffer must not be modified and will become invalid when reading from the stream or filling the buffer.
- read_byte(cancellable: Cancellable | None = None) int
Tries to read a single byte from the stream or the buffer. Will block during this read.
On success, the byte read from the stream is returned. On end of stream
-1 is returned but it’s not an exceptional error and
error
is not set.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. 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:
cancellable – optional
Cancellable
object,None
to ignore
Properties
Virtual Methods
- class BufferedInputStream
- do_fill(count: int, cancellable: Cancellable | None = None) int
Tries to read
count
bytes from the stream into the buffer. Will block during this read.If
count
is zero, returns zero and does nothing. A value ofcount
larger than%G_MAXSSIZE
will cause aINVALID_ARGUMENT
error.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
count
is zero), but never otherwise.If
count
is -1 then the attempted read size is equal to the number of bytes that are required to fill the buffer.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. 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.For the asynchronous, non-blocking, version of this function, see
fill_async()
.- Parameters:
count – the number of bytes that will be read from the stream
cancellable – optional
Cancellable
object,None
to ignore
- do_fill_async(count: int, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
Reads data into
stream
’s buffer asynchronously, up tocount
size.io_priority
can be used to prioritize reads. For the synchronous version of this function, seefill()
.If
count
is -1 then the attempted read size is equal to the number of bytes that are required to fill the buffer.- Parameters:
count – the number of bytes that will be read from the stream
io_priority – the [I/O priority][io-priority] of the request
cancellable – optional
Cancellable
objectcallback – a
AsyncReadyCallback
user_data – a
gpointer
- do_fill_finish(result: AsyncResult) int
Finishes an asynchronous read.
- Parameters:
result – a
AsyncResult