public final class IoUtils
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
readUtf8File(java.nio.file.Path path)
Reads a file into a UTF-8 encoded string.
|
static java.lang.String |
readUtf8File(java.lang.String 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 java.lang.String |
runCommand(java.lang.String command)
Runs a process using the given
command at the current directory
specified by System.getProperty("user.dir") . |
static java.lang.String |
runCommand(java.lang.String command,
java.nio.file.Path directory)
Runs a process using the given
command relative to the given
directory . |
static int |
runCommand(java.lang.String command,
java.nio.file.Path directory,
java.lang.Appendable output)
Runs a process using the given
command relative to the given
directory and writes stdout and stderr to output . |
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.
|
public static byte[] toByteArray(java.io.InputStream is)
is
- The input stream to convert.public static java.lang.String toUtf8String(java.io.InputStream is)
is
- The input stream to convert.public static java.lang.String readUtf8File(java.lang.String path)
path
- Path to the file to read.java.lang.RuntimeException
- if the file can't be read or encoded.public static java.lang.String readUtf8File(java.nio.file.Path path)
path
- Path to the file to read.java.lang.RuntimeException
- if the file can't be read or encoded.public static java.lang.String readUtf8Resource(java.lang.ClassLoader classLoader, java.lang.String resourcePath)
This is equivalent to reading the contents of an InputStream
from ClassLoader.getResourceAsStream(java.lang.String)
.
classLoader
- Class loader to load from.resourcePath
- Path to the resource to load.java.io.UncheckedIOException
- if the resource cannot be loaded.public static java.lang.String readUtf8Resource(java.lang.Class<?> clazz, java.lang.String resourcePath)
This is equivalent to reading the contents of an InputStream
from Class.getResourceAsStream(String)
.
clazz
- Class to load from.resourcePath
- Path to the resource to load.java.io.UncheckedIOException
- if the resource cannot be loaded.public static java.lang.String readUtf8Url(java.net.URL url)
url
- URL to load from.java.io.UncheckedIOException
- if the resource cannot be loaded.public static java.lang.String runCommand(java.lang.String command)
command
at the current directory
specified by System.getProperty("user.dir")
.
stderr is redirected to stdout in the return value of this method.
command
- Process command to execute.java.lang.RuntimeException
- if the process returns a non-zero exit code or fails.public static java.lang.String runCommand(java.lang.String command, java.nio.file.Path directory)
command
relative to the given
directory
.
stderr is redirected to stdout in the return value of this method.
command
- Process command to execute.directory
- Directory to use as the working directory.java.lang.RuntimeException
- if the process returns a non-zero exit code or fails.public static int runCommand(java.lang.String command, java.nio.file.Path directory, java.lang.Appendable output)
command
relative to the given
directory
and writes stdout and stderr to output
.
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.
command
- Process command to execute.directory
- Directory to use as the working directory.output
- Where stdout and stderr is written.