Module: anynet.streams
Provides classes to parse and build files in a stream-like fashion.
class StreamOut
A byte stream that writes to a memory buffer.
class StreamIn
A byte stream that reads from a memory buffer.
class BitStreamOut(StreamOut)
An output stream that can write individual bits.
class BitStreamIn(StreamIn)
An input stream that can read individual bits.
StreamOut
def __init__(endian: str)
Creates a new output stream with an empty memory buffer. endian should be either "<" (little-endian) or ">" (big-endian).
def push() -> None
Saves the current stream position on a stack.
def pop() -> None
Restores the old stream position from the stack.
def get() -> bytes
Returns a copy of the memory buffer.
def size() -> int
Returns the size of the current memory buffer in bytes.
def tell() -> int
Returns the current position of the stream in bytes.
def seek(int: pos) -> None
Moves the stream to a different position in bytes. If the stream is moved past the end of the current memory buffer it is filled with null bytes up to the given pos.
def skip(int: num) -> None
Increases the stream position by num bytes. If the stream is moved past the end of the current memory buffer it is filled with null bytes up to the new position.
def align(int: num) -> None
Makes sure that the stream position is a multiple of num bytes (increasing it if necessary). If the stream is moved past the end of the current memory buffer it is filled with null bytes up to the new position.
def available() -> int
Returns the number of bytes between the current stream position and the end of the current memory buffer.
def eof() -> bool
Returns True if the stream points at the end of the current memory buffer.
def write(data: bytes) -> None
Writes data into the memory buffer and increases the stream position.
def pad(num: int, char: bytes = b"\0") -> None
Writes num copies of char into the stream.
def ascii(data: str) -> None
Writes an ascii string into the stream (without null terminator).
def u8(value: int) -> None
Writes an unsigned 8-bit integer into the stream.
def u16(value: int) -> None
Writes an unsigned 16-bit integer into the stream.
def u24(value: int) -> None
Writes an unsigned 24-bit integer into the stream.
def u32(value: int) -> None
Writes an unsigned 32-bit integer into the stream.
def u64(value: int) -> None
Writes an unsigned 64-bit integer into the stream.
def s8(value: int) -> None
Writes a signed 8-bit integer into the stream.
def s16(value: int) -> None
Writes a signed 16-bit integer into the stream.
def s32(value: int) -> None
Writes a signed 32-bit integer into the stream.
def s64(value: int) -> None
Writes a signed 64-bit integer into the stream.
def float(value: float) -> None
Writes a 32-bit float into the stream.
def double(value: float) -> None
Writes a 64-bit float into the stream.
def bool(value: bool) -> None
Writes a bool into the stream as an 8-bit integer (either 0 or 1).
def char(value: str) -> None
Writes a single 8-bit unicode character into the stream.
def wchar(value: str) -> None
Writes a single 16-bit unicode character into the stream.
def chars(value: str) -> None
Writes a string of 8-bit unicode characters into the stream.
def wchars(value: str) -> None
Writes a string of 16-bit unicode characters into the stream.
def repeat(list: list, func: Callable) -> None
Invokes func on every item in list. This function can be used to write a list of values into the stream. For example: stream.repeat([1, 2, 3], stream.u8).
StreamIn
Any operation that moves the stream position past the end of the buffer raises OverflowError.
def __init__(data: bytes, endian: str)
Creates a new input stream from the given memory buffer. endian should be either "<" (little-endian) or ">" (big-endian).
def push() -> None
Saves the current stream position on a stack.
def pop() -> None
Restores the old stream position from the stack.
def get() -> bytes
Returns the whole memory buffer.
def size() -> int
Returns the size of the memory buffer in bytes.
def tell() -> int
Returns the current position of the stream in bytes.
def seek(int: pos) -> None
Moves the stream to a different position in bytes.
def skip(int: num) -> None
Increases the stream position by num bytes.
def align(int: num) -> None
Makes sure that the stream position is a multiple of num bytes (increasing it if necessary).
def available() -> int
Returns the number of bytes between the current stream position and the end of the memory buffer.
def eof() -> bool
Returns True if the stream points at the end of the memory buffer.
def peek(num: int) -> bytes
Reads num bytes from the memory buffer without advancing the stream pointer. Raises OverflowError if less than num bytes are available.
def read(num: int) -> bytes
Reads num bytes from the memory buffer and advances the stream pointer accordingly.
def readall() -> bytes
Reads all remaining bytes from the memory buffer.
def pad(num: int, char: bytes = b"\0") -> None
Consumes num bytes from the memory buffer and makes sure that they are equal to char. Raises ValueError if any of the bytes is not equal to char.
def ascii(num: int) -> str
Reads an ascii string from the stream of num bytes.
def u8() -> int
Reads an unsigned 8-bit integer from the stream.
def u16() -> int
Reads an unsigned 16-bit integer from the stream.
def u24() -> int
Reads an unsigned 24-bit integer from the stream.
def u32() -> int
Reads an unsigned 32-bit integer from the stream.
def u64() -> int
Reads an unsigned 64-bit integer from the stream.
def s8() -> int
Reads a signed 8-bit integer from the stream.
def s16() -> int
Reads a signed 16-bit integer from the stream.
def s32() -> int
Reads a signed 32-bit integer from the stream.
def s64() -> int
Reads a signed 64-bit integer from the stream.
def float() -> float
Reads a 32-bit float from the stream.
def double() -> float
Reads a 64-bit float from the stream.
def bool() -> bool
Reads an 8-bit integer from the stream. Returns True if the integer is non-zero.
def char() -> str
Reads an 8-bit unicode character from the stream.
def wchar() -> str
Reads a 16-bit unicode character from the stream.
def chars(num: int) -> str
Reads num 8-bit unicode characters from the stream.
def wchars(num: int) -> str
Reads num 16-bit unicode characters from the stream.
def repeat(func: Callable, num: int) -> list
Invokes func exactly num times and returns its return values as a list. This function can be used to read a list of values from the stream. For example: values = stream.repeat(stream.u8, 10)
BitStreamOut
This class inherits StreamOut. All regular functions are still supported. However, writes are much faster if the stream is byte aligned.
def seekbits(pos: int) -> None
Moves the stream to a different position in bits.
def tellbits() -> int
Returns the current position of the stream in bits
def bytealign() -> None
Ensures that stream points to the start of a byte. If the stream points into the middle of a byte it is moved to the next byte.
def bit(value: int) -> None
Writes a single bit into the stream.
def bits(value: int, num: int) -> None
Writes an unsigned integer of num bits into the stream in big-endian bit and byte order.
BitStreamIn
This class inherits StreamIn. All regular functions are still supported. However, reads are much faster if the stream is byte aligned.
def seekbits(pos: int) -> None
Moves the stream to a different position in bits.
def tellbits() -> int
Returns the current position of the stream in bits
def bytealign() -> None
Ensures that stream points to the start of a byte. If the stream points into the middle of a byte it is moved to the next byte.
def bit() -> int
Reads a single bit from the stream.
def bits(num: int) -> int
Reads an unsigned integer of num bits from the stream in big-endian bit and byte order.