
    lcS                     .    d Z ddlZ G d de          ZdS )zEImplements ByteBuffer class for amortizing network transfer overhead.    Nc                   P    e Zd ZdZej        fdZd ZddZddZ	d Z
ddZd	 Zd
S )
ByteBuffera  Implements a 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 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).

    Example
    -------

    Note that while this example works in both Python 2 and 3, the doctest only
    passes in Python 3 due to the bytestring literals in the expected values.

    >>> 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                 <    || _         |                                  dS )aJ  Create a ByteBuffer instance that reads chunk_size bytes when filled.
        Note that the buffer has no maximum size.

        Parameters
        -----------
        chunk_size: int, optional
            The the number of bytes that will be read from the supplied reader
            or iterable when filling the buffer.
        N)_chunk_sizeempty)self
chunk_sizes     5lib/python3.11/site-packages/smart_open/bytebuffer.py__init__zByteBuffer.__init__8   s     &

    c                 :    t          | j                  | j        z
  S )z9Return the number of unread bytes in the buffer as an intlen_bytes_posr   s    r
   __len__zByteBuffer.__len__E   s    4;$)++r   c                 j    |                      |          }| xj        t          |          z  c_        |S )aZ  Read bytes from the buffer and advance the read position. Returns
        the bytes in a bytestring.

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

        Returns
        -------
        bytes
        )peekr   r   r   sizeparts      r
   readzByteBuffer.readI   s.     yy		SYY		r   c                     |dk     s|t          |           k    rt          |           }| j        | j        | j        |z            }|S )ac  Get bytes from the buffer without advancing the read position.
        Returns the bytes in a bytestring.

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

        Returns
        -------
        bytes
        r   r   r   s      r
   r   zByteBuffer.peek[   sK     !8 	tc$ii' 	t99D{49TYt^34r   c                 "    d| _         d| _        dS )z Remove all bytes from the bufferr   r   N)r   r   r   s    r
   r   zByteBuffer.emptyo   s    			r   c                 l   |dk    r|n| j         }t          || j                   }| j        dk    r | j        | j        d         | _        d| _        t	          |d          r|                    |          }n!d}|D ]}||z  }t          |          |k    r n| xj        |z  c_        t          |          S )a=  Fill the buffer with bytes 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;
        Returns the number of new bytes added to the buffer.
        Note: all previously-read bytes in the buffer are removed.

        Parameters
        ----------
        source: a file-like object, or iterable/list that contains bytes
            The source of bytes to fill the buffer with. 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: int, optional
            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
        -------
        int, the number of new bytes added to the buffer.
        r   Nr   r   )r   minr   r   hasattrr   r   )r   sourcer   	new_bytes
more_bytess        r
   fillzByteBuffer.fillt   s    8 qy6ttd&64)**9> 	+dijj1DKDI66"" 	D))III$  
Z'	y>>T) E 	y 9~~r   c                     | j                             || j                  }|dk    rt          |           }n|| j        z
  dz   }|                     |          S )a#  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

        :param byte terminator: The line terminator character.
        :rtype: bytes

        r      )r   findr   r   r   )r   
terminatorindexr   s       r
   readlinezByteBuffer.readline   sW       TY77B; 	)t99DD49$q(Dyyr   N)r   )__name__
__module____qualname____doc__ioDEFAULT_BUFFER_SIZEr   r   r   r   r   r#   r)    r   r
   r   r      s        ( (T #%"8    , , ,   $   (  
- - - -^    r   r   )r-   r.   objectr   r0   r   r
   <module>r2      sU    L K 				g g g g g g g g g gr   