Class WeakZipFileFactory.WeakZipFile

  • Enclosing class:
    WeakZipFileFactory

    public class WeakZipFileFactory.WeakZipFile
    extends java.lang.Object
    This class wraps a ZipFile to making it possible to weakly close it; this means the underlying zip file will be automatically reopened on demand if anyone tries to use it.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) class  WeakZipFileFactory.WeakZipFile.WeakZipInputStream
      This is an InputStream wrapper that will properly reopen the underlying zip file if it is weakly closed and create the underlying input stream.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.ref.SoftReference<java.util.List<java.util.zip.ZipEntry>> m_entries  
      private java.io.File m_file  
      private Mutex m_localMutex  
      private int m_status  
      private long m_timestamp  
      private java.util.zip.ZipFile m_zipFile  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private WeakZipFile​(java.io.File file)
      Constructor is private since instances need to be centrally managed.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void __close()
      This internal method is used to clear the zip file from the data structures and reset its state.
      private void __reopenZipFile()
      Thie internal method is used to reopen a weakly closed zip file.
      private void _closeWeakly()
      This method is used internally to weakly close a zip file.
      void close()
      This method permanently closes the zip file.
      (package private) void closeWeakly()
      Weakly closes the zip file, which means that it will be reopened if anyone tries to use it again.
      private void ensureZipFileIsOpen()
      This method ensures that the zip file associated with this weak zip file instance is actually open and acquires the local weak zip file mutex.
      java.util.Enumeration<java.util.zip.ZipEntry> entries()
      Returns an enumeration of zip entries from the zip file.
      java.util.zip.ZipEntry getEntry​(java.lang.String name)
      Returns the specified entry from the zip file.
      java.io.InputStream getInputStream​(java.util.zip.ZipEntry ze)
      Returns an input stream for the specified zip entry.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_file

        private final java.io.File m_file
      • m_localMutex

        private final Mutex m_localMutex
      • m_zipFile

        private java.util.zip.ZipFile m_zipFile
      • m_status

        private int m_status
      • m_timestamp

        private long m_timestamp
      • m_entries

        private volatile java.lang.ref.SoftReference<java.util.List<java.util.zip.ZipEntry>> m_entries
    • Constructor Detail

      • WeakZipFile

        private WeakZipFile​(java.io.File file)
                     throws java.io.IOException
        Constructor is private since instances need to be centrally managed.
        Parameters:
        file - the target zip file.
        Throws:
        java.io.IOException - if the zip file could not be opened.
    • Method Detail

      • getEntry

        public java.util.zip.ZipEntry getEntry​(java.lang.String name)
        Returns the specified entry from the zip file.
        Parameters:
        name - the name of the entry to return.
        Returns:
        the zip entry associated with the specified name or null if it does not exist.
      • entries

        public java.util.Enumeration<java.util.zip.ZipEntry> entries()
        Returns an enumeration of zip entries from the zip file.
        Returns:
        an enumeration of zip entries.
      • getInputStream

        public java.io.InputStream getInputStream​(java.util.zip.ZipEntry ze)
                                           throws java.io.IOException
        Returns an input stream for the specified zip entry.
        Parameters:
        ze - the zip entry whose input stream is to be retrieved.
        Returns:
        an input stream to the zip entry.
        Throws:
        java.io.IOException - if the input stream cannot be opened.
      • closeWeakly

        void closeWeakly()
        Weakly closes the zip file, which means that it will be reopened if anyone tries to use it again.
      • _closeWeakly

        private void _closeWeakly()
        This method is used internally to weakly close a zip file. It should only be called when already holding the global lock, otherwise use closeWeakly().
      • close

        public void close()
                   throws java.io.IOException
        This method permanently closes the zip file.
        Throws:
        java.io.IOException - if any error occurs while trying to close the zip file.
      • __close

        private void __close()
        This internal method is used to clear the zip file from the data structures and reset its state. It should only be called when holding the global and local mutexes.
      • ensureZipFileIsOpen

        private void ensureZipFileIsOpen()
        This method ensures that the zip file associated with this weak zip file instance is actually open and acquires the local weak zip file mutex. If the underlying zip file is closed, then the local mutex is released and an IllegalStateException is thrown. If the zip file is weakly closed, then it is reopened. If the zip file is already opened, then no additional action is necessary. If this method does not throw an exception, then the end result is the zip file member field is non-null and the local mutex has been acquired.
      • __reopenZipFile

        private void __reopenZipFile()
                              throws java.io.IOException
        Thie internal method is used to reopen a weakly closed zip file. It makes a best effort, but may fail and leave the zip file member field null. Any failure reopening a zip file results in it being permanently closed. This method should only be invoked when holding the global and local mutexes.
        Throws:
        java.io.IOException