Class IoUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
static String
readUtf8File
(String path) Reads a file into a UTF-8 encoded string.static String
readUtf8File
(Path path) Reads a file into a UTF-8 encoded string.static String
readUtf8Resource
(Class<?> clazz, String resourcePath) Reads a class resource into a UTF-8 string.static String
readUtf8Resource
(ClassLoader classLoader, String resourcePath) Reads a class loader resource into a UTF-8 string.static String
readUtf8Url
(URL url) Reads a URL resource into a UTF-8 string.static boolean
Delete a directory and all files within.static String
runCommand
(String command) Helper to run a process.static String
runCommand
(String command, Path directory) Helper to run a process.static int
runCommand
(String command, Path directory, Appendable output) Helper to run a process.static int
runCommand
(String command, Path directory, Appendable output, Map<String, String> env) Helper to run a process.static int
runCommand
(List<String> args, Path directory, InputStream input, Appendable output, Map<String, String> env) Helper to run a process.static int
runCommand
(List<String> args, Path directory, Appendable output, Map<String, String> env) Helper to run a process.static byte[]
Reads and returns the rest of the given input stream as a byte array.static String
Reads and returns the rest of the given input stream as a string.
-
Method Details
-
toByteArray
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
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
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:
RuntimeException
- if the file can't be read or encoded.
-
readUtf8File
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:
RuntimeException
- if the file can't be read or encoded.
-
readUtf8Resource
Reads a class loader resource into a UTF-8 string.This is equivalent to reading the contents of an
InputStream
fromClassLoader.getResourceAsStream(java.lang.String)
.- Parameters:
classLoader
- Class loader to load from.resourcePath
- Path to the resource to load.- Returns:
- Returns the loaded resource.
- Throws:
UncheckedIOException
- if the resource cannot be loaded.
-
readUtf8Resource
Reads a class resource into a UTF-8 string.This is equivalent to reading the contents of an
InputStream
fromClass.getResourceAsStream(String)
.- Parameters:
clazz
- Class to load from.resourcePath
- Path to the resource to load.- Returns:
- Returns the loaded resource.
- Throws:
UncheckedIOException
- if the resource cannot be loaded.
-
readUtf8Url
Reads a URL resource into a UTF-8 string.- Parameters:
url
- URL to load from.- Returns:
- Returns the loaded resource.
- Throws:
UncheckedIOException
- if the resource cannot be loaded.
-
runCommand
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:
RuntimeException
- if the process returns a non-zero exit code or fails.
-
runCommand
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:
RuntimeException
- if the process returns a non-zero exit code or fails.
-
runCommand
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, useProcessBuilder
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(String command, Path directory, Appendable output, Map<String, 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, useProcessBuilder
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(List<String> args, Path directory, Appendable output, Map<String, 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, useProcessBuilder
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(List<String> args, Path directory, InputStream input, Appendable output, Map<String, 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, useProcessBuilder
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. Usenull
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
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:
IllegalArgumentException
- if the given path is not a directory.RuntimeException
- if unable to delete a file or directory.
-
copyDir
-