def read(self, size): """Read data from RAW file. Args: size: Number of bytes to read as integer. Actual number of bytes read might be less than specified, but it's never 0 unless current offset is at the end of the file. Returns: A string with data read. """ self._verify_read_mode() if self._content_type != RAW: raise UnsupportedContentTypeError('Unsupported content type: %s' % self._content_type) request = file_service_pb.ReadRequest() response = file_service_pb.ReadResponse() request.set_filename(self._filename) request.set_pos(self._offset) request.set_max_bytes(size) self._make_rpc_call_with_retry('Read', request, response) result = response.data() self._offset += len(result) return result
def read(self, size=None): """Read data from RAW file. Args: size: Number of bytes to read as integer. Actual number of bytes read might be less than specified, but it's never 0 unless current offset is at the end of the file. If it is None, then file is read until the end. Returns: A string with data read. """ self._verify_read_mode() if self._content_type != RAW: raise UnsupportedContentTypeError('Unsupported content type: %s' % self._content_type) buf = StringIO.StringIO() original_offset = self._offset try: if size is None: size = sys.maxint while size > 0: request = file_service_pb.ReadRequest() response = file_service_pb.ReadResponse() request.set_filename(self._filename) request.set_pos(self._offset) request.set_max_bytes(min(READ_BLOCK_SIZE, size)) self._make_rpc_call_with_retry('Read', request, response) chunk = response.data() self._offset += len(chunk) if len(chunk) == 0: break buf.write(chunk) size -= len(chunk) return buf.getvalue() except: self._offset = original_offset raise finally: buf.close()