Read UTF8 string.
324665
def readLongString(self):
        
        l = self.stream.read_ulong()

        bytes = self.stream.read(l)

        return self.context.getStringForBytes(bytes)
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)
779831
def _set_file_encoding_utf8(filename):
    """"""
    with open(filename, 'r+') as f:
        content = f.read()
        f.seek(0, 0)
        f.write("# -*- coding: utf-8 -*-\n" + content)
1233392
def utf8(s):
    
    if isinstance(s, mitogen.core.UnicodeType):
        s = s.encode('utf-8')
    return s
574299
def _decode8(self, offset):
        
        # UTF-8 Strings contain two lengths, as they might differ:
        # 1) the UTF-16 length
        str_len, skip = self._decode_length(offset, 1)
        offset += skip

        # 2) the utf-8 string length
        encoded_bytes, skip = self._decode_length(offset, 1)
        offset += skip

        data = self.m_charbuff[offset: offset + encoded_bytes]

        assert self.m_charbuff[offset + encoded_bytes] == 0, \
            "UTF-8 String is not null terminated! At offset={}".format(offset)

        return self._decode_bytes(data, 'utf-8', str_len)
62162
def read(path, encoding="utf-8"):
    
    try:
        with io.open(path, encoding=encoding) as f:
            return f.read()
    except Exception as e:
        logger.error("read: %s failed. Error: %s", path, e)
        return ""
812395
def Read(self):
    
    encoded_string = self._file_object.readline()

    if isinstance(encoded_string, py2to3.UNICODE_TYPE):
      return encoded_string

    try:
      string = codecs.decode(encoded_string, self._encoding, self._errors)
    except UnicodeDecodeError:
      if self._errors == 'strict':
        logger.error(
            'Unable to properly read input due to encoding error. '
            'Switching to error tolerant encoding which can result in '
            'non Basic Latin (C0) characters to be replaced with "?" or '
            '"\\ufffd".')
        self._errors = 'replace'

      string = codecs.decode(encoded_string, self._encoding, self._errors)

    return string
130202
def readString(self):
        
        resultStr = ""
        while self.data[self.offset] != "\x00":
            resultStr += self.data[self.offset]
            self.offset += 1
        return resultStr
654671
def universal_read(fname):
    ''''''
    with open(fname,'rU') as f:
        data = f.read()
    enc_guess = chardet.detect(data)
    return data.decode(enc_guess['encoding'])
1262963
def _get_string(data, position, obj_end, dummy):
    """"""
    length = _UNPACK_INT(data[position:position + 4])[0]
    position += 4
    if length < 1 or obj_end - position < length:
        raise InvalidBSON("invalid string length")
    end = position + length - 1
    if data[end:end + 1] != b"\x00":
        raise InvalidBSON("invalid end of string")
    return _utf_8_decode(data[position:end], None, True)[0], end + 1
701517
private static int utf8Length(String string) {
    int stringLength = string.length();
    int utf8Length = 0;
    for (int i = 0; i < stringLength; i++) {
      int c = string.charAt(i);
      if (c <= 0x007F) {
        utf8Length++;
      } else if (c > 0x07FF) {
        utf8Length += 3;
      } else {
        utf8Length += 2;
      }
    }
    return utf8Length;
  }
1307783
def unicode2str(content):
    """"""
    if isinstance(content, dict):
        result = {}
        for key in content.keys():
            result[unicode2str(key)] = unicode2str(content[key])
        return result
    elif isinstance(content, list):
        return [unicode2str(element) for element in content]
    elif isinstance(content, int) or isinstance(content, float):
        return content
    else:
        return content.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)
487645
def read_shortstr(self):
        
        self.bitcount = self.bits = 0
        slen = unpack('B', self.input.read(1))[0]
        return self.input.read(slen).decode('utf-8')
90582
def read_text_file(filename, encoding="utf-8"):
    
    try:
        with open(filename, 'r', encoding) as f:
            r = f.read()
    except TypeError:
        with open(filename, 'r') as f:
            r = f.read()
    return r
1233823
def _get_unicode(data, force=False):
    """"""
    if isinstance(data, binary_type):
        return data.decode('utf-8')
    elif data is None:
        return ''
    elif force:
        if PY2:
            return unicode(data)
        else:
            return str(data)
    else:
        return data
1303453
def read(self):
        """"""
        lines = self.readlines()
        if self.metadata and 'encoding' in self.metadata:
            encoding = self.metadata['encoding']
        else:
            encoding = 'utf-8'
        if sys.version < '3':
            return "\n".join( unicode(line, 'utf-8') if isinstance(line, str) else line for line in lines)
        else:
            return "\n".join( str(line, 'utf-8') if isinstance(line, bytes) else line for line in lines)