Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

io.BufferedIOBase

Defines the common interface for buffered binary streams with byte-oriented reads and writes.

Version: Python 3.14; repository image, production runtime not attested

Syntax

io.BufferedIOBase()

Defines the common interface for buffered binary streams with byte-oriented reads and writes.

A buffered reader returns bytes and signals end of input with an empty byte string, even if the contents decode to ordinary text.

Use readable() and writable() to inspect the concrete stream. A BufferedReader can return fewer bytes than requested at EOF. Reading zero bytes also returns b'' even when data remains. Writing to this reader raises io.UnsupportedOperation. IOBase provides operations such as close. Concrete streams supply supported read, read1, write and detach operations. The readinto and readinto1 mixins fill a caller-supplied writable buffer and return the number of bytes placed there.

import io
with io.BufferedReader(io.BytesIO(b'xy')) as reader:
    assert reader.read(0)==b'' and reader.read(1)==b'x'
    assert reader.read(10)==b'y' and reader.read(1)==b''
    try: reader.write(b'z')
    except io.UnsupportedOperation: pass
    else: raise AssertionError
with io.BufferedWriter(io.BytesIO()) as writer:
    assert isinstance(writer, io.BufferedIOBase) and writer.write(b'x')==1
with io.BufferedReader(io.BytesIO(b'xyz')) as reader:
    target=bytearray(2)
    assert reader.readinto(target)==2 and target==b'xy'
    assert reader.readinto1(target)==1 and target==b'zy'
    assert reader.readinto(target)==0

Examples

import io
with io.BufferedReader(io.BytesIO(b"abc")) as stream:
    print(isinstance(stream, io.BufferedIOBase))
    print(stream.read(2), stream.read(2), stream.read(2))

Result

True
b'ab' b'c' b''

Use readable() and writable() to inspect the concrete stream. A BufferedReader can return fewer bytes than requested at EOF. Reading zero bytes also returns b'' even when data remains. Writing to this reader raises io.UnsupportedOperation. IOBase provides operations such as close. Concrete streams supply supported read, read1, write and detach operations. The readinto and readinto1 mixins fill a caller-supplied writable buffer and return the number of bytes placed there.

Sources