File size: 1,241 Bytes
692f0d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Bounded byte ring buffer used for pre-roll and audio staging."""

from __future__ import annotations

from collections import deque


class RingBuffer:
    def __init__(self, capacity_bytes: int) -> None:
        if capacity_bytes <= 0:
            raise ValueError("capacity_bytes must be positive")
        self.capacity_bytes = capacity_bytes
        self._chunks: deque[bytes] = deque()
        self._size = 0

    @property
    def size_bytes(self) -> int:
        return self._size

    def append(self, data: bytes) -> None:
        chunk = bytes(data)
        if not chunk:
            return
        self._chunks.append(chunk)
        self._size += len(chunk)
        self._trim()

    def _trim(self) -> None:
        while self._size > self.capacity_bytes and self._chunks:
            over = self._size - self.capacity_bytes
            first = self._chunks[0]
            if len(first) <= over:
                self._size -= len(first)
                self._chunks.popleft()
            else:
                self._chunks[0] = first[over:]
                self._size -= over

    def read(self) -> bytes:
        return b"".join(self._chunks)

    def clear(self) -> None:
        self._chunks.clear()
        self._size = 0