Skip to main content

LazyStream

The LazyStream wrapper allows one to get and "unget" bytes from a stream.

Attributes

AttributeTypeDescription
_produceriterableAn iterable that returns a bytestring each time it is called to provide the underlying data stream.
_emptyboolean = FalseA boolean flag indicating whether the stream has been exhausted.
_leftoverbytes = b""Stores bytes that have been "unget" or read from the producer but not yet consumed by the user.
lengthintegerThe total expected length of the stream in bytes, if known.
positioninteger = 0The current byte offset within the stream, which is updated during read and unget operations.
_remainingintegerThe number of bytes remaining to be read from the stream based on the initial length.
_unget_historylist = []A list tracking the sizes of recent unget operations to detect potential infinite loops caused by malformed input.

Constructor

Signature

def LazyStream(
producer: iterable,
length: int = None
) - > null

Parameters

NameTypeDescription
produceriterableAn iterable that returns a string each time it is called.
lengthint = NoneThe total length of the stream, if known.

Methods


tell()

@classmethod
def tell() - > int

Returns the current byte position within the stream, accounting for both read and unget operations.

Returns

TypeDescription
intThe current integer offset from the start of the stream

read()

@classmethod
def read(
size: int = None
) - > bytes

Reads a specified number of bytes from the stream, managing internal buffers and producer iteration to return exactly the requested amount.

Parameters

NameTypeDescription
sizeint = NoneThe maximum number of bytes to read; if None, reads until the stream is exhausted

Returns

TypeDescription
bytesThe bytestring containing the data read from the stream

close()

@classmethod
def close()

Used to invalidate/disable this lazy stream. Replace the producer with an empty list. Any leftover bytes that have already been read will still be reported upon read() and/or next().


unget()

@classmethod
def unget(
bytes: bytes
)

Place bytes back onto the front of the lazy stream. Future calls to read() will return those bytes first. The stream position and thus tell() will be rewound.

Parameters

NameTypeDescription
bytesbytesThe bytestring to be pushed back into the stream's internal buffer