CKTIS / Python
io.BufferedIOBase
Základní třída popisuje binární proudy s vyrovnávací pamětí.
Verze: Python 3.14; repository image, production runtime not attested
Syntaxe
io.BufferedIOBase()Základní třída popisuje binární proudy s vyrovnávací pamětí.
Zavírání se dědí z IOBase. Konkrétní proud implementuje podporované operace z nabídky read, read1, write a detach. Poskytnuté metody readinto a readinto1 naplní předanou zapisovatelnou vyrovnávací paměť a vrátí počet zapsaných bajtů. readable() a writable() určují dostupné operace. Pro b'abc' vrátí BufferedReader při opakovaném read(2) postupně b'ab', b'c' a b''. Poslední blok může být kratší. read(0) vrací b'' i před koncem.
Zápis do této čtečky vyvolá io.UnsupportedOperation.
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
Příklady
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))
Výsledek
True b'ab' b'c' b''
Zápis do této čtečky vyvolá io.UnsupportedOperation.