
    #HJj                    T    d Z ddlmZ ddlZddlmZmZmZ erddlm	Z	  G d d      Z
y)zEImplements ByteBuffer class for amortizing network transfer overhead.    )annotationsN)IOTYPE_CHECKINGcast)Iterablec                  f    e Zd ZdZej
                  fd
dZddZdddZdddZ	ddZ
dddZddZy	)
ByteBuffera  Byte buffer that allows callers to read data with minimal copying, and has a fast ``__len__`` method.

    The buffer is parametrized by its ``chunk_size``, which is the number of
    bytes that it will read in from the supplied reader or iterable when the
    buffer is being filled. As the primary use case for this buffer is to
    amortize the overhead costs of transferring data over the network (rather
    than capping memory consumption), it leads to more predictable performance
    to always read the same amount of bytes each time the buffer is filled,
    hence the ``chunk_size`` parameter instead of some fixed capacity.

    The bytes are stored in a bytestring, and previously-read bytes are freed
    when the buffer is next filled (by slicing the bytestring into a smaller
    copy).

    Args:
        chunk_size: The number of bytes that will be read from the supplied reader
            or iterable when filling the buffer.

    Example:
        >>> buf = ByteBuffer(chunk_size=8)
        >>> message_bytes = iter([b"Hello, W", b"orld!"])
        >>> buf.fill(message_bytes)
        8
        >>> len(buf)  # only chunk_size bytes are filled
        8
        >>> buf.peek()
        b'Hello, W'
        >>> len(buf)  # peek() does not change read position
        8
        >>> buf.read(6)
        b'Hello,'
        >>> len(buf)  # read() does change read position
        2
        >>> buf.fill(message_bytes)
        5
        >>> buf.read()
        b' World!'
        >>> len(buf)
        0
    c                2    || _         | j                          y )N)_chunk_sizeempty)self
chunk_sizes     _/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/smart_open/bytebuffer.py__init__zByteBuffer.__init__<   s    %

    c                F    t        | j                        | j                  z
  S )z:Return the number of unread bytes in the buffer as an int.)len_bytes_posr   s    r   __len__zByteBuffer.__len__@   s    4;;$))++r   c                d    | j                  |      }| xj                  t        |      z  c_        |S )a  Read bytes from the buffer and advance the read position.

        Args:
            size: Maximum number of bytes to read. If negative or not supplied, read
                all unread bytes in the buffer.

        Returns:
            The bytes read from the buffer.
        )peekr   r   )r   sizeparts      r   readzByteBuffer.readD   s(     yy		SY	r   c                    |dk  s|t        |       kD  rt        |       }t        | j                  | j                  | j                  |z          S )a  Get bytes from the buffer without advancing the read position.

        Args:
            size: Maximum number of bytes to return. If negative or not supplied,
                return all unread bytes in the buffer.

        Returns:
            The peeked bytes from the buffer.
        r   )r   bytesr   r   )r   r   s     r   r   zByteBuffer.peekR   sB     !8tc$i't9DT[[TYY-=>??r   c                0    t               | _        d| _        y)z!Remove all bytes from the buffer.r   N)	bytearrayr   r   r   s    r   r   zByteBuffer.emptya   s    k	r   c                   |dk\  r|n| j                   }t        || j                         }| j                  dk7  r%| j                  | j                  d | _        d| _        t	        |d      rt        d|      j                  |      }n&t               }|D ]  }||z  }t        |      |k\  s n | xj                  |z  c_        t        |      S )a  Fill the buffer with bytes from source.

        Reads from ``source`` until one of these conditions is met:

        * ``size`` bytes have been read from source (if ``size >= 0``);
        * ``chunk_size`` bytes have been read from source;
        * no more bytes can be read from source.

        Note:
            All previously-read bytes in the buffer are removed.

        Args:
            source: The source of bytes to fill the buffer with, either a file-like
                object or an iterable/list of bytes. If this argument has the ``read``
                attribute, it's assumed to be a file-like object and ``read`` is called
                to get the bytes; otherwise it's assumed to be an iterable or list that
                contains bytes, and a for loop is used to get the bytes.
            size: The number of bytes to try to read from source. If not supplied,
                negative, or larger than the buffer's ``chunk_size``, then ``chunk_size``
                bytes are read. Note that if source is an iterable or list, then
                it's possible that more than size bytes will be read if iterating
                over source produces more than one byte at a time.

        Returns:
            The number of new bytes added to the buffer.
        r   Nr   z	IO[bytes])	r   minr   r   hasattrr   r   r    r   )r   sourcer   	new_bytes
more_bytess        r   fillzByteBuffer.fillf   s    6 qytd&6&64))*99>++diik2DKDI66"[&166t<I!I$
Z'	y>T) %
 	y 9~r   c                    | j                   j                  || j                        }|dk(  rt        |       n|| j                  z
  dz   }| j	                  |      S )aa  Read a line from this buffer efficiently.

        A line is a contiguous sequence of bytes that ends with either:

        1. The ``terminator`` character
        2. The end of the buffer itself

        Args:
            terminator: The line terminator byte.

        Returns:
            The line bytes (including the terminator if present).
           )r   findr   r   r   )r   
terminatorindexr   s       r   readlinezByteBuffer.readline   sK       TYY7!RKs4yUTYY->-Byyr   N)r   intreturnNone)r0   r/   )r)   )r   r/   r0   r   )r0   r1   )r$   zIO[bytes] | Iterable[bytes]r   r/   r0   r/   )r,   r   r0   r   )__name__
__module____qualname____doc__ioDEFAULT_BUFFER_SIZEr   r   r   r   r   r'   r.    r   r   r	   r	      s8    'R *,)?)? ,@
,\r   r	   )r5   
__future__r   r6   typingr   r   r   collections.abcr   r	   r8   r   r   <module>r<      s)    L " 	 * *(R Rr   