U
    nËj”  ã                   @  s~   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z	 ddl
mZmZmZmZ ddlmZmZ edd	�G d
d„ deƒƒZdS )é    )Úannotations)ÚCallableÚMapping)Ú	dataclassÚfield)ÚAnyé   )ÚClosedResourceErrorÚDelimiterNotFoundÚEndOfStreamÚIncompleteRead)ÚAnyByteReceiveStreamÚByteReceiveStreamF)Úeqc                   @  s¨   e Zd ZU dZded< eded�Zded< eddd�Zd	ed
< ddœdd„Z	e
ddœdd„ƒZe
ddœdd„ƒZd!dddœdd„Zdddœdd„Zddddœdd„Zd S )"ÚBufferedByteReceiveStreamz’
    Wraps any bytes-based receive stream and uses a buffer to provide sophisticated
    receiving capabilities in the form of a byte stream.
    r   Úreceive_streamF)ÚinitÚdefault_factoryÚ	bytearrayÚ_buffer)r   ÚdefaultÚboolÚ_closedÚNone)Úreturnc                 Ã  s   | j  ¡ I d H  d| _d S )NT)r   Úacloser   ©Úself© r   ú:/tmp/pip-unpacked-wheel-k8mofgf9/anyio/streams/buffered.pyr      s    z BufferedByteReceiveStream.acloseÚbytesc                 C  s
   t | jƒS )z"The bytes currently in the buffer.)r    r   r   r   r   r   Úbuffer   s    z BufferedByteReceiveStream.bufferzMapping[Any, Callable[[], Any]]c                 C  s   | j jS ©N)r   Úextra_attributesr   r   r   r   r#      s    z*BufferedByteReceiveStream.extra_attributesé   Úint)Ú	max_bytesr   c                 Ã  s”   | j r
t‚| jr2t| jd |… ƒ}| jd |…= |S t| jtƒrP| j |¡I d H S | j ¡ I d H }t|ƒ|krŒ| j 	||d … ¡ |d |… S |S d S r"   )
r   r	   r   r    Ú
isinstancer   r   ÚreceiveÚlenÚextend)r   r&   Úchunkr   r   r   r(   #   s    z!BufferedByteReceiveStream.receive)Únbytesr   c              
   Ã  s¨   |t | jƒ }|dkr8| jd|… }| jd|…= t|ƒS z4t| jtƒrZ| j |¡I dH }n| j ¡ I dH }W n( tk
r” } z
t|‚W 5 d}~X Y nX | j 	|¡ q dS )a-  
        Read exactly the given amount of bytes from the stream.

        :param nbytes: the number of bytes to read
        :return: the bytes read
        :raises ~anyio.IncompleteRead: if the stream was closed before the requested
            amount of bytes could be read from the stream

        r   N)
r)   r   r    r'   r   r   r(   r   r   r*   )r   r,   Ú	remainingÚretvalr+   Úexcr   r   r   Úreceive_exactly8   s    z)BufferedByteReceiveStream.receive_exactly)Ú	delimiterr&   r   c           	   
   Ã  sÊ   t |ƒ}d}| j ||¡}|dkrL| jd|… }| jd|t |ƒ …= t|ƒS t | jƒ|krbt|ƒ‚z| j ¡ I dH }W n( tk
rž } z
t|‚W 5 d}~X Y nX t	t | jƒ| d dƒ}| j 
|¡ qdS )aM  
        Read from the stream until the delimiter is found or max_bytes have been read.

        :param delimiter: the marker to look for in the stream
        :param max_bytes: maximum number of bytes that will be read before raising
            :exc:`~anyio.DelimiterNotFound`
        :return: the bytes read (not including the delimiter)
        :raises ~anyio.IncompleteRead: if the stream was closed before the delimiter
            was found
        :raises ~anyio.DelimiterNotFound: if the delimiter is not found within the
            bytes read up to the maximum allowed

        r   Né   )r)   r   Úfindr    r
   r   r(   r   r   Úmaxr*   )	r   r1   r&   Zdelimiter_sizeÚoffsetÚindexÚfoundÚdatar/   r   r   r   Úreceive_untilS   s    z'BufferedByteReceiveStream.receive_untilN)r$   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__r   r   r   r   r   Úpropertyr!   r#   r(   r0   r9   r   r   r   r   r      s   
r   N)Ú
__future__r   Úcollections.abcr   r   Zdataclassesr   r   Útypingr   Ú r	   r
   r   r   Úabcr   r   r   r   r   r   r   Ú<module>   s   