Internal method used to convert the utf-8 encoded bytestring into unicode.
        If the conversion fails, the socket will be closed.
2890
def _decode_bytes(self, bytestring):
        
        if not bytestring:
            return u''
        try:
            return bytestring.decode('utf-8')
        except UnicodeDecodeError:
            self.close(1007)
            raise
1108228
def _to_unicode(instr):
    '''
    
    '''
    if instr is None or isinstance(instr, six.text_type):
        return instr
    else:
        return six.text_type(instr, 'utf8')
1304980
def force_bytes(s, encoding='utf-8', errors='strict'):
    
    # Handle the common case first for performance reasons.
    if isinstance(s, bytes):
        if encoding == 'utf-8':
            return s
        else:
            return s.decode('utf-8', errors).encode(encoding, errors)
    else:
        return s.encode(encoding, errors)
1166353
def encode_modified_utf8(u: str) -> bytearray:
    
    final_string = bytearray()

    for c in [ord(char) for char in u]:
        if c == 0x00 or (0x80 < c < 0x7FF):
            final_string.extend([
                (0xC0 | (0x1F & (c >> 6))),
                (0x80 | (0x3F & c))]
            )
        elif c < 0x7F:
            final_string.append(c)
        elif 0x800 < c < 0xFFFF:
            final_string.extend([
                (0xE0 | (0x0F & (c >> 12))),
                (0x80 | (0x3F & (c >> 6))),
                (0x80 | (0x3F & c))]
            )

    return final_string
77629
def encode(self, string):
        
        if isinstance(string, unicode):
            string = string.encode("utf-8")
        return quote(string, safe="~")
1027527
def get_unicode(self):
        
        s = mutf8.decode(self.data)
        if len(s) != self.utf16_size:
            raise ValueError("UTF16 Length does not match!")

        # Return a UTF16 String
        return s
933444
def to_bytes(value):
    """"""
    if isinstance(value, unicode):
        return value.encode('utf8')
    elif not isinstance(value, str):
        return str(value)
    return value
423504
public static byte[] fromString(String unicodeString) {

        byte[] result = null;
        if (unicodeString != null) {
            result = unicodeString.getBytes(StandardCharsets.UTF_8);
        }
        return result;
    }
97313
public static ByteBuffer fromUtf8String(CharSequence utfString) {
        if (utfString == null || utfString.length() == 0) {
            return Buffers.EMPTY_BYTE_BUFFER;
        } else {
            return ByteBuffer.wrap(utfString.toString().getBytes(StandardCharsets.UTF_8));
        }
    }
348982
def u2b(string):
    
    if ((PY2 and isinstance(string, unicode)) or
        ((not PY2) and isinstance(string, str))):
        return string.encode('utf-8')
    return string
584144
def bytes_to_str(s, encoding='utf-8'):
    """"""
    if six.PY3 and isinstance(s, bytes):
        return s.decode(encoding)
    return s
679048
def unicodestr(s, encoding='utf-8', fallback='iso-8859-1'):
    """"""

    if isinstance(s, unicode):
        return s

    try:
        return s.decode(encoding)
    except UnicodeError:
        return s.decode(fallback)
612657
def to_unicode(value):
  
  if isinstance(value, str):
    return value.decode('utf-8')

  assert isinstance(value, unicode)
  return value
1259522
def to_str(s):
    
    if isinstance(s, bytes):
        s = s.decode('utf-8')
    elif not isinstance(s, str):
        s = str(s)
    return s
1103707
def _utf8(value):
    
    if isinstance(value, _UTF8_TYPES):
        return value
    if not isinstance(value, unicode_type):
        raise TypeError(
            "Expected bytes, unicode, or None; got %r" % type(value)
        )
    return value.encode("utf-8")
406771
def utf8(value):
    
    """
    try:
        if isinstance(value, bytes):
            return value.decode('utf-8')
        else:
            return value
    except Exception:
        raise ValueError('Not UTF-8: %r' % value)
463467
def _handle_utf8_payload(body, properties):
        
        if 'content_encoding' not in properties:
            properties['content_encoding'] = 'utf-8'
        encoding = properties['content_encoding']
        if compatibility.is_unicode(body):
            body = body.encode(encoding)
        elif compatibility.PYTHON3 and isinstance(body, str):
            body = bytes(body, encoding=encoding)
        return body