Skip to Content
Binal
API Reference

I/O Reference

Partial decoding, incremental parsing, streams, and low-level I/O

decode_partial

Model.decode_partial(data, offset=0) decodes one record starting at offset and returns (instance, consumed). Bytes after the record are left for the caller — unlike decode(), which rejects trailing data. Error offsets are relative to the start of data, not to offset, and offset itself must satisfy 0 <= offset <= len(data).

from binal import BinaryModel, UInt8


class Packet(BinaryModel, byte_order="big"):
    version: UInt8


instance, consumed = Packet.decode_partial(b"\x07\x08\x09", offset=1)
# instance.version == 8, consumed == 1

IncrementalParser

Model.incremental_parser() returns a parser that decodes records from chunks fed over time. feed(data) appends to a buffer and returns the records completed by that chunk; bytes after a record’s end are retained for the next record. A decode failure with reason "Unexpected end of input" means the record is incomplete, so the bytes are kept; any other decode error means the input is invalid and is raised immediately. close() signals end of input and returns the final record, or None if the buffer is empty; it raises DecodeError on a truncated leftover or trailing data. feed after close raises. A model with a greedy bytes field only ends at EOF, so its record is produced by close().

from binal import BinaryModel, UInt8


class Packet(BinaryModel, byte_order="big"):
    version: UInt8


parser = Packet.incremental_parser()
records = parser.feed(b"\x01\x02")  # records completed by this chunk
records += parser.feed(b"\x03")
final = parser.close()  # the last record, or None

iter_read / iter_read_async

Packet.iter_read(stream) yields records from a binary stream until end of stream, buffering only the current record. Packet.iter_read_async(reader) is the async equivalent, for any async reader such as asyncio.StreamReader. Use the iterators (not repeated read() / read_async() calls) to read several records from one stream, so bytes buffered past one record’s end are kept for the next. A record that makes no progress raises DecodeError instead of looping forever. See I/O for the stream semantics.

BinaryReader / BinaryWriter

The low-level primitives BinaryModel is built on, for code that needs to read or write a binary format without declaring a model: custom codecs, ad-hoc parsers, and formats whose shape is only known at runtime. For declarative, type-safe modeling, use BinaryModel instead.

BinaryReader(data) reads from bytes, bytearray, or memoryview. read_int(bits, signed, byte_order), read_float(bits, byte_order), read_varint(kind, zigzag, bits=None), read_bits(width), read_bytes(n), and read_byte() consume input and raise DecodeError on truncation or an overlong varint. seek(pos) moves to an absolute byte position; remaining(), at_eof(), bytes_consumed(), and bits_consumed() report position. Limits (max_field_length, max_varint_bytes, …) come from Limits and are checked before allocation.

BinaryWriter() accumulates output. write_int(value, bits, signed, byte_order), write_float(value, bits, byte_order), write_varint(value, kind, zigzag, bits=None), write_bits(value, width), and write_bytes(value) append; bytes() returns the accumulated bytes. tell() is the current byte position (a pending partial byte counts); tell_bits() is the bit position. flush_bits() pads the current partial byte.

from binal import BinaryReader, BinaryWriter

writer = BinaryWriter()
writer.write_int(0x1234, 16, False, "big")
writer.write_bits(0b101, 3)
data = writer.bytes()

reader = BinaryReader(data)
reader.read_int(16, False, "big")  # 0x1234
reader.read_bits(3)  # 0b101

StreamReader and AsyncRecordReader are the internal stream readers behind read() / iter_read() / read_async() / iter_read_async(); they are not part of the public API.