| OLD | NEW |
| (Empty) | |
| 1 """Small helper class to provide a small slice of a stream.""" |
| 2 |
| 3 from gslib.third_party.storage_apitools import exceptions |
| 4 |
| 5 class StreamSlice(object): |
| 6 |
| 7 def __init__(self, stream, max_bytes): |
| 8 self.__stream = stream |
| 9 self.__remaining_bytes = max_bytes |
| 10 self.__max_bytes = max_bytes |
| 11 |
| 12 def __str__(self): |
| 13 return 'Slice of stream %s with %s/%s bytes not yet read' % ( |
| 14 self.__stream, self.__remaining_bytes, self.__max_bytes) |
| 15 |
| 16 def __len__(self): |
| 17 return self.__max_bytes |
| 18 |
| 19 def read(self, size=None): |
| 20 if size is not None: |
| 21 size = min(size, self.__remaining_bytes) |
| 22 else: |
| 23 size = self.__remaining_bytes |
| 24 data = self.__stream.read(size) |
| 25 if not data and self.__remaining_bytes: |
| 26 raise exceptions.TransferInvalidError( |
| 27 'Not enough bytes in stream; expected %d, stream exhasted after %d' % ( |
| 28 self.__max_bytes, self.__remaining_bytes)) |
| 29 self.__remaining_bytes -= len(data) |
| 30 return data |
| OLD | NEW |