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 Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
  • Constructor Details

    • IoUtil

      private IoUtil()
  • Method Details

    • readBytes

      public static byte[] readBytes(InputStream stream) throws IOException
      Read and return the entire contents of the supplied stream. 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 supplied file.
      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 resource
      classLoader - the classloader that should be used to load the resource as a stream; may be null
      clazz - the class that should be used to load the resource as a stream; may be null
      lineProcessor - 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 null
      lineProcessor - 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 null
      lineProcessor - the function that this method calls for each line read from the supplied stream; may not be null
      charset - 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 null
      lineProcessor - 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 supplied Reader. 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 supplied InputStream. 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 supplied InputStream. This method always closes the stream when finished reading.
      Parameters:
      stream - the streamed contents; may be null
      charset - 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 supplied File.
      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 the input stream to the resource given by the supplied path. This method performs these operations in order, returning as soon as a file is found:
      1. look for a file on the file system at the given absolute path; otherwise
      2. look for a file on the file system at the given path relative to the JVM process; otherwise
      3. if a classloader is supplied, use it to load the file on the classpath at the given path; otherwise
      4. if a clazz is supplied, use it to load the file on its classpath at the given path; otherwise
      5. try to convert the path to a URL and obtain the referenced resource
      If all of these fail, this method returns null.
      Parameters:
      resourcePath - the logical path to the classpath, file, or URL resource
      classLoader - the classloader that should be used to load the resource as a stream; may be null
      clazz - the class that should be used to load the resource as a stream; may be null
      resourceDesc - the description of the resource to be used in messages sent to logger; may be null
      logger - 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 null
      removeExistingContent - 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 a ServerSocket 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 null
      classpathResource - 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 null
      classpathResource - 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 null
      classpathResource - 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
    • readClassPathResource

      public static String readClassPathResource(String classpathResource)
      Read a resource on classpath as a String
      Parameters:
      classpathResource -
      Returns:
      the content of resource as String