'Decodes bytes with the given encoding and, if successful,
sets `self.text` with the decoded result and returns True.
Otherwise, returns False.
Side-effects: On success, sets `self.text` and `self.encoding`.
Optimization: First an attempt is made to decode
`self.head_bytes` instead of all of `self.bytes`. This allows
for the normal usage in `TextInfo._classify_encoding()` to *not*
bother fully reading binary files that could not be decoded.
Optimization: Decoding attempts are cached to not bother
attempting a failed decode twice.'

def decode(self, encoding):

    if (self._unsuccessful_encodings is None):
        self._unsuccessful_encodings = set()
    if (encoding in self._unsuccessful_encodings):
        return False
    elif (encoding == self.encoding):
        return True
    head_bytes = self.head_bytes
    try:
        head_bytes.decode(encoding, 'strict')
    except LookupError as ex:
        log.debug('encoding    lookup    error:    %r', encoding)
        self._unsuccessful_encodings.add(encoding)
        return False
    except UnicodeError as ex:
        if (ex.start >= (self.HEAD_SIZE - 5)):
            pass
        else:
            self._unsuccessful_encodings.add(encoding)
            return False
    try:
        self.text = self.bytes.decode(encoding, 'strict')
    except UnicodeError as ex:
        self._unsuccessful_encodings.add(encoding)
        return False
    self.encoding = encoding
    return True
