Class GeometryIOUtils
- java.lang.Object
-
- org.apache.commons.geometry.io.core.internal.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 anyIOException
withUncheckedIOException
.static <T> int
applyAsIntUnchecked(IOToIntFunction<T> fn, T arg)
Call the given function with the argument and return theint
result, wrapping anyIOException
withUncheckedIOException
.private static java.lang.Runnable
closeAsUncheckedRunnable(java.io.Closeable closeable)
Return aRunnable
that callsclose()
on the argument, wrapping anyIOException
withUncheckedIOException
.static void
closeUnchecked(java.io.Closeable closeable)
Close the argument, wrapping any IO exceptions withUncheckedIOException
.static java.io.BufferedReader
createBufferedReader(GeometryInput input, java.nio.charset.Charset defaultCharset)
Create aBufferedReader
for reading from the given input.static java.io.BufferedWriter
createBufferedWriter(GeometryOutput output, java.nio.charset.Charset defaultCharset)
Create aBufferedWriter
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 fromsupplier
, wrapping anyIOException
withUncheckedIOException
.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>
TtryApplyCloseable(IOFunction<C,T> function, IOSupplier<? extends C> closeableSupplier)
Pass a suppliedCloseable
instance tofunction
and return the result.
-
-
-
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
-
-
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 aBufferedReader
for reading from the given input. The charset used is the charset defined ininput
ordefaultCharset
if null.- Parameters:
input
- input to read fromdefaultCharset
- 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 aBufferedWriter
for writing to the given output. The charset used is the charset defined inoutput
ordefaultCharset
if null.- Parameters:
output
- output to write todefaultCharset
- 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 fromsupplier
, wrapping anyIOException
withUncheckedIOException
.- 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 anyIOException
withUncheckedIOException
.- Type Parameters:
T
- argument type- Parameters:
consumer
- function to callarg
- 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 theint
result, wrapping anyIOException
withUncheckedIOException
.- Type Parameters:
T
- argument type- Parameters:
fn
- function to callarg
- 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 withUncheckedIOException
.- 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 messagecause
- 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 suppliedCloseable
instance tofunction
and return the result. TheCloseable
instance returned by the supplier is closed if function execution fails, otherwise the instance is not closed.- Type Parameters:
T
- Return typeC
- Closeable type- Parameters:
function
- function called with the supplied Closeable instancecloseableSupplier
- 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. AnyIOException
thrown when the input stream is closed after the return of this method are wrapped withUncheckedIOException
.- Type Parameters:
T
- Stream element typeI
- Input stream type- Parameters:
streamFunction
- function accepting an input stream and returning a streaminputStreamSupplier
- 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 aRunnable
that callsclose()
on the argument, wrapping anyIOException
withUncheckedIOException
.- Parameters:
closeable
- instance to be closed- Returns:
- runnable that calls {@code close()) on the argument
-
-