Class FileID

java.lang.Object
org.eclipse.jetty.util.FileID

public class FileID extends Object
Simple, yet surprisingly common utility methods for identifying various file types commonly seen and worked with in a deployment scenario.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Retrieve the basename of a path.
    static String
    getExtension(String filename)
    Retrieve the extension of a file path (not a directory).
    static String
    Retrieve the extension of a URI path.
    static String
    Retrieve the extension of a file path (not a directory).
    static String
    Get the last segment of a String path returning it as the filename
    static String
    Get the last segment of the URI returning it as the filename
    static boolean
    hasNamedPathSegment(Path path, String segmentName)
    Does the provided path have a directory segment with the given name.
    static boolean
    isArchive(String filename)
    Test if filename is any supported Java Archive type (ends in jar, war, or zip).
    static boolean
    Test if URI is any supported Java Archive type.
    static boolean
    Test if Path is any supported Java Archive type (ends in jar, war, or zip).
    static boolean
    Predicate to test for class files
    static boolean
    isExtension(String filename, String... extensions)
    Test if filename matches any of the indicated extensions.
    static boolean
    isExtension(URI uri, String... extensions)
    Test if URI matches any of the indicated extensions.
    static boolean
    isExtension(Path path, String... extensions)
    Test if Path matches any of the indicated extensions.
    static boolean
    isHidden(Path path)
    Predicate useful for Stream<Path> to exclude hidden paths following filesystem rules for hidden directories and files.
    static boolean
    isHidden(Path base, Path path)
    Predicate useful for Stream<Path> to exclude hidden paths following filesystem rules for hidden directories and files.
    static boolean
    Is the filename a JAR file.
    static boolean
    Is the URI pointing to a Java Archive (JAR) File (not directory)
    static boolean
    Is the path a Java Archive (JAR) File (not directory)
    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)
    static boolean
    Test if URI is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)
    static boolean
    Test if Path is any supported Java Library Archive type (suitable to use as a library in a classpath/classloader)
    static boolean
    Predicate to filter on META-INF/versions/* tree in walk/stream results.
    static boolean
    Predicate to skip module-info.class files.
    static boolean
    Predicate to skip META-INF/versions/* tree from walk/stream results.
    static boolean
    Predicate to skip module-info.class files.
    static boolean
    isTld(Path path)
    Is the path a TLD File
    static boolean
    isWebArchive(String filename)
    Is the filename a WAR file.
    static boolean
    Is the path a Web Archive File (not directory)
    static boolean
    Is the path a Web Archive File (not directory)
    static boolean
    isXml(String filename)
    Is the Path a file that ends in XML?
    static boolean
    isXml(Path path)
    Is the Path a file that ends in XML?

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileID

      public FileID()
  • Method Details

    • getBasename

      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 - The string path
      Returns:
      The last segment of the path without any dot suffix
    • getFileName

      public static String getFileName(URI uri)
      Get the last segment of the URI returning it as the filename
      Parameters:
      uri - the URI to look for the filename
      Returns:
      The last segment of the uri
    • getFileName

      public static String getFileName(String path)
      Get the last segment of a String path returning it as the filename
      Parameters:
      path - the string path to look for the filename
      Returns:
      The last segment of the path
    • getExtension

      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 - The URI to search
      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

      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 - The string path
      Returns:
      The last segment extension, or null if not a file, or null if there is no extension present
    • getExtension

      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 - The string path
      Returns:
      The last segment extension excluding the leading dot; or null if not a file; or null if there is no extension present
    • isExtension

      public static boolean isExtension(Path path, String... extensions)
      Test if Path matches any of the indicated extensions.
      Parameters:
      path - the Path to test
      extensions - the list of extensions (all lowercase, without preceding . dot)
      Returns:
      true if Path is a file, and has an extension, and it matches any of the indicated extensions
    • isExtension

      public static boolean isExtension(URI uri, String... extensions)
      Test if URI matches any of the indicated extensions.
      Parameters:
      uri - the URI to test
      extensions - the list of extensions (all lowercase, without preceding . dot)
      Returns:
      true if URI has an extension, and it matches any of the indicated extensions
    • isExtension

      public static boolean isExtension(String filename, String... extensions)
      Test if filename matches any of the indicated extensions.
      Parameters:
      filename - the filename to test
      extensions - the list of extensions (all lowercase, without preceding . dot)
      Returns:
      true if filename has an extension, and it matches any of the indicated extensions
    • hasNamedPathSegment

      public static boolean hasNamedPathSegment(Path path, String segmentName)
      Does the provided path have a directory segment with the given name.
      Parameters:
      path - the path to search
      segmentName - 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:
      true if the directory name exists in path, false if otherwise
    • isArchive

      public static boolean isArchive(Path path)
      Test if Path is any supported Java Archive type (ends in jar, war, or zip).
      Parameters:
      path - the path to test
      Returns:
      true if path is a file, and an extension of jar, war, or zip
      See Also:
    • isArchive

      public static boolean isArchive(String filename)
      Test if filename is any supported Java Archive type (ends in jar, war, or zip).
      Parameters:
      filename - the filename to test
      Returns:
      true if path is a file and name ends with jar, war, or zip
      See Also:
    • isArchive

      public static boolean isArchive(URI uri)
      Test if URI is any supported Java Archive type.
      Parameters:
      uri - the URI to test
      Returns:
      true if the URI has a path that seems to point to a (jar, war, or zip).
      See Also:
    • isLibArchive

      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 - the path to test
      Returns:
      true if path is a file, and an extension of jar, or zip
      See Also:
    • isLibArchive

      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 - the filename to test
      Returns:
      true if path is a file and name ends with jar, or zip
      See Also:
    • isLibArchive

      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 - the URI to test
      Returns:
      true if the URI has a path that seems to point to a (jar, or zip).
      See Also:
    • isClassFile

      public static boolean isClassFile(Path path)
      Predicate to test for class files
      Parameters:
      path - the path to test
      Returns:
      true if the filename ends with .class
    • isHidden

      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 - the possibly relative path to evaluate
      Returns:
      true if hidden by FileSystem rules, false if not
      See Also:
    • isHidden

      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 - the base path to search from (anything above this path is not evaluated)
      path - the path to evaluate
      Returns:
      true if hidden by FileSystem rules, false if not
      See Also:
    • isJavaArchive

      public static boolean isJavaArchive(URI uri)
      Is the URI pointing to a Java Archive (JAR) File (not directory)
      Parameters:
      uri - the uri to test.
      Returns:
      True if a jar file.
    • isJavaArchive

      public static boolean isJavaArchive(Path path)
      Is the path a Java Archive (JAR) File (not directory)
      Parameters:
      path - the path to test.
      Returns:
      True if a jar file.
    • isJavaArchive

      public static boolean isJavaArchive(String filename)
      Is the filename a JAR file.
      Parameters:
      filename - the filename to test.
      Returns:
      True if a jar file.
    • isMetaInfVersions

      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 - the path to test
      Returns:
      true if path is in META-INF/versions/* tree
    • isNotMetaInfVersions

      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 - the path to test
      Returns:
      true if not in META-INF/versions/* tree
    • isModuleInfoClass

      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 - the path to test
      Returns:
      true if not a module-info.class file
    • isNotModuleInfoClass

      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 - the path to test
      Returns:
      true if not a module-info.class file
    • isTld

      public static boolean isTld(Path path)
      Is the path a TLD File
      Parameters:
      path - the path to test.
      Returns:
      True if a .tld file.
    • isWebArchive

      public static boolean isWebArchive(Path path)
      Is the path a Web Archive File (not directory)
      Parameters:
      path - the path to test.
      Returns:
      True if a war file.
    • isWebArchive

      public static boolean isWebArchive(URI uri)
      Is the path a Web Archive File (not directory)
      Parameters:
      uri - the uri to test.
      Returns:
      True if a war file.
    • isWebArchive

      public static boolean isWebArchive(String filename)
      Is the filename a WAR file.
      Parameters:
      filename - the filename to test.
      Returns:
      True if a war file.
    • isXml

      public static boolean isXml(Path path)
      Is the Path a file that ends in XML?
      Parameters:
      path - the path to test
      Returns:
      true if a xml, false otherwise
    • isXml

      public static boolean isXml(String filename)
      Is the Path a file that ends in XML?
      Parameters:
      filename - the filename to test
      Returns:
      true if a xml, false otherwise