Charset (@NotNull CharSequence content) { // check for <meta http-equiv="charset=CharsetName" > or <meta charset="CharsetName"> and return Charset // because we will lightly parse and explicit charset isn't used very often do quick check for applicability int charPrefix = StringUtil.indexOf(content, CHARSET); do { if (charPrefix == -1) return null; int charsetPrefixEnd = charPrefix + CHARSET.length(); while (charsetPrefixEnd < content.length() && Character.isWhitespace(content.charAt(charsetPrefixEnd))) ++charsetPrefixEnd; if (charsetPrefixEnd < content.length() && content.charAt(charsetPrefixEnd) == '=') break; charPrefix = StringUtil.indexOf(content, CHARSET, charsetPrefixEnd); } while (true); if (content.length() > charPrefix + 200) { String name = tryFetchCharsetFromFileContent(content.subSequence(0, charPrefix + 200)); if (name != null) { return CharsetToolkit.forName(name); } } String name = tryFetchCharsetFromFileContent(content); return CharsetToolkit.forName(name); }