private static String findRealEncoding(String presetEncoding, URLConnection connection) {
        String realEncoding = connection.getContentEncoding();
        if (realEncoding != null)
            return realEncoding;

        String type = connection.getContentType();
        if (type != null) {
            final String CHARSET_ATTRIBUTE = "charset=";
            int pos = type.indexOf(CHARSET_ATTRIBUTE);
            if (pos != NOT_FOUND)
                return type.substring(pos + CHARSET_ATTRIBUTE.length() );
        }

        return presetEncoding;
    }