FileUtil { public static FileSystem write(final FileSystem fs, final Path path, final byte[] bytes) throws IOException { Objects.requireNonNull(path); Objects.requireNonNull(bytes); try (FSDataOutputStream out = fs.createFile(path).overwrite(true).build()) { out.write(bytes); } return fs; }  static Path[] stat2Paths(FileStatus[] stats); static Path[] stat2Paths(FileStatus[] stats, Path path); static void fullyDeleteOnExit(final File file); static boolean fullyDelete(final File dir); static boolean fullyDelete(final File dir, boolean tryGrantPermissions); static String readLink(File f); static boolean fullyDeleteContents(final File dir); static boolean fullyDeleteContents(final File dir, final boolean tryGrantPermissions); @Deprecated static void fullyDelete(FileSystem fs, Path dir); static boolean copy(FileSystem srcFS, Path src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             Configuration conf); static boolean copy(FileSystem srcFS, Path[] srcs,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite, Configuration conf); static boolean copy(FileSystem srcFS, Path src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite,
                             Configuration conf); static boolean copy(FileSystem srcFS, FileStatus srcStatus,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             boolean overwrite,
                             Configuration conf); static boolean copy(File src,
                             FileSystem dstFS, Path dst,
                             boolean deleteSource,
                             Configuration conf); static boolean copy(FileSystem srcFS, Path src,
                             File dst, boolean deleteSource,
                             Configuration conf); static String makeShellPath(String filename); static String makeShellPath(File file); static String makeSecureShellPath(File file); static String makeShellPath(File file, boolean makeCanonicalPath); static long getDU(File dir); static void unZip(InputStream inputStream, File toDir); static void unZip(File inFile, File unzipDir); static void unTar(InputStream inputStream, File untarDir,
                           boolean gzipped); static void unTar(File inFile, File untarDir); static int symLink(String target, String linkname); static int chmod(String filename, String perm
                          ); static int chmod(String filename, String perm, boolean recursive); static void setOwner(File file, String username,
      String groupname); static boolean setReadable(File f, boolean readable); static boolean setWritable(File f, boolean writable); static boolean setExecutable(File f, boolean executable); static boolean canRead(File f); static boolean canWrite(File f); static boolean canExecute(File f); static void setPermission(File f, FsPermission permission
                                   ); static final File createLocalTempFile(final File basefile,
                                               final String prefix,
                                               final boolean isDeleteOnExit); static void replaceFile(File src, File target); static File[] listFiles(File dir); static String[] list(File dir); static String[] createJarWithClassPath(String inputClassPath, Path pwd,
      Map<String, String> callerEnv); static String[] createJarWithClassPath(String inputClassPath, Path pwd,
      Path targetDir,
      Map<String, String> callerEnv); static List<Path> getJarsInDirectory(String path); static List<Path> getJarsInDirectory(String path, boolean useLocal); static boolean compareFs(FileSystem srcFs, FileSystem destFs); static FileSystem write(final FileSystem fs, final Path path,
      final byte[] bytes); static FileContext write(final FileContext fileContext,
      final Path path, final byte[] bytes); static FileSystem write(final FileSystem fs, final Path path,
      final Iterable<? extends CharSequence> lines, final Charset cs); static FileContext write(final FileContext fileContext,
      final Path path, final Iterable<? extends CharSequence> lines,
      final Charset cs); static FileSystem write(final FileSystem fs, final Path path,
      final CharSequence charseq, final Charset cs); static FileContext write(final FileContext fs, final Path path,
      final CharSequence charseq, final Charset cs); static FileSystem write(final FileSystem fs, final Path path,
      final CharSequence charseq); static FileContext write(final FileContext fileContext,
      final Path path, final CharSequence charseq); static final int SYMLINK_NO_PRIVILEGE; }
@Test public void testWriteBytesFileSystem() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(uri, conf); Path testPath = new Path(new Path(uri), "writebytes.out"); byte[] write = new byte[] {0x00, 0x01, 0x02, 0x03}; FileUtil.write(fs, testPath, write); byte[] read = FileUtils.readFileToByteArray(new File(testPath.toUri())); assertArrayEquals(write, read); }
@Test public void testWriteStringsFileSystem() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(uri, conf); Path testPath = new Path(new Path(uri), "writestrings.out"); Collection<String> write = Arrays.asList("over", "the", "lazy", "dog"); FileUtil.write(fs, testPath, write, StandardCharsets.UTF_8); List<String> read = FileUtils.readLines(new File(testPath.toUri()), StandardCharsets.UTF_8); assertEquals(write, read); }
@Test public void testWriteStringFileSystem() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(uri, conf); Path testPath = new Path(new Path(uri), "writestring.out"); String write = "A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"; FileUtil.write(fs, testPath, write, StandardCharsets.UTF_8); String read = FileUtils.readFileToString(new File(testPath.toUri()), StandardCharsets.UTF_8); assertEquals(write, read); }
@Test public void testWriteStringNoCharSetFileSystem() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(uri, conf); Path testPath = new Path(new Path(uri), "writestring.out"); String write = "A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"; FileUtil.write(fs, testPath, write); String read = FileUtils.readFileToString(new File(testPath.toUri()), StandardCharsets.UTF_8); assertEquals(write, read); }
@Test public void testWriteBytesFileContext() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileContext fc = FileContext.getFileContext(uri, conf); Path testPath = new Path(new Path(uri), "writebytes.out"); byte[] write = new byte[] {0x00, 0x01, 0x02, 0x03}; FileUtil.write(fc, testPath, write); byte[] read = FileUtils.readFileToByteArray(new File(testPath.toUri())); assertArrayEquals(write, read); }
@Test public void testWriteStringsFileContext() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileContext fc = FileContext.getFileContext(uri, conf); Path testPath = new Path(new Path(uri), "writestrings.out"); Collection<String> write = Arrays.asList("over", "the", "lazy", "dog"); FileUtil.write(fc, testPath, write, StandardCharsets.UTF_8); List<String> read = FileUtils.readLines(new File(testPath.toUri()), StandardCharsets.UTF_8); assertEquals(write, read); }
@Test public void testWriteStringFileContext() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileContext fc = FileContext.getFileContext(uri, conf); Path testPath = new Path(new Path(uri), "writestring.out"); String write = "A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"; FileUtil.write(fc, testPath, write, StandardCharsets.UTF_8); String read = FileUtils.readFileToString(new File(testPath.toUri()), StandardCharsets.UTF_8); assertEquals(write, read); }
@Test public void testWriteStringNoCharSetFileContext() throws IOException { URI uri = tmp.toURI(); Configuration conf = new Configuration(); FileContext fc = FileContext.getFileContext(uri, conf); Path testPath = new Path(new Path(uri), "writestring.out"); String write = "A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"; FileUtil.write(fc, testPath, write); String read = FileUtils.readFileToString(new File(testPath.toUri()), StandardCharsets.UTF_8); assertEquals(write, read); }