Top Description Fields Constructors Methods
io.smallrye.common.classloader

public Class ClassPathUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .UncheckedIOException, java.net.MalformedURLException, .URISyntaxException, .URL, .URLConnection, java.nio.file.FileSystem, .Files, .Path, .Paths, java.nio.file.spi.FileSystemProvider, java.util.Enumeration, .Map, .NoSuchElementException, java.util.function.Consumer, .Function

Miscellaneous class path related utilities.

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private static final FileSystemProvider

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
consumeAsPath(URL
resource url
url
,
Consumer<Path>
resource path consumer
consumer
)

Attempts to represent a resource as a local file system path to be processed by a consumer.

public static void
consumeAsPaths(String
resource path
resource
,
Consumer<Path>
resource path consumer
consumer
)

Invokes consumeAsPaths(ClassLoader, String, Consumer) passing in an instance of the current thread's context classloader as the classloader from which to load the resources.

public static void
consumeAsPaths(ClassLoader
classloader to load the resources from
cl
,
String
resource path
resource
,
Consumer<Path>
resource path consumer
consumer
)

Locates specified resources on the classpath and attempts to represent them as local file system paths to be processed by a consumer.

public static void
consumeAsStreams(String
resource path
resource
,
Consumer<InputStream>
resource input stream consumer
consumer
)

Invokes consumeAsStreams(ClassLoader, String, Consumer) passing in an instance of the current thread's context classloader as the classloader from which to load the resources.

public static void
consumeAsStreams(ClassLoader
classloader to load the resources from
cl
,
String
resource path
resource
,
Consumer<InputStream>
resource input stream consumer
consumer
)

Locates all the occurrences of a resource on the classpath of the provided classloader and invokes the consumer providing the input streams for each located resource.

public static void
consumeStream(URL
URL
url
,
Consumer<InputStream>
input stream consumer
consumer
)

Invokes a consumer providing the input streams to read the content of the URL.

private static <R> R
processAsJarPath(Path jarPath, String path, Function<Path, R> function)

public static <
the result type
R
>
R

Returns:

the result of the function
processAsPath
(URL
resource url
url
,
Function<Path, R>
resource path function
function
)

Attempts to represent a resource as a local file system path to be processed by a function.

public static <
the result type
R
>
R

Returns:

the result of the function
readStream
(URL
URL
url
,
Function<InputStream, R>
input stream processing function
function
)

Invokes a function providing the input streams to read the content of the URL.

public static Path

Returns:

local file system path
toLocalPath
(final URL
URL
url
)

Translates a URL to local file system path.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

FILEback to summary
private static final String FILE
JARback to summary
private static final String JAR
JAR_PROVIDERback to summary
private static final FileSystemProvider JAR_PROVIDER

Constructor Detail

ClassPathUtilsback to summary
private ClassPathUtils()

Method Detail

consumeAsPathback to summary
public static void consumeAsPath(URL url, Consumer<Path> consumer)

Attempts to represent a resource as a local file system path to be processed by a consumer. If a resource appears to be an actual file or a directory, it is simply passed to the consumer as-is. If a resource is an entry in a JAR, the entry will be resolved as an instance of java.nio.file.Path in a java.nio.file.FileSystem representing the JAR. If the protocol of the URL representing the resource is neither 'file' nor 'jar', the method will fail with an exception.

Parameters
url:URL

resource url

consumer:Consumer<Path>

resource path consumer

consumeAsPathsback to summary
public static void consumeAsPaths(String resource, Consumer<Path> consumer) throws IOException

Invokes consumeAsPaths(ClassLoader, String, Consumer) passing in an instance of the current thread's context classloader as the classloader from which to load the resources.

Parameters
resource:String

resource path

consumer:Consumer<Path>

resource path consumer

Exceptions
IOException:
in case of an IO failure
consumeAsPathsback to summary
public static void consumeAsPaths(ClassLoader cl, String resource, Consumer<Path> consumer) throws IOException

Locates specified resources on the classpath and attempts to represent them as local file system paths to be processed by a consumer. If a resource appears to be an actual file or a directory, it is simply passed to the consumer as-is. If a resource is an entry in a JAR, the entry will be resolved as an instance of java.nio.file.Path in a java.nio.file.FileSystem representing the JAR. If the protocol of the URL representing the resource is neither 'file' nor 'jar', the method will fail with an exception.

Parameters
cl:ClassLoader

classloader to load the resources from

resource:String

resource path

consumer:Consumer<Path>

resource path consumer

Exceptions
IOException:
in case of an IO failure
consumeAsStreamsback to summary
public static void consumeAsStreams(String resource, Consumer<InputStream> consumer) throws IOException

Invokes consumeAsStreams(ClassLoader, String, Consumer) passing in an instance of the current thread's context classloader as the classloader from which to load the resources.

Parameters
resource:String

resource path

consumer:Consumer<InputStream>

resource input stream consumer

Exceptions
IOException:
in case of an IO failure
consumeAsStreamsback to summary
public static void consumeAsStreams(ClassLoader cl, String resource, Consumer<InputStream> consumer) throws IOException

Locates all the occurrences of a resource on the classpath of the provided classloader and invokes the consumer providing the input streams for each located resource. The consumer does not have to close the provided input stream. This method was introduced to avoid calling java.net.URL#openStream() which in case the resource is found in an archive (such as JAR) locks the containing archive even if the caller properly closes the stream.

Parameters
cl:ClassLoader

classloader to load the resources from

resource:String

resource path

consumer:Consumer<InputStream>

resource input stream consumer

Exceptions
IOException:
in case of an IO failure
consumeStreamback to summary
public static void consumeStream(URL url, Consumer<InputStream> consumer) throws IOException

Invokes a consumer providing the input streams to read the content of the URL. The consumer does not have to close the provided input stream. This method was introduced to avoid calling java.net.URL#openStream() which in case the resource is found in an archive (such as JAR) locks the containing archive even if the caller properly closes the stream.

Parameters
url:URL

URL

consumer:Consumer<InputStream>

input stream consumer

Exceptions
IOException:
in case of an IO failure
processAsJarPathback to summary
private static <R> R processAsJarPath(Path jarPath, String path, Function<Path, R> function)
processAsPathback to summary
public static <R> R processAsPath(URL url, Function<Path, R> function)

Attempts to represent a resource as a local file system path to be processed by a function. If a resource appears to be an actual file or a directory, it is simply passed to the function as-is. If a resource is an entry in a JAR, the entry will be resolved as an instance of java.nio.file.Path in a java.nio.file.FileSystem representing the JAR. If the protocol of the URL representing the resource is neither 'file' nor 'jar', the method will fail with an exception.

Parameters
<R>
the result type
url:URL

resource url

function:Function<Path, R>

resource path function

Returns:R

the result of the function

readStreamback to summary
public static <R> R readStream(URL url, Function<InputStream, R> function) throws IOException

Invokes a function providing the input streams to read the content of the URL. The function does not have to close the provided input stream. This method was introduced to avoid calling java.net.URL#openStream() directly, which in case the resource is found in an archive (such as JAR) locks the containing archive even if the caller properly closes the stream.

Parameters
<R>
the result type
url:URL

URL

function:Function<InputStream, R>

input stream processing function

Returns:R

the result of the function

Exceptions
IOException:
in case of an IO failure
toLocalPathback to summary
public static Path toLocalPath(final URL url)

Translates a URL to local file system path. In case the URL couldn't be translated to a file system path, an instance of IllegalArgumentException will be thrown.

Parameters
url:URL

URL

Returns:Path

local file system path