Class PathResource

java.lang.Object
org.eclipse.jetty.util.resource.Resource
org.eclipse.jetty.util.resource.PathResource
All Implemented Interfaces:
Closeable, AutoCloseable, ResourceFactory

public class PathResource extends Resource
Java NIO Path Resource.
  • Constructor Details

    • PathResource

      public PathResource(File file)
      Construct a new PathResource from a File object.

      An invocation of this convenience constructor of the form.

       new PathResource(file);
       

      behaves in exactly the same way as the expression

       new PathResource(file.toPath());
       
      Parameters:
      file - the file to use
    • PathResource

      public PathResource(Path path)
      Construct a new PathResource from a Path object.
      Parameters:
      path - the path to use
    • PathResource

      public PathResource(URI uri) throws IOException
      Construct a new PathResource from a URI object.

      Must be an absolute URI using the file scheme.

      Parameters:
      uri - the URI to build this PathResource from.
      Throws:
      IOException - if unable to construct the PathResource from the URI.
    • PathResource

      public PathResource(URL url) throws IOException, URISyntaxException
      Create a new PathResource from a provided URL object.

      An invocation of this convenience constructor of the form.

       new PathResource(url);
       

      behaves in exactly the same way as the expression

       new PathResource(url.toURI());
       
      Parameters:
      url - the url to attempt to create PathResource from
      Throws:
      IOException - if URL doesn't point to a location that can be transformed to a PathResource
      URISyntaxException - if the provided URL was malformed
  • Method Details

    • isSameName

      public static boolean isSameName(Path pathA, Path pathB)
      Test if the paths are the same name.

      If the real path is not the same as the absolute path then we know that the real path is the alias for the provided path.

      For OS's that are case insensitive, this should return the real (on-disk / case correct) version of the path.

      We have to be careful on Windows and OSX.

      Assume we have the following scenario:

         Path a = new File("foo").toPath();
         Files.createFile(a);
         Path b = new File("FOO").toPath();
       

      There now exists a file called foo on disk. Using Windows or OSX, with a Path reference of FOO, Foo, fOO, etc.. means the following

                              |  OSX    |  Windows   |  Linux
       -----------------------+---------+------------+---------
       Files.exists(a)        |  True   |  True      |  True
       Files.exists(b)        |  True   |  True      |  False
       Files.isSameFile(a,b)  |  True   |  True      |  False
       a.equals(b)            |  False  |  True      |  False
       

      See the javadoc for Path.equals() for details about this FileSystem behavior difference

      We also cannot rely on a.compareTo(b) as this is roughly equivalent in implementation to a.equals(b)

    • isSame

      public boolean isSame(Resource resource)
      Description copied from class: Resource
      Return true if the passed Resource represents the same resource as the Resource. For many resource types, this is equivalent to Object.equals(Object), however for resources types that support aliasing, this maybe some other check (e.g. Files.isSameFile(Path, Path)).
      Overrides:
      isSame in class Resource
      Parameters:
      resource - The resource to check
      Returns:
      true if the passed resource represents the same resource.
    • addPath

      public Resource addPath(String subPath) throws IOException
      Description copied from class: Resource
      Returns the resource contained inside the current resource with the given name, which may or may not exist.
      Specified by:
      addPath in class Resource
      Parameters:
      subPath - The path segment to add, which is not encoded. The path may be non canonical, but if so then the resulting Resource will return true from Resource.isAlias().
      Returns:
      the Resource for the resolved path within this Resource, never null
      Throws:
      IOException - if unable to resolve the path
      MalformedURLException - if the resolution of the path fails because the input path parameter is malformed, or a relative path attempts to access above the root resource.
    • close

      public void close()
      Description copied from class: Resource
      Release any temporary resources held by the resource.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Resource
    • delete

      public boolean delete() throws SecurityException
      Description copied from class: Resource
      Deletes the given resource
      Specified by:
      delete in class Resource
      Returns:
      true if resource was found and successfully deleted, false if resource didn't exist or was unable to be deleted.
      Throws:
      SecurityException - if unable to delete due to permissions
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • exists

      public boolean exists()
      Specified by:
      exists in class Resource
      Returns:
      true if the represented resource exists.
    • getFile

      public File getFile() throws IOException
      Description copied from class: Resource
      File representing the given resource.
      Specified by:
      getFile in class Resource
      Returns:
      an File representing the given resource or NULL if this is not possible.
      Throws:
      IOException - if unable to get the resource due to permissions
    • getPath

      public Path getPath()
      Returns:
      the Path of the resource
    • getInputStream

      public InputStream getInputStream() throws IOException
      Description copied from class: Resource
      Input stream to the resource
      Specified by:
      getInputStream in class Resource
      Returns:
      an input stream to the resource
      Throws:
      IOException - if unable to open the input stream
    • getName

      public String getName()
      Description copied from class: Resource
      The name of the resource.
      Specified by:
      getName in class Resource
      Returns:
      the name of the resource
    • getReadableByteChannel

      public ReadableByteChannel getReadableByteChannel() throws IOException
      Description copied from class: Resource
      Readable ByteChannel for the resource.
      Specified by:
      getReadableByteChannel in class Resource
      Returns:
      an readable bytechannel to the resource or null if one is not available.
      Throws:
      IOException - if unable to open the readable bytechannel for the resource.
    • newSeekableByteChannel

      public SeekableByteChannel newSeekableByteChannel() throws IOException
      Throws:
      IOException
    • getURI

      public URI getURI()
      Description copied from class: Resource
      URI representing the resource.
      Specified by:
      getURI in class Resource
      Returns:
      an URI representing the given resource
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • isContainedIn

      public boolean isContainedIn(Resource r)
      Specified by:
      isContainedIn in class Resource
    • isDirectory

      public boolean isDirectory()
      Specified by:
      isDirectory in class Resource
      Returns:
      true if the represented resource is a container/directory.
    • lastModified

      public long lastModified()
      Description copied from class: Resource
      Time resource was last modified.
      Specified by:
      lastModified in class Resource
      Returns:
      the last modified time as milliseconds since unix epoch
    • length

      public long length()
      Description copied from class: Resource
      Length of the resource.
      Specified by:
      length in class Resource
      Returns:
      the length of the resource
    • isAlias

      public boolean isAlias()
      Overrides:
      isAlias in class Resource
      Returns:
      true if this Resource is an alias to another real Resource
    • getAliasPath

      public Path getAliasPath()
      The Alias as a Path.

      Note: this cannot return the alias as a DIFFERENT path in 100% of situations, due to Java's internal Path/File normalization.

      Returns:
      the alias as a path.
    • getAlias

      public URI getAlias()
      Overrides:
      getAlias in class Resource
      Returns:
      The canonical Alias of this resource or null if none.
    • list

      public String[] list()
      Description copied from class: Resource
      list of resource names contained in the given resource. Ordering is unspecified, so callers may wish to sort the return value to ensure deterministic behavior.
      Specified by:
      list in class Resource
      Returns:
      a list of resource names contained in the given resource, or null. Note: The resource names are not URL encoded.
    • renameTo

      public boolean renameTo(Resource dest) throws SecurityException
      Description copied from class: Resource
      Rename the given resource
      Specified by:
      renameTo in class Resource
      Parameters:
      dest - the destination name for the resource
      Returns:
      true if the resource was renamed, false if the resource didn't exist or was unable to be renamed.
      Throws:
      SecurityException - if unable to rename due to permissions
    • copyTo

      public void copyTo(File destination) throws IOException
      Description copied from class: Resource
      Copy the Resource to the new destination file.

      Will not replace existing destination file.

      Overrides:
      copyTo in class Resource
      Parameters:
      destination - the destination file to create
      Throws:
      IOException - if unable to copy the resource
    • toString

      public String toString()
      Overrides:
      toString in class Object