Get a unicode filename and return bytestring, encoded to file system default.

    If the argument already is a bytestring, return as is
505092
def _encode_filename_to_filesystem(f):
    ''''''
    log.debug('_encode_filename_to_filesystem(%s)', repr(f))
    if isinstance(f, str):
        return f
    try:
        return f.encode(sys.getfilesystemencoding())
    except UnicodeEncodeError:
        raise
1220499
def getBytesForString(self, u):
        
        h = hash(u)
        s = self._unicodes.get(h, None)

        if s is not None:
            return s

        s = self._unicodes[h] = u.encode('utf-8')

        return s
2890
def _decode_bytes(self, bytestring):
        
        if not bytestring:
            return u''
        try:
            return bytestring.decode('utf-8')
        except UnicodeDecodeError:
            self.close(1007)
            raise
335010
def _ostaunicode(src):
    # type: (str) -> bytes
    '''
    
    '''
    if have_py_3:
        bytename = src
    else:
        bytename = src.decode('utf-8')  # type: ignore

    try:
        enc = bytename.encode('latin-1')
        encbyte = b'\x08'
    except (UnicodeEncodeError, UnicodeDecodeError):
        enc = bytename.encode('utf-16_be')
        encbyte = b'\x10'
    return encbyte + enc
1282900
public static String asString(byte[] bytes, Charset charset)
    {
        if (bytes == null)
        {
            return null;
        }

        if (charset == null)
        {
            throw new IllegalArgumentException("Cannot get bytes without a Charset");
        }

        try
        {
            return new String(bytes, charset.name());
        }
        catch (UnsupportedEncodingException e)
        {
            throw new IllegalStateException(charset.name() + " must be present", e);
        }
    }
13350
def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
    
    if isinstance(s, memoryview):
        s = bytes(s)
    if isinstance(s, bytes):
        if encoding == 'utf-8':
            return s
        else:
            return s.decode('utf-8', errors).encode(encoding, errors)
    if strings_only and (s is None or isinstance(s, int)):
        return s
    if not isinstance(s, six.string_types):
        try:
            if six.PY3:
                return six.text_type(s).encode(encoding)
            else:
                return bytes(s)
        except UnicodeEncodeError:
            if isinstance(s, Exception):
                # An Exception subclass containing non-ASCII data that doesn't
                # know how to print itself properly. We shouldn't raise a
                # further exception.
                return b' '.join([force_bytes(arg, encoding, strings_only,
                                              errors) for arg in s])
            return six.text_type(s).encode(encoding, errors)
    else:
        return s.encode(encoding, errors)
1135006
function getFromFileNameV2 (filename) {
	var result = mime.getType(filename);

	// Add charset just in case for some buggy browsers.
	if (result === 'application/javascript' || result === 'application/json' || result === 'text/plain') {
		result += '; charset=UTF-8';
	}

	return result;
}
23530
function (bytes = 0, si = true) {
    if ($bc_.pN) {
      return (si ? $bc_.pN.app.getFileSizeString1000(bytes) : $bc_.pN.app.getFileSizeString1024(bytes))
    }

    return ''
  }
710864
def get_filename(disposition):
    
    if disposition:
        params = [param.strip() for param in disposition.split(';')[1:]]
        for param in params:
            if '=' in param:
                name, value = param.split('=', 1)
                if name == 'filename':
                    return value.strip('"')
1344604
def force_bytes(value):
    
    if IS_PY3:
        if isinstance(value, str):
            value = value.encode('utf-8', 'backslashreplace')
    else:
        if isinstance(value, unicode):  # NOQA: F821
            value = value.encode('utf-8')

    return value
510229
def unicodename(self):
        
        n = self._lib.basename(self.path)
        if self._backend is unicode:
            return n
        else:
            return n.decode(self._encoding, 'replace')
103870
private static String getEncodeFileName(String userAgent, String fileName) {
    String encodedFileName = fileName;
    try {
      if (userAgent.contains("MSIE") || userAgent.contains("Opera")) {
        encodedFileName = URLEncoder.encode(fileName, "UTF-8");
      } else {
        encodedFileName = "=?UTF-8?B?" + new String(BaseEncoding.base64().encode(fileName.getBytes("UTF-8"))) + "?=";
      }
    } catch (Exception e) {
      LOGGER.error(e.getMessage());
    }

    return encodedFileName;
  }
1125317
def unicodify(filename):
    
    if isinstance(filename, bytes):
        return filename.decode(locale.getpreferredencoding())
    else:
        return filename
1085317
def encode_to_py3bytes_or_py2str(s):
    

    fallback_encoding = "utf8"

    if IS_PY3:
        # if we're already bytes, do nothing
        if isinstance(s, bytes):
            pass
        else:
            s = str(s)
            try:
                s = bytes(s, DEFAULT_ENCODING)
            except UnicodeEncodeError:
                s = bytes(s, fallback_encoding)
    else:
        # attempt to convert the thing to unicode from the system's encoding
        try:
            s = unicode(s, DEFAULT_ENCODING)
        # if the thing is already unicode, or it's a number, it can't be
        # coerced to unicode with an encoding argument, but if we leave out
        # the encoding argument, it will convert it to a string, then to unicode
        except TypeError:
            s = unicode(s)

        # now that we have guaranteed unicode, encode to our system encoding,
        # but attempt to fall back to something
        try:
            s = s.encode(DEFAULT_ENCODING)
        except:
            s = s.encode(fallback_encoding, "replace")
    return s
1259522
def to_str(s):
    
    if isinstance(s, bytes):
        s = s.decode('utf-8')
    elif not isinstance(s, str):
        s = str(s)
    return s
365377
def _get_file_name(file_path):
    

    file_name = basename(file_path)

    utf8_file_name = bytearray(file_name, "utf8")
    utf8_file_name.append(0)

    return utf8_file_name
833040
def fs_str(string):
    

    if isinstance(string, str):
        return string
    assert not isinstance(string, bytes)
    return string.encode(_fs_encoding)