Class GeometryIOUtils


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

      Fields 
      Modifier and Type Field Description
      private static char UNIX_PATH_SEP
      Path separator character used on Unix-like systems.
      private static char WINDOWS_PATH_SEP
      Path separator character used on Windows.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GeometryIOUtils()
      Utility class; no instantiation.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> void acceptUnchecked​(IOConsumer<T> consumer, T arg)
      Pass the given argument to the consumer, wrapping any IOException with UncheckedIOException.
      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.
      private static java.lang.Runnable closeAsUncheckedRunnable​(java.io.Closeable closeable)
      Return a Runnable that calls close() on the argument, wrapping any IOException with UncheckedIOException.
      static void closeUnchecked​(java.io.Closeable closeable)
      Close the argument, wrapping any IO exceptions with UncheckedIOException.
      static java.io.BufferedReader createBufferedReader​(GeometryInput input, java.nio.charset.Charset defaultCharset)
      Create a BufferedReader for reading from the given input.
      static java.io.BufferedWriter createBufferedWriter​(GeometryOutput output, java.nio.charset.Charset defaultCharset)
      Create a BufferedWriter for writing to the given output.
      static <T,​I extends java.io.InputStream>
      java.util.stream.Stream<T>
      createCloseableStream​(IOFunction<I,​java.util.stream.Stream<T>> streamFunction, IOSupplier<? extends I> inputStreamSupplier)
      Create a stream associated with an input stream.
      static java.io.UncheckedIOException createUnchecked​(java.io.IOException exc)
      Create an unchecked exception from the given checked exception.
      static java.lang.String getFileExtension​(java.lang.String fileName)
      Get the part of the file name after the last dot.
      static java.lang.String getFileName​(java.lang.String path)
      Get the file name from the given path string, defined as the substring following the last path separator character.
      static java.lang.String getFileName​(java.net.URL url)
      Get the file name of the given url or null if one does not exist or is the empty string.
      static java.lang.String getFileName​(java.nio.file.Path path)
      Get the file name of the given path or null if one does not exist or is the empty string.
      static <T> T getUnchecked​(IOSupplier<T> supplier)
      Get a value from supplier, wrapping any IOException with UncheckedIOException.
      static java.lang.IllegalStateException parseError​(java.lang.String msg)
      Create an exception indicating a parsing or syntax error.
      static java.lang.IllegalStateException parseError​(java.lang.String msg, java.lang.Throwable cause)
      Create an exception indicating a parsing or syntax error.
      static <T,​C extends java.io.Closeable>
      T
      tryApplyCloseable​(IOFunction<C,​T> function, IOSupplier<? extends C> closeableSupplier)
      Pass a supplied Closeable instance to function and return the result.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNIX_PATH_SEP

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

        private static final char WINDOWS_PATH_SEP
        Path separator character used on Windows.
        See Also:
        Constant Field Values
    • Constructor Detail

      • GeometryIOUtils

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

      • getFileName

        public static java.lang.String getFileName​(java.nio.file.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 java.lang.String getFileName​(java.net.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 java.lang.String getFileName​(java.lang.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 java.lang.String getFileExtension​(java.lang.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 java.io.BufferedReader createBufferedReader​(GeometryInput input,
                                                                  java.nio.charset.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:
        java.io.UncheckedIOException - if an I/O error occurs
      • createBufferedWriter

        public static java.io.BufferedWriter createBufferedWriter​(GeometryOutput output,
                                                                  java.nio.charset.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:
        java.io.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:
        java.io.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:
        java.io.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:
        java.io.UncheckedIOException - if an I/O error occurs
      • closeUnchecked

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

        public static java.io.UncheckedIOException createUnchecked​(java.io.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 java.lang.IllegalStateException parseError​(java.lang.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 java.lang.IllegalStateException parseError​(java.lang.String msg,
                                                                 java.lang.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 java.io.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:
        java.io.UncheckedIOException - if an I/O error occurs
      • createCloseableStream

        public static <T,​I extends java.io.InputStream> java.util.stream.Stream<T> createCloseableStream​(IOFunction<I,​java.util.stream.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:
        java.io.UncheckedIOException - if an I/O error occurs during input stream and stream creation
      • closeAsUncheckedRunnable

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