B
    ]b                 @  s*   d dl mZ d dlmZ G dd dZdS )    )annotations)	Generatorc               @  s   e Zd ZdZddddZdddd	d
ZdddddZdddddZddddZdddddZ	ddddZ
ddddZdS )StreamReaderz
    Generator-based stream reader.

    This class doesn't support concurrent calls to :meth:`read_line`,
    :meth:`read_exact`, or :meth:`read_to_eof`. Make sure calls are
    serialized.

    None)returnc             C  s   t  | _d| _d S )NF)	bytearraybuffereof)self r   1lib/python3.7/site-packages/websockets/streams.py__init__   s    zStreamReader.__init__intzGenerator[None, None, bytes])mr   c             c  s   d}d}xd| j d|d }|dkr&P t| j }||krNtd| d| d| jrdtd| dd	V  q
W ||krtd| d| d| j d	| }| j d	|= |S )
a  
        Read a LF-terminated line from the stream.

        This is a generator-based coroutine.

        The return value includes the LF character.

        Args:
            m: maximum number bytes to read; this is a security limit.

        Raises:
            EOFError: if the stream ends without a LF.
            RuntimeError: if the stream ends in more than ``m`` bytes.

        r      
   zread z bytes, expected no more than z byteszstream ends after z bytes, before end of lineN)r   findlenRuntimeErrorr	   EOFError)r
   r   nprr   r   r   	read_line   s"    

zStreamReader.read_line)r   r   c             c  sj   |dkst x>t| j|k rJ| jrBt| j}td| d| ddV  qW | jd| }| jd|= |S )z
        Read a given number of bytes from the stream.

        This is a generator-based coroutine.

        Args:
            n: how many bytes to read.

        Raises:
            EOFError: if the stream ends in less than ``n`` bytes.

        r   zstream ends after z bytes, expected z bytesN)AssertionErrorr   r   r	   r   )r
   r   r   r   r   r   r   
read_exact6   s    

zStreamReader.read_exactc             c  sX   x8| j s8t| j}||kr0td| d| ddV  qW | jdd }| jdd= |S )a  
        Read all bytes from the stream.

        This is a generator-based coroutine.

        Args:
            m: maximum number bytes to read; this is a security limit.

        Raises:
            RuntimeError: if the stream ends in more than ``m`` bytes.

        zread z bytes, expected no more than z bytesN)r	   r   r   r   )r
   r   r   r   r   r   r   read_to_eofM   s    

zStreamReader.read_to_eofzGenerator[None, None, bool]c             c  s$   x| j rdS | jrdS dV  qW dS )zy
        Tell whether the stream has ended and all data was read.

        This is a generator-based coroutine.

        FTN)r   r	   )r
   r   r   r   at_eofc   s    zStreamReader.at_eofbytes)datar   c             C  s    | j rtd|  j|7  _dS )z
        Write data to the stream.

        :meth:`feed_data` cannot be called after :meth:`feed_eof`.

        Args:
            data: data to write.

        Raises:
            EOFError: if the stream has ended.

        zstream endedN)r	   r   r   )r
   r   r   r   r   	feed_datas   s    zStreamReader.feed_datac             C  s   | j rtdd| _ dS )z
        End the stream.

        :meth:`feed_eof` cannot be called more than once.

        Raises:
            EOFError: if the stream has ended.

        zstream endedTN)r	   r   )r
   r   r   r   feed_eof   s    
zStreamReader.feed_eofc             C  s   | j dd= dS )zG
        Discard all buffered data, but don't end the stream.

        N)r   )r
   r   r   r   discard   s    zStreamReader.discardN)__name__
__module____qualname____doc__r   r   r   r   r   r    r!   r"   r   r   r   r   r      s   "r   N)Z
__future__r   typingr   r   r   r   r   r   <module>   s   