### Question:
PropertiesUtil { public static Charset detectEncoding(InputStream in) throws IOException { try (BufferedReader br = new BufferedReader(new InputStreamReader(in, DEFAULT_ENCODING))) { String firstLine = br.readLine(); if (firstLine != null) { Matcher matcher = DETECT_ENCODING_PATTERN.matcher(firstLine); if (matcher.find()) { String encoding = matcher.group(1); if (Charset.isSupported(encoding)) { return Charset.forName(encoding); } else { logger.warnv("Unsupported encoding: {0}", encoding); } } } } return DEFAULT_ENCODING; }  static Charset detectEncoding(InputStream in); static final Pattern DETECT_ENCODING_PATTERN; static final Charset DEFAULT_ENCODING; }### Answer:
@Test public void testDetectEncoding() throws Exception { Charset encoding = PropertiesUtil.detectEncoding(new ByteArrayInputStream("# encoding: utf-8\nkey=value".getBytes())); assertEquals(Charset.forName("utf-8"), encoding); encoding = PropertiesUtil.detectEncoding(new ByteArrayInputStream("# encoding: Shift_JIS\nkey=value".getBytes())); assertEquals(Charset.forName("Shift_JIS"), encoding); }
@Test public void testDefaultEncoding() throws Exception { Charset encoding = PropertiesUtil.detectEncoding(new ByteArrayInputStream("key=value".getBytes())); assertEquals(Charset.forName("ISO-8859-1"), encoding); encoding = PropertiesUtil.detectEncoding(new ByteArrayInputStream("# encoding: unknown\nkey=value".getBytes())); assertEquals(Charset.forName("ISO-8859-1"), encoding); encoding = PropertiesUtil.detectEncoding(new ByteArrayInputStream("\n# encoding: utf-8\nkey=value".getBytes())); assertEquals(Charset.forName("ISO-8859-1"), encoding); encoding = PropertiesUtil.detectEncoding(new ByteArrayInputStream("".getBytes())); assertEquals(Charset.forName("ISO-8859-1"), encoding); }