Package loci.common

Class Location

  • Direct Known Subclasses:
    CaseInsensitiveLocation

    public class Location
    extends Object
    Pseudo-extension of File that supports reading over HTTP (among other things). It is strongly recommended to use this instead of java.io.File.
    • Constructor Summary

      Constructors 
      Constructor Description
      Location​(File file)
      Construct a Location using the given file on disk.
      Location​(String pathname)
      Construct a Location using the given path.
      Location​(String parent, String child)
      Construct a Location using the given directory and relative path.
      Location​(Location parent, String child)
      Construct a Location using the given directory and relative path.
    • Constructor Detail

      • Location

        public Location​(File file)
        Construct a Location using the given file on disk.
        Parameters:
        file - a file on disk
      • Location

        public Location​(String parent,
                        String child)
        Construct a Location using the given directory and relative path. The two parameters are joined with a file separator and passed to #Location(String)
        Parameters:
        parent - the directory path name
        child - the relative path name
      • Location

        public Location​(Location parent,
                        String child)
        Construct a Location using the given directory and relative path.
        Parameters:
        parent - a Location representing the directory name
        child - the relative path name
        See Also:
        Location(String, String)
    • Method Detail

      • reset

        public static void reset()
        Clear all caches and reset cache-related bookkeeping variables to their original values.
      • cacheDirectoryListings

        public static void cacheDirectoryListings​(boolean cache)
        Turn cacheing of directory listings on or off. Cacheing is turned off by default. Reasons to cache - directory listings over network shares can be very expensive, especially in HCS experiments with thousands of files in the same directory. Technically, if you use a directory listing and then go and access the file, you are using stale information. Unlike a database, there's no transactional integrity to file system operations, so the directory could change by the time you access the file. Reasons not to cache - the contents of the directories might change during the program invocation.
        Parameters:
        cache - - true to turn cacheing on, false to leave it off.
      • setCacheDirectoryTimeout

        public static void setCacheDirectoryTimeout​(double sec)
        Cache directory listings for this many seconds before relisting.
        Parameters:
        sec - - use the cache if a directory list was done within this many seconds.
      • clearDirectoryListingsCache

        public static void clearDirectoryListingsCache()
        Clear the directory listings cache. Do this if directory contents might have changed in a significant way.
      • cleanStaleCacheEntries

        public static void cleanStaleCacheEntries()
        Remove any cached directory listings that have expired.
      • mapId

        public static void mapId​(String id,
                                 String filename)
        Maps the given id to an actual filename on disk. Typically actual filenames are used for ids, making this step unnecessary, but in some cases it is useful; e.g., if the file has been renamed to conform to a standard naming scheme and the original file extension is lost, then using the original filename as the id assists format handlers with type identification and pattern matching, and the id can be mapped to the actual filename for reading the file's contents.
        Parameters:
        id - the mapped name
        filename - the actual filename on disk. If null, any existing mapping for id will be cleared.
        See Also:
        getMappedId(String)
      • mapFile

        public static void mapFile​(String id,
                                   IRandomAccess ira)
        Maps the given id to the given IRandomAccess object.
        Parameters:
        id - the mapped name
        ira - the IRandomAccess object that will be referenced by id. If null, any existing mapping for id will be cleared.
        See Also:
        getMappedFile(String)
      • getMappedId

        public static String getMappedId​(String id)
        Gets the actual filename on disk for the given id. Typically the id itself is the filename, but in some cases may not be; e.g., if OMEIS has renamed a file from its original name to a standard location such as Files/101, the original filename is useful for checking the file extension and doing pattern matching, but the renamed filename is required to read its contents.
        Parameters:
        id - the mapped name
        Returns:
        the corresponding file name on disk, or null if there is no mapping
        See Also:
        mapId(String, String)
      • getMappedFile

        public static IRandomAccess getMappedFile​(String id)
        Gets the random access handle for the given id.
        Parameters:
        id - the mapped name
        Returns:
        the corresponding IRandomAccess, or null if there is no mapping
        See Also:
        mapFile(String, IRandomAccess)
      • setIdMap

        public static void setIdMap​(HashMap<String,​Object> map)
        Set the id mapping using the given HashMap.
        Parameters:
        map - the new mapping from names to filesystem paths and IRandomAccess objects
        Throws:
        IllegalArgumentException - if the given HashMap is null.
      • getHandle

        public static IRandomAccess getHandle​(String id,
                                              boolean writable)
                                       throws IOException
        Gets an IRandomAccess object that can read from or write to the given file.
        Parameters:
        id - the name for which to locate an IRandomAccess
        writable - true if the returned IRandomAccess should have write permission
        Returns:
        a previously mapped IRandomAccess, or a new IRandomAccess according to the name's type (URL, filesystem path, etc.)
        Throws:
        IOException - if a valid IRandomAccess cannot be created
        See Also:
        getHandle(String, boolean, boolean, int), IRandomAccess
      • getHandle

        public static IRandomAccess getHandle​(String id,
                                              boolean writable,
                                              boolean allowArchiveHandles)
                                       throws IOException
        Gets an IRandomAccess object that can read from or write to the given file.
        Parameters:
        id - the name for which to locate an IRandomAccess
        writable - true if the returned IRandomAccess should have write permission
        allowArchiveHandles - true if checks for compressed/archive file types (e.g. Zip, GZip, BZip2) should be enabled
        Returns:
        a previously mapped IRandomAccess, or a new IRandomAccess according to the name's type (URL, filesystem path, etc.)
        Throws:
        IOException - if a valid IRandomAccess cannot be created
        See Also:
        getHandle(String, boolean, boolean, int), IRandomAccess
      • getHandle

        public static IRandomAccess getHandle​(String id,
                                              boolean writable,
                                              boolean allowArchiveHandles,
                                              int bufferSize)
                                       throws IOException
        Gets an IRandomAccess object that can read from or write to the given file.
        Parameters:
        id - the name for which to locate an IRandomAccess
        writable - true if the returned IRandomAccess should have write permission
        allowArchiveHandles - true if checks for compressed/archive file types (e.g. Zip, GZip, BZip2) should be enabled
        bufferSize - the buffer size to use when constructing a NIOFileHandle. Ignored when non-positive.
        Returns:
        a previously mapped IRandomAccess, or a new IRandomAccess according to the name's type (URL, filesystem path, etc.)
        Throws:
        IOException - if a valid IRandomAccess cannot be created
        See Also:
        IRandomAccess
      • checkValidId

        public static void checkValidId​(String id)
                                 throws IOException
        Checks that the given id points at a valid data stream.
        Parameters:
        id - The id string to validate.
        Throws:
        IOException - if the id is not valid.
      • list

        public String[] list​(boolean noHiddenFiles)
        Return a list of all of the files in this directory. If 'noHiddenFiles' is set to true, then hidden files are omitted.
        Parameters:
        noHiddenFiles - true if hidden files should be omitted
        Returns:
        an unsorted list of all relative files in the directory represented by this Location
        See Also:
        File.list()
      • canRead

        public boolean canRead()
        If the underlying location is a URL, this method will return true if the URL exists. Otherwise, it will return true iff the file exists and is readable.
        Returns:
        see above
        See Also:
        File.canRead()
      • canWrite

        public boolean canWrite()
        If the underlying location is a URL, this method will always return false. Otherwise, it will return true iff the file exists and is writable.
        Returns:
        see above
        See Also:
        File.canWrite()
      • createNewFile

        public boolean createNewFile()
                              throws IOException
        Creates a new empty file named by this Location's path name iff a file with this name does not already exist. Note that this operation is only supported if the path name can be interpreted as a path to a file on disk (i.e. is not a URL).
        Returns:
        true if the file was created successfully
        Throws:
        IOException - if an I/O error occurred, or the abstract pathname is a URL
        See Also:
        File.createNewFile()
      • mkdirs

        public boolean mkdirs()
        Creates a directory structures described by this Location's internal File instance.
        Returns:
        true if the directory structure was created successfully.
        See Also:
        File.mkdirs()
      • delete

        public boolean delete()
        Deletes this file. If isDirectory() returns true, then the directory must be empty in order to be deleted. URLs cannot be deleted.
        Returns:
        true if the file was successfully deleted
        See Also:
        File.delete()
      • deleteOnExit

        public void deleteOnExit()
        Request that this file be deleted when the JVM terminates. This method will do nothing if the pathname represents a URL.
        See Also:
        File.deleteOnExit()
      • hashCode

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

        public boolean exists()
        Returns whether or not the pathname exists. If the pathname is a URL, then existence is determined based on whether or not we can successfully read content from the URL.
        Returns:
        true if there is a way to read bytes from this Location's name
        See Also:
        File.exists()
      • getAbsoluteFile

        public Location getAbsoluteFile()
        Return the absolute form of this abstract location.
        Returns:
        a Location representing @see #getAbsolutePath
        See Also:
        File.getAbsoluteFile()
      • getAbsolutePath

        public String getAbsolutePath()
        Return the absolute form of this abstract location.
        Returns:
        a string representing the absolute path
        See Also:
        File.getAbsolutePath()
      • getCanonicalPath

        public String getCanonicalPath()
                                throws IOException
        Returns the canonical path to this file. If the file is a URL, then the canonical path is equivalent to the absolute path (getAbsolutePath()). Otherwise, this method will delegate to File.getCanonicalPath().
        Returns:
        see above
        Throws:
        IOException - if the path cannot be retrieved
      • getName

        public String getName()
        Returns the name of this file, i.e. the last name in the path name sequence.
        Returns:
        the relative path name
        See Also:
        File.getName()
      • getParent

        public String getParent()
        Returns the pathname string of this abstract pathname's parent, or null if this pathname does not have a parent directory. The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.
        Returns:
        see above
        See Also:
        File.getParent()
      • getParentFile

        public Location getParentFile()
        Returns the abstract pathname of this abstract pathname's parent, or null if this pathname does not name a parent directory. The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.
        Returns:
        The abstract pathname of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
        See Also:
        File.getParentFile()
      • getPath

        public String getPath()
        Returns the file's path.
        Returns:
        this file's path name
        See Also:
        File.getPath()
      • isAbsolute

        public boolean isAbsolute()
        Returns whether or not this file's path is absolute.
        Returns:
        true if this path name is absolute. If the path name is a URL, this method will always return true.
        See Also:
        File.isAbsolute()
      • isDirectory

        public boolean isDirectory()
        Returns whether or not this file is a directory.
        Returns:
        true if this pathname exists and represents a directory.
        See Also:
        File.isDirectory()
      • isFile

        public boolean isFile()
        Returns whether or not this file is a regular file (i.e. not a directory).
        Returns:
        true if this pathname exists and represents a regular file.
        See Also:
        File.exists()
      • isHidden

        public boolean isHidden()
        Returns whether or not this file is marked hidden by the filesystem.
        Returns:
        true if the pathname is 'hidden'. This method will always return false if the pathname corresponds to a URL.
        See Also:
        File.isHidden()
      • lastModified

        public long lastModified()
        Return the last modification time of the file.
        Returns:
        the last modification time of this file, in milliseconds since the UNIX epoch. If the file does not exist, 0 is returned.
        See Also:
        File.lastModified(), URLConnection.getLastModified()
      • list

        public String[] list()
        Return a list of relative file names in this directory, or null if this file is not a directory.
        Returns:
        a list of file names in this directory. Hidden files will be included in the list. If this is not a directory, return null.
        See Also:
        list(boolean)
      • listFiles

        public Location[] listFiles()
        Return a list of files in this directory, or null if this file is not a directory.
        Returns:
        a list of absolute files in this directory. Hidden files will be included in the list. If this is not a directory, return null.
        See Also:
        list()