RuleSetFactoryCompatibility { String determineEncoding(byte[] bytes) { String firstBytes = new String(bytes, 0, bytes.length > 1024 ? 1024 : bytes.length, StandardCharsets.ISO_8859_1); Matcher matcher = ENCODING_PATTERN.matcher(firstBytes); String encoding = StandardCharsets.UTF_8.name(); if (matcher.find()) { encoding = matcher.group(1); } return encoding; } RuleSetFactoryCompatibility(); Reader filterRuleSetFile(InputStream stream);  }
@Test public void testEncoding() { RuleSetFactoryCompatibility rsfc = new RuleSetFactoryCompatibility(); String testString; testString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><x></x>"; Assert.assertEquals("ISO-8859-1", rsfc.determineEncoding(testString.getBytes(StandardCharsets.ISO_8859_1))); testString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><x></x>"; Assert.assertEquals("UTF-8", rsfc.determineEncoding(testString.getBytes(StandardCharsets.ISO_8859_1))); }