Class GeometryIOUtils

java.lang.Object
org.apache.commons.geometry.io.core.internal.GeometryIOUtils

public final class GeometryIOUtils extends Object
Internal class containing utility methods for IO operations.
  • Field Details

    • UNIX_PATH_SEP

      private static final char UNIX_PATH_SEP
      Path separator character used on Unix-like systems.
      See Also:
    • WINDOWS_PATH_SEP

      private static final char WINDOWS_PATH_SEP
      Path separator character used on Windows.
      See Also:
  • Constructor Details

    • GeometryIOUtils

      private GeometryIOUtils()
      Utility class; no instantiation.
  • Method Details

    • getFileName

      public static String getFileName(Path path)
      Get the file name of the given path or null if one does not exist or is the empty string.
      Parameters:
      path - path to get the file name of
      Returns:
      file name of the given path
    • getFileName

      public static String getFileName(URL url)
      Get the file name of the given url or null if one does not exist or is the empty string.
      Parameters:
      url - url to get the file name of
      Returns:
      file name of the given url
    • getFileName

      public static String getFileName(String path)
      Get the file name from the given path string, defined as the substring following the last path separator character. Null is returned if the argument is null or the file name is the empty string.
      Parameters:
      path - path to get the file name from
      Returns:
      file name of the given path string or null if a non-empty file name does not exist
    • getFileExtension

      public static String getFileExtension(String fileName)
      Get the part of the file name after the last dot.
      Parameters:
      fileName - file name to get the extension for
      Returns:
      the extension of the file name, the empty string if no extension is found, or null if the argument is null
    • createBufferedReader

      public static BufferedReader createBufferedReader(GeometryInput input, Charset defaultCharset)
      Create a BufferedReader for reading from the given input. The charset used is the charset defined in input or defaultCharset if null.
      Parameters:
      input - input to read from
      defaultCharset - charset to use if no charset is defined in the input
      Returns:
      new reader instance
      Throws:
      UncheckedIOException - if an I/O error occurs
    • createBufferedWriter

      public static BufferedWriter createBufferedWriter(GeometryOutput output, Charset defaultCharset)
      Create a BufferedWriter for writing to the given output. The charset used is the charset defined in output or defaultCharset if null.
      Parameters:
      output - output to write to
      defaultCharset - charset to use if no charset is defined in the output
      Returns:
      new writer instance
      Throws:
      UncheckedIOException - if an I/O error occurs
    • getUnchecked

      public static <T> T getUnchecked(IOSupplier<T> supplier)
      Get a value from supplier, wrapping any IOException with UncheckedIOException.
      Type Parameters:
      T - returned type
      Parameters:
      supplier - object supplying the return value
      Returns:
      supplied value
      Throws:
      UncheckedIOException - if an I/O error occurs
    • acceptUnchecked

      public static <T> void acceptUnchecked(IOConsumer<T> consumer, T arg)
      Pass the given argument to the consumer, wrapping any IOException with UncheckedIOException.
      Type Parameters:
      T - argument type
      Parameters:
      consumer - function to call
      arg - function argument
      Throws:
      UncheckedIOException - if an I/O error occurs
    • applyAsIntUnchecked

      public static <T> int applyAsIntUnchecked(IOToIntFunction<T> fn, T arg)
      Call the given function with the argument and return the int result, wrapping any IOException with UncheckedIOException.
      Type Parameters:
      T - argument type
      Parameters:
      fn - function to call
      arg - function argument
      Returns:
      int value
      Throws:
      UncheckedIOException - if an I/O error occurs
    • closeUnchecked

      public static void closeUnchecked(Closeable closeable)
      Close the argument, wrapping any IO exceptions with UncheckedIOException.
      Parameters:
      closeable - argument to close
      Throws:
      UncheckedIOException - if an I/O error occurs
    • createUnchecked

      public static UncheckedIOException createUnchecked(IOException exc)
      Create an unchecked exception from the given checked exception. The message of the returned exception contains the original exception's type and message.
      Parameters:
      exc - exception to wrap in an unchecked exception
      Returns:
      the unchecked exception
    • parseError

      public static IllegalStateException parseError(String msg)
      Create an exception indicating a parsing or syntax error.
      Parameters:
      msg - exception message
      Returns:
      an exception indicating a parsing or syntax error
    • parseError

      public static IllegalStateException parseError(String msg, Throwable cause)
      Create an exception indicating a parsing or syntax error.
      Parameters:
      msg - exception message
      cause - exception cause
      Returns:
      an exception indicating a parsing or syntax error
    • tryApplyCloseable

      public static <T, C extends Closeable> T tryApplyCloseable(IOFunction<C,T> function, IOSupplier<? extends C> closeableSupplier)
      Pass a supplied Closeable instance to function and return the result. The Closeable instance returned by the supplier is closed if function execution fails, otherwise the instance is not closed.
      Type Parameters:
      T - Return type
      C - Closeable type
      Parameters:
      function - function called with the supplied Closeable instance
      closeableSupplier - supplier used to obtain a Closeable instance
      Returns:
      result of calling function with a supplied Closeable instance
      Throws:
      UncheckedIOException - if an I/O error occurs
    • createCloseableStream

      public static <T, I extends InputStream> Stream<T> createCloseableStream(IOFunction<I,Stream<T>> streamFunction, IOSupplier<? extends I> inputStreamSupplier)
      Create a stream associated with an input stream. The input stream is closed when the stream is closed and also closed if stream creation fails. Any IOException thrown when the input stream is closed after the return of this method are wrapped with UncheckedIOException.
      Type Parameters:
      T - Stream element type
      I - Input stream type
      Parameters:
      streamFunction - function accepting an input stream and returning a stream
      inputStreamSupplier - supplier used to obtain the input stream
      Returns:
      stream associated with the input stream return by the supplier
      Throws:
      UncheckedIOException - if an I/O error occurs during input stream and stream creation
    • closeAsUncheckedRunnable

      private static Runnable closeAsUncheckedRunnable(Closeable closeable)
      Return a Runnable that calls
      invalid reference
      close()
      on the argument, wrapping any IOException with UncheckedIOException.
      Parameters:
      closeable - instance to be closed
      Returns:
      runnable that calls
      invalid @code
      {@code close()) on the argument