Package software.amazon.smithy.utils
Class IoUtils
java.lang.Object
software.amazon.smithy.utils.IoUtils
Utilities for IO operations.
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringreadUtf8File(String path) Reads a file into a UTF-8 encoded string.static StringreadUtf8File(Path path) Reads a file into a UTF-8 encoded string.static StringreadUtf8Resource(Class<?> clazz, String resourcePath) Reads a class resource into a UTF-8 string.static StringreadUtf8Resource(ClassLoader classLoader, String resourcePath) Reads a class loader resource into a UTF-8 string.static StringreadUtf8Url(URL url) Reads a URL resource into a UTF-8 string.static StringrunCommand(String command) Runs a process using the givencommandat the current directory specified bySystem.getProperty("user.dir").static StringrunCommand(String command, Path directory) Runs a process using the givencommandrelative to the givendirectory.static intrunCommand(String command, Path directory, Appendable output) Runs a process using the givencommandrelative to the givendirectoryand writes stdout and stderr tooutput.static byte[]Reads and returns the rest of the given input stream as a byte array.static StringReads 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
InputStreamfromClassLoader.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
InputStreamfromClass.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
Runs a process using the givencommandat the current directory specified bySystem.getProperty("user.dir").stderr is redirected to stdout in the return value of this method.
- Parameters:
command- Process command to execute.- Returns:
- Returns the combined stdout and stderr of the process.
- Throws:
RuntimeException- if the process returns a non-zero exit code or fails.
-
runCommand
Runs a process using the givencommandrelative to the givendirectory.stderr is redirected to stdout in the return value of this method.
- Parameters:
command- Process command to execute.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
Runs a process using the givencommandrelative to the givendirectoryand writes stdout and stderr tooutput.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, useProcessBuilderdirectly.- Parameters:
command- Process command to execute.directory- Directory to use as the working directory.output- Where stdout and stderr is written.- Returns:
- Returns the exit code of the process.
-