U
    ddU                     @   sj   d dl mZmZ d dlmZmZ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 )    )	dataclassfield)AnyCallableMapping   )ClosedResourceErrorDelimiterNotFoundEndOfStreamIncompleteRead)AnyByteReceiveStreamByteReceiveStreamF)eqc                   @   s   e Zd ZU dZeed< ededZeed< edddZ	e
ed< dd	d
dZeed	ddZeeeeg ef f d	ddZdeedddZeedddZeee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.
    receive_streamF)initdefault_factory_buffer)r   default_closedN)returnc                    s   | j  I d H  d| _d S )NT)r   acloser   self r   :/tmp/pip-unpacked-wheel-yaxr6kle/anyio/streams/buffered.pyr      s    z BufferedByteReceiveStream.aclosec                 C   s
   t | jS )z"The bytes currently in the buffer.)bytesr   r   r   r   r   buffer   s    z BufferedByteReceiveStream.bufferc                 C   s   | j jS N)r   extra_attributesr   r   r   r   r      s    z*BufferedByteReceiveStream.extra_attributes   )	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_exactly5   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   r,   r!   Zdelimiter_sizeoffsetindexfounddatar*   r   r   r   receive_untilP   s    z'BufferedByteReceiveStream.receive_until)r    )__name__
__module____qualname____doc__r   __annotations__r   	bytearrayr   r   boolr   propertyr   r   r   r   r   r   intr#   r+   r4   r   r   r   r   r      s   
 r   N)Zdataclassesr   r   typingr   r   r    r   r	   r
   r   abcr   r   r   r   r   r   r   <module>   s
   