Top Description Fields Constructors Methods
org.eclipse.jetty.util

public Class FileID

extends Object
Class Inheritance
Imports
java.io.File, .IOException, java.net.URI, java.nio.file.Files, .Path, java.util.Locale, java.util.regex.Pattern

Simple, yet surprisingly common utility methods for identifying various file types commonly seen and worked with in a deployment scenario.

Field Summary

Modifier and TypeField and Description
private static final Pattern

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

The last segment of the path without any dot suffix
getBasename
(Path
The string path
path
)

Retrieve the basename of a path.

public static String

Returns:

The last segment extension. Null if input uri is null, or scheme is null, or URI is not a `jar:file:` or `file:` based URI
getExtension
(URI
The URI to search
uri
)

Retrieve the extension of a URI path.

public static String

Returns:

The last segment extension, or null if not a file, or null if there is no extension present
getExtension
(Path
The string path
path
)

Retrieve the extension of a file path (not a directory).

public static String

Returns:

The last segment extension excluding the leading dot; or null if not a file; or null if there is no extension present
getExtension
(String
The string path
filename
)

Retrieve the extension of a file path (not a directory).

public static String

Returns:

The last segment of the uri
getFileName
(URI
the URI to look for the filename
uri
)

Get the last segment of the URI returning it as the filename

public static String

Returns:

The last segment of the path
getFileName
(String
the string path to look for the filename
path
)

Get the last segment of a String path returning it as the filename

public static boolean

Returns:

true if the directory name exists in path, false if otherwise
hasNamedPathSegment
(Path
the path to search
path
,
String
the segment name (of the given path) to look for (case-insensitive lookup), only capable of searching 1 segment name at a time, does not support "foo/bar" multi-segment names.
segmentName
)

Does the provided path have a directory segment with the given name.

public static boolean

Returns:

true if path is a file, and an extension of jar, war, or zip
isArchive
(Path
the path to test
path
)

Test if Path is any supported Java Archive type (ends in jar, war, or zip).

public static boolean

Returns:

true if path is a file and name ends with jar, war, or zip
isArchive
(String
the filename to test
filename
)

Test if filename is any supported Java Archive type (ends in jar, war, or zip).

public static boolean

Returns:

true if the URI has a path that seems to point to a (jar, war, or zip).
isArchive
(URI
the URI to test
uri
)

Test if URI is any supported Java Archive type.

public static boolean

Returns:

true if the filename ends with .class
isClassFile
(Path
the path to test
path
)

Predicate to test for class files

public static boolean

Returns:

true if Path is a file, and has an extension, and it matches any of the indicated extensions
isExtension
(Path
the Path to test
path
,
String...
the list of extensions (all lowercase, without preceding . dot)
extensions
)

Test if Path matches any of the indicated extensions.

public static boolean

Returns:

true if URI has an extension, and it matches any of the indicated extensions
isExtension
(URI
the URI to test
uri
,
String...
the list of extensions (all lowercase, without preceding . dot)
extensions
)

Test if URI matches any of the indicated extensions.

public static boolean

Returns:

true if filename has an extension, and it matches any of the indicated extensions
isExtension
(String
the filename to test
filename
,
String...
the list of extensions (all lowercase, without preceding . dot)
extensions
)

Test if filename matches any of the indicated extensions.

public static boolean

Returns:

true if hidden by FileSystem rules, false if not
isHidden
(Path
the possibly relative path to evaluate
path
)

Predicate useful for Stream<Path> to exclude hidden paths following filesystem rules for hidden directories and files.

public static boolean

Returns:

true if hidden by FileSystem rules, false if not
isHidden
(Path
the base path to search from (anything above this path is not evaluated)
base
,
Path
the path to evaluate
path
)

Predicate useful for Stream<Path> to exclude hidden paths following filesystem rules for hidden directories and files.

public static boolean

Returns:

True if a jar file.
isJavaArchive
(URI
the uri to test.
uri
)

Is the URI pointing to a Java Archive (JAR) File (not directory)

public static boolean

Returns:

True if a jar file.
isJavaArchive
(Path
the path to test.
path
)

Is the path a Java Archive (JAR) File (not directory)

public static boolean

Returns:

True if a jar file.
isJavaArchive
(String
the filename to test.
filename
)

Is the filename a JAR file.

public static boolean

Returns:

true if path is a file, and an extension of jar, or zip
isLibArchive
(Path
the path to test
path
)

Test if Path is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)

public static boolean

Returns:

true if path is a file and name ends with jar, or zip
isLibArchive
(String
the filename to test
filename
)

Test if filename is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)

public static boolean

Returns:

true if the URI has a path that seems to point to a (jar, or zip).
isLibArchive
(URI
the URI to test
uri
)

Test if URI is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)

public static boolean

Returns:

true if path is in META-INF/versions/* tree
isMetaInfVersions
(Path
the path to test
path
)

Predicate to filter on META-INF/versions/* tree in walk/stream results.

public static boolean

Returns:

true if not a module-info.class file
isModuleInfoClass
(Path
the path to test
path
)

Predicate to skip module-info.class files.

public static boolean

Returns:

true if not in META-INF/versions/* tree
isNotMetaInfVersions
(Path
the path to test
path
)

Predicate to skip META-INF/versions/* tree from walk/stream results.

public static boolean

Returns:

true if not a module-info.class file
isNotModuleInfoClass
(Path
the path to test
path
)

Predicate to skip module-info.class files.

public static boolean

Returns:

True if a .tld file.
isTld
(Path
the path to test.
path
)

Is the path a TLD File

public static boolean

Returns:

True if a war file.
isWebArchive
(Path
the path to test.
path
)

Is the path a Web Archive File (not directory)

public static boolean

Returns:

True if a war file.
isWebArchive
(URI
the uri to test.
uri
)

Is the path a Web Archive File (not directory)

public static boolean

Returns:

True if a war file.
isWebArchive
(String
the filename to test.
filename
)

Is the filename a WAR file.

public static boolean

Returns:

true if a xml, false otherwise
isXml
(Path
the path to test
path
)

Is the Path a file that ends in XML?

public static boolean

Returns:

true if a xml, false otherwise
isXml
(String
the filename to test
filename
)

Is the Path a file that ends in XML?

private static boolean
matchesExtension(String ext, String... extensions)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

NUMBERback to summary
private static final Pattern NUMBER

Constructor Detail

FileIDback to summary
public FileID()

Method Detail

getBasenameback to summary
public static String getBasename(Path path)

Retrieve the basename of a path. This is the name of the last segment of the path, with any dot suffix (e.g. ".war") removed

Parameters
path:Path

The string path

Returns:String

The last segment of the path without any dot suffix

getExtensionback to summary
public static String getExtension(URI uri)

Retrieve the extension of a URI path.

This is the name of the last segment of the URI path with a substring for the extension (if any), excluding the dot, lower-cased.

"foo.tar.gz" "gz"
"foo.bar"    "bar"
"foo."       ""
"foo"        null
".bar"       null
null         null
Parameters
uri:URI

The URI to search

Returns:String

The last segment extension. Null if input uri is null, or scheme is null, or URI is not a `jar:file:` or `file:` based URI

getExtensionback to summary
public static String getExtension(Path path)

Retrieve the extension of a file path (not a directory).

This is the name of the last segment of the file path with a substring for the extension (if any), excluding the dot, lower-cased.

"foo.tar.gz" "gz"
"foo.bar"    "bar"
"foo."       ""
"foo"        null
".bar"       null
null         null
Parameters
path:Path

The string path

Returns:String

The last segment extension, or null if not a file, or null if there is no extension present

getExtensionback to summary
public static String getExtension(String filename)

Retrieve the extension of a file path (not a directory). This is the extension of filename of the last segment of the file path with a substring for the extension (if any), including the dot, lower-cased.

Parameters
filename:String

The string path

Returns:String

The last segment extension excluding the leading dot; or null if not a file; or null if there is no extension present

getFileNameback to summary
public static String getFileName(URI uri)

Get the last segment of the URI returning it as the filename

Parameters
uri:URI

the URI to look for the filename

Returns:String

The last segment of the uri

getFileNameback to summary
public static String getFileName(String path)

Get the last segment of a String path returning it as the filename

Parameters
path:String

the string path to look for the filename

Returns:String

The last segment of the path

hasNamedPathSegmentback to summary
public static boolean hasNamedPathSegment(Path path, String segmentName)

Does the provided path have a directory segment with the given name.

Parameters
path:Path

the path to search

segmentName:String

the segment name (of the given path) to look for (case-insensitive lookup), only capable of searching 1 segment name at a time, does not support "foo/bar" multi-segment names.

Returns:boolean

true if the directory name exists in path, false if otherwise

isArchiveback to summary
public static boolean isArchive(Path path)

Test if Path is any supported Java Archive type (ends in jar, war, or zip).

Parameters
path:Path

the path to test

Returns:boolean

true if path is a file, and an extension of jar, war, or zip

See Also
getExtension(Path)
isArchiveback to summary
public static boolean isArchive(String filename)

Test if filename is any supported Java Archive type (ends in jar, war, or zip).

Parameters
filename:String

the filename to test

Returns:boolean

true if path is a file and name ends with jar, war, or zip

See Also
getExtension(String)
isArchiveback to summary
public static boolean isArchive(URI uri)

Test if URI is any supported Java Archive type.

Parameters
uri:URI

the URI to test

Returns:boolean

true if the URI has a path that seems to point to a (jar, war, or zip).

See Also
isArchive(String)
isClassFileback to summary
public static boolean isClassFile(Path path)

Predicate to test for class files

Parameters
path:Path

the path to test

Returns:boolean

true if the filename ends with .class

isExtensionback to summary
public static boolean isExtension(Path path, String... extensions)

Test if Path matches any of the indicated extensions.

Parameters
path:Path

the Path to test

extensions:String[]

the list of extensions (all lowercase, without preceding . dot)

Returns:boolean

true if Path is a file, and has an extension, and it matches any of the indicated extensions

isExtensionback to summary
public static boolean isExtension(URI uri, String... extensions)

Test if URI matches any of the indicated extensions.

Parameters
uri:URI

the URI to test

extensions:String[]

the list of extensions (all lowercase, without preceding . dot)

Returns:boolean

true if URI has an extension, and it matches any of the indicated extensions

isExtensionback to summary
public static boolean isExtension(String filename, String... extensions)

Test if filename matches any of the indicated extensions.

Parameters
filename:String

the filename to test

extensions:String[]

the list of extensions (all lowercase, without preceding . dot)

Returns:boolean

true if filename has an extension, and it matches any of the indicated extensions

isHiddenback to summary
public static boolean isHidden(Path path)

Predicate useful for Stream<Path> to exclude hidden paths following filesystem rules for hidden directories and files.

Parameters
path:Path

the possibly relative path to evaluate

Returns:boolean

true if hidden by FileSystem rules, false if not

See Also
Files#isHidden(Path)
isHiddenback to summary
public static boolean isHidden(Path base, Path path)

Predicate useful for Stream<Path> to exclude hidden paths following filesystem rules for hidden directories and files.

Parameters
base:Path

the base path to search from (anything above this path is not evaluated)

path:Path

the path to evaluate

Returns:boolean

true if hidden by FileSystem rules, false if not

See Also
Files#isHidden(Path)
isJavaArchiveback to summary
public static boolean isJavaArchive(URI uri)

Is the URI pointing to a Java Archive (JAR) File (not directory)

Parameters
uri:URI

the uri to test.

Returns:boolean

True if a jar file.

isJavaArchiveback to summary
public static boolean isJavaArchive(Path path)

Is the path a Java Archive (JAR) File (not directory)

Parameters
path:Path

the path to test.

Returns:boolean

True if a jar file.

isJavaArchiveback to summary
public static boolean isJavaArchive(String filename)

Is the filename a JAR file.

Parameters
filename:String

the filename to test.

Returns:boolean

True if a jar file.

isLibArchiveback to summary
public static boolean isLibArchive(Path path)

Test if Path is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)

Parameters
path:Path

the path to test

Returns:boolean

true if path is a file, and an extension of jar, or zip

See Also
getExtension(Path)
isLibArchiveback to summary
public static boolean isLibArchive(String filename)

Test if filename is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)

Parameters
filename:String

the filename to test

Returns:boolean

true if path is a file and name ends with jar, or zip

See Also
getExtension(String)
isLibArchiveback to summary
public static boolean isLibArchive(URI uri)

Test if URI is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)

Parameters
uri:URI

the URI to test

Returns:boolean

true if the URI has a path that seems to point to a (jar, or zip).

See Also
getExtension(URI)
isMetaInfVersionsback to summary
public static boolean isMetaInfVersions(Path path)

Predicate to filter on META-INF/versions/* tree in walk/stream results.

This only works with a zipfs based FileSystem

Parameters
path:Path

the path to test

Returns:boolean

true if path is in META-INF/versions/* tree

isModuleInfoClassback to summary
public static boolean isModuleInfoClass(Path path)

Predicate to skip module-info.class files.

This is a simple test against the last path segment using Path#getFileName()

Parameters
path:Path

the path to test

Returns:boolean

true if not a module-info.class file

isNotMetaInfVersionsback to summary
public static boolean isNotMetaInfVersions(Path path)

Predicate to skip META-INF/versions/* tree from walk/stream results.

This only works with a zipfs based FileSystem

Parameters
path:Path

the path to test

Returns:boolean

true if not in META-INF/versions/* tree

isNotModuleInfoClassback to summary
public static boolean isNotModuleInfoClass(Path path)

Predicate to skip module-info.class files.

This is a simple test against the last path segment using Path#getFileName()

Parameters
path:Path

the path to test

Returns:boolean

true if not a module-info.class file

isTldback to summary
public static boolean isTld(Path path)

Is the path a TLD File

Parameters
path:Path

the path to test.

Returns:boolean

True if a .tld file.

isWebArchiveback to summary
public static boolean isWebArchive(Path path)

Is the path a Web Archive File (not directory)

Parameters
path:Path

the path to test.

Returns:boolean

True if a war file.

isWebArchiveback to summary
public static boolean isWebArchive(URI uri)

Is the path a Web Archive File (not directory)

Parameters
uri:URI

the uri to test.

Returns:boolean

True if a war file.

isWebArchiveback to summary
public static boolean isWebArchive(String filename)

Is the filename a WAR file.

Parameters
filename:String

the filename to test.

Returns:boolean

True if a war file.

isXmlback to summary
public static boolean isXml(Path path)

Is the Path a file that ends in XML?

Parameters
path:Path

the path to test

Returns:boolean

true if a xml, false otherwise

isXmlback to summary
public static boolean isXml(String filename)

Is the Path a file that ends in XML?

Parameters
filename:String

the filename to test

Returns:boolean

true if a xml, false otherwise

matchesExtensionback to summary
private static boolean matchesExtension(String ext, String... extensions)