Class IoUtils


  • public final class IoUtils
    extends java.lang.Object
    Utilities for IO operations.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyDir​(java.nio.file.Path src, java.nio.file.Path dest)  
      static java.lang.String readUtf8File​(java.lang.String path)
      Reads a file into a UTF-8 encoded string.
      static java.lang.String readUtf8File​(java.nio.file.Path path)
      Reads a file into a UTF-8 encoded string.
      static java.lang.String readUtf8Resource​(java.lang.Class<?> clazz, java.lang.String resourcePath)
      Reads a class resource into a UTF-8 string.
      static java.lang.String readUtf8Resource​(java.lang.ClassLoader classLoader, java.lang.String resourcePath)
      Reads a class loader resource into a UTF-8 string.
      static java.lang.String readUtf8Url​(java.net.URL url)
      Reads a URL resource into a UTF-8 string.
      static boolean rmdir​(java.nio.file.Path dir)
      Delete a directory and all files within.
      static java.lang.String runCommand​(java.lang.String command)
      Helper to run a process.
      static java.lang.String runCommand​(java.lang.String command, java.nio.file.Path directory)
      Helper to run a process.
      static int runCommand​(java.lang.String command, java.nio.file.Path directory, java.lang.Appendable output)
      Helper to run a process.
      static int runCommand​(java.lang.String command, java.nio.file.Path directory, java.lang.Appendable output, java.util.Map<java.lang.String,​java.lang.String> env)
      Helper to run a process.
      static int runCommand​(java.util.List<java.lang.String> args, java.nio.file.Path directory, java.io.InputStream input, java.lang.Appendable output, java.util.Map<java.lang.String,​java.lang.String> env)
      Helper to run a process.
      static int runCommand​(java.util.List<java.lang.String> args, java.nio.file.Path directory, java.lang.Appendable output, java.util.Map<java.lang.String,​java.lang.String> env)
      Helper to run a process.
      static byte[] toByteArray​(java.io.InputStream is)
      Reads and returns the rest of the given input stream as a byte array.
      static java.lang.String toUtf8String​(java.io.InputStream is)
      Reads and returns the rest of the given input stream as a string.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream is)
        Reads and returns the rest of the given input stream as a byte array. Caller is responsible for closing the given input stream.
        Parameters:
        is - The input stream to convert.
        Returns:
        The converted bytes.
      • toUtf8String

        public static java.lang.String toUtf8String​(java.io.InputStream is)
        Reads and returns the rest of the given input stream as a string. Caller is responsible for closing the given input stream.
        Parameters:
        is - The input stream to convert.
        Returns:
        The converted string.
      • readUtf8File

        public static java.lang.String readUtf8File​(java.lang.String path)
        Reads a file into a UTF-8 encoded string.
        Parameters:
        path - Path to the file to read.
        Returns:
        Returns the contents of the file.
        Throws:
        java.lang.RuntimeException - if the file can't be read or encoded.
      • readUtf8File

        public static java.lang.String readUtf8File​(java.nio.file.Path path)
        Reads a file into a UTF-8 encoded string.
        Parameters:
        path - Path to the file to read.
        Returns:
        Returns the contents of the file.
        Throws:
        java.lang.RuntimeException - if the file can't be read or encoded.
      • readUtf8Resource

        public static java.lang.String readUtf8Resource​(java.lang.ClassLoader classLoader,
                                                        java.lang.String resourcePath)
        Reads a class loader resource into a UTF-8 string.

        This is equivalent to reading the contents of an InputStream from ClassLoader.getResourceAsStream(java.lang.String).

        Parameters:
        classLoader - Class loader to load from.
        resourcePath - Path to the resource to load.
        Returns:
        Returns the loaded resource.
        Throws:
        java.io.UncheckedIOException - if the resource cannot be loaded.
      • readUtf8Resource

        public static java.lang.String readUtf8Resource​(java.lang.Class<?> clazz,
                                                        java.lang.String resourcePath)
        Reads a class resource into a UTF-8 string.

        This is equivalent to reading the contents of an InputStream from Class.getResourceAsStream(String).

        Parameters:
        clazz - Class to load from.
        resourcePath - Path to the resource to load.
        Returns:
        Returns the loaded resource.
        Throws:
        java.io.UncheckedIOException - if the resource cannot be loaded.
      • readUtf8Url

        public static java.lang.String readUtf8Url​(java.net.URL url)
        Reads a URL resource into a UTF-8 string.
        Parameters:
        url - URL to load from.
        Returns:
        Returns the loaded resource.
        Throws:
        java.io.UncheckedIOException - if the resource cannot be loaded.
      • runCommand

        public static java.lang.String runCommand​(java.lang.String command)
        Helper to run a process.

        stderr is redirected to stdout in the return value of this method.

        Parameters:
        command - String that contains the command and arguments of the command.
        Returns:
        Returns the combined stdout and stderr of the process.
        Throws:
        java.lang.RuntimeException - if the process returns a non-zero exit code or fails.
      • runCommand

        public static java.lang.String runCommand​(java.lang.String command,
                                                  java.nio.file.Path directory)
        Helper to run a process.

        stderr is redirected to stdout in the return value of this method.

        Parameters:
        command - String that contains the command and arguments of the command.
        directory - Directory to use as the working directory.
        Returns:
        Returns the combined stdout and stderr of the process.
        Throws:
        java.lang.RuntimeException - if the process returns a non-zero exit code or fails.
      • runCommand

        public static int runCommand​(java.lang.String command,
                                     java.nio.file.Path directory,
                                     java.lang.Appendable output)
        Helper to run a process.

        stderr is redirected to stdout when writing to output. This method does not throw when a non-zero exit code is encountered. For any more complex use cases, use ProcessBuilder directly.

        Parameters:
        command - String that contains the command and arguments of the command.
        directory - Directory to use as the working directory.
        output - Sink destination for lines from stdout and stderr of the process.
        Returns:
        Returns the exit code of the process.
      • runCommand

        public static int runCommand​(java.lang.String command,
                                     java.nio.file.Path directory,
                                     java.lang.Appendable output,
                                     java.util.Map<java.lang.String,​java.lang.String> env)
        Helper to run a process.

        stderr is redirected to stdout when writing to output. This method does not throw when a non-zero exit code is encountered. For any more complex use cases, use ProcessBuilder directly.

        Parameters:
        command - String that contains the command and arguments of the command.
        directory - Directory to use as the working directory.
        output - Sink destination for lines from stdout and stderr of the process.
        env - Environment variables to set, possibly overriding specific inherited variables.
        Returns:
        Returns the exit code of the process.
      • runCommand

        public static int runCommand​(java.util.List<java.lang.String> args,
                                     java.nio.file.Path directory,
                                     java.lang.Appendable output,
                                     java.util.Map<java.lang.String,​java.lang.String> env)
        Helper to run a process.

        stderr is redirected to stdout when writing to output. This method does not throw when a non-zero exit code is encountered. For any more complex use cases, use ProcessBuilder directly.

        Parameters:
        args - List containing the program and the arguments.
        directory - Directory to use as the working directory.
        output - Sink destination for lines from stdout and stderr of the process.
        env - Environment variables to set, possibly overriding specific inherited variables.
        Returns:
        Returns the exit code of the process.
      • runCommand

        public static int runCommand​(java.util.List<java.lang.String> args,
                                     java.nio.file.Path directory,
                                     java.io.InputStream input,
                                     java.lang.Appendable output,
                                     java.util.Map<java.lang.String,​java.lang.String> env)
        Helper to run a process.

        stderr is redirected to stdout when writing to output This method does not throw when a non-zero exit code is encountered. For any more complex use cases, use ProcessBuilder directly.

        Parameters:
        args - List containing the program and the arguments.
        directory - Directory to use as the working directory.
        input - Data to write to the stdin of the process. Use null to send no input.
        output - Sink destination for lines from stdout and stderr of the process.
        env - Environment variables to set, possibly overriding specific inherited variables.
        Returns:
        Returns the exit code of the process.
      • rmdir

        public static boolean rmdir​(java.nio.file.Path dir)
        Delete a directory and all files within.

        Any found symlink is deleted, but the contents of a symlink are not deleted.

        Parameters:
        dir - Directory to delete.
        Returns:
        Returns true if the directory was deleted, or false if the directory does not exist.
        Throws:
        java.lang.IllegalArgumentException - if the given path is not a directory.
        java.lang.RuntimeException - if unable to delete a file or directory.
      • copyDir

        public static void copyDir​(java.nio.file.Path src,
                                   java.nio.file.Path dest)