Package io.debezium.util
Class IoUtil
- java.lang.Object
-
- io.debezium.util.IoUtil
-
@Immutable public class IoUtil extends Object
A set of utilities for more easily performing I/O.
-
-
Field Summary
Fields Modifier and Type Field Description private static org.slf4j.Logger
LOGGER
-
Constructor Summary
Constructors Modifier Constructor Description private
IoUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static File
createDirectory(Path path)
Create a directory at the given absolute or relative path.static File
createDirectory(Path path, boolean removeExistingContent)
Create a directory at the given absolute or relative path, removing any existing content beforehand.static File
createFile(Path path)
Create a file at the given absolute or relative path.static void
delete(File fileOrFolder)
A method that will delete a file or folder.static void
delete(File... filesOrFolder)
A method that will delete multiple file and/or folders.static void
delete(String path)
A method that will delete a file or folder only if it is within the 'target' directory (for safety).static void
delete(Path path)
A method that will recursively delete a file or folder.static int
getAvailablePort()
Find a port that is available.static InputStream
getResourceAsStream(String resourcePath, ClassLoader classLoader, Class<?> clazz, String resourceDesc, Consumer<String> logger)
Get theinput stream
to the resource given by the supplied path.static Properties
loadProperties(Class<?> clazz, String classpathResource)
Atomically load the properties file at the given location within the designated class' class loader.static Properties
loadProperties(ClassLoader classLoader, String classpathResource)
Atomically load the properties file at the given location within the designated class loader.static Properties
loadProperties(Supplier<ClassLoader> classLoader, String classpathResource)
Atomically load the properties file at the given location within the designated class loader.private static void
logMessage(InputStream stream, Consumer<String> logger, String resourceDesc, String msg)
static String
read(File file)
Read and return the entire contents of the suppliedFile
.static String
read(InputStream stream)
Read and return the entire contents of the suppliedInputStream
.static String
read(InputStream stream, String charset)
Read and return the entire contents of the suppliedInputStream
.static String
read(Reader reader)
Read and return the entire contents of the suppliedReader
.static byte[]
readBytes(File file)
Read and return the entire contents of the suppliedfile
.static byte[]
readBytes(InputStream stream)
Read and return the entire contents of the suppliedstream
.static void
readLines(InputStream stream, Consumer<String> lineProcessor)
Read the lines from the supplied stream.static void
readLines(InputStream stream, Consumer<String> lineProcessor, Charset charset)
Read the lines from the supplied stream.static void
readLines(String resourcePath, ClassLoader classLoader, Class<?> clazz, Consumer<String> lineProcessor)
Read the lines from the content of the resource file at the given path on the classpath.static void
readLines(Path path, Consumer<String> lineProcessor)
Read the lines from the supplied stream.
-
-
-
Method Detail
-
readBytes
public static byte[] readBytes(InputStream stream) throws IOException
Read and return the entire contents of the suppliedstream
. This method always closes the stream when finished reading.- Parameters:
stream
- the stream to the contents; may be null- Returns:
- the contents, or an empty byte array if the supplied reader is null
- Throws:
IOException
- if there is an error reading the content
-
readBytes
public static byte[] readBytes(File file) throws IOException
Read and return the entire contents of the suppliedfile
.- Parameters:
file
- the file containing the contents; may be null- Returns:
- the contents, or an empty byte array if the supplied file is null
- Throws:
IOException
- if there is an error reading the content
-
readLines
public static void readLines(String resourcePath, ClassLoader classLoader, Class<?> clazz, Consumer<String> lineProcessor) throws IOException
Read the lines from the content of the resource file at the given path on the classpath.- Parameters:
resourcePath
- the logical path to the classpath, file, or URL resourceclassLoader
- the classloader that should be used to load the resource as a stream; may be nullclazz
- the class that should be used to load the resource as a stream; may be nulllineProcessor
- the function that this method calls for each line read from the supplied stream; may not be null- Throws:
IOException
- if an I/O error occurs
-
readLines
public static void readLines(InputStream stream, Consumer<String> lineProcessor) throws IOException
Read the lines from the supplied stream. This function completely reads the stream and therefore closes the stream.- Parameters:
stream
- the stream with the contents to be read; may not be nulllineProcessor
- the function that this method calls for each line read from the supplied stream; may not be null- Throws:
IOException
- if an I/O error occurs
-
readLines
public static void readLines(InputStream stream, Consumer<String> lineProcessor, Charset charset) throws IOException
Read the lines from the supplied stream. This function completely reads the stream and therefore closes the stream.- Parameters:
stream
- the stream with the contents to be read; may not be nulllineProcessor
- the function that this method calls for each line read from the supplied stream; may not be nullcharset
- the character set used to interpret the stream content- Throws:
IOException
- if an I/O error occurs
-
readLines
public static void readLines(Path path, Consumer<String> lineProcessor) throws IOException
Read the lines from the supplied stream. This function completely reads the stream and therefore closes the stream.- Parameters:
path
- path to the file with the contents to be read; may not be nulllineProcessor
- the function that this method calls for each line read from the supplied stream; may not be null- Throws:
IOException
- if an I/O error occurs
-
read
public static String read(Reader reader) throws IOException
Read and return the entire contents of the suppliedReader
. This method always closes the reader when finished reading.- Parameters:
reader
- the reader of the contents; may be null- Returns:
- the contents, or an empty string if the supplied reader is null
- Throws:
IOException
- if there is an error reading the content
-
read
public static String read(InputStream stream) throws IOException
Read and return the entire contents of the suppliedInputStream
. This method always closes the stream when finished reading.- Parameters:
stream
- the streamed contents; may be null- Returns:
- the contents, or an empty string if the supplied stream is null
- Throws:
IOException
- if there is an error reading the content
-
read
public static String read(InputStream stream, String charset) throws IOException
Read and return the entire contents of the suppliedInputStream
. This method always closes the stream when finished reading.- Parameters:
stream
- the streamed contents; may be nullcharset
- character set of the stream data; may not be null- Returns:
- the contents, or an empty string if the supplied stream is null
- Throws:
IOException
- if there is an error reading the content
-
read
public static String read(File file) throws IOException
Read and return the entire contents of the suppliedFile
.- Parameters:
file
- the file containing the information to be read; may be null- Returns:
- the contents, or an empty string if the supplied reader is null
- Throws:
IOException
- if there is an error reading the content
-
getResourceAsStream
public static InputStream getResourceAsStream(String resourcePath, ClassLoader classLoader, Class<?> clazz, String resourceDesc, Consumer<String> logger)
Get theinput stream
to the resource given by the supplied path. This method performs these operations in order, returning as soon as a file is found:- look for a file on the file system at the given absolute path; otherwise
- look for a file on the file system at the given path relative to the JVM process; otherwise
- if a
classloader
is supplied, use it to load the file on the classpath at the given path; otherwise - if a
clazz
is supplied, use it to load the file on its classpath at the given path; otherwise - try to convert the path to a URL and obtain the referenced resource
- Parameters:
resourcePath
- the logical path to the classpath, file, or URL resourceclassLoader
- the classloader that should be used to load the resource as a stream; may be nullclazz
- the class that should be used to load the resource as a stream; may be nullresourceDesc
- the description of the resource to be used in messages sent tologger
; may be nulllogger
- a function that is to be called with log messages describing what is being tried; may be null- Returns:
- an input stream to the resource; or null if the resource could not be found
- Throws:
IllegalArgumentException
- if the resource path is null or empty
-
createDirectory
public static File createDirectory(Path path)
Create a directory at the given absolute or relative path.- Parameters:
path
- the relative or absolute path of the directory; may not be null- Returns:
- the reference to the existing readable and writable directory
-
createFile
public static File createFile(Path path)
Create a file at the given absolute or relative path.- Parameters:
path
- the relative or absolute path of the file to create; may not be null- Returns:
- the reference to the existing readable and writable file
-
createDirectory
public static File createDirectory(Path path, boolean removeExistingContent) throws IOException
Create a directory at the given absolute or relative path, removing any existing content beforehand.- Parameters:
path
- the relative or absolute path of the directory to recreate; may not be nullremoveExistingContent
- true if any existing content should be removed- Returns:
- the reference to the existing readable and writable directory
- Throws:
IOException
- if there is a problem deleting the files at this path
-
delete
public static void delete(String path) throws IOException
A method that will delete a file or folder only if it is within the 'target' directory (for safety). Folders are removed recursively.- Parameters:
path
- the path to the file or folder in the target directory- Throws:
IOException
- if there is a problem deleting the file at the given path
-
delete
public static void delete(File fileOrFolder) throws IOException
A method that will delete a file or folder. Folders are removed recursively.- Parameters:
fileOrFolder
- the file or folder to be deleted- Throws:
IOException
- if there is a problem deleting the file at the given path
-
delete
public static void delete(File... filesOrFolder) throws IOException
A method that will delete multiple file and/or folders. Folders are removed recursively.- Parameters:
filesOrFolder
- the files and folders to be deleted- Throws:
IOException
- if there is a problem deleting the file at the given path
-
delete
public static void delete(Path path) throws IOException
A method that will recursively delete a file or folder.- Parameters:
path
- the path to the file or folder in the target directory- Throws:
IOException
- if there is a problem deleting the file at the given path
-
getAvailablePort
public static int getAvailablePort()
Find a port that is available. This method starts aServerSocket
and obtains the port on which the socket is listening, and then shuts down the socket so the port becomes available.- Returns:
- the number of the now-available port
- Throws:
IllegalStateException
- if it cannot find an available port
-
logMessage
private static void logMessage(InputStream stream, Consumer<String> logger, String resourceDesc, String msg)
-
loadProperties
public static Properties loadProperties(Supplier<ClassLoader> classLoader, String classpathResource)
Atomically load the properties file at the given location within the designated class loader.- Parameters:
classLoader
- the supplier for the class loader; may not be null or return nullclasspathResource
- the path to the resource file; may not be null- Returns:
- the properties object; never null, but possibly empty
- Throws:
IllegalStateException
- if the file could not be found or read
-
loadProperties
public static Properties loadProperties(ClassLoader classLoader, String classpathResource)
Atomically load the properties file at the given location within the designated class loader.- Parameters:
classLoader
- the class loader; may not be nullclasspathResource
- the path to the resource file; may not be null- Returns:
- the properties object; never null, but possibly empty
- Throws:
IllegalStateException
- if the file could not be found or read
-
loadProperties
public static Properties loadProperties(Class<?> clazz, String classpathResource)
Atomically load the properties file at the given location within the designated class' class loader.- Parameters:
clazz
- the class whose class loader is to be used; may not be nullclasspathResource
- the path to the resource file; may not be null- Returns:
- the properties object; never null, but possibly empty
- Throws:
IllegalStateException
- if the file could not be found or read
-
-