
                         j    d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ  G d dej                        Z	y)	z.Utilities for representing a part of a stream.    )absolute_import)division)unicode_literalsN)upload_streamc                   `     e Zd ZdZ	 	 d fd	Z fdZd fd	Zej                  f fd	Z	 xZ
S )ComponentStreamaN  Implements a subset of the io.IOBase API exposing part of a stream.

  This class behaves as a contiguous subset of the underlying stream.

  This is helpful for composite uploads since even when total_size is specified,
  apitools.transfer.Upload looks at the size of the source file to ensure
  that all bytes have been uploaded.
  c                     t         |   ||||       || _        | j                  | j                  z   | _        | j
                  j                  | j                         y)a  Initializes a ComponentStream instance.

    Args:
      stream (io.IOBase): See super class.
      offset (int|None): The position (in bytes) in the wrapped stream that
        corresponds to the first byte of the ComponentStream.
      length (int|None): The total number of bytes readable from the
        ComponentStream.
      digesters (dict[util.HashAlgorithm, hashlib hash object]|None): See super
        class.
      progress_callback (func[int]|None): See super class.
    )streamlength	digestersprogress_callbackN)super__init___start_byte_length	_end_byte_streamseek)selfr
   offsetr   r   r   	__class__s         :lib/googlecloudsdk/command_lib/storage/component_stream.pyr   zComponentStream.__init__$   s_    $ 
)T#+	 $ - D%%4DN 	LLd&&'    c                 :    t         |          | j                  z
  S )z?Returns the current position relative to the part's start byte.)r   tellr   )r   r   s    r   r   zComponentStream.tellB   s    D&(4+;+;;;r   c                     |dk  r| j                   }t        || j                  t        |          z
        }t        |   t        d|            S )z?Reads `size` bytes from a stream, or all bytes when `size` < 0.r   )r   minr   r   r   readmax)r   sizer   s     r   r   zComponentStream.readF   sJ    ax\\dtT^^eIt&A&CCDDD&s1d|44r   c                     |t         j                  k(  r|| j                  z   }n4|t         j                  k(  rt        |          |z   }n|| j                  z   }t        |   |      | j                  z
  S )a  Goes to a specific point in the stream.

    Args:
      offset (int): The number of bytes to move.
      whence: Specifies the position offset is added to.
        os.SEEK_END: offset is added to the last byte in the FilePart.
        os.SEEK_CUR: offset is added to the current position.
        os.SEEK_SET: offset is added to the first byte in the FilePart.

    Returns:
      The new relative position in the stream (int).
    )osSEEK_ENDr   SEEK_CURr   r   r   r   )r   r   whencenew_absolute_indexr   s       r   r   zComponentStream.seekM   sp     !DNN2	2;;	 D686A!D$4$44D&'9:T=M=MMMr   )NN))__name__
__module____qualname____doc__r   r   r   r"   SEEK_SETr   __classcell__)r   s   @r   r   r      s4     !%(<<5 !# N Nr   r   )
r+   
__future__r   r   r   r"   "googlecloudsdk.command_lib.storager   UploadStreamr    r   r   <module>r2      s/    5 &  ' 	 <GNm00 GNr   