示例#1
0
    def read(self, size=-1):
        if size == -1:
            return self.readall()

        chunks = [self.last_chunk]
        total_size = len(self.last_chunk)
        self.last_chunk = b''

        # If we're not closed, read chunks from the iterator until we have
        # enough data.
        if not self._closed:
            try:
                while total_size < size:
                    curr_chunk = advance_iterator(self.iter)
                    chunks.append(curr_chunk)
                    total_size += len(curr_chunk)
            except StopIteration:
                self._closed = True

        # Make return value
        return_val = b''.join(chunks)

        # Trim the returned size, if necessary.
        if len(return_val) > size:
            return_val, self.last_chunk = return_val[:size], return_val[size:]

        # Return it!
        return return_val
示例#2
0
    def read(self, num=-1):
        if self.eof:
            return b''
        if num == -1:
            return self.readall()

        # Try getting our cache first.
        chunks = [self.__cache]
        total_len = len(self.__cache)

        try:
            while total_len < num:
                next_chunk = advance_iterator(self.__iter)
                chunks.append(next_chunk)
                total_len += len(next_chunk)
        except StopIteration:
            self.eof = True

        # Join together.
        bstr = b''.join(chunks)

        # Trim to required length.
        if len(bstr) > num:
            bstr, cache = bstr[:num], bstr[num:]
        else:
            cache = b''

        # Reset cache, return value.
        self.__cache = cache
        return bstr
示例#3
0
    def read(self, size=-1):
        if size == -1:
            return self.readall()

        chunks = [self.last_chunk]
        total_size = len(self.last_chunk)
        self.last_chunk = b''

        # If we're not closed, read chunks from the iterator until we have
        # enough data.
        if not self._closed:
            try:
                while total_size < size:
                    curr_chunk = advance_iterator(self.iter)
                    chunks.append(curr_chunk)
                    total_size += len(curr_chunk)
            except StopIteration:
                self._closed = True

        # Make return value
        return_val = b''.join(chunks)

        # Trim the returned size, if necessary.
        if len(return_val) > size:
            return_val, self.last_chunk = return_val[:size], return_val[size:]

        # Return it!
        return return_val
示例#4
0
文件: util.py 项目: andrew-d/Hoboken
    def read(self, num=-1):
        if self.eof:
            return b''
        if num == -1:
            return self.readall()

        # Try getting our cache first.
        chunks = [self.__cache]
        total_len = len(self.__cache)

        try:
            while total_len < num:
                next_chunk = advance_iterator(self.__iter)
                chunks.append(next_chunk)
                total_len += len(next_chunk)
        except StopIteration:
            self.eof = True

        # Join together.
        bstr = b''.join(chunks)

        # Trim to required length.
        if len(bstr) > num:
            bstr, cache = bstr[:num], bstr[num:]
        else:
            cache = b''

        # Reset cache, return value.
        self.__cache = cache
        return bstr