FileUtils { public static Encoding detectEncoding(final Path file, FileSystem fileSystem, Encoding fallback, Reference<InputStream> gnawedInputStreamReference) throws IOException { final InputStream fileInputStream = open(file, fileSystem); return IoUtils.detectEncoding(fileInputStream, gnawedInputStreamReference).orElseGet(() -> { LOGGER.warn("Using fallback encoding for {}.", file); return fallback; }); } private  FileUtils(); static List<Path> gatherFiles(final int depth,
                                         final String... inputPaths); static Path[] createPaths(final String... pathStrings); static List<Path> gatherFiles(final int depth,
                                         final Path... inputPaths); static Path ensureEmptyDirectory(final Path parentPath,
                                            final String name, final FileSystem fs); static void ensureEmptyDirectory(final Path path, FileSystem fs); static FileSystem ensureFileSystem(final Path path,
                                              final FileSystem fs); static boolean remove(final Path file, final boolean recursive); static InputStream open(final Path file, FileSystem fileSystem); static InflaterInputStreamFactory<?> findInflaterInputStreamFactory(Path file); static Path findCommonParentPath(Path path1, Path path2); static String findCommonParent(Collection<String> filePaths); static String extractExtension(final Path file); static Encoding detectEncoding(final Path file, FileSystem fileSystem, Encoding fallback,
                                          Reference<InputStream> gnawedInputStreamReference);  }
@Test public void testCharsetDetection() throws URISyntaxException, IOException { Collection<Tuple3<String, Encoding.ByteOrderMark, String>> testEntries = new LinkedList<>(); testEntries.add(new Tuple3<>("/test.utf8-bom.csv", Encoding.ByteOrderMark.UTF_8, "UTF-8")); testEntries.add(new Tuple3<>("/test.utf8.csv", Encoding.ByteOrderMark.NONE, "UTF-8")); testEntries.add(new Tuple3<>("/test.utf16le-bom.csv", Encoding.ByteOrderMark.UTF_16_LE, "UTF-16LE")); testEntries.add(new Tuple3<>("/test.utf16be-bom.csv", Encoding.ByteOrderMark.UTF_16_BE, "UTF-16BE")); testEntries.add(new Tuple3<>("/test.ucs2le.csv", Encoding.ByteOrderMark.UTF_16_LE, "UTF-16LE")); testEntries.add(new Tuple3<>("/test.ucs2be.csv", Encoding.ByteOrderMark.UTF_16_BE, "UTF-16BE")); testEntries.add(new Tuple3<>("/test.ansi.csv", Encoding.ByteOrderMark.NONE, "UTF-8")); testEntries.add(new Tuple3<>("/empty.csv", Encoding.DEFAULT_ENCODING.getByteOrderMark(), Encoding.DEFAULT_ENCODING.getCharset().toString())); for (Tuple3<String, Encoding.ByteOrderMark, String> testEntry : testEntries) { Path path = new Path(getClass().getResource(testEntry.f0).toURI().toString()); Encoding encoding = FileUtils.detectEncoding(path, null, Encoding.DEFAULT_ENCODING, null); Assert.assertEquals(String.format("in %s", testEntry.f0), testEntry.f1 == null ? null : new Encoding(testEntry.f1, testEntry.f2), encoding); } }