'Helper function for proxing reads to the underlying stream.
Args:
size (int): Maximum number of bytes to read. Will be
coerced, if None or -1, to the number of remaining bytes
in the stream. Will likewise be coerced if greater than
the number of remaining bytes, to avoid making a
blocking call to the wrapped stream.
target (callable): Once `size` has been fixed up, this function
will be called to actually do the work.
Returns:
bytes: Data read from the stream, as returned by `target`.'

def _read(self, size, target):

    if ((size is None) or (size == (-1)) or (size > self._bytes_remaining)):
        size = self._bytes_remaining
    self._bytes_remaining -= size
    return target(size)
