Class XJarFile

  • All Implemented Interfaces:
    IJarFile, java.io.Closeable, java.lang.AutoCloseable

    public class XJarFile
    extends java.util.jar.JarFile
    implements IJarFile
    Extends the normal JAR file for processing files with various character case, and mixture of forward and backslashes. It also supports loading the entire file into memory and then unlocking the file in the file system. A check if the file has changed is also supported, in order to create manual or automatic file reload (i.e. application re-deploy).

    Please note that this class currently only supports file items smaller than 2 GB, i.e. a 31 bit long positive integer value.

    Author:
    Christopher Mindus
    • Constructor Summary

      Constructors 
      ConstructorDescription
      XJarFile​(java.io.File file)
      Loads or opens the JAR file "inMemory", without "winStyle" and "verify".
      XJarFile​(java.io.File file, boolean inMemory, boolean winStyle, boolean verify)
      Loads or opens the JAR file.
      XJarFile​(java.lang.String name)
      Loads or opens the JAR file "inMemory", without "winStyle", but with "verify".
      XJarFile​(java.lang.String name, boolean inMemory, boolean winStyle, boolean verify)
      Loads or opens the JAR file.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      static voidclearLoadedFilesList()
      Clears the loaded files list.
      voidclose()
      Closes the Jar file.
      java.util.Enumeration<java.util.jar.JarEntry>entries()
      Returns an enumeration of the Jar file entries.
      java.util.zip.ZipEntrygetEntry​(java.lang.String name)
      Gets the ZIP entry with the specified name.
      byte[]getEntryData​(java.lang.String name)
      Gets the byte [] data for an entry if present.
      byte[]getEntryData​(java.util.zip.ZipEntry ze)
      Gets the byte [] data for an entry if present.
      HandlergetHandler()
      Gets the URL stream handler for the MemoryJarFile.
      java.io.InputStreamgetInputStream​(java.util.zip.ZipEntry ze)
      Returns an input stream for reading the contents of the specified zip file entry.
      java.util.jar.JarEntry[]getJarEntries()
      Gets all Jar entries if the Jar file is loaded into memory.
      java.util.jar.JarEntrygetJarEntry​(java.lang.String name)
      Gets the JAR entry with the specified name.
      java.io.FilegetJarFile()
      Gets the file of the Jar file.
      static XJarFilegetJarFile​(java.io.File file)
      Loads the requested XJarFile or grabs one from the table of loaded files and reloads it as required.
      static XJarFilegetJarFile​(java.lang.String fileName)
      Loads the requested XJarFile or grabs one from the table of loaded files and reloads it as required.
      java.util.jar.ManifestgetManifest()
      Returns the jar file manifest, or null if none.
      java.net.URLgetURL()
      Gets the XJar URL to be used with e.g.
      java.net.URLgetURL​(java.lang.String path)
      Gets the XJar URL to be used with e.g.
      java.net.URLgetURL​(java.util.zip.ZipEntry entry)
      Gets the XJar URL to be used with e.g.
      booleanhasChanged()
      Checks if the Jar file has changed since last call to this method, i.e.
      longlastModified()
      Returns the time that the file denoted by this abstract pathname was last modified.
      intsize()
      Returns the number of entries in the Jar file.
      java.util.stream.Stream<java.util.jar.JarEntry>stream()
      Returns a stream of Jar entries.
      • Methods inherited from class java.util.jar.JarFile

        baseVersion, getVersion, isMultiRelease, runtimeVersion, versionedStream
      • Methods inherited from class java.util.zip.ZipFile

        finalize, getComment, getName
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • XJarFile

        public XJarFile​(java.lang.String name)
                 throws java.io.IOException
        Loads or opens the JAR file "inMemory", without "winStyle", but with "verify".
        Parameters:
        name - the file name.
        Throws:
        java.io.IOException - for file failures.
      • XJarFile

        public XJarFile​(java.io.File file)
                 throws java.io.IOException
        Loads or opens the JAR file "inMemory", without "winStyle" and "verify".
        Parameters:
        file - the file.
        Throws:
        java.io.IOException - for file failures.
      • XJarFile

        public XJarFile​(java.lang.String name,
                        boolean inMemory,
                        boolean winStyle,
                        boolean verify)
                 throws java.io.IOException
        Loads or opens the JAR file.
        Parameters:
        name - the file name.
        inMemory - if true, the entire Jar file is loaded into memory and then closed. Subsequent operations can be performed as normal.
        winStyle - if true, the entries are case insensitive and backslash can be used with forward slash as well.
        verify - if the Jar file should be verified.
        Throws:
        java.io.IOException - for file failures.
      • XJarFile

        public XJarFile​(java.io.File file,
                        boolean inMemory,
                        boolean winStyle,
                        boolean verify)
                 throws java.io.IOException
        Loads or opens the JAR file.
        Parameters:
        file - the file.
        inMemory - if true, the entire Jar file is loaded into memory and then closed. Subsequent operations can be performed as normal.
        winStyle - if true, the entries are case insensitive and backslash can be used with forward slash as well.
        verify - if the Jar file should be verified.
        Throws:
        java.io.IOException - for file failures.
    • Method Detail

      • clearLoadedFilesList

        public static void clearLoadedFilesList()
        Clears the loaded files list.
      • getJarFile

        public static XJarFile getJarFile​(java.lang.String fileName)
                                   throws java.io.IOException
        Loads the requested XJarFile or grabs one from the table of loaded files and reloads it as required.
        Throws:
        java.io.IOException - for file failures.
      • getJarFile

        public static XJarFile getJarFile​(java.io.File file)
                                   throws java.io.IOException
        Loads the requested XJarFile or grabs one from the table of loaded files and reloads it as required. If the loading of this Jar file is done in another thread, this thread will first complete it's loading.
        Throws:
        java.io.IOException - for file failures.
      • lastModified

        public long lastModified()
        Returns the time that the file denoted by this abstract pathname was last modified.

        Where it is required to distinguish an I/O exception from the case where 0L is returned, or where several attributes of the same file are required at the same time, or where the time of last access or the creation time are required, then the Files.readAttributes method may be used.

        Specified by:
        lastModified in interface IJarFile
        Returns:
        A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs
        Throws:
        java.lang.SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file
      • getJarFile

        public java.io.File getJarFile()
        Gets the file of the Jar file.
        Specified by:
        getJarFile in interface IJarFile
        Returns:
        The file.
      • getHandler

        public Handler getHandler()
        Gets the URL stream handler for the MemoryJarFile.
        Specified by:
        getHandler in interface IJarFile
        Returns:
        The stream handler.
      • getURL

        public java.net.URL getURL()
        Gets the XJar URL to be used with e.g. a class loader.
        Specified by:
        getURL in interface IJarFile
        Returns:
        An URL with the stream handler initialized to this Jar file.
      • getURL

        public java.net.URL getURL​(java.util.zip.ZipEntry entry)
        Gets the XJar URL to be used with e.g. a class loader.
        Specified by:
        getURL in interface IJarFile
        Parameters:
        entry - The Jar entry.
        Returns:
        An URL with the stream handler initialized to this Jar file.
      • getURL

        public java.net.URL getURL​(java.lang.String path)
        Gets the XJar URL to be used with e.g. a class loader.
        Specified by:
        getURL in interface IJarFile
        Parameters:
        path - A path specification.
        Returns:
        An URL with the stream handler initialized to this Jar file.
      • hasChanged

        public boolean hasChanged()
        Checks if the Jar file has changed since last call to this method, i.e. this means that the new last modified time and file size is saved.
        Specified by:
        hasChanged in interface IJarFile
        Returns:
        true if the file has changed in size or last modified time, false otherwise.
      • entries

        public java.util.Enumeration<java.util.jar.JarEntry> entries()
        Returns an enumeration of the Jar file entries.
        Specified by:
        entries in interface IJarFile
        Overrides:
        entries in class java.util.jar.JarFile
        Returns:
        an enumeration of the Jar file entries.
      • stream

        public java.util.stream.Stream<java.util.jar.JarEntry> stream()
        Returns a stream of Jar entries.
        Specified by:
        stream in interface IJarFile
        Overrides:
        stream in class java.util.jar.JarFile
        Returns:
        an ordered, distinct, immutable and non-null stream of the Jar file entries.
      • getJarEntries

        public java.util.jar.JarEntry[] getJarEntries()
        Gets all Jar entries if the Jar file is loaded into memory.
        Specified by:
        getJarEntries in interface IJarFile
        Returns:
        All the entries as a new array.
      • getInputStream

        public java.io.InputStream getInputStream​(java.util.zip.ZipEntry ze)
                                           throws java.io.IOException
        Returns an input stream for reading the contents of the specified zip file entry.
        Specified by:
        getInputStream in interface IJarFile
        Overrides:
        getInputStream in class java.util.jar.JarFile
        Parameters:
        ze - The Zip or Jar file entry.
        Returns:
        an input stream for reading the contents of the specified zip file entry
        Throws:
        java.util.zip.ZipException - if a zip file format error has occurred.
        java.io.IOException - if an I/O error has occurred.
        java.lang.SecurityException - if any of the jar file entries are incorrectly signed.
        java.lang.IllegalStateException - may be thrown if the jar file has been closed.
      • getJarEntry

        public java.util.jar.JarEntry getJarEntry​(java.lang.String name)
        Gets the JAR entry with the specified name.
        Specified by:
        getJarEntry in interface IJarFile
        Overrides:
        getJarEntry in class java.util.jar.JarFile
        Parameters:
        name - The Jar file entry name.
        Returns:
        The JarEntry for the given entry name or null if not found.
        See Also:
        JarEntry
      • getEntry

        public java.util.zip.ZipEntry getEntry​(java.lang.String name)
        Gets the ZIP entry with the specified name.
        Specified by:
        getEntry in interface IJarFile
        Overrides:
        getEntry in class java.util.jar.JarFile
        Parameters:
        name - The name of the entry.
        Returns:
        The Zip file entry, or null if not found.
      • size

        public int size()
        Returns the number of entries in the Jar file.
        Overrides:
        size in class java.util.zip.ZipFile
        Returns:
        the number of entries in the Jar file.
        Throws:
        java.lang.IllegalStateException - if the Jar file has been closed.
      • getManifest

        public java.util.jar.Manifest getManifest()
                                           throws java.io.IOException
        Returns the jar file manifest, or null if none.
        Specified by:
        getManifest in interface IJarFile
        Overrides:
        getManifest in class java.util.jar.JarFile
        Returns:
        the jar file manifest, or null if none.
        Throws:
        java.lang.IllegalStateException - may be thrown if the jar file has been closed
        java.io.IOException - If an I/O error has occurred.
      • getEntryData

        public byte[] getEntryData​(java.lang.String name)
        Gets the byte [] data for an entry if present. This method only works when the file has been read into memory.
        Specified by:
        getEntryData in interface IJarFile
        Parameters:
        name - the name of the entry.
        Returns:
        the byte array or null if not found.
      • getEntryData

        public byte[] getEntryData​(java.util.zip.ZipEntry ze)
        Gets the byte [] data for an entry if present. This method only works when the file has been read into memory.
        Specified by:
        getEntryData in interface IJarFile
        Parameters:
        ze - the name of the entry.
        Returns:
        the byte array or null if not found.
      • close

        public void close()
                   throws java.io.IOException
        Closes the Jar file.

        Closing this Jar file will close all of the input streams previously returned by invocations of the getInputStream method.

        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface IJarFile
        Overrides:
        close in class java.util.zip.ZipFile
        Throws:
        java.io.IOException - if an I/O error has occurred.