def decode(b):
    """
    Decode bytes as MUTF-8
    See https://docs.oracle.com/javase/6/docs/api/java/io/DataInput.html#modified-utf-8
    for more information

    Surrogates will be returned as two 16 bit characters.

    :param b: bytes to decode
    :rtype: unicode (py2), str (py3) of 16bit chars
    :raises: UnicodeDecodeError if string is not decodable
    """
    res = u''
    b = iter(bytearray(b))
    for x in b:
        if x >> 7 == 0:
            res += chr(x & 127)
        elif x >> 5 == 6:
            b2 = next(b)
            if b2 >> 6 != 2:
                raise UnicodeDecodeError(
                    'Second byte of 2 byte sequence does not looks right.')
            res += chr((x & 31) << 6 | b2 & 63)
        elif x >> 4 == 14:
            b2 = next(b)
            b3 = next(b)
            if b2 >> 6 != 2:
                raise UnicodeDecodeError(
                    'Second byte of 3 byte sequence does not looks right.')
            if b3 >> 6 != 2:
                raise UnicodeDecodeError(
                    'Third byte of 3 byte sequence does not looks right.')
            res += chr((x & 15) << 12 | (b2 & 63) << 6 | b3 & 63)
        else:
            raise UnicodeDecodeError('Could not decode byte')
    return res
