File: //opt/alt/python37/lib/python3.7/site-packages/pyasn1/codec/__pycache__/streaming.cpython-37.pyc
B
��f � @ st d dl Z d dlZd dlZd dlmZ d dlmZ ejdk ZG dd� de j �Z
dd� Zd d
� Zddd
�Z
ddd�ZdS )� N)�error)�univ)� c @ sd e Zd ZdZdd� Zdd� Zdd� Zdejfd d
�Z ddd�Z
ed
d� �Zej
dd� �Zdd� ZdS )�CachingStreamWrappera Wrapper around non-seekable streams.
Note that the implementation is tied to the decoder,
not checking for dangerous arguments for the sake
of performance.
The read bytes are kept in an internal cache until
setting _markedPosition which may reset the cache.
c C s || _ t�� | _d| _d S )Nr )�_raw�io�BytesIO�_cache�_markedPosition)�self�raw� r
�G/opt/alt/python37/lib/python3.7/site-packages/pyasn1/codec/streaming.py�__init__ s
zCachingStreamWrapper.__init__c C s$ | � |�}| j�t|� tj� |S )N)�readr �seek�len�os�SEEK_CUR)r �n�resultr
r
r �peek s
zCachingStreamWrapper.peekc C s dS )NTr
)r r
r
r �seekable% s zCachingStreamWrapper.seekable���c C s | j �||�S )N)r r )r r �whencer
r
r r ( s zCachingStreamWrapper.seekc C sH | j �|�}|dkr(|t|�8 }|s(|S | j�|�}| j �|� || S )Nr )r r r r �write)r r Zread_from_cacheZ
read_from_rawr
r
r r , s zCachingStreamWrapper.readc C s | j S )a Position where the currently processed element starts.
This is used for back-tracking in SingleItemDecoder.__call__
and (indefLen)ValueDecoder and should not be used for other purposes.
The client is not supposed to ever seek before this position.
)r
)r r
r
r �markedPosition9 s z#CachingStreamWrapper.markedPositionc C s2 || _ | j�� tjkr.t�| j�� �| _d| _ d S )Nr )r
r �tellr �DEFAULT_BUFFER_SIZEr r )r �valuer
r
r r C s c C s
| j �� S )N)r r )r r
r
r r Q s zCachingStreamWrapper.tellN)r )�__name__�
__module__�__qualname__�__doc__r r r r �SEEK_SETr r �propertyr �setterr r
r
r
r r s
r c C s� t | tj�r| S t | t�r$t�| �S t | tj�r>t�| �� �S y0trXt | t�rXt� | �S | �
� rd| S t| �S W n* tk
r� t
�d| jj d ��Y nX dS )ai Convert object to seekable byte-stream.
Parameters
----------
substrate: :py:class:`bytes` or :py:class:`io.IOBase` or :py:class:`univ.OctetString`
Returns
-------
: :py:class:`io.IOBase`
Raises
------
: :py:class:`~pyasn1.error.PyAsn1Error`
If the supplied substrate cannot be converted to a seekable stream.
zCannot convert z to a seekable bit stream.N)�
isinstancer r �bytesr ZOctetStringZasOctets�_PY2�file�BufferedReaderr r �AttributeErrorr ZUnsupportedSubstrateError� __class__r )� substrater
r
r �asSeekableStreamU s
r/ c c sz t | tj�rD| �� }| �dtj� | �� |k}| �|tj� |V n2| �d�}|dkr\dV |rn| �dtj � | V dS )a Check whether we have reached the end of a stream.
Although it is more effective to read and catch exceptions, this
function
Parameters
----------
substrate: :py:class:`IOBase`
Stream to check
Returns
-------
: :py:class:`bool`
r � Nr )
r'