Class GeometryIOUtils
java.lang.Object
org.apache.commons.geometry.io.core.internal.GeometryIOUtils
Internal class containing utility methods for IO operations.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final char
Path separator character used on Unix-like systems.private static final char
Path separator character used on Windows. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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 Runnable
closeAsUncheckedRunnable
(Closeable closeable) Return aRunnable
that callsinvalid reference
close()
IOException
withUncheckedIOException
.static void
closeUnchecked
(Closeable closeable) Close the argument, wrapping any IO exceptions withUncheckedIOException
.static BufferedReader
createBufferedReader
(GeometryInput input, Charset defaultCharset) Create aBufferedReader
for reading from the given input.static BufferedWriter
createBufferedWriter
(GeometryOutput output, Charset defaultCharset) Create aBufferedWriter
for writing to the given output.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.static UncheckedIOException
Create an unchecked exception from the given checked exception.static String
getFileExtension
(String fileName) Get the part of the file name after the last dot.static String
getFileName
(String path) Get the file name from the given path string, defined as the substring following the last path separator character.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.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.static <T> T
getUnchecked
(IOSupplier<T> supplier) static IllegalStateException
parseError
(String msg) Create an exception indicating a parsing or syntax error.static IllegalStateException
parseError
(String msg, Throwable cause) Create an exception indicating a parsing or syntax error.static <T,
C extends Closeable>
TtryApplyCloseable
(IOFunction<C, T> function, IOSupplier<? extends C> closeableSupplier) Pass a suppliedCloseable
instance tofunction
and return the result.
-
Field Details
-
UNIX_PATH_SEP
private static final char UNIX_PATH_SEPPath separator character used on Unix-like systems.- See Also:
-
WINDOWS_PATH_SEP
private static final char WINDOWS_PATH_SEPPath separator character used on Windows.- See Also:
-
-
Constructor Details
-
GeometryIOUtils
private GeometryIOUtils()Utility class; no instantiation.
-
-
Method Details
-
getFileName
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
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
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
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
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:
UncheckedIOException
- if an I/O error occurs
-
createBufferedWriter
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:
UncheckedIOException
- if an I/O error occurs
-
getUnchecked
- Type Parameters:
T
- returned type- Parameters:
supplier
- object supplying the return value- Returns:
- supplied value
- Throws:
UncheckedIOException
- if an I/O error occurs
-
acceptUnchecked
Pass the given argument to the consumer, wrapping anyIOException
withUncheckedIOException
.- Type Parameters:
T
- argument type- Parameters:
consumer
- function to callarg
- function argument- Throws:
UncheckedIOException
- if an I/O error occurs
-
applyAsIntUnchecked
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:
UncheckedIOException
- if an I/O error occurs
-
closeUnchecked
Close the argument, wrapping any IO exceptions withUncheckedIOException
.- Parameters:
closeable
- argument to close- Throws:
UncheckedIOException
- if an I/O error occurs
-
createUnchecked
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
Create an exception indicating a parsing or syntax error.- Parameters:
msg
- exception message- Returns:
- an exception indicating a parsing or syntax error
-
parseError
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 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:
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. 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:
UncheckedIOException
- if an I/O error occurs during input stream and stream creation
-
closeAsUncheckedRunnable
Return aRunnable
that callsinvalid reference
close()
IOException
withUncheckedIOException
.- Parameters:
closeable
- instance to be closed- Returns:
- runnable that calls
invalid @code
{@code close()) on the argument
-