Class XJarFile

All Implemented Interfaces:
IJarFile, Closeable, AutoCloseable

public class XJarFile extends 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
  • Field Details

  • Constructor Details

    • XJarFile

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

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

      public XJarFile(String name, boolean inMemory, boolean winStyle, boolean verify) throws 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:
      IOException - for file failures.
    • XJarFile

      public XJarFile(File file, boolean inMemory, boolean winStyle, boolean verify) throws 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:
      IOException - for file failures.
  • Method Details

    • clearLoadedFilesList

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

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

      public static XJarFile getJarFile(File file) throws 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:
      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:
      SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file
    • getJarFile

      public 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 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 URL getURL(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 URL getURL(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 Enumeration<JarEntry> entries()
      Returns an enumeration of the Jar file entries.
      Specified by:
      entries in interface IJarFile
      Overrides:
      entries in class JarFile
      Returns:
      an enumeration of the Jar file entries.
    • stream

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

      public 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 InputStream getInputStream(ZipEntry ze) throws 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 JarFile
      Parameters:
      ze - The Zip or Jar file entry.
      Returns:
      an input stream for reading the contents of the specified zip file entry
      Throws:
      ZipException - if a zip file format error has occurred.
      IOException - if an I/O error has occurred.
      SecurityException - if any of the jar file entries are incorrectly signed.
      IllegalStateException - may be thrown if the jar file has been closed.
    • getJarEntry

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

      public ZipEntry getEntry(String name)
      Gets the ZIP entry with the specified name.
      Specified by:
      getEntry in interface IJarFile
      Overrides:
      getEntry in class 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 ZipFile
      Returns:
      the number of entries in the Jar file.
      Throws:
      IllegalStateException - if the Jar file has been closed.
    • getManifest

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

      public byte[] getEntryData(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(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 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 AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface IJarFile
      Overrides:
      close in class ZipFile
      Throws:
      IOException - if an I/O error has occurred.